File: | tools/lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp |
Warning: | line 1034, column 3 Value stored to 'do_dlopen_function' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- PlatformPOSIX.cpp ---------------------------------------*- C++ -*-===// |
2 | // |
3 | // The LLVM Compiler Infrastructure |
4 | // |
5 | // This file is distributed under the University of Illinois Open Source |
6 | // License. See LICENSE.TXT for details. |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | #include "PlatformPOSIX.h" |
11 | |
12 | // C Includes |
13 | // C++ Includes |
14 | // Other libraries and framework includes |
15 | // Project includes |
16 | |
17 | #include "lldb/Core/Debugger.h" |
18 | #include "lldb/Core/Module.h" |
19 | #include "lldb/Core/ModuleSpec.h" |
20 | #include "lldb/Core/ValueObject.h" |
21 | #include "lldb/Expression/DiagnosticManager.h" |
22 | #include "lldb/Expression/FunctionCaller.h" |
23 | #include "lldb/Expression/UserExpression.h" |
24 | #include "lldb/Expression/UtilityFunction.h" |
25 | #include "lldb/Host/File.h" |
26 | #include "lldb/Host/FileCache.h" |
27 | #include "lldb/Host/FileSystem.h" |
28 | #include "lldb/Host/Host.h" |
29 | #include "lldb/Host/HostInfo.h" |
30 | #include "lldb/Symbol/ClangASTContext.h" |
31 | #include "lldb/Target/DynamicLoader.h" |
32 | #include "lldb/Target/ExecutionContext.h" |
33 | #include "lldb/Target/Process.h" |
34 | #include "lldb/Target/ProcessLaunchInfo.h" |
35 | #include "lldb/Target/Thread.h" |
36 | #include "lldb/Utility/CleanUp.h" |
37 | #include "lldb/Utility/DataBufferHeap.h" |
38 | #include "lldb/Utility/FileSpec.h" |
39 | #include "lldb/Utility/Log.h" |
40 | #include "lldb/Utility/StreamString.h" |
41 | |
42 | using namespace lldb; |
43 | using namespace lldb_private; |
44 | |
45 | //------------------------------------------------------------------ |
46 | /// Default Constructor |
47 | //------------------------------------------------------------------ |
48 | PlatformPOSIX::PlatformPOSIX(bool is_host) |
49 | : Platform(is_host), // This is the local host platform |
50 | m_option_group_platform_rsync(new OptionGroupPlatformRSync()), |
51 | m_option_group_platform_ssh(new OptionGroupPlatformSSH()), |
52 | m_option_group_platform_caching(new OptionGroupPlatformCaching()), |
53 | m_remote_platform_sp() {} |
54 | |
55 | //------------------------------------------------------------------ |
56 | /// Destructor. |
57 | /// |
58 | /// The destructor is virtual since this class is designed to be |
59 | /// inherited from by the plug-in instance. |
60 | //------------------------------------------------------------------ |
61 | PlatformPOSIX::~PlatformPOSIX() {} |
62 | |
63 | bool PlatformPOSIX::GetModuleSpec(const FileSpec &module_file_spec, |
64 | const ArchSpec &arch, |
65 | ModuleSpec &module_spec) { |
66 | if (m_remote_platform_sp) |
67 | return m_remote_platform_sp->GetModuleSpec(module_file_spec, arch, |
68 | module_spec); |
69 | |
70 | return Platform::GetModuleSpec(module_file_spec, arch, module_spec); |
71 | } |
72 | |
73 | lldb_private::OptionGroupOptions *PlatformPOSIX::GetConnectionOptions( |
74 | lldb_private::CommandInterpreter &interpreter) { |
75 | auto iter = m_options.find(&interpreter), end = m_options.end(); |
76 | if (iter == end) { |
77 | std::unique_ptr<lldb_private::OptionGroupOptions> options( |
78 | new OptionGroupOptions()); |
79 | options->Append(m_option_group_platform_rsync.get()); |
80 | options->Append(m_option_group_platform_ssh.get()); |
81 | options->Append(m_option_group_platform_caching.get()); |
82 | m_options[&interpreter] = std::move(options); |
83 | } |
84 | |
85 | return m_options.at(&interpreter).get(); |
86 | } |
87 | |
88 | bool PlatformPOSIX::IsConnected() const { |
89 | if (IsHost()) |
90 | return true; |
91 | else if (m_remote_platform_sp) |
92 | return m_remote_platform_sp->IsConnected(); |
93 | return false; |
94 | } |
95 | |
96 | lldb_private::Status PlatformPOSIX::RunShellCommand( |
97 | const char *command, // Shouldn't be NULL |
98 | const FileSpec & |
99 | working_dir, // Pass empty FileSpec to use the current working directory |
100 | int *status_ptr, // Pass NULL if you don't want the process exit status |
101 | int *signo_ptr, // Pass NULL if you don't want the signal that caused the |
102 | // process to exit |
103 | std::string |
104 | *command_output, // Pass NULL if you don't want the command output |
105 | const Timeout<std::micro> &timeout) { |
106 | if (IsHost()) |
107 | return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr, |
108 | command_output, timeout); |
109 | else { |
110 | if (m_remote_platform_sp) |
111 | return m_remote_platform_sp->RunShellCommand( |
112 | command, working_dir, status_ptr, signo_ptr, command_output, timeout); |
113 | else |
114 | return Status("unable to run a remote command without a platform"); |
115 | } |
116 | } |
117 | |
118 | Status |
119 | PlatformPOSIX::ResolveExecutable(const ModuleSpec &module_spec, |
120 | lldb::ModuleSP &exe_module_sp, |
121 | const FileSpecList *module_search_paths_ptr) { |
122 | Status error; |
123 | // Nothing special to do here, just use the actual file and architecture |
124 | |
125 | char exe_path[PATH_MAX4096]; |
126 | ModuleSpec resolved_module_spec(module_spec); |
127 | |
128 | if (IsHost()) { |
129 | // If we have "ls" as the exe_file, resolve the executable location based |
130 | // on the current path variables |
131 | if (!resolved_module_spec.GetFileSpec().Exists()) { |
132 | resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path)); |
133 | resolved_module_spec.GetFileSpec().SetFile(exe_path, true, |
134 | FileSpec::Style::native); |
135 | } |
136 | |
137 | if (!resolved_module_spec.GetFileSpec().Exists()) |
138 | resolved_module_spec.GetFileSpec().ResolveExecutableLocation(); |
139 | |
140 | // Resolve any executable within a bundle on MacOSX |
141 | Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec()); |
142 | |
143 | if (resolved_module_spec.GetFileSpec().Exists()) |
144 | error.Clear(); |
145 | else { |
146 | const uint32_t permissions = |
147 | resolved_module_spec.GetFileSpec().GetPermissions(); |
148 | if (permissions && (permissions & eFilePermissionsEveryoneR) == 0) |
149 | error.SetErrorStringWithFormat( |
150 | "executable '%s' is not readable", |
151 | resolved_module_spec.GetFileSpec().GetPath().c_str()); |
152 | else |
153 | error.SetErrorStringWithFormat( |
154 | "unable to find executable for '%s'", |
155 | resolved_module_spec.GetFileSpec().GetPath().c_str()); |
156 | } |
157 | } else { |
158 | if (m_remote_platform_sp) { |
159 | error = |
160 | GetCachedExecutable(resolved_module_spec, exe_module_sp, |
161 | module_search_paths_ptr, *m_remote_platform_sp); |
162 | } else { |
163 | // We may connect to a process and use the provided executable (Don't use |
164 | // local $PATH). |
165 | |
166 | // Resolve any executable within a bundle on MacOSX |
167 | Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec()); |
168 | |
169 | if (resolved_module_spec.GetFileSpec().Exists()) |
170 | error.Clear(); |
171 | else |
172 | error.SetErrorStringWithFormat("the platform is not currently " |
173 | "connected, and '%s' doesn't exist in " |
174 | "the system root.", |
175 | exe_path); |
176 | } |
177 | } |
178 | |
179 | if (error.Success()) { |
180 | if (resolved_module_spec.GetArchitecture().IsValid()) { |
181 | error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp, |
182 | module_search_paths_ptr, nullptr, nullptr); |
183 | if (error.Fail()) { |
184 | // If we failed, it may be because the vendor and os aren't known. If |
185 | // that is the case, try setting them to the host architecture and give |
186 | // it another try. |
187 | llvm::Triple &module_triple = |
188 | resolved_module_spec.GetArchitecture().GetTriple(); |
189 | bool is_vendor_specified = |
190 | (module_triple.getVendor() != llvm::Triple::UnknownVendor); |
191 | bool is_os_specified = |
192 | (module_triple.getOS() != llvm::Triple::UnknownOS); |
193 | if (!is_vendor_specified || !is_os_specified) { |
194 | const llvm::Triple &host_triple = |
195 | HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple(); |
196 | |
197 | if (!is_vendor_specified) |
198 | module_triple.setVendorName(host_triple.getVendorName()); |
199 | if (!is_os_specified) |
200 | module_triple.setOSName(host_triple.getOSName()); |
201 | |
202 | error = ModuleList::GetSharedModule(resolved_module_spec, |
203 | exe_module_sp, module_search_paths_ptr, nullptr, nullptr); |
204 | } |
205 | } |
206 | |
207 | // TODO find out why exe_module_sp might be NULL |
208 | if (error.Fail() || !exe_module_sp || !exe_module_sp->GetObjectFile()) { |
209 | exe_module_sp.reset(); |
210 | error.SetErrorStringWithFormat( |
211 | "'%s' doesn't contain the architecture %s", |
212 | resolved_module_spec.GetFileSpec().GetPath().c_str(), |
213 | resolved_module_spec.GetArchitecture().GetArchitectureName()); |
214 | } |
215 | } else { |
216 | // No valid architecture was specified, ask the platform for the |
217 | // architectures that we should be using (in the correct order) and see |
218 | // if we can find a match that way |
219 | StreamString arch_names; |
220 | for (uint32_t idx = 0; GetSupportedArchitectureAtIndex( |
221 | idx, resolved_module_spec.GetArchitecture()); |
222 | ++idx) { |
223 | error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp, |
224 | module_search_paths_ptr, nullptr, nullptr); |
225 | // Did we find an executable using one of the |
226 | if (error.Success()) { |
227 | if (exe_module_sp && exe_module_sp->GetObjectFile()) |
228 | break; |
229 | else |
230 | error.SetErrorToGenericError(); |
231 | } |
232 | |
233 | if (idx > 0) |
234 | arch_names.PutCString(", "); |
235 | arch_names.PutCString( |
236 | resolved_module_spec.GetArchitecture().GetArchitectureName()); |
237 | } |
238 | |
239 | if (error.Fail() || !exe_module_sp) { |
240 | if (resolved_module_spec.GetFileSpec().Readable()) { |
241 | error.SetErrorStringWithFormat( |
242 | "'%s' doesn't contain any '%s' platform architectures: %s", |
243 | resolved_module_spec.GetFileSpec().GetPath().c_str(), |
244 | GetPluginName().GetCString(), arch_names.GetData()); |
245 | } else { |
246 | error.SetErrorStringWithFormat( |
247 | "'%s' is not readable", |
248 | resolved_module_spec.GetFileSpec().GetPath().c_str()); |
249 | } |
250 | } |
251 | } |
252 | } |
253 | |
254 | return error; |
255 | } |
256 | |
257 | Status PlatformPOSIX::GetFileWithUUID(const FileSpec &platform_file, |
258 | const UUID *uuid_ptr, |
259 | FileSpec &local_file) { |
260 | if (IsRemote() && m_remote_platform_sp) |
261 | return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr, |
262 | local_file); |
263 | |
264 | // Default to the local case |
265 | local_file = platform_file; |
266 | return Status(); |
267 | } |
268 | |
269 | bool PlatformPOSIX::GetProcessInfo(lldb::pid_t pid, |
270 | ProcessInstanceInfo &process_info) { |
271 | if (IsHost()) |
272 | return Platform::GetProcessInfo(pid, process_info); |
273 | if (m_remote_platform_sp) |
274 | return m_remote_platform_sp->GetProcessInfo(pid, process_info); |
275 | return false; |
276 | } |
277 | |
278 | uint32_t |
279 | PlatformPOSIX::FindProcesses(const ProcessInstanceInfoMatch &match_info, |
280 | ProcessInstanceInfoList &process_infos) { |
281 | if (IsHost()) |
282 | return Platform::FindProcesses(match_info, process_infos); |
283 | if (m_remote_platform_sp) |
284 | return |
285 | m_remote_platform_sp->FindProcesses(match_info, process_infos); |
286 | return 0; |
287 | } |
288 | |
289 | Status PlatformPOSIX::MakeDirectory(const FileSpec &file_spec, |
290 | uint32_t file_permissions) { |
291 | if (m_remote_platform_sp) |
292 | return m_remote_platform_sp->MakeDirectory(file_spec, file_permissions); |
293 | else |
294 | return Platform::MakeDirectory(file_spec, file_permissions); |
295 | } |
296 | |
297 | Status PlatformPOSIX::GetFilePermissions(const FileSpec &file_spec, |
298 | uint32_t &file_permissions) { |
299 | if (m_remote_platform_sp) |
300 | return m_remote_platform_sp->GetFilePermissions(file_spec, |
301 | file_permissions); |
302 | else |
303 | return Platform::GetFilePermissions(file_spec, file_permissions); |
304 | } |
305 | |
306 | Status PlatformPOSIX::SetFilePermissions(const FileSpec &file_spec, |
307 | uint32_t file_permissions) { |
308 | if (m_remote_platform_sp) |
309 | return m_remote_platform_sp->SetFilePermissions(file_spec, |
310 | file_permissions); |
311 | else |
312 | return Platform::SetFilePermissions(file_spec, file_permissions); |
313 | } |
314 | |
315 | lldb::user_id_t PlatformPOSIX::OpenFile(const FileSpec &file_spec, |
316 | uint32_t flags, uint32_t mode, |
317 | Status &error) { |
318 | if (IsHost()) |
319 | return FileCache::GetInstance().OpenFile(file_spec, flags, mode, error); |
320 | else if (m_remote_platform_sp) |
321 | return m_remote_platform_sp->OpenFile(file_spec, flags, mode, error); |
322 | else |
323 | return Platform::OpenFile(file_spec, flags, mode, error); |
324 | } |
325 | |
326 | bool PlatformPOSIX::CloseFile(lldb::user_id_t fd, Status &error) { |
327 | if (IsHost()) |
328 | return FileCache::GetInstance().CloseFile(fd, error); |
329 | else if (m_remote_platform_sp) |
330 | return m_remote_platform_sp->CloseFile(fd, error); |
331 | else |
332 | return Platform::CloseFile(fd, error); |
333 | } |
334 | |
335 | uint64_t PlatformPOSIX::ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst, |
336 | uint64_t dst_len, Status &error) { |
337 | if (IsHost()) |
338 | return FileCache::GetInstance().ReadFile(fd, offset, dst, dst_len, error); |
339 | else if (m_remote_platform_sp) |
340 | return m_remote_platform_sp->ReadFile(fd, offset, dst, dst_len, error); |
341 | else |
342 | return Platform::ReadFile(fd, offset, dst, dst_len, error); |
343 | } |
344 | |
345 | uint64_t PlatformPOSIX::WriteFile(lldb::user_id_t fd, uint64_t offset, |
346 | const void *src, uint64_t src_len, |
347 | Status &error) { |
348 | if (IsHost()) |
349 | return FileCache::GetInstance().WriteFile(fd, offset, src, src_len, error); |
350 | else if (m_remote_platform_sp) |
351 | return m_remote_platform_sp->WriteFile(fd, offset, src, src_len, error); |
352 | else |
353 | return Platform::WriteFile(fd, offset, src, src_len, error); |
354 | } |
355 | |
356 | static uint32_t chown_file(Platform *platform, const char *path, |
357 | uint32_t uid = UINT32_MAX(4294967295U), |
358 | uint32_t gid = UINT32_MAX(4294967295U)) { |
359 | if (!platform || !path || *path == 0) |
360 | return UINT32_MAX(4294967295U); |
361 | |
362 | if (uid == UINT32_MAX(4294967295U) && gid == UINT32_MAX(4294967295U)) |
363 | return 0; // pretend I did chown correctly - actually I just didn't care |
364 | |
365 | StreamString command; |
366 | command.PutCString("chown "); |
367 | if (uid != UINT32_MAX(4294967295U)) |
368 | command.Printf("%d", uid); |
369 | if (gid != UINT32_MAX(4294967295U)) |
370 | command.Printf(":%d", gid); |
371 | command.Printf("%s", path); |
372 | int status; |
373 | platform->RunShellCommand(command.GetData(), NULL__null, &status, NULL__null, NULL__null, |
374 | std::chrono::seconds(10)); |
375 | return status; |
376 | } |
377 | |
378 | lldb_private::Status |
379 | PlatformPOSIX::PutFile(const lldb_private::FileSpec &source, |
380 | const lldb_private::FileSpec &destination, uint32_t uid, |
381 | uint32_t gid) { |
382 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM(1u << 25))); |
383 | |
384 | if (IsHost()) { |
385 | if (FileSpec::Equal(source, destination, true)) |
386 | return Status(); |
387 | // cp src dst |
388 | // chown uid:gid dst |
389 | std::string src_path(source.GetPath()); |
390 | if (src_path.empty()) |
391 | return Status("unable to get file path for source"); |
392 | std::string dst_path(destination.GetPath()); |
393 | if (dst_path.empty()) |
394 | return Status("unable to get file path for destination"); |
395 | StreamString command; |
396 | command.Printf("cp %s %s", src_path.c_str(), dst_path.c_str()); |
397 | int status; |
398 | RunShellCommand(command.GetData(), NULL__null, &status, NULL__null, NULL__null, |
399 | std::chrono::seconds(10)); |
400 | if (status != 0) |
401 | return Status("unable to perform copy"); |
402 | if (uid == UINT32_MAX(4294967295U) && gid == UINT32_MAX(4294967295U)) |
403 | return Status(); |
404 | if (chown_file(this, dst_path.c_str(), uid, gid) != 0) |
405 | return Status("unable to perform chown"); |
406 | return Status(); |
407 | } else if (m_remote_platform_sp) { |
408 | if (GetSupportsRSync()) { |
409 | std::string src_path(source.GetPath()); |
410 | if (src_path.empty()) |
411 | return Status("unable to get file path for source"); |
412 | std::string dst_path(destination.GetPath()); |
413 | if (dst_path.empty()) |
414 | return Status("unable to get file path for destination"); |
415 | StreamString command; |
416 | if (GetIgnoresRemoteHostname()) { |
417 | if (!GetRSyncPrefix()) |
418 | command.Printf("rsync %s %s %s", GetRSyncOpts(), src_path.c_str(), |
419 | dst_path.c_str()); |
420 | else |
421 | command.Printf("rsync %s %s %s%s", GetRSyncOpts(), src_path.c_str(), |
422 | GetRSyncPrefix(), dst_path.c_str()); |
423 | } else |
424 | command.Printf("rsync %s %s %s:%s", GetRSyncOpts(), src_path.c_str(), |
425 | GetHostname(), dst_path.c_str()); |
426 | if (log) |
427 | log->Printf("[PutFile] Running command: %s\n", command.GetData()); |
428 | int retcode; |
429 | Host::RunShellCommand(command.GetData(), NULL__null, &retcode, NULL__null, NULL__null, |
430 | std::chrono::minutes(1)); |
431 | if (retcode == 0) { |
432 | // Don't chown a local file for a remote system |
433 | // if (chown_file(this,dst_path.c_str(),uid,gid) != 0) |
434 | // return Status("unable to perform chown"); |
435 | return Status(); |
436 | } |
437 | // if we are still here rsync has failed - let's try the slow way before |
438 | // giving up |
439 | } |
440 | } |
441 | return Platform::PutFile(source, destination, uid, gid); |
442 | } |
443 | |
444 | lldb::user_id_t PlatformPOSIX::GetFileSize(const FileSpec &file_spec) { |
445 | if (IsHost()) { |
446 | uint64_t Size; |
447 | if (llvm::sys::fs::file_size(file_spec.GetPath(), Size)) |
448 | return 0; |
449 | return Size; |
450 | } else if (m_remote_platform_sp) |
451 | return m_remote_platform_sp->GetFileSize(file_spec); |
452 | else |
453 | return Platform::GetFileSize(file_spec); |
454 | } |
455 | |
456 | Status PlatformPOSIX::CreateSymlink(const FileSpec &src, const FileSpec &dst) { |
457 | if (IsHost()) |
458 | return FileSystem::Symlink(src, dst); |
459 | else if (m_remote_platform_sp) |
460 | return m_remote_platform_sp->CreateSymlink(src, dst); |
461 | else |
462 | return Platform::CreateSymlink(src, dst); |
463 | } |
464 | |
465 | bool PlatformPOSIX::GetFileExists(const FileSpec &file_spec) { |
466 | if (IsHost()) |
467 | return file_spec.Exists(); |
468 | else if (m_remote_platform_sp) |
469 | return m_remote_platform_sp->GetFileExists(file_spec); |
470 | else |
471 | return Platform::GetFileExists(file_spec); |
472 | } |
473 | |
474 | Status PlatformPOSIX::Unlink(const FileSpec &file_spec) { |
475 | if (IsHost()) |
476 | return llvm::sys::fs::remove(file_spec.GetPath()); |
477 | else if (m_remote_platform_sp) |
478 | return m_remote_platform_sp->Unlink(file_spec); |
479 | else |
480 | return Platform::Unlink(file_spec); |
481 | } |
482 | |
483 | lldb_private::Status PlatformPOSIX::GetFile( |
484 | const lldb_private::FileSpec &source, // remote file path |
485 | const lldb_private::FileSpec &destination) // local file path |
486 | { |
487 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM(1u << 25))); |
488 | |
489 | // Check the args, first. |
490 | std::string src_path(source.GetPath()); |
491 | if (src_path.empty()) |
492 | return Status("unable to get file path for source"); |
493 | std::string dst_path(destination.GetPath()); |
494 | if (dst_path.empty()) |
495 | return Status("unable to get file path for destination"); |
496 | if (IsHost()) { |
497 | if (FileSpec::Equal(source, destination, true)) |
498 | return Status("local scenario->source and destination are the same file " |
499 | "path: no operation performed"); |
500 | // cp src dst |
501 | StreamString cp_command; |
502 | cp_command.Printf("cp %s %s", src_path.c_str(), dst_path.c_str()); |
503 | int status; |
504 | RunShellCommand(cp_command.GetData(), NULL__null, &status, NULL__null, NULL__null, |
505 | std::chrono::seconds(10)); |
506 | if (status != 0) |
507 | return Status("unable to perform copy"); |
508 | return Status(); |
509 | } else if (m_remote_platform_sp) { |
510 | if (GetSupportsRSync()) { |
511 | StreamString command; |
512 | if (GetIgnoresRemoteHostname()) { |
513 | if (!GetRSyncPrefix()) |
514 | command.Printf("rsync %s %s %s", GetRSyncOpts(), src_path.c_str(), |
515 | dst_path.c_str()); |
516 | else |
517 | command.Printf("rsync %s %s%s %s", GetRSyncOpts(), GetRSyncPrefix(), |
518 | src_path.c_str(), dst_path.c_str()); |
519 | } else |
520 | command.Printf("rsync %s %s:%s %s", GetRSyncOpts(), |
521 | m_remote_platform_sp->GetHostname(), src_path.c_str(), |
522 | dst_path.c_str()); |
523 | if (log) |
524 | log->Printf("[GetFile] Running command: %s\n", command.GetData()); |
525 | int retcode; |
526 | Host::RunShellCommand(command.GetData(), NULL__null, &retcode, NULL__null, NULL__null, |
527 | std::chrono::minutes(1)); |
528 | if (retcode == 0) |
529 | return Status(); |
530 | // If we are here, rsync has failed - let's try the slow way before |
531 | // giving up |
532 | } |
533 | // open src and dst |
534 | // read/write, read/write, read/write, ... |
535 | // close src |
536 | // close dst |
537 | if (log) |
538 | log->Printf("[GetFile] Using block by block transfer....\n"); |
539 | Status error; |
540 | user_id_t fd_src = OpenFile(source, File::eOpenOptionRead, |
541 | lldb::eFilePermissionsFileDefault, error); |
542 | |
543 | if (fd_src == UINT64_MAX(18446744073709551615UL)) |
544 | return Status("unable to open source file"); |
545 | |
546 | uint32_t permissions = 0; |
547 | error = GetFilePermissions(source, permissions); |
548 | |
549 | if (permissions == 0) |
550 | permissions = lldb::eFilePermissionsFileDefault; |
551 | |
552 | user_id_t fd_dst = FileCache::GetInstance().OpenFile( |
553 | destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite | |
554 | File::eOpenOptionTruncate, |
555 | permissions, error); |
556 | |
557 | if (fd_dst == UINT64_MAX(18446744073709551615UL)) { |
558 | if (error.Success()) |
559 | error.SetErrorString("unable to open destination file"); |
560 | } |
561 | |
562 | if (error.Success()) { |
563 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0)); |
564 | uint64_t offset = 0; |
565 | error.Clear(); |
566 | while (error.Success()) { |
567 | const uint64_t n_read = ReadFile(fd_src, offset, buffer_sp->GetBytes(), |
568 | buffer_sp->GetByteSize(), error); |
569 | if (error.Fail()) |
570 | break; |
571 | if (n_read == 0) |
572 | break; |
573 | if (FileCache::GetInstance().WriteFile(fd_dst, offset, |
574 | buffer_sp->GetBytes(), n_read, |
575 | error) != n_read) { |
576 | if (!error.Fail()) |
577 | error.SetErrorString("unable to write to destination file"); |
578 | break; |
579 | } |
580 | offset += n_read; |
581 | } |
582 | } |
583 | // Ignore the close error of src. |
584 | if (fd_src != UINT64_MAX(18446744073709551615UL)) |
585 | CloseFile(fd_src, error); |
586 | // And close the dst file descriptot. |
587 | if (fd_dst != UINT64_MAX(18446744073709551615UL) && |
588 | !FileCache::GetInstance().CloseFile(fd_dst, error)) { |
589 | if (!error.Fail()) |
590 | error.SetErrorString("unable to close destination file"); |
591 | } |
592 | return error; |
593 | } |
594 | return Platform::GetFile(source, destination); |
595 | } |
596 | |
597 | std::string PlatformPOSIX::GetPlatformSpecificConnectionInformation() { |
598 | StreamString stream; |
599 | if (GetSupportsRSync()) { |
600 | stream.PutCString("rsync"); |
601 | if ((GetRSyncOpts() && *GetRSyncOpts()) || |
602 | (GetRSyncPrefix() && *GetRSyncPrefix()) || GetIgnoresRemoteHostname()) { |
603 | stream.Printf(", options: "); |
604 | if (GetRSyncOpts() && *GetRSyncOpts()) |
605 | stream.Printf("'%s' ", GetRSyncOpts()); |
606 | stream.Printf(", prefix: "); |
607 | if (GetRSyncPrefix() && *GetRSyncPrefix()) |
608 | stream.Printf("'%s' ", GetRSyncPrefix()); |
609 | if (GetIgnoresRemoteHostname()) |
610 | stream.Printf("ignore remote-hostname "); |
611 | } |
612 | } |
613 | if (GetSupportsSSH()) { |
614 | stream.PutCString("ssh"); |
615 | if (GetSSHOpts() && *GetSSHOpts()) |
616 | stream.Printf(", options: '%s' ", GetSSHOpts()); |
617 | } |
618 | if (GetLocalCacheDirectory() && *GetLocalCacheDirectory()) |
619 | stream.Printf("cache dir: %s", GetLocalCacheDirectory()); |
620 | if (stream.GetSize()) |
621 | return stream.GetString(); |
622 | else |
623 | return ""; |
624 | } |
625 | |
626 | bool PlatformPOSIX::CalculateMD5(const FileSpec &file_spec, uint64_t &low, |
627 | uint64_t &high) { |
628 | if (IsHost()) |
629 | return Platform::CalculateMD5(file_spec, low, high); |
630 | if (m_remote_platform_sp) |
631 | return m_remote_platform_sp->CalculateMD5(file_spec, low, high); |
632 | return false; |
633 | } |
634 | |
635 | const lldb::UnixSignalsSP &PlatformPOSIX::GetRemoteUnixSignals() { |
636 | if (IsRemote() && m_remote_platform_sp) |
637 | return m_remote_platform_sp->GetRemoteUnixSignals(); |
638 | return Platform::GetRemoteUnixSignals(); |
639 | } |
640 | |
641 | FileSpec PlatformPOSIX::GetRemoteWorkingDirectory() { |
642 | if (IsRemote() && m_remote_platform_sp) |
643 | return m_remote_platform_sp->GetRemoteWorkingDirectory(); |
644 | else |
645 | return Platform::GetRemoteWorkingDirectory(); |
646 | } |
647 | |
648 | bool PlatformPOSIX::SetRemoteWorkingDirectory(const FileSpec &working_dir) { |
649 | if (IsRemote() && m_remote_platform_sp) |
650 | return m_remote_platform_sp->SetRemoteWorkingDirectory(working_dir); |
651 | else |
652 | return Platform::SetRemoteWorkingDirectory(working_dir); |
653 | } |
654 | |
655 | bool PlatformPOSIX::GetRemoteOSVersion() { |
656 | if (m_remote_platform_sp) { |
657 | m_os_version = m_remote_platform_sp->GetOSVersion(); |
658 | return !m_os_version.empty(); |
659 | } |
660 | return false; |
661 | } |
662 | |
663 | bool PlatformPOSIX::GetRemoteOSBuildString(std::string &s) { |
664 | if (m_remote_platform_sp) |
665 | return m_remote_platform_sp->GetRemoteOSBuildString(s); |
666 | s.clear(); |
667 | return false; |
668 | } |
669 | |
670 | Environment PlatformPOSIX::GetEnvironment() { |
671 | if (IsRemote()) { |
672 | if (m_remote_platform_sp) |
673 | return m_remote_platform_sp->GetEnvironment(); |
674 | return Environment(); |
675 | } |
676 | return Host::GetEnvironment(); |
677 | } |
678 | |
679 | bool PlatformPOSIX::GetRemoteOSKernelDescription(std::string &s) { |
680 | if (m_remote_platform_sp) |
681 | return m_remote_platform_sp->GetRemoteOSKernelDescription(s); |
682 | s.clear(); |
683 | return false; |
684 | } |
685 | |
686 | // Remote Platform subclasses need to override this function |
687 | ArchSpec PlatformPOSIX::GetRemoteSystemArchitecture() { |
688 | if (m_remote_platform_sp) |
689 | return m_remote_platform_sp->GetRemoteSystemArchitecture(); |
690 | return ArchSpec(); |
691 | } |
692 | |
693 | const char *PlatformPOSIX::GetHostname() { |
694 | if (IsHost()) |
695 | return Platform::GetHostname(); |
696 | |
697 | if (m_remote_platform_sp) |
698 | return m_remote_platform_sp->GetHostname(); |
699 | return NULL__null; |
700 | } |
701 | |
702 | const char *PlatformPOSIX::GetUserName(uint32_t uid) { |
703 | // Check the cache in Platform in case we have already looked this uid up |
704 | const char *user_name = Platform::GetUserName(uid); |
705 | if (user_name) |
706 | return user_name; |
707 | |
708 | if (IsRemote() && m_remote_platform_sp) |
709 | return m_remote_platform_sp->GetUserName(uid); |
710 | return NULL__null; |
711 | } |
712 | |
713 | const char *PlatformPOSIX::GetGroupName(uint32_t gid) { |
714 | const char *group_name = Platform::GetGroupName(gid); |
715 | if (group_name) |
716 | return group_name; |
717 | |
718 | if (IsRemote() && m_remote_platform_sp) |
719 | return m_remote_platform_sp->GetGroupName(gid); |
720 | return NULL__null; |
721 | } |
722 | |
723 | Status PlatformPOSIX::ConnectRemote(Args &args) { |
724 | Status error; |
725 | if (IsHost()) { |
726 | error.SetErrorStringWithFormat( |
727 | "can't connect to the host platform '%s', always connected", |
728 | GetPluginName().GetCString()); |
729 | } else { |
730 | if (!m_remote_platform_sp) |
731 | m_remote_platform_sp = |
732 | Platform::Create(ConstString("remote-gdb-server"), error); |
733 | |
734 | if (m_remote_platform_sp && error.Success()) |
735 | error = m_remote_platform_sp->ConnectRemote(args); |
736 | else |
737 | error.SetErrorString("failed to create a 'remote-gdb-server' platform"); |
738 | |
739 | if (error.Fail()) |
740 | m_remote_platform_sp.reset(); |
741 | } |
742 | |
743 | if (error.Success() && m_remote_platform_sp) { |
744 | if (m_option_group_platform_rsync.get() && |
745 | m_option_group_platform_ssh.get() && |
746 | m_option_group_platform_caching.get()) { |
747 | if (m_option_group_platform_rsync->m_rsync) { |
748 | SetSupportsRSync(true); |
749 | SetRSyncOpts(m_option_group_platform_rsync->m_rsync_opts.c_str()); |
750 | SetRSyncPrefix(m_option_group_platform_rsync->m_rsync_prefix.c_str()); |
751 | SetIgnoresRemoteHostname( |
752 | m_option_group_platform_rsync->m_ignores_remote_hostname); |
753 | } |
754 | if (m_option_group_platform_ssh->m_ssh) { |
755 | SetSupportsSSH(true); |
756 | SetSSHOpts(m_option_group_platform_ssh->m_ssh_opts.c_str()); |
757 | } |
758 | SetLocalCacheDirectory( |
759 | m_option_group_platform_caching->m_cache_dir.c_str()); |
760 | } |
761 | } |
762 | |
763 | return error; |
764 | } |
765 | |
766 | Status PlatformPOSIX::DisconnectRemote() { |
767 | Status error; |
768 | |
769 | if (IsHost()) { |
770 | error.SetErrorStringWithFormat( |
771 | "can't disconnect from the host platform '%s', always connected", |
772 | GetPluginName().GetCString()); |
773 | } else { |
774 | if (m_remote_platform_sp) |
775 | error = m_remote_platform_sp->DisconnectRemote(); |
776 | else |
777 | error.SetErrorString("the platform is not currently connected"); |
778 | } |
779 | return error; |
780 | } |
781 | |
782 | Status PlatformPOSIX::LaunchProcess(ProcessLaunchInfo &launch_info) { |
783 | Status error; |
784 | |
785 | if (IsHost()) { |
786 | error = Platform::LaunchProcess(launch_info); |
787 | } else { |
788 | if (m_remote_platform_sp) |
789 | error = m_remote_platform_sp->LaunchProcess(launch_info); |
790 | else |
791 | error.SetErrorString("the platform is not currently connected"); |
792 | } |
793 | return error; |
794 | } |
795 | |
796 | lldb_private::Status PlatformPOSIX::KillProcess(const lldb::pid_t pid) { |
797 | if (IsHost()) |
798 | return Platform::KillProcess(pid); |
799 | |
800 | if (m_remote_platform_sp) |
801 | return m_remote_platform_sp->KillProcess(pid); |
802 | |
803 | return Status("the platform is not currently connected"); |
804 | } |
805 | |
806 | lldb::ProcessSP PlatformPOSIX::Attach(ProcessAttachInfo &attach_info, |
807 | Debugger &debugger, Target *target, |
808 | Status &error) { |
809 | lldb::ProcessSP process_sp; |
810 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM(1u << 25))); |
811 | |
812 | if (IsHost()) { |
813 | if (target == NULL__null) { |
814 | TargetSP new_target_sp; |
815 | |
816 | error = debugger.GetTargetList().CreateTarget( |
817 | debugger, "", "", eLoadDependentsNo, NULL__null, new_target_sp); |
818 | target = new_target_sp.get(); |
819 | if (log) |
820 | log->Printf("PlatformPOSIX::%s created new target", __FUNCTION__); |
821 | } else { |
822 | error.Clear(); |
823 | if (log) |
824 | log->Printf("PlatformPOSIX::%s target already existed, setting target", |
825 | __FUNCTION__); |
826 | } |
827 | |
828 | if (target && error.Success()) { |
829 | debugger.GetTargetList().SetSelectedTarget(target); |
830 | if (log) { |
831 | ModuleSP exe_module_sp = target->GetExecutableModule(); |
832 | log->Printf("PlatformPOSIX::%s set selected target to %p %s", |
833 | __FUNCTION__, (void *)target, |
834 | exe_module_sp |
835 | ? exe_module_sp->GetFileSpec().GetPath().c_str() |
836 | : "<null>"); |
837 | } |
838 | |
839 | process_sp = |
840 | target->CreateProcess(attach_info.GetListenerForProcess(debugger), |
841 | attach_info.GetProcessPluginName(), NULL__null); |
842 | |
843 | if (process_sp) { |
844 | ListenerSP listener_sp = attach_info.GetHijackListener(); |
845 | if (listener_sp == nullptr) { |
846 | listener_sp = |
847 | Listener::MakeListener("lldb.PlatformPOSIX.attach.hijack"); |
848 | attach_info.SetHijackListener(listener_sp); |
849 | } |
850 | process_sp->HijackProcessEvents(listener_sp); |
851 | error = process_sp->Attach(attach_info); |
852 | } |
853 | } |
854 | } else { |
855 | if (m_remote_platform_sp) |
856 | process_sp = |
857 | m_remote_platform_sp->Attach(attach_info, debugger, target, error); |
858 | else |
859 | error.SetErrorString("the platform is not currently connected"); |
860 | } |
861 | return process_sp; |
862 | } |
863 | |
864 | lldb::ProcessSP |
865 | PlatformPOSIX::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger, |
866 | Target *target, // Can be NULL, if NULL create a new |
867 | // target, else use existing one |
868 | Status &error) { |
869 | ProcessSP process_sp; |
870 | |
871 | if (IsHost()) { |
872 | // We are going to hand this process off to debugserver which will be in |
873 | // charge of setting the exit status. However, we still need to reap it |
874 | // from lldb. So, make sure we use a exit callback which does not set exit |
875 | // status. |
876 | const bool monitor_signals = false; |
877 | launch_info.SetMonitorProcessCallback( |
878 | &ProcessLaunchInfo::NoOpMonitorCallback, monitor_signals); |
879 | process_sp = Platform::DebugProcess(launch_info, debugger, target, error); |
880 | } else { |
881 | if (m_remote_platform_sp) |
882 | process_sp = m_remote_platform_sp->DebugProcess(launch_info, debugger, |
883 | target, error); |
884 | else |
885 | error.SetErrorString("the platform is not currently connected"); |
886 | } |
887 | return process_sp; |
888 | } |
889 | |
890 | void PlatformPOSIX::CalculateTrapHandlerSymbolNames() { |
891 | m_trap_handlers.push_back(ConstString("_sigtramp")); |
892 | } |
893 | |
894 | Status PlatformPOSIX::EvaluateLibdlExpression( |
895 | lldb_private::Process *process, const char *expr_cstr, |
896 | llvm::StringRef expr_prefix, lldb::ValueObjectSP &result_valobj_sp) { |
897 | DynamicLoader *loader = process->GetDynamicLoader(); |
898 | if (loader) { |
899 | Status error = loader->CanLoadImage(); |
900 | if (error.Fail()) |
901 | return error; |
902 | } |
903 | |
904 | ThreadSP thread_sp(process->GetThreadList().GetExpressionExecutionThread()); |
905 | if (!thread_sp) |
906 | return Status("Selected thread isn't valid"); |
907 | |
908 | StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0)); |
909 | if (!frame_sp) |
910 | return Status("Frame 0 isn't valid"); |
911 | |
912 | ExecutionContext exe_ctx; |
913 | frame_sp->CalculateExecutionContext(exe_ctx); |
914 | EvaluateExpressionOptions expr_options; |
915 | expr_options.SetUnwindOnError(true); |
916 | expr_options.SetIgnoreBreakpoints(true); |
917 | expr_options.SetExecutionPolicy(eExecutionPolicyAlways); |
918 | expr_options.SetLanguage(eLanguageTypeC_plus_plus); |
919 | expr_options.SetTrapExceptions(false); // dlopen can't throw exceptions, so |
920 | // don't do the work to trap them. |
921 | expr_options.SetTimeout(std::chrono::seconds(2)); |
922 | |
923 | Status expr_error; |
924 | ExpressionResults result = |
925 | UserExpression::Evaluate(exe_ctx, expr_options, expr_cstr, expr_prefix, |
926 | result_valobj_sp, expr_error); |
927 | if (result != eExpressionCompleted) |
928 | return expr_error; |
929 | |
930 | if (result_valobj_sp->GetError().Fail()) |
931 | return result_valobj_sp->GetError(); |
932 | return Status(); |
933 | } |
934 | |
935 | std::unique_ptr<UtilityFunction> |
936 | PlatformPOSIX::MakeLoadImageUtilityFunction(ExecutionContext &exe_ctx, |
937 | Status &error) { |
938 | // Remember to prepend this with the prefix from |
939 | // GetLibdlFunctionDeclarations. The returned values are all in |
940 | // __lldb_dlopen_result for consistency. The wrapper returns a void * but |
941 | // doesn't use it because UtilityFunctions don't work with void returns at |
942 | // present. |
943 | static const char *dlopen_wrapper_code = R"( |
944 | struct __lldb_dlopen_result { |
945 | void *image_ptr; |
946 | const char *error_str; |
947 | }; |
948 | |
949 | extern void *memcpy(void *, const void *, size_t size); |
950 | extern size_t strlen(const char *); |
951 | |
952 | |
953 | void * __lldb_dlopen_wrapper (const char *name, |
954 | const char *path_strings, |
955 | char *buffer, |
956 | __lldb_dlopen_result *result_ptr) |
957 | { |
958 | // This is the case where the name is the full path: |
959 | if (!path_strings) { |
960 | result_ptr->image_ptr = dlopen(name, 2); |
961 | if (result_ptr->image_ptr) |
962 | result_ptr->error_str = nullptr; |
963 | return nullptr; |
964 | } |
965 | |
966 | // This is the case where we have a list of paths: |
967 | size_t name_len = strlen(name); |
968 | while (path_strings && path_strings[0] != '\0') { |
969 | size_t path_len = strlen(path_strings); |
970 | memcpy((void *) buffer, (void *) path_strings, path_len); |
971 | buffer[path_len] = '/'; |
972 | char *target_ptr = buffer+path_len+1; |
973 | memcpy((void *) target_ptr, (void *) name, name_len + 1); |
974 | result_ptr->image_ptr = dlopen(buffer, 2); |
975 | if (result_ptr->image_ptr) { |
976 | result_ptr->error_str = nullptr; |
977 | break; |
978 | } |
979 | result_ptr->error_str = dlerror(); |
980 | path_strings = path_strings + path_len + 1; |
981 | } |
982 | return nullptr; |
983 | } |
984 | )"; |
985 | |
986 | static const char *dlopen_wrapper_name = "__lldb_dlopen_wrapper"; |
987 | Process *process = exe_ctx.GetProcessSP().get(); |
988 | // Insert the dlopen shim defines into our generic expression: |
989 | std::string expr(GetLibdlFunctionDeclarations(process)); |
990 | expr.append(dlopen_wrapper_code); |
991 | Status utility_error; |
992 | DiagnosticManager diagnostics; |
993 | |
994 | std::unique_ptr<UtilityFunction> dlopen_utility_func_up(process |
995 | ->GetTarget().GetUtilityFunctionForLanguage(expr.c_str(), |
996 | eLanguageTypeObjC, |
997 | dlopen_wrapper_name, |
998 | utility_error)); |
999 | if (utility_error.Fail()) { |
1000 | error.SetErrorStringWithFormat("dlopen error: could not make utility" |
1001 | "function: %s", utility_error.AsCString()); |
1002 | return nullptr; |
1003 | } |
1004 | if (!dlopen_utility_func_up->Install(diagnostics, exe_ctx)) { |
1005 | error.SetErrorStringWithFormat("dlopen error: could not install utility" |
1006 | "function: %s", |
1007 | diagnostics.GetString().c_str()); |
1008 | return nullptr; |
1009 | } |
1010 | |
1011 | Value value; |
1012 | ValueList arguments; |
1013 | FunctionCaller *do_dlopen_function = nullptr; |
1014 | |
1015 | // Fetch the clang types we will need: |
1016 | ClangASTContext *ast = process->GetTarget().GetScratchClangASTContext(); |
1017 | |
1018 | CompilerType clang_void_pointer_type |
1019 | = ast->GetBasicType(eBasicTypeVoid).GetPointerType(); |
1020 | CompilerType clang_char_pointer_type |
1021 | = ast->GetBasicType(eBasicTypeChar).GetPointerType(); |
1022 | |
1023 | // We are passing four arguments, the basename, the list of places to look, |
1024 | // a buffer big enough for all the path + name combos, and |
1025 | // a pointer to the storage we've made for the result: |
1026 | value.SetValueType(Value::eValueTypeScalar); |
1027 | value.SetCompilerType(clang_void_pointer_type); |
1028 | arguments.PushValue(value); |
1029 | value.SetCompilerType(clang_char_pointer_type); |
1030 | arguments.PushValue(value); |
1031 | arguments.PushValue(value); |
1032 | arguments.PushValue(value); |
1033 | |
1034 | do_dlopen_function = dlopen_utility_func_up->MakeFunctionCaller( |
Value stored to 'do_dlopen_function' is never read | |
1035 | clang_void_pointer_type, arguments, exe_ctx.GetThreadSP(), utility_error); |
1036 | if (utility_error.Fail()) { |
1037 | error.SetErrorStringWithFormat("dlopen error: could not make function" |
1038 | "caller: %s", utility_error.AsCString()); |
1039 | return nullptr; |
1040 | } |
1041 | |
1042 | do_dlopen_function = dlopen_utility_func_up->GetFunctionCaller(); |
1043 | if (!do_dlopen_function) { |
1044 | error.SetErrorString("dlopen error: could not get function caller."); |
1045 | return nullptr; |
1046 | } |
1047 | |
1048 | // We made a good utility function, so cache it in the process: |
1049 | return dlopen_utility_func_up; |
1050 | } |
1051 | |
1052 | uint32_t PlatformPOSIX::DoLoadImage(lldb_private::Process *process, |
1053 | const lldb_private::FileSpec &remote_file, |
1054 | const std::vector<std::string> *paths, |
1055 | lldb_private::Status &error, |
1056 | lldb_private::FileSpec *loaded_image) { |
1057 | if (loaded_image) |
1058 | loaded_image->Clear(); |
1059 | |
1060 | std::string path; |
1061 | path = remote_file.GetPath(); |
1062 | |
1063 | ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread(); |
1064 | if (!thread_sp) { |
1065 | error.SetErrorString("dlopen error: no thread available to call dlopen."); |
1066 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1067 | } |
1068 | |
1069 | DiagnosticManager diagnostics; |
1070 | |
1071 | ExecutionContext exe_ctx; |
1072 | thread_sp->CalculateExecutionContext(exe_ctx); |
1073 | |
1074 | Status utility_error; |
1075 | UtilityFunction *dlopen_utility_func; |
1076 | ValueList arguments; |
1077 | FunctionCaller *do_dlopen_function = nullptr; |
1078 | |
1079 | // The UtilityFunction is held in the Process. Platforms don't track the |
1080 | // lifespan of the Targets that use them, we can't put this in the Platform. |
1081 | dlopen_utility_func = process->GetLoadImageUtilityFunction( |
1082 | this, [&]() -> std::unique_ptr<UtilityFunction> { |
1083 | return MakeLoadImageUtilityFunction(exe_ctx, error); |
1084 | }); |
1085 | // If we couldn't make it, the error will be in error, so we can exit here. |
1086 | if (!dlopen_utility_func) |
1087 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1088 | |
1089 | do_dlopen_function = dlopen_utility_func->GetFunctionCaller(); |
1090 | if (!do_dlopen_function) { |
1091 | error.SetErrorString("dlopen error: could not get function caller."); |
1092 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1093 | } |
1094 | arguments = do_dlopen_function->GetArgumentValues(); |
1095 | |
1096 | // Now insert the path we are searching for and the result structure into the |
1097 | // target. |
1098 | uint32_t permissions = ePermissionsReadable|ePermissionsWritable; |
1099 | size_t path_len = path.size() + 1; |
1100 | lldb::addr_t path_addr = process->AllocateMemory(path_len, |
1101 | permissions, |
1102 | utility_error); |
1103 | if (path_addr == LLDB_INVALID_ADDRESS(18446744073709551615UL)) { |
1104 | error.SetErrorStringWithFormat("dlopen error: could not allocate memory" |
1105 | "for path: %s", utility_error.AsCString()); |
1106 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1107 | } |
1108 | |
1109 | // Make sure we deallocate the input string memory: |
1110 | CleanUp path_cleanup([process, path_addr] { |
1111 | process->DeallocateMemory(path_addr); |
1112 | }); |
1113 | |
1114 | process->WriteMemory(path_addr, path.c_str(), path_len, utility_error); |
1115 | if (utility_error.Fail()) { |
1116 | error.SetErrorStringWithFormat("dlopen error: could not write path string:" |
1117 | " %s", utility_error.AsCString()); |
1118 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1119 | } |
1120 | |
1121 | // Make space for our return structure. It is two pointers big: the token |
1122 | // and the error string. |
1123 | const uint32_t addr_size = process->GetAddressByteSize(); |
1124 | lldb::addr_t return_addr = process->CallocateMemory(2*addr_size, |
1125 | permissions, |
1126 | utility_error); |
1127 | if (utility_error.Fail()) { |
1128 | error.SetErrorStringWithFormat("dlopen error: could not allocate memory" |
1129 | "for path: %s", utility_error.AsCString()); |
1130 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1131 | } |
1132 | |
1133 | // Make sure we deallocate the result structure memory |
1134 | CleanUp return_cleanup([process, return_addr] { |
1135 | process->DeallocateMemory(return_addr); |
1136 | }); |
1137 | |
1138 | // This will be the address of the storage for paths, if we are using them, |
1139 | // or nullptr to signal we aren't. |
1140 | lldb::addr_t path_array_addr = 0x0; |
1141 | llvm::Optional<CleanUp> path_array_cleanup; |
1142 | |
1143 | // This is the address to a buffer large enough to hold the largest path |
1144 | // conjoined with the library name we're passing in. This is a convenience |
1145 | // to avoid having to call malloc in the dlopen function. |
1146 | lldb::addr_t buffer_addr = 0x0; |
1147 | llvm::Optional<CleanUp> buffer_cleanup; |
1148 | |
1149 | // Set the values into our args and write them to the target: |
1150 | if (paths != nullptr) { |
1151 | // First insert the paths into the target. This is expected to be a |
1152 | // continuous buffer with the strings laid out null terminated and |
1153 | // end to end with an empty string terminating the buffer. |
1154 | // We also compute the buffer's required size as we go. |
1155 | size_t buffer_size = 0; |
1156 | std::string path_array; |
1157 | for (auto path : *paths) { |
1158 | // Don't insert empty paths, they will make us abort the path |
1159 | // search prematurely. |
1160 | if (path.empty()) |
1161 | continue; |
1162 | size_t path_size = path.size(); |
1163 | path_array.append(path); |
1164 | path_array.push_back('\0'); |
1165 | if (path_size > buffer_size) |
1166 | buffer_size = path_size; |
1167 | } |
1168 | path_array.push_back('\0'); |
1169 | |
1170 | path_array_addr = process->AllocateMemory(path_array.size(), |
1171 | permissions, |
1172 | utility_error); |
1173 | if (path_array_addr == LLDB_INVALID_ADDRESS(18446744073709551615UL)) { |
1174 | error.SetErrorStringWithFormat("dlopen error: could not allocate memory" |
1175 | "for path array: %s", |
1176 | utility_error.AsCString()); |
1177 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1178 | } |
1179 | |
1180 | // Make sure we deallocate the paths array. |
1181 | path_array_cleanup.emplace([process, path_array_addr] { |
1182 | process->DeallocateMemory(path_array_addr); |
1183 | }); |
1184 | |
1185 | process->WriteMemory(path_array_addr, path_array.data(), |
1186 | path_array.size(), utility_error); |
1187 | |
1188 | if (utility_error.Fail()) { |
1189 | error.SetErrorStringWithFormat("dlopen error: could not write path array:" |
1190 | " %s", utility_error.AsCString()); |
1191 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1192 | } |
1193 | // Now make spaces in the target for the buffer. We need to add one for |
1194 | // the '/' that the utility function will insert and one for the '\0': |
1195 | buffer_size += path.size() + 2; |
1196 | |
1197 | buffer_addr = process->AllocateMemory(buffer_size, |
1198 | permissions, |
1199 | utility_error); |
1200 | if (buffer_addr == LLDB_INVALID_ADDRESS(18446744073709551615UL)) { |
1201 | error.SetErrorStringWithFormat("dlopen error: could not allocate memory" |
1202 | "for buffer: %s", |
1203 | utility_error.AsCString()); |
1204 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1205 | } |
1206 | |
1207 | // Make sure we deallocate the buffer memory: |
1208 | buffer_cleanup.emplace([process, buffer_addr] { |
1209 | process->DeallocateMemory(buffer_addr); |
1210 | }); |
1211 | } |
1212 | |
1213 | arguments.GetValueAtIndex(0)->GetScalar() = path_addr; |
1214 | arguments.GetValueAtIndex(1)->GetScalar() = path_array_addr; |
1215 | arguments.GetValueAtIndex(2)->GetScalar() = buffer_addr; |
1216 | arguments.GetValueAtIndex(3)->GetScalar() = return_addr; |
1217 | |
1218 | lldb::addr_t func_args_addr = LLDB_INVALID_ADDRESS(18446744073709551615UL); |
1219 | |
1220 | diagnostics.Clear(); |
1221 | if (!do_dlopen_function->WriteFunctionArguments(exe_ctx, |
1222 | func_args_addr, |
1223 | arguments, |
1224 | diagnostics)) { |
1225 | error.SetErrorStringWithFormat("dlopen error: could not write function " |
1226 | "arguments: %s", |
1227 | diagnostics.GetString().c_str()); |
1228 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1229 | } |
1230 | |
1231 | // Make sure we clean up the args structure. We can't reuse it because the |
1232 | // Platform lives longer than the process and the Platforms don't get a |
1233 | // signal to clean up cached data when a process goes away. |
1234 | CleanUp args_cleanup([do_dlopen_function, &exe_ctx, func_args_addr] { |
1235 | do_dlopen_function->DeallocateFunctionResults(exe_ctx, func_args_addr); |
1236 | }); |
1237 | |
1238 | // Now run the caller: |
1239 | EvaluateExpressionOptions options; |
1240 | options.SetExecutionPolicy(eExecutionPolicyAlways); |
1241 | options.SetLanguage(eLanguageTypeC_plus_plus); |
1242 | options.SetIgnoreBreakpoints(true); |
1243 | options.SetUnwindOnError(true); |
1244 | options.SetTrapExceptions(false); // dlopen can't throw exceptions, so |
1245 | // don't do the work to trap them. |
1246 | options.SetTimeout(std::chrono::seconds(2)); |
1247 | options.SetIsForUtilityExpr(true); |
1248 | |
1249 | Value return_value; |
1250 | // Fetch the clang types we will need: |
1251 | ClangASTContext *ast = process->GetTarget().GetScratchClangASTContext(); |
1252 | |
1253 | CompilerType clang_void_pointer_type |
1254 | = ast->GetBasicType(eBasicTypeVoid).GetPointerType(); |
1255 | |
1256 | return_value.SetCompilerType(clang_void_pointer_type); |
1257 | |
1258 | ExpressionResults results = do_dlopen_function->ExecuteFunction( |
1259 | exe_ctx, &func_args_addr, options, diagnostics, return_value); |
1260 | if (results != eExpressionCompleted) { |
1261 | error.SetErrorStringWithFormat("dlopen error: failed executing " |
1262 | "dlopen wrapper function: %s", |
1263 | diagnostics.GetString().c_str()); |
1264 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1265 | } |
1266 | |
1267 | // Read the dlopen token from the return area: |
1268 | lldb::addr_t token = process->ReadPointerFromMemory(return_addr, |
1269 | utility_error); |
1270 | if (utility_error.Fail()) { |
1271 | error.SetErrorStringWithFormat("dlopen error: could not read the return " |
1272 | "struct: %s", utility_error.AsCString()); |
1273 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1274 | } |
1275 | |
1276 | // The dlopen succeeded! |
1277 | if (token != 0x0) { |
1278 | if (loaded_image && buffer_addr != 0x0) |
1279 | { |
1280 | // Capture the image which was loaded. We leave it in the buffer on |
1281 | // exit from the dlopen function, so we can just read it from there: |
1282 | std::string name_string; |
1283 | process->ReadCStringFromMemory(buffer_addr, name_string, utility_error); |
1284 | if (utility_error.Success()) |
1285 | loaded_image->SetFile(name_string, false, |
1286 | llvm::sys::path::Style::posix); |
1287 | } |
1288 | return process->AddImageToken(token); |
1289 | } |
1290 | |
1291 | // We got an error, lets read in the error string: |
1292 | std::string dlopen_error_str; |
1293 | lldb::addr_t error_addr |
1294 | = process->ReadPointerFromMemory(return_addr + addr_size, utility_error); |
1295 | if (utility_error.Fail()) { |
1296 | error.SetErrorStringWithFormat("dlopen error: could not read error string: " |
1297 | "%s", utility_error.AsCString()); |
1298 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1299 | } |
1300 | |
1301 | size_t num_chars = process->ReadCStringFromMemory(error_addr + addr_size, |
1302 | dlopen_error_str, |
1303 | utility_error); |
1304 | if (utility_error.Success() && num_chars > 0) |
1305 | error.SetErrorStringWithFormat("dlopen error: %s", |
1306 | dlopen_error_str.c_str()); |
1307 | else |
1308 | error.SetErrorStringWithFormat("dlopen failed for unknown reasons."); |
1309 | |
1310 | return LLDB_INVALID_IMAGE_TOKEN(4294967295U); |
1311 | } |
1312 | |
1313 | Status PlatformPOSIX::UnloadImage(lldb_private::Process *process, |
1314 | uint32_t image_token) { |
1315 | const addr_t image_addr = process->GetImagePtrFromToken(image_token); |
1316 | if (image_addr == LLDB_INVALID_ADDRESS(18446744073709551615UL)) |
1317 | return Status("Invalid image token"); |
1318 | |
1319 | StreamString expr; |
1320 | expr.Printf("dlclose((void *)0x%" PRIx64"l" "x" ")", image_addr); |
1321 | llvm::StringRef prefix = GetLibdlFunctionDeclarations(process); |
1322 | lldb::ValueObjectSP result_valobj_sp; |
1323 | Status error = EvaluateLibdlExpression(process, expr.GetData(), prefix, |
1324 | result_valobj_sp); |
1325 | if (error.Fail()) |
1326 | return error; |
1327 | |
1328 | if (result_valobj_sp->GetError().Fail()) |
1329 | return result_valobj_sp->GetError(); |
1330 | |
1331 | Scalar scalar; |
1332 | if (result_valobj_sp->ResolveValue(scalar)) { |
1333 | if (scalar.UInt(1)) |
1334 | return Status("expression failed: \"%s\"", expr.GetData()); |
1335 | process->ResetImageToken(image_token); |
1336 | } |
1337 | return Status(); |
1338 | } |
1339 | |
1340 | lldb::ProcessSP PlatformPOSIX::ConnectProcess(llvm::StringRef connect_url, |
1341 | llvm::StringRef plugin_name, |
1342 | lldb_private::Debugger &debugger, |
1343 | lldb_private::Target *target, |
1344 | lldb_private::Status &error) { |
1345 | if (m_remote_platform_sp) |
1346 | return m_remote_platform_sp->ConnectProcess(connect_url, plugin_name, |
1347 | debugger, target, error); |
1348 | |
1349 | return Platform::ConnectProcess(connect_url, plugin_name, debugger, target, |
1350 | error); |
1351 | } |
1352 | |
1353 | llvm::StringRef |
1354 | PlatformPOSIX::GetLibdlFunctionDeclarations(lldb_private::Process *process) { |
1355 | return R"( |
1356 | extern "C" void* dlopen(const char*, int); |
1357 | extern "C" void* dlsym(void*, const char*); |
1358 | extern "C" int dlclose(void*); |
1359 | extern "C" char* dlerror(void); |
1360 | )"; |
1361 | } |
1362 | |
1363 | size_t PlatformPOSIX::ConnectToWaitingProcesses(Debugger &debugger, |
1364 | Status &error) { |
1365 | if (m_remote_platform_sp) |
1366 | return m_remote_platform_sp->ConnectToWaitingProcesses(debugger, error); |
1367 | return Platform::ConnectToWaitingProcesses(debugger, error); |
1368 | } |
1369 | |
1370 | ConstString PlatformPOSIX::GetFullNameForDylib(ConstString basename) { |
1371 | if (basename.IsEmpty()) |
1372 | return basename; |
1373 | |
1374 | StreamString stream; |
1375 | stream.Printf("lib%s.so", basename.GetCString()); |
1376 | return ConstString(stream.GetString()); |
1377 | } |