Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit ed0a0fb

Browse files
author
Greg Clayton
committedOct 18, 2012
<rdar://problem/12462048>
<rdar://problem/12068650> More fixes to how we handle paths that are used to create a target. This modification centralizes the location where and how what the user specifies gets resolved. Prior to this fix, the TargetList::CreateTarget variants took a FileSpec object which meant everyone had the opportunity to resolve the path their own way. Now both CreateTarget variants take a "const char *use_exe_path" which allows the TargetList::CreateTarget to centralize where the resolving happens and "do the right thing". git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@166186 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 0c8446c commit ed0a0fb

File tree

12 files changed

+96
-64
lines changed

12 files changed

+96
-64
lines changed
 

‎include/lldb/Host/FileSpec.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,17 @@ class FileSpec
342342
bool
343343
IsSourceImplementationFile () const;
344344

345+
//------------------------------------------------------------------
346+
/// Returns true if the filespec represents path that is relative
347+
/// path to the current working directory.
348+
///
349+
/// @return
350+
/// \b true if the filespec represents a current working
351+
/// directory relative path, \b false otherwise.
352+
//------------------------------------------------------------------
353+
bool
354+
IsRelativeToCurrentWorkingDirectory () const;
355+
345356
TimeValue
346357
GetModificationTime () const;
347358

‎include/lldb/Target/TargetList.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class TargetList : public Broadcaster
9898
//------------------------------------------------------------------
9999
Error
100100
CreateTarget (Debugger &debugger,
101-
const FileSpec& file_spec,
101+
const char *user_exe_path,
102102
const char *triple_cstr,
103103
bool get_dependent_modules,
104104
const OptionGroupPlatform *platform_options,
@@ -112,7 +112,7 @@ class TargetList : public Broadcaster
112112
//------------------------------------------------------------------
113113
Error
114114
CreateTarget (Debugger &debugger,
115-
const FileSpec& file_spec,
115+
const char *user_exe_path,
116116
const ArchSpec& arch,
117117
bool get_dependent_modules,
118118
lldb::PlatformSP &platform_sp,

‎source/API/SBDebugger.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,11 @@ SBDebugger::CreateTarget (const char *filename,
511511
if (m_opaque_sp)
512512
{
513513
sb_error.Clear();
514-
FileSpec filename_spec (filename, true);
515514
OptionGroupPlatform platform_options (false);
516515
platform_options.SetPlatformName (platform_name);
517516

518517
sb_error.ref() = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
519-
filename_spec,
518+
filename,
520519
target_triple,
521520
add_dependent_modules,
522521
&platform_options,
@@ -554,10 +553,9 @@ SBDebugger::CreateTargetWithFileAndTargetTriple (const char *filename,
554553
TargetSP target_sp;
555554
if (m_opaque_sp)
556555
{
557-
FileSpec file_spec (filename, true);
558556
const bool add_dependent_modules = true;
559557
Error error (m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
560-
file_spec,
558+
filename,
561559
target_triple,
562560
add_dependent_modules,
563561
NULL,
@@ -584,12 +582,11 @@ SBDebugger::CreateTargetWithFileAndArch (const char *filename, const char *arch_
584582
TargetSP target_sp;
585583
if (m_opaque_sp)
586584
{
587-
FileSpec file (filename, true);
588585
Error error;
589586
const bool add_dependent_modules = true;
590587

591-
error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
592-
file,
588+
error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
589+
filename,
593590
arch_cstr,
594591
add_dependent_modules,
595592
NULL,
@@ -618,14 +615,13 @@ SBDebugger::CreateTarget (const char *filename)
618615
TargetSP target_sp;
619616
if (m_opaque_sp)
620617
{
621-
FileSpec file (filename, true);
622618
ArchSpec arch = Target::GetDefaultArchitecture ();
623619
Error error;
624620
const bool add_dependent_modules = true;
625621

626622
PlatformSP platform_sp(m_opaque_sp->GetPlatformList().GetSelectedPlatform());
627623
error = m_opaque_sp->GetTargetList().CreateTarget (*m_opaque_sp,
628-
file,
624+
filename,
629625
arch,
630626
add_dependent_modules,
631627
platform_sp,

‎source/Commands/CommandObjectProcess.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,10 @@ class CommandObjectProcessAttach : public CommandObjectParsed
521521
{
522522
// If there isn't a current target create one.
523523
TargetSP new_target_sp;
524-
FileSpec emptyFileSpec;
525524
Error error;
526525

527526
error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
528-
emptyFileSpec,
527+
NULL,
529528
NULL,
530529
false,
531530
NULL, // No platform options
@@ -1022,10 +1021,9 @@ class CommandObjectProcessConnect : public CommandObjectParsed
10221021
if (!target_sp)
10231022
{
10241023
// If there isn't a current target create one.
1025-
FileSpec emptyFileSpec;
10261024

10271025
error = m_interpreter.GetDebugger().GetTargetList().CreateTarget (m_interpreter.GetDebugger(),
1028-
emptyFileSpec,
1026+
NULL,
10291027
NULL,
10301028
false,
10311029
NULL, // No platform options

‎source/Commands/CommandObjectTarget.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,12 @@ class CommandObjectTargetCreate : public CommandObjectParsed
221221
{
222222
const char *file_path = command.GetArgumentAtIndex(0);
223223
Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path);
224-
FileSpec file_spec;
225-
226-
if (file_path)
227-
file_spec.SetFile (file_path, true);
228-
229224
TargetSP target_sp;
230225
Debugger &debugger = m_interpreter.GetDebugger();
231226
const char *arch_cstr = m_arch_option.GetArchitectureName();
232227
const bool get_dependent_files = true;
233228
Error error (debugger.GetTargetList().CreateTarget (debugger,
234-
file_spec,
229+
file_path,
235230
arch_cstr,
236231
get_dependent_files,
237232
&m_platform_options,
@@ -1241,14 +1236,11 @@ DumpModuleUUID (Stream &strm, Module *module)
12411236
}
12421237

12431238
static uint32_t
1244-
DumpCompileUnitLineTable
1245-
(
1246-
CommandInterpreter &interpreter,
1247-
Stream &strm,
1248-
Module *module,
1249-
const FileSpec &file_spec,
1250-
bool load_addresses
1251-
)
1239+
DumpCompileUnitLineTable (CommandInterpreter &interpreter,
1240+
Stream &strm,
1241+
Module *module,
1242+
const FileSpec &file_spec,
1243+
bool load_addresses)
12521244
{
12531245
uint32_t num_matches = 0;
12541246
if (module)

‎source/Host/common/FileSpec.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,4 +996,28 @@ FileSpec::IsSourceImplementationFile () const
996996
return false;
997997
}
998998

999+
bool
1000+
FileSpec::IsRelativeToCurrentWorkingDirectory () const
1001+
{
1002+
const char *directory = m_directory.GetCString();
1003+
if (directory && directory[0])
1004+
{
1005+
// If the path doesn't start with '/' or '~', return true
1006+
switch (directory[0])
1007+
{
1008+
case '/':
1009+
case '~':
1010+
return false;
1011+
default:
1012+
return true;
1013+
}
1014+
}
1015+
else if (m_filename)
1016+
{
1017+
// No directory, just a basename, return true
1018+
return true;
1019+
}
1020+
return false;
1021+
}
1022+
9991023

‎source/Host/common/Host.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ Host::GetDummyTarget (lldb_private::Debugger &debugger)
12471247
if (!arch.IsValid())
12481248
arch = Host::GetArchitecture ();
12491249
Error err = debugger.GetTargetList().CreateTarget(debugger,
1250-
FileSpec(),
1250+
NULL,
12511251
arch.GetTriple().getTriple().c_str(),
12521252
false,
12531253
NULL,

‎source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,10 @@ PlatformFreeBSD::Attach(ProcessAttachInfo &attach_info,
498498
if (target == NULL)
499499
{
500500
TargetSP new_target_sp;
501-
FileSpec emptyFileSpec;
502501
ArchSpec emptyArchSpec;
503502

504503
error = debugger.GetTargetList().CreateTarget (debugger,
505-
emptyFileSpec,
504+
NULL,
506505
emptyArchSpec,
507506
false,
508507
m_remote_platform_sp,

‎source/Plugins/Platform/Linux/PlatformLinux.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,10 @@ PlatformLinux::Attach(ProcessAttachInfo &attach_info,
408408
if (target == NULL)
409409
{
410410
TargetSP new_target_sp;
411-
FileSpec emptyFileSpec;
412411
ArchSpec emptyArchSpec;
413412

414413
error = debugger.GetTargetList().CreateTarget (debugger,
415-
emptyFileSpec,
414+
NULL,
416415
emptyArchSpec,
417416
false,
418417
m_remote_platform_sp,

‎source/Plugins/Platform/MacOSX/PlatformDarwin.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,9 @@ PlatformDarwin::Attach (ProcessAttachInfo &attach_info,
533533
if (target == NULL)
534534
{
535535
TargetSP new_target_sp;
536-
FileSpec emptyFileSpec;
537536

538537
error = debugger.GetTargetList().CreateTarget (debugger,
539-
emptyFileSpec,
538+
NULL,
540539
NULL,
541540
false,
542541
NULL,

‎source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,9 @@ PlatformRemoteGDBServer::Attach (lldb_private::ProcessAttachInfo &attach_info,
368368
if (target == NULL)
369369
{
370370
TargetSP new_target_sp;
371-
FileSpec emptyFileSpec;
372371

373372
error = debugger.GetTargetList().CreateTarget (debugger,
374-
emptyFileSpec,
373+
NULL,
375374
NULL,
376375
false,
377376
NULL,

‎source/Target/TargetList.cpp

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ TargetList::~TargetList()
5757

5858
Error
5959
TargetList::CreateTarget (Debugger &debugger,
60-
const FileSpec& file,
60+
const char *user_exe_path,
6161
const char *triple_cstr,
6262
bool get_dependent_files,
6363
const OptionGroupPlatform *platform_options,
@@ -112,39 +112,25 @@ TargetList::CreateTarget (Debugger &debugger,
112112
platform_arch = arch;
113113

114114
error = TargetList::CreateTarget (debugger,
115-
file,
115+
user_exe_path,
116116
platform_arch,
117117
get_dependent_files,
118118
platform_sp,
119119
target_sp);
120-
121-
if (target_sp)
122-
{
123-
if (file.GetDirectory())
124-
{
125-
FileSpec file_dir;
126-
file_dir.GetDirectory() = file.GetDirectory();
127-
target_sp->GetExecutableSearchPaths ().Append (file_dir);
128-
}
129-
}
130120
return error;
131121
}
132122

133123
Error
134-
TargetList::CreateTarget
135-
(
136-
Debugger &debugger,
137-
const FileSpec& file,
138-
const ArchSpec& specified_arch,
139-
bool get_dependent_files,
140-
PlatformSP &platform_sp,
141-
TargetSP &target_sp
142-
)
124+
TargetList::CreateTarget (Debugger &debugger,
125+
const char *user_exe_path,
126+
const ArchSpec& specified_arch,
127+
bool get_dependent_files,
128+
PlatformSP &platform_sp,
129+
TargetSP &target_sp)
143130
{
144131
Timer scoped_timer (__PRETTY_FUNCTION__,
145-
"TargetList::CreateTarget (file = '%s/%s', arch = '%s')",
146-
file.GetDirectory().AsCString(),
147-
file.GetFilename().AsCString(),
132+
"TargetList::CreateTarget (file = '%s', arch = '%s')",
133+
user_exe_path,
148134
specified_arch.GetArchitectureName());
149135
Error error;
150136

@@ -168,12 +154,28 @@ TargetList::CreateTarget
168154

169155
if (!arch.IsValid())
170156
arch = specified_arch;
171-
172157

158+
FileSpec file (user_exe_path, false);
173159
if (file)
174160
{
161+
if (file.IsRelativeToCurrentWorkingDirectory())
162+
{
163+
// Ignore paths that start with "./" and "../"
164+
if (!((user_exe_path[0] == '.' && user_exe_path[1] == '/') ||
165+
(user_exe_path[0] == '.' && user_exe_path[1] == '.' && user_exe_path[2] == '/')))
166+
{
167+
char cwd[PATH_MAX];
168+
if (getcwd (cwd, sizeof(cwd)))
169+
{
170+
std::string cwd_user_exe_path (cwd);
171+
cwd_user_exe_path += '/';
172+
cwd_user_exe_path += user_exe_path;
173+
file.SetFile(cwd_user_exe_path.c_str(), false);
174+
}
175+
}
176+
}
177+
175178
ModuleSP exe_module_sp;
176-
FileSpec resolved_file(file);
177179
if (platform_sp)
178180
{
179181
FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
@@ -217,9 +219,22 @@ TargetList::CreateTarget
217219

218220
if (target_sp)
219221
{
222+
if (user_exe_path)
223+
{
224+
// Use exactly what the user typed as the first argument when we exec or posix_spawn
225+
target_sp->SetArg0 (user_exe_path);
226+
}
227+
if (file.GetDirectory())
228+
{
229+
FileSpec file_dir;
230+
file_dir.GetDirectory() = file.GetDirectory();
231+
target_sp->GetExecutableSearchPaths ().Append (file_dir);
232+
}
220233
Mutex::Locker locker(m_target_list_mutex);
221234
m_selected_target_idx = m_target_list.size();
222235
m_target_list.push_back(target_sp);
236+
237+
223238
}
224239

225240
return error;

0 commit comments

Comments
 (0)