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