1e0f8f574SDaniel Malea //===-- PlatformPOSIX.cpp ---------------------------------------*- C++ -*-===//
2e0f8f574SDaniel Malea //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e0f8f574SDaniel Malea //
7e0f8f574SDaniel Malea //===----------------------------------------------------------------------===//
8e0f8f574SDaniel Malea 
9e0f8f574SDaniel Malea #include "PlatformPOSIX.h"
10e0f8f574SDaniel Malea 
11348fb385STodd Fiala #include "lldb/Core/Debugger.h"
12348fb385STodd Fiala #include "lldb/Core/Module.h"
13fc89bbccSPavel Labath #include "lldb/Core/ModuleSpec.h"
143cb132a0STamas Berghammer #include "lldb/Core/ValueObject.h"
151ecb34f4SJim Ingham #include "lldb/Expression/DiagnosticManager.h"
161ecb34f4SJim Ingham #include "lldb/Expression/FunctionCaller.h"
173cb132a0STamas Berghammer #include "lldb/Expression/UserExpression.h"
181ecb34f4SJim Ingham #include "lldb/Expression/UtilityFunction.h"
19e0f8f574SDaniel Malea #include "lldb/Host/File.h"
20c00cf4a0SZachary Turner #include "lldb/Host/FileCache.h"
21c00cf4a0SZachary Turner #include "lldb/Host/FileSystem.h"
22e0f8f574SDaniel Malea #include "lldb/Host/Host.h"
23fc89bbccSPavel Labath #include "lldb/Host/HostInfo.h"
24eef758e9SPavel Labath #include "lldb/Host/ProcessLaunchInfo.h"
251ecb34f4SJim Ingham #include "lldb/Symbol/ClangASTContext.h"
263cb132a0STamas Berghammer #include "lldb/Target/DynamicLoader.h"
273cb132a0STamas Berghammer #include "lldb/Target/ExecutionContext.h"
28a78bd7ffSZachary Turner #include "lldb/Target/Process.h"
293cb132a0STamas Berghammer #include "lldb/Target/Thread.h"
301ecb34f4SJim Ingham #include "lldb/Utility/CleanUp.h"
31666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
325713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
336f9e6901SZachary Turner #include "lldb/Utility/Log.h"
34bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
35e0f8f574SDaniel Malea 
36e0f8f574SDaniel Malea using namespace lldb;
37e0f8f574SDaniel Malea using namespace lldb_private;
38e0f8f574SDaniel Malea 
39e0f8f574SDaniel Malea //------------------------------------------------------------------
40e0f8f574SDaniel Malea /// Default Constructor
41e0f8f574SDaniel Malea //------------------------------------------------------------------
42b9c1b51eSKate Stone PlatformPOSIX::PlatformPOSIX(bool is_host)
4352d9c62aSPavel Labath     : RemoteAwarePlatform(is_host), // This is the local host platform
440a66e2f1SEnrico Granata       m_option_group_platform_rsync(new OptionGroupPlatformRSync()),
450a66e2f1SEnrico Granata       m_option_group_platform_ssh(new OptionGroupPlatformSSH()),
4652d9c62aSPavel Labath       m_option_group_platform_caching(new OptionGroupPlatformCaching()) {}
47e0f8f574SDaniel Malea 
48e0f8f574SDaniel Malea //------------------------------------------------------------------
49e0f8f574SDaniel Malea /// Destructor.
50e0f8f574SDaniel Malea ///
51e0f8f574SDaniel Malea /// The destructor is virtual since this class is designed to be
52e0f8f574SDaniel Malea /// inherited from by the plug-in instance.
53e0f8f574SDaniel Malea //------------------------------------------------------------------
54b9c1b51eSKate Stone PlatformPOSIX::~PlatformPOSIX() {}
55e0f8f574SDaniel Malea 
56b9c1b51eSKate Stone lldb_private::OptionGroupOptions *PlatformPOSIX::GetConnectionOptions(
57b9c1b51eSKate Stone     lldb_private::CommandInterpreter &interpreter) {
580a66e2f1SEnrico Granata   auto iter = m_options.find(&interpreter), end = m_options.end();
59b9c1b51eSKate Stone   if (iter == end) {
60b9c1b51eSKate Stone     std::unique_ptr<lldb_private::OptionGroupOptions> options(
61b9c1b51eSKate Stone         new OptionGroupOptions());
620a66e2f1SEnrico Granata     options->Append(m_option_group_platform_rsync.get());
630a66e2f1SEnrico Granata     options->Append(m_option_group_platform_ssh.get());
640a66e2f1SEnrico Granata     options->Append(m_option_group_platform_caching.get());
650a66e2f1SEnrico Granata     m_options[&interpreter] = std::move(options);
66e0f8f574SDaniel Malea   }
670a66e2f1SEnrico Granata 
680a66e2f1SEnrico Granata   return m_options.at(&interpreter).get();
69e0f8f574SDaniel Malea }
70e0f8f574SDaniel Malea 
7197206d57SZachary Turner Status
7297206d57SZachary Turner PlatformPOSIX::ResolveExecutable(const ModuleSpec &module_spec,
73fc89bbccSPavel Labath                                  lldb::ModuleSP &exe_module_sp,
74fc89bbccSPavel Labath                                  const FileSpecList *module_search_paths_ptr) {
7597206d57SZachary Turner   Status error;
76fc89bbccSPavel Labath   // Nothing special to do here, just use the actual file and architecture
77fc89bbccSPavel Labath 
78fc89bbccSPavel Labath   char exe_path[PATH_MAX];
79fc89bbccSPavel Labath   ModuleSpec resolved_module_spec(module_spec);
80fc89bbccSPavel Labath 
81fc89bbccSPavel Labath   if (IsHost()) {
8205097246SAdrian Prantl     // If we have "ls" as the exe_file, resolve the executable location based
8305097246SAdrian Prantl     // on the current path variables
84dbd7fabaSJonas Devlieghere     if (!FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec())) {
85fc89bbccSPavel Labath       resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path));
868f3be7a3SJonas Devlieghere       resolved_module_spec.GetFileSpec().SetFile(exe_path,
87937348cdSJonas Devlieghere                                                  FileSpec::Style::native);
888f3be7a3SJonas Devlieghere       FileSystem::Instance().Resolve(resolved_module_spec.GetFileSpec());
89fc89bbccSPavel Labath     }
90fc89bbccSPavel Labath 
91dbd7fabaSJonas Devlieghere     if (!FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()))
922c22c800SJonas Devlieghere       FileSystem::Instance().ResolveExecutableLocation(
932c22c800SJonas Devlieghere           resolved_module_spec.GetFileSpec());
94fc89bbccSPavel Labath 
95fc89bbccSPavel Labath     // Resolve any executable within a bundle on MacOSX
96fc89bbccSPavel Labath     Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec());
97fc89bbccSPavel Labath 
98dbd7fabaSJonas Devlieghere     if (FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()))
99fc89bbccSPavel Labath       error.Clear();
100fc89bbccSPavel Labath     else {
1017c5310bbSJonas Devlieghere       const uint32_t permissions = FileSystem::Instance().GetPermissions(
1027c5310bbSJonas Devlieghere           resolved_module_spec.GetFileSpec());
103fc89bbccSPavel Labath       if (permissions && (permissions & eFilePermissionsEveryoneR) == 0)
104fc89bbccSPavel Labath         error.SetErrorStringWithFormat(
105fc89bbccSPavel Labath             "executable '%s' is not readable",
106fc89bbccSPavel Labath             resolved_module_spec.GetFileSpec().GetPath().c_str());
107fc89bbccSPavel Labath       else
108fc89bbccSPavel Labath         error.SetErrorStringWithFormat(
109fc89bbccSPavel Labath             "unable to find executable for '%s'",
110fc89bbccSPavel Labath             resolved_module_spec.GetFileSpec().GetPath().c_str());
111fc89bbccSPavel Labath     }
112fc89bbccSPavel Labath   } else {
113fc89bbccSPavel Labath     if (m_remote_platform_sp) {
114fc89bbccSPavel Labath       error =
115fc89bbccSPavel Labath           GetCachedExecutable(resolved_module_spec, exe_module_sp,
116fc89bbccSPavel Labath                               module_search_paths_ptr, *m_remote_platform_sp);
117fc89bbccSPavel Labath     } else {
118fc89bbccSPavel Labath       // We may connect to a process and use the provided executable (Don't use
119fc89bbccSPavel Labath       // local $PATH).
120fc89bbccSPavel Labath 
121fc89bbccSPavel Labath       // Resolve any executable within a bundle on MacOSX
122fc89bbccSPavel Labath       Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec());
123fc89bbccSPavel Labath 
124dbd7fabaSJonas Devlieghere       if (FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()))
125fc89bbccSPavel Labath         error.Clear();
126fc89bbccSPavel Labath       else
127fc89bbccSPavel Labath         error.SetErrorStringWithFormat("the platform is not currently "
128fc89bbccSPavel Labath                                        "connected, and '%s' doesn't exist in "
129fc89bbccSPavel Labath                                        "the system root.",
130fc89bbccSPavel Labath                                        exe_path);
131fc89bbccSPavel Labath     }
132fc89bbccSPavel Labath   }
133fc89bbccSPavel Labath 
134fc89bbccSPavel Labath   if (error.Success()) {
135fc89bbccSPavel Labath     if (resolved_module_spec.GetArchitecture().IsValid()) {
136fc89bbccSPavel Labath       error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
137fc89bbccSPavel Labath                                           module_search_paths_ptr, nullptr, nullptr);
138fc89bbccSPavel Labath       if (error.Fail()) {
139fc89bbccSPavel Labath         // If we failed, it may be because the vendor and os aren't known. If
140fc89bbccSPavel Labath 	// that is the case, try setting them to the host architecture and give
141fc89bbccSPavel Labath 	// it another try.
142fc89bbccSPavel Labath         llvm::Triple &module_triple =
143fc89bbccSPavel Labath             resolved_module_spec.GetArchitecture().GetTriple();
144fc89bbccSPavel Labath         bool is_vendor_specified =
145fc89bbccSPavel Labath             (module_triple.getVendor() != llvm::Triple::UnknownVendor);
146fc89bbccSPavel Labath         bool is_os_specified =
147fc89bbccSPavel Labath             (module_triple.getOS() != llvm::Triple::UnknownOS);
148fc89bbccSPavel Labath         if (!is_vendor_specified || !is_os_specified) {
149fc89bbccSPavel Labath           const llvm::Triple &host_triple =
150fc89bbccSPavel Labath               HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple();
151fc89bbccSPavel Labath 
152fc89bbccSPavel Labath           if (!is_vendor_specified)
153fc89bbccSPavel Labath             module_triple.setVendorName(host_triple.getVendorName());
154fc89bbccSPavel Labath           if (!is_os_specified)
155fc89bbccSPavel Labath             module_triple.setOSName(host_triple.getOSName());
156fc89bbccSPavel Labath 
157fc89bbccSPavel Labath           error = ModuleList::GetSharedModule(resolved_module_spec,
158fc89bbccSPavel Labath                                               exe_module_sp, module_search_paths_ptr, nullptr, nullptr);
159fc89bbccSPavel Labath         }
160fc89bbccSPavel Labath       }
161fc89bbccSPavel Labath 
162fc89bbccSPavel Labath       // TODO find out why exe_module_sp might be NULL
163fc89bbccSPavel Labath       if (error.Fail() || !exe_module_sp || !exe_module_sp->GetObjectFile()) {
164fc89bbccSPavel Labath         exe_module_sp.reset();
165fc89bbccSPavel Labath         error.SetErrorStringWithFormat(
166fc89bbccSPavel Labath             "'%s' doesn't contain the architecture %s",
167fc89bbccSPavel Labath             resolved_module_spec.GetFileSpec().GetPath().c_str(),
168fc89bbccSPavel Labath             resolved_module_spec.GetArchitecture().GetArchitectureName());
169fc89bbccSPavel Labath       }
170fc89bbccSPavel Labath     } else {
17105097246SAdrian Prantl       // No valid architecture was specified, ask the platform for the
17205097246SAdrian Prantl       // architectures that we should be using (in the correct order) and see
17305097246SAdrian Prantl       // if we can find a match that way
174fc89bbccSPavel Labath       StreamString arch_names;
175fc89bbccSPavel Labath       for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
176fc89bbccSPavel Labath                idx, resolved_module_spec.GetArchitecture());
177fc89bbccSPavel Labath            ++idx) {
178fc89bbccSPavel Labath         error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
179fc89bbccSPavel Labath                                             module_search_paths_ptr, nullptr, nullptr);
180fc89bbccSPavel Labath         // Did we find an executable using one of the
181fc89bbccSPavel Labath         if (error.Success()) {
182fc89bbccSPavel Labath           if (exe_module_sp && exe_module_sp->GetObjectFile())
183fc89bbccSPavel Labath             break;
184fc89bbccSPavel Labath           else
185fc89bbccSPavel Labath             error.SetErrorToGenericError();
186fc89bbccSPavel Labath         }
187fc89bbccSPavel Labath 
188fc89bbccSPavel Labath         if (idx > 0)
189fc89bbccSPavel Labath           arch_names.PutCString(", ");
190fc89bbccSPavel Labath         arch_names.PutCString(
191fc89bbccSPavel Labath             resolved_module_spec.GetArchitecture().GetArchitectureName());
192fc89bbccSPavel Labath       }
193fc89bbccSPavel Labath 
194fc89bbccSPavel Labath       if (error.Fail() || !exe_module_sp) {
1957c5310bbSJonas Devlieghere         if (FileSystem::Instance().Readable(
1967c5310bbSJonas Devlieghere                 resolved_module_spec.GetFileSpec())) {
197fc89bbccSPavel Labath           error.SetErrorStringWithFormat(
198fc89bbccSPavel Labath               "'%s' doesn't contain any '%s' platform architectures: %s",
199fc89bbccSPavel Labath               resolved_module_spec.GetFileSpec().GetPath().c_str(),
200fc89bbccSPavel Labath               GetPluginName().GetCString(), arch_names.GetData());
201fc89bbccSPavel Labath         } else {
202fc89bbccSPavel Labath           error.SetErrorStringWithFormat(
203fc89bbccSPavel Labath               "'%s' is not readable",
204fc89bbccSPavel Labath               resolved_module_spec.GetFileSpec().GetPath().c_str());
205fc89bbccSPavel Labath         }
206fc89bbccSPavel Labath       }
207fc89bbccSPavel Labath     }
208fc89bbccSPavel Labath   }
209fc89bbccSPavel Labath 
210fc89bbccSPavel Labath   return error;
211fc89bbccSPavel Labath }
212fc89bbccSPavel Labath 
213b9c1b51eSKate Stone static uint32_t chown_file(Platform *platform, const char *path,
214e0f8f574SDaniel Malea                            uint32_t uid = UINT32_MAX,
215b9c1b51eSKate Stone                            uint32_t gid = UINT32_MAX) {
216e0f8f574SDaniel Malea   if (!platform || !path || *path == 0)
217e0f8f574SDaniel Malea     return UINT32_MAX;
218e0f8f574SDaniel Malea 
219e0f8f574SDaniel Malea   if (uid == UINT32_MAX && gid == UINT32_MAX)
220e0f8f574SDaniel Malea     return 0; // pretend I did chown correctly - actually I just didn't care
221e0f8f574SDaniel Malea 
222e0f8f574SDaniel Malea   StreamString command;
223e0f8f574SDaniel Malea   command.PutCString("chown ");
224e0f8f574SDaniel Malea   if (uid != UINT32_MAX)
225e0f8f574SDaniel Malea     command.Printf("%d", uid);
226e0f8f574SDaniel Malea   if (gid != UINT32_MAX)
227e0f8f574SDaniel Malea     command.Printf(":%d", gid);
228e0f8f574SDaniel Malea   command.Printf("%s", path);
229e0f8f574SDaniel Malea   int status;
23019dd1a0eSPavel Labath   platform->RunShellCommand(command.GetData(), NULL, &status, NULL, NULL,
23119dd1a0eSPavel Labath                             std::chrono::seconds(10));
232e0f8f574SDaniel Malea   return status;
233e0f8f574SDaniel Malea }
234e0f8f574SDaniel Malea 
23597206d57SZachary Turner lldb_private::Status
236e0f8f574SDaniel Malea PlatformPOSIX::PutFile(const lldb_private::FileSpec &source,
237b9c1b51eSKate Stone                        const lldb_private::FileSpec &destination, uint32_t uid,
238b9c1b51eSKate Stone                        uint32_t gid) {
239fbb76349SGreg Clayton   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
240fbb76349SGreg Clayton 
241b9c1b51eSKate Stone   if (IsHost()) {
242e0f8f574SDaniel Malea     if (FileSpec::Equal(source, destination, true))
24397206d57SZachary Turner       return Status();
244e0f8f574SDaniel Malea     // cp src dst
245e0f8f574SDaniel Malea     // chown uid:gid dst
246e0f8f574SDaniel Malea     std::string src_path(source.GetPath());
247e0f8f574SDaniel Malea     if (src_path.empty())
24897206d57SZachary Turner       return Status("unable to get file path for source");
249e0f8f574SDaniel Malea     std::string dst_path(destination.GetPath());
250e0f8f574SDaniel Malea     if (dst_path.empty())
25197206d57SZachary Turner       return Status("unable to get file path for destination");
252e0f8f574SDaniel Malea     StreamString command;
253e0f8f574SDaniel Malea     command.Printf("cp %s %s", src_path.c_str(), dst_path.c_str());
254e0f8f574SDaniel Malea     int status;
25519dd1a0eSPavel Labath     RunShellCommand(command.GetData(), NULL, &status, NULL, NULL,
25619dd1a0eSPavel Labath                     std::chrono::seconds(10));
257e0f8f574SDaniel Malea     if (status != 0)
25897206d57SZachary Turner       return Status("unable to perform copy");
259e0f8f574SDaniel Malea     if (uid == UINT32_MAX && gid == UINT32_MAX)
26097206d57SZachary Turner       return Status();
261e0f8f574SDaniel Malea     if (chown_file(this, dst_path.c_str(), uid, gid) != 0)
26297206d57SZachary Turner       return Status("unable to perform chown");
26397206d57SZachary Turner     return Status();
264b9c1b51eSKate Stone   } else if (m_remote_platform_sp) {
265b9c1b51eSKate Stone     if (GetSupportsRSync()) {
266e0f8f574SDaniel Malea       std::string src_path(source.GetPath());
267e0f8f574SDaniel Malea       if (src_path.empty())
26897206d57SZachary Turner         return Status("unable to get file path for source");
269e0f8f574SDaniel Malea       std::string dst_path(destination.GetPath());
270e0f8f574SDaniel Malea       if (dst_path.empty())
27197206d57SZachary Turner         return Status("unable to get file path for destination");
272e0f8f574SDaniel Malea       StreamString command;
273b9c1b51eSKate Stone       if (GetIgnoresRemoteHostname()) {
274e0f8f574SDaniel Malea         if (!GetRSyncPrefix())
275b9c1b51eSKate Stone           command.Printf("rsync %s %s %s", GetRSyncOpts(), src_path.c_str(),
276e0f8f574SDaniel Malea                          dst_path.c_str());
277e0f8f574SDaniel Malea         else
278b9c1b51eSKate Stone           command.Printf("rsync %s %s %s%s", GetRSyncOpts(), src_path.c_str(),
279b9c1b51eSKate Stone                          GetRSyncPrefix(), dst_path.c_str());
280b9c1b51eSKate Stone       } else
281b9c1b51eSKate Stone         command.Printf("rsync %s %s %s:%s", GetRSyncOpts(), src_path.c_str(),
282b9c1b51eSKate Stone                        GetHostname(), dst_path.c_str());
283e0f8f574SDaniel Malea       if (log)
284e0f8f574SDaniel Malea         log->Printf("[PutFile] Running command: %s\n", command.GetData());
285e0f8f574SDaniel Malea       int retcode;
28619dd1a0eSPavel Labath       Host::RunShellCommand(command.GetData(), NULL, &retcode, NULL, NULL,
28719dd1a0eSPavel Labath                             std::chrono::minutes(1));
288b9c1b51eSKate Stone       if (retcode == 0) {
289e0f8f574SDaniel Malea         // Don't chown a local file for a remote system
290e0f8f574SDaniel Malea         //                if (chown_file(this,dst_path.c_str(),uid,gid) != 0)
29197206d57SZachary Turner         //                    return Status("unable to perform chown");
29297206d57SZachary Turner         return Status();
293e0f8f574SDaniel Malea       }
294b9c1b51eSKate Stone       // if we are still here rsync has failed - let's try the slow way before
295b9c1b51eSKate Stone       // giving up
296e0f8f574SDaniel Malea     }
297e0f8f574SDaniel Malea   }
298e0f8f574SDaniel Malea   return Platform::PutFile(source, destination, uid, gid);
299e0f8f574SDaniel Malea }
300e0f8f574SDaniel Malea 
30197206d57SZachary Turner lldb_private::Status PlatformPOSIX::GetFile(
302b9c1b51eSKate Stone     const lldb_private::FileSpec &source,      // remote file path
303d3173f34SChaoren Lin     const lldb_private::FileSpec &destination) // local file path
304e0f8f574SDaniel Malea {
305fbb76349SGreg Clayton   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
306fbb76349SGreg Clayton 
307e0f8f574SDaniel Malea   // Check the args, first.
308e0f8f574SDaniel Malea   std::string src_path(source.GetPath());
309e0f8f574SDaniel Malea   if (src_path.empty())
31097206d57SZachary Turner     return Status("unable to get file path for source");
311e0f8f574SDaniel Malea   std::string dst_path(destination.GetPath());
312e0f8f574SDaniel Malea   if (dst_path.empty())
31397206d57SZachary Turner     return Status("unable to get file path for destination");
314b9c1b51eSKate Stone   if (IsHost()) {
315e0f8f574SDaniel Malea     if (FileSpec::Equal(source, destination, true))
31697206d57SZachary Turner       return Status("local scenario->source and destination are the same file "
317b9c1b51eSKate Stone                     "path: no operation performed");
318e0f8f574SDaniel Malea     // cp src dst
319e0f8f574SDaniel Malea     StreamString cp_command;
320e0f8f574SDaniel Malea     cp_command.Printf("cp %s %s", src_path.c_str(), dst_path.c_str());
321e0f8f574SDaniel Malea     int status;
32219dd1a0eSPavel Labath     RunShellCommand(cp_command.GetData(), NULL, &status, NULL, NULL,
32319dd1a0eSPavel Labath                     std::chrono::seconds(10));
324e0f8f574SDaniel Malea     if (status != 0)
32597206d57SZachary Turner       return Status("unable to perform copy");
32697206d57SZachary Turner     return Status();
327b9c1b51eSKate Stone   } else if (m_remote_platform_sp) {
328b9c1b51eSKate Stone     if (GetSupportsRSync()) {
329e0f8f574SDaniel Malea       StreamString command;
330b9c1b51eSKate Stone       if (GetIgnoresRemoteHostname()) {
331e0f8f574SDaniel Malea         if (!GetRSyncPrefix())
332b9c1b51eSKate Stone           command.Printf("rsync %s %s %s", GetRSyncOpts(), src_path.c_str(),
333e0f8f574SDaniel Malea                          dst_path.c_str());
334e0f8f574SDaniel Malea         else
335b9c1b51eSKate Stone           command.Printf("rsync %s %s%s %s", GetRSyncOpts(), GetRSyncPrefix(),
336b9c1b51eSKate Stone                          src_path.c_str(), dst_path.c_str());
337b9c1b51eSKate Stone       } else
338b9c1b51eSKate Stone         command.Printf("rsync %s %s:%s %s", GetRSyncOpts(),
339b9c1b51eSKate Stone                        m_remote_platform_sp->GetHostname(), src_path.c_str(),
340e0f8f574SDaniel Malea                        dst_path.c_str());
341e0f8f574SDaniel Malea       if (log)
342e0f8f574SDaniel Malea         log->Printf("[GetFile] Running command: %s\n", command.GetData());
343e0f8f574SDaniel Malea       int retcode;
34419dd1a0eSPavel Labath       Host::RunShellCommand(command.GetData(), NULL, &retcode, NULL, NULL,
34519dd1a0eSPavel Labath                             std::chrono::minutes(1));
346e0f8f574SDaniel Malea       if (retcode == 0)
34797206d57SZachary Turner         return Status();
34805097246SAdrian Prantl       // If we are here, rsync has failed - let's try the slow way before
34905097246SAdrian Prantl       // giving up
350e0f8f574SDaniel Malea     }
351e0f8f574SDaniel Malea     // open src and dst
352e0f8f574SDaniel Malea     // read/write, read/write, read/write, ...
353e0f8f574SDaniel Malea     // close src
354e0f8f574SDaniel Malea     // close dst
355e0f8f574SDaniel Malea     if (log)
356e0f8f574SDaniel Malea       log->Printf("[GetFile] Using block by block transfer....\n");
35797206d57SZachary Turner     Status error;
358b9c1b51eSKate Stone     user_id_t fd_src = OpenFile(source, File::eOpenOptionRead,
359b9c1b51eSKate Stone                                 lldb::eFilePermissionsFileDefault, error);
360e0f8f574SDaniel Malea 
361e0f8f574SDaniel Malea     if (fd_src == UINT64_MAX)
36297206d57SZachary Turner       return Status("unable to open source file");
363e0f8f574SDaniel Malea 
364fbb76349SGreg Clayton     uint32_t permissions = 0;
365d3173f34SChaoren Lin     error = GetFilePermissions(source, permissions);
366e0f8f574SDaniel Malea 
367e0f8f574SDaniel Malea     if (permissions == 0)
368fbb76349SGreg Clayton       permissions = lldb::eFilePermissionsFileDefault;
369e0f8f574SDaniel Malea 
370c00cf4a0SZachary Turner     user_id_t fd_dst = FileCache::GetInstance().OpenFile(
371b9c1b51eSKate Stone         destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
372b9c1b51eSKate Stone                          File::eOpenOptionTruncate,
373b9c1b51eSKate Stone         permissions, error);
374e0f8f574SDaniel Malea 
375b9c1b51eSKate Stone     if (fd_dst == UINT64_MAX) {
376e0f8f574SDaniel Malea       if (error.Success())
377e0f8f574SDaniel Malea         error.SetErrorString("unable to open destination file");
378e0f8f574SDaniel Malea     }
379e0f8f574SDaniel Malea 
380b9c1b51eSKate Stone     if (error.Success()) {
381e0f8f574SDaniel Malea       lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
382e0f8f574SDaniel Malea       uint64_t offset = 0;
383e0f8f574SDaniel Malea       error.Clear();
384b9c1b51eSKate Stone       while (error.Success()) {
385b9c1b51eSKate Stone         const uint64_t n_read = ReadFile(fd_src, offset, buffer_sp->GetBytes(),
386b9c1b51eSKate Stone                                          buffer_sp->GetByteSize(), error);
387e0f8f574SDaniel Malea         if (error.Fail())
388e0f8f574SDaniel Malea           break;
389e0f8f574SDaniel Malea         if (n_read == 0)
390e0f8f574SDaniel Malea           break;
391b9c1b51eSKate Stone         if (FileCache::GetInstance().WriteFile(fd_dst, offset,
392b9c1b51eSKate Stone                                                buffer_sp->GetBytes(), n_read,
393b9c1b51eSKate Stone                                                error) != n_read) {
394e0f8f574SDaniel Malea           if (!error.Fail())
395e0f8f574SDaniel Malea             error.SetErrorString("unable to write to destination file");
396e0f8f574SDaniel Malea           break;
397e0f8f574SDaniel Malea         }
398e0f8f574SDaniel Malea         offset += n_read;
399e0f8f574SDaniel Malea       }
400e0f8f574SDaniel Malea     }
401e0f8f574SDaniel Malea     // Ignore the close error of src.
402e0f8f574SDaniel Malea     if (fd_src != UINT64_MAX)
403e0f8f574SDaniel Malea       CloseFile(fd_src, error);
404e0f8f574SDaniel Malea     // And close the dst file descriptot.
405b9c1b51eSKate Stone     if (fd_dst != UINT64_MAX &&
406b9c1b51eSKate Stone         !FileCache::GetInstance().CloseFile(fd_dst, error)) {
407e0f8f574SDaniel Malea       if (!error.Fail())
408e0f8f574SDaniel Malea         error.SetErrorString("unable to close destination file");
409e0f8f574SDaniel Malea     }
410e0f8f574SDaniel Malea     return error;
411e0f8f574SDaniel Malea   }
412e0f8f574SDaniel Malea   return Platform::GetFile(source, destination);
413e0f8f574SDaniel Malea }
414e0f8f574SDaniel Malea 
415b9c1b51eSKate Stone std::string PlatformPOSIX::GetPlatformSpecificConnectionInformation() {
416e0f8f574SDaniel Malea   StreamString stream;
417b9c1b51eSKate Stone   if (GetSupportsRSync()) {
418e0f8f574SDaniel Malea     stream.PutCString("rsync");
419e0f8f574SDaniel Malea     if ((GetRSyncOpts() && *GetRSyncOpts()) ||
420b9c1b51eSKate Stone         (GetRSyncPrefix() && *GetRSyncPrefix()) || GetIgnoresRemoteHostname()) {
421e0f8f574SDaniel Malea       stream.Printf(", options: ");
422e0f8f574SDaniel Malea       if (GetRSyncOpts() && *GetRSyncOpts())
423e0f8f574SDaniel Malea         stream.Printf("'%s' ", GetRSyncOpts());
424e0f8f574SDaniel Malea       stream.Printf(", prefix: ");
425e0f8f574SDaniel Malea       if (GetRSyncPrefix() && *GetRSyncPrefix())
426e0f8f574SDaniel Malea         stream.Printf("'%s' ", GetRSyncPrefix());
427e0f8f574SDaniel Malea       if (GetIgnoresRemoteHostname())
428e0f8f574SDaniel Malea         stream.Printf("ignore remote-hostname ");
429e0f8f574SDaniel Malea     }
430e0f8f574SDaniel Malea   }
431b9c1b51eSKate Stone   if (GetSupportsSSH()) {
432e0f8f574SDaniel Malea     stream.PutCString("ssh");
433e0f8f574SDaniel Malea     if (GetSSHOpts() && *GetSSHOpts())
434e0f8f574SDaniel Malea       stream.Printf(", options: '%s' ", GetSSHOpts());
435e0f8f574SDaniel Malea   }
436e0f8f574SDaniel Malea   if (GetLocalCacheDirectory() && *GetLocalCacheDirectory())
437e0f8f574SDaniel Malea     stream.Printf("cache dir: %s", GetLocalCacheDirectory());
438e0f8f574SDaniel Malea   if (stream.GetSize())
439c156427dSZachary Turner     return stream.GetString();
440e0f8f574SDaniel Malea   else
441e0f8f574SDaniel Malea     return "";
442e0f8f574SDaniel Malea }
443e0f8f574SDaniel Malea 
444b9c1b51eSKate Stone const lldb::UnixSignalsSP &PlatformPOSIX::GetRemoteUnixSignals() {
44598d0a4b3SChaoren Lin   if (IsRemote() && m_remote_platform_sp)
44698d0a4b3SChaoren Lin     return m_remote_platform_sp->GetRemoteUnixSignals();
44798d0a4b3SChaoren Lin   return Platform::GetRemoteUnixSignals();
44898d0a4b3SChaoren Lin }
44998d0a4b3SChaoren Lin 
45097206d57SZachary Turner Status PlatformPOSIX::ConnectRemote(Args &args) {
45197206d57SZachary Turner   Status error;
452b9c1b51eSKate Stone   if (IsHost()) {
453b9c1b51eSKate Stone     error.SetErrorStringWithFormat(
454b9c1b51eSKate Stone         "can't connect to the host platform '%s', always connected",
455b9c1b51eSKate Stone         GetPluginName().GetCString());
456b9c1b51eSKate Stone   } else {
457015d818bSTodd Fiala     if (!m_remote_platform_sp)
458b9c1b51eSKate Stone       m_remote_platform_sp =
459b9c1b51eSKate Stone           Platform::Create(ConstString("remote-gdb-server"), error);
460015d818bSTodd Fiala 
461015d818bSTodd Fiala     if (m_remote_platform_sp && error.Success())
462015d818bSTodd Fiala       error = m_remote_platform_sp->ConnectRemote(args);
463015d818bSTodd Fiala     else
464015d818bSTodd Fiala       error.SetErrorString("failed to create a 'remote-gdb-server' platform");
465015d818bSTodd Fiala 
466015d818bSTodd Fiala     if (error.Fail())
467015d818bSTodd Fiala       m_remote_platform_sp.reset();
468015d818bSTodd Fiala   }
469015d818bSTodd Fiala 
470b9c1b51eSKate Stone   if (error.Success() && m_remote_platform_sp) {
471b9c1b51eSKate Stone     if (m_option_group_platform_rsync.get() &&
472b9c1b51eSKate Stone         m_option_group_platform_ssh.get() &&
473b9c1b51eSKate Stone         m_option_group_platform_caching.get()) {
474b9c1b51eSKate Stone       if (m_option_group_platform_rsync->m_rsync) {
475015d818bSTodd Fiala         SetSupportsRSync(true);
4760a66e2f1SEnrico Granata         SetRSyncOpts(m_option_group_platform_rsync->m_rsync_opts.c_str());
4770a66e2f1SEnrico Granata         SetRSyncPrefix(m_option_group_platform_rsync->m_rsync_prefix.c_str());
478b9c1b51eSKate Stone         SetIgnoresRemoteHostname(
479b9c1b51eSKate Stone             m_option_group_platform_rsync->m_ignores_remote_hostname);
480015d818bSTodd Fiala       }
481b9c1b51eSKate Stone       if (m_option_group_platform_ssh->m_ssh) {
482015d818bSTodd Fiala         SetSupportsSSH(true);
4830a66e2f1SEnrico Granata         SetSSHOpts(m_option_group_platform_ssh->m_ssh_opts.c_str());
484015d818bSTodd Fiala       }
485b9c1b51eSKate Stone       SetLocalCacheDirectory(
486b9c1b51eSKate Stone           m_option_group_platform_caching->m_cache_dir.c_str());
487015d818bSTodd Fiala     }
488015d818bSTodd Fiala   }
489015d818bSTodd Fiala 
490015d818bSTodd Fiala   return error;
491015d818bSTodd Fiala }
492015d818bSTodd Fiala 
49397206d57SZachary Turner Status PlatformPOSIX::DisconnectRemote() {
49497206d57SZachary Turner   Status error;
495015d818bSTodd Fiala 
496b9c1b51eSKate Stone   if (IsHost()) {
497b9c1b51eSKate Stone     error.SetErrorStringWithFormat(
498b9c1b51eSKate Stone         "can't disconnect from the host platform '%s', always connected",
499b9c1b51eSKate Stone         GetPluginName().GetCString());
500b9c1b51eSKate Stone   } else {
501015d818bSTodd Fiala     if (m_remote_platform_sp)
502015d818bSTodd Fiala       error = m_remote_platform_sp->DisconnectRemote();
503015d818bSTodd Fiala     else
504015d818bSTodd Fiala       error.SetErrorString("the platform is not currently connected");
505015d818bSTodd Fiala   }
506015d818bSTodd Fiala   return error;
507015d818bSTodd Fiala }
508015d818bSTodd Fiala 
509b9c1b51eSKate Stone lldb::ProcessSP PlatformPOSIX::Attach(ProcessAttachInfo &attach_info,
510b9c1b51eSKate Stone                                       Debugger &debugger, Target *target,
51197206d57SZachary Turner                                       Status &error) {
512348fb385STodd Fiala   lldb::ProcessSP process_sp;
513348fb385STodd Fiala   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
514348fb385STodd Fiala 
515b9c1b51eSKate Stone   if (IsHost()) {
516b9c1b51eSKate Stone     if (target == NULL) {
517348fb385STodd Fiala       TargetSP new_target_sp;
518348fb385STodd Fiala 
519f9a07e9fSJonas Devlieghere       error = debugger.GetTargetList().CreateTarget(
520f9a07e9fSJonas Devlieghere           debugger, "", "", eLoadDependentsNo, NULL, new_target_sp);
521348fb385STodd Fiala       target = new_target_sp.get();
522348fb385STodd Fiala       if (log)
523348fb385STodd Fiala         log->Printf("PlatformPOSIX::%s created new target", __FUNCTION__);
524b9c1b51eSKate Stone     } else {
525348fb385STodd Fiala       error.Clear();
526348fb385STodd Fiala       if (log)
527b9c1b51eSKate Stone         log->Printf("PlatformPOSIX::%s target already existed, setting target",
528b9c1b51eSKate Stone                     __FUNCTION__);
529348fb385STodd Fiala     }
530348fb385STodd Fiala 
531b9c1b51eSKate Stone     if (target && error.Success()) {
532348fb385STodd Fiala       debugger.GetTargetList().SetSelectedTarget(target);
533b9c1b51eSKate Stone       if (log) {
534348fb385STodd Fiala         ModuleSP exe_module_sp = target->GetExecutableModule();
535b9c1b51eSKate Stone         log->Printf("PlatformPOSIX::%s set selected target to %p %s",
536b9c1b51eSKate Stone                     __FUNCTION__, (void *)target,
537b9c1b51eSKate Stone                     exe_module_sp
538b9c1b51eSKate Stone                         ? exe_module_sp->GetFileSpec().GetPath().c_str()
539b9c1b51eSKate Stone                         : "<null>");
540348fb385STodd Fiala       }
541348fb385STodd Fiala 
542b9c1b51eSKate Stone       process_sp =
543b9c1b51eSKate Stone           target->CreateProcess(attach_info.GetListenerForProcess(debugger),
544b9c1b51eSKate Stone                                 attach_info.GetProcessPluginName(), NULL);
545348fb385STodd Fiala 
546b9c1b51eSKate Stone       if (process_sp) {
547583bbb1dSJim Ingham         ListenerSP listener_sp = attach_info.GetHijackListener();
548b9c1b51eSKate Stone         if (listener_sp == nullptr) {
549b9c1b51eSKate Stone           listener_sp =
550b9c1b51eSKate Stone               Listener::MakeListener("lldb.PlatformPOSIX.attach.hijack");
551348fb385STodd Fiala           attach_info.SetHijackListener(listener_sp);
552926af0cdSOleksiy Vyalov         }
553583bbb1dSJim Ingham         process_sp->HijackProcessEvents(listener_sp);
554348fb385STodd Fiala         error = process_sp->Attach(attach_info);
555348fb385STodd Fiala       }
556348fb385STodd Fiala     }
557b9c1b51eSKate Stone   } else {
558348fb385STodd Fiala     if (m_remote_platform_sp)
559b9c1b51eSKate Stone       process_sp =
560b9c1b51eSKate Stone           m_remote_platform_sp->Attach(attach_info, debugger, target, error);
561348fb385STodd Fiala     else
562348fb385STodd Fiala       error.SetErrorString("the platform is not currently connected");
563348fb385STodd Fiala   }
564348fb385STodd Fiala   return process_sp;
565348fb385STodd Fiala }
566348fb385STodd Fiala 
567015d818bSTodd Fiala lldb::ProcessSP
568b9c1b51eSKate Stone PlatformPOSIX::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
569b9c1b51eSKate Stone                             Target *target, // Can be NULL, if NULL create a new
570b9c1b51eSKate Stone                                             // target, else use existing one
57197206d57SZachary Turner                             Status &error) {
572015d818bSTodd Fiala   ProcessSP process_sp;
573015d818bSTodd Fiala 
574b9c1b51eSKate Stone   if (IsHost()) {
575b9c1b51eSKate Stone     // We are going to hand this process off to debugserver which will be in
576245dd2e0SPavel Labath     // charge of setting the exit status.  However, we still need to reap it
577245dd2e0SPavel Labath     // from lldb. So, make sure we use a exit callback which does not set exit
578245dd2e0SPavel Labath     // status.
579245dd2e0SPavel Labath     const bool monitor_signals = false;
580245dd2e0SPavel Labath     launch_info.SetMonitorProcessCallback(
581245dd2e0SPavel Labath         &ProcessLaunchInfo::NoOpMonitorCallback, monitor_signals);
5828012cadbSGreg Clayton     process_sp = Platform::DebugProcess(launch_info, debugger, target, error);
583b9c1b51eSKate Stone   } else {
584015d818bSTodd Fiala     if (m_remote_platform_sp)
585b9c1b51eSKate Stone       process_sp = m_remote_platform_sp->DebugProcess(launch_info, debugger,
586b9c1b51eSKate Stone                                                       target, error);
587015d818bSTodd Fiala     else
588015d818bSTodd Fiala       error.SetErrorString("the platform is not currently connected");
589015d818bSTodd Fiala   }
590015d818bSTodd Fiala   return process_sp;
591015d818bSTodd Fiala }
592015d818bSTodd Fiala 
593b9c1b51eSKate Stone void PlatformPOSIX::CalculateTrapHandlerSymbolNames() {
5942094dbf4SJason Molenda   m_trap_handlers.push_back(ConstString("_sigtramp"));
5952094dbf4SJason Molenda }
5963cb132a0STamas Berghammer 
59797206d57SZachary Turner Status PlatformPOSIX::EvaluateLibdlExpression(
598b9c1b51eSKate Stone     lldb_private::Process *process, const char *expr_cstr,
599d37c9469SPavel Labath     llvm::StringRef expr_prefix, lldb::ValueObjectSP &result_valobj_sp) {
6003cb132a0STamas Berghammer   DynamicLoader *loader = process->GetDynamicLoader();
601b9c1b51eSKate Stone   if (loader) {
60297206d57SZachary Turner     Status error = loader->CanLoadImage();
6033cb132a0STamas Berghammer     if (error.Fail())
6043cb132a0STamas Berghammer       return error;
6053cb132a0STamas Berghammer   }
6063cb132a0STamas Berghammer 
6078d94ba0fSJim Ingham   ThreadSP thread_sp(process->GetThreadList().GetExpressionExecutionThread());
6083cb132a0STamas Berghammer   if (!thread_sp)
60997206d57SZachary Turner     return Status("Selected thread isn't valid");
6103cb132a0STamas Berghammer 
6113cb132a0STamas Berghammer   StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
6123cb132a0STamas Berghammer   if (!frame_sp)
61397206d57SZachary Turner     return Status("Frame 0 isn't valid");
6143cb132a0STamas Berghammer 
6153cb132a0STamas Berghammer   ExecutionContext exe_ctx;
6163cb132a0STamas Berghammer   frame_sp->CalculateExecutionContext(exe_ctx);
6173cb132a0STamas Berghammer   EvaluateExpressionOptions expr_options;
6183cb132a0STamas Berghammer   expr_options.SetUnwindOnError(true);
6193cb132a0STamas Berghammer   expr_options.SetIgnoreBreakpoints(true);
6203cb132a0STamas Berghammer   expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
6213cb132a0STamas Berghammer   expr_options.SetLanguage(eLanguageTypeC_plus_plus);
622b9c1b51eSKate Stone   expr_options.SetTrapExceptions(false); // dlopen can't throw exceptions, so
623b9c1b51eSKate Stone                                          // don't do the work to trap them.
624*4c03ea14SAdrian Prantl   expr_options.SetTimeout(process->GetUtilityExpressionTimeout());
6253cb132a0STamas Berghammer 
62697206d57SZachary Turner   Status expr_error;
62702980825SJim Ingham   ExpressionResults result =
628b9c1b51eSKate Stone       UserExpression::Evaluate(exe_ctx, expr_options, expr_cstr, expr_prefix,
629b9c1b51eSKate Stone                                result_valobj_sp, expr_error);
63002980825SJim Ingham   if (result != eExpressionCompleted)
63102980825SJim Ingham     return expr_error;
63202980825SJim Ingham 
6333cb132a0STamas Berghammer   if (result_valobj_sp->GetError().Fail())
6343cb132a0STamas Berghammer     return result_valobj_sp->GetError();
63597206d57SZachary Turner   return Status();
6363cb132a0STamas Berghammer }
6373cb132a0STamas Berghammer 
638d10d3795SFrederic Riss std::unique_ptr<UtilityFunction>
6391ecb34f4SJim Ingham PlatformPOSIX::MakeLoadImageUtilityFunction(ExecutionContext &exe_ctx,
640d10d3795SFrederic Riss                                             Status &error) {
64105097246SAdrian Prantl   // Remember to prepend this with the prefix from
64205097246SAdrian Prantl   // GetLibdlFunctionDeclarations. The returned values are all in
64305097246SAdrian Prantl   // __lldb_dlopen_result for consistency. The wrapper returns a void * but
64405097246SAdrian Prantl   // doesn't use it because UtilityFunctions don't work with void returns at
64505097246SAdrian Prantl   // present.
6461ecb34f4SJim Ingham   static const char *dlopen_wrapper_code = R"(
6471ecb34f4SJim Ingham   struct __lldb_dlopen_result {
6481ecb34f4SJim Ingham     void *image_ptr;
6491ecb34f4SJim Ingham     const char *error_str;
6501ecb34f4SJim Ingham   };
6511ecb34f4SJim Ingham 
65203856dd9SJim Ingham   extern void *memcpy(void *, const void *, size_t size);
6530d231f71SJim Ingham   extern size_t strlen(const char *);
6540d231f71SJim Ingham 
6550d231f71SJim Ingham 
6560d231f71SJim Ingham   void * __lldb_dlopen_wrapper (const char *name,
6570d231f71SJim Ingham                                 const char *path_strings,
6580d231f71SJim Ingham                                 char *buffer,
6591ecb34f4SJim Ingham                                 __lldb_dlopen_result *result_ptr)
6601ecb34f4SJim Ingham   {
6610d231f71SJim Ingham     // This is the case where the name is the full path:
66203856dd9SJim Ingham     if (!path_strings) {
6630d231f71SJim Ingham       result_ptr->image_ptr = dlopen(name, 2);
66403856dd9SJim Ingham       if (result_ptr->image_ptr)
6650d231f71SJim Ingham         result_ptr->error_str = nullptr;
6660d231f71SJim Ingham       return nullptr;
6670d231f71SJim Ingham     }
6680d231f71SJim Ingham 
6690d231f71SJim Ingham     // This is the case where we have a list of paths:
6700d231f71SJim Ingham     size_t name_len = strlen(name);
67103856dd9SJim Ingham     while (path_strings && path_strings[0] != '\0') {
6720d231f71SJim Ingham       size_t path_len = strlen(path_strings);
6730d231f71SJim Ingham       memcpy((void *) buffer, (void *) path_strings, path_len);
6740d231f71SJim Ingham       buffer[path_len] = '/';
6750d231f71SJim Ingham       char *target_ptr = buffer+path_len+1;
6760d231f71SJim Ingham       memcpy((void *) target_ptr, (void *) name, name_len + 1);
6770d231f71SJim Ingham       result_ptr->image_ptr = dlopen(buffer, 2);
67803856dd9SJim Ingham       if (result_ptr->image_ptr) {
6790d231f71SJim Ingham         result_ptr->error_str = nullptr;
6800d231f71SJim Ingham         break;
6810d231f71SJim Ingham       }
6821ecb34f4SJim Ingham       result_ptr->error_str = dlerror();
6830d231f71SJim Ingham       path_strings = path_strings + path_len + 1;
6840d231f71SJim Ingham     }
6851ecb34f4SJim Ingham     return nullptr;
6861ecb34f4SJim Ingham   }
6871ecb34f4SJim Ingham   )";
6881ecb34f4SJim Ingham 
6891ecb34f4SJim Ingham   static const char *dlopen_wrapper_name = "__lldb_dlopen_wrapper";
6901ecb34f4SJim Ingham   Process *process = exe_ctx.GetProcessSP().get();
6911ecb34f4SJim Ingham   // Insert the dlopen shim defines into our generic expression:
6921ecb34f4SJim Ingham   std::string expr(GetLibdlFunctionDeclarations(process));
6931ecb34f4SJim Ingham   expr.append(dlopen_wrapper_code);
6941ecb34f4SJim Ingham   Status utility_error;
6951ecb34f4SJim Ingham   DiagnosticManager diagnostics;
6961ecb34f4SJim Ingham 
6971ecb34f4SJim Ingham   std::unique_ptr<UtilityFunction> dlopen_utility_func_up(process
6981ecb34f4SJim Ingham       ->GetTarget().GetUtilityFunctionForLanguage(expr.c_str(),
6991ecb34f4SJim Ingham                                                   eLanguageTypeObjC,
7001ecb34f4SJim Ingham                                                   dlopen_wrapper_name,
7011ecb34f4SJim Ingham                                                   utility_error));
7021ecb34f4SJim Ingham   if (utility_error.Fail()) {
7031ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not make utility"
7041ecb34f4SJim Ingham                                    "function: %s", utility_error.AsCString());
7051ecb34f4SJim Ingham     return nullptr;
7061ecb34f4SJim Ingham   }
7071ecb34f4SJim Ingham   if (!dlopen_utility_func_up->Install(diagnostics, exe_ctx)) {
7081ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not install utility"
7091ecb34f4SJim Ingham                                    "function: %s",
7101ecb34f4SJim Ingham                                    diagnostics.GetString().c_str());
7111ecb34f4SJim Ingham     return nullptr;
7121ecb34f4SJim Ingham   }
7131ecb34f4SJim Ingham 
7141ecb34f4SJim Ingham   Value value;
7151ecb34f4SJim Ingham   ValueList arguments;
7161ecb34f4SJim Ingham   FunctionCaller *do_dlopen_function = nullptr;
7171ecb34f4SJim Ingham 
7181ecb34f4SJim Ingham   // Fetch the clang types we will need:
7191ecb34f4SJim Ingham   ClangASTContext *ast = process->GetTarget().GetScratchClangASTContext();
7201ecb34f4SJim Ingham 
7211ecb34f4SJim Ingham   CompilerType clang_void_pointer_type
7221ecb34f4SJim Ingham       = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
7231ecb34f4SJim Ingham   CompilerType clang_char_pointer_type
7241ecb34f4SJim Ingham         = ast->GetBasicType(eBasicTypeChar).GetPointerType();
7251ecb34f4SJim Ingham 
7260d231f71SJim Ingham   // We are passing four arguments, the basename, the list of places to look,
7270d231f71SJim Ingham   // a buffer big enough for all the path + name combos, and
7280d231f71SJim Ingham   // a pointer to the storage we've made for the result:
7291ecb34f4SJim Ingham   value.SetValueType(Value::eValueTypeScalar);
7301ecb34f4SJim Ingham   value.SetCompilerType(clang_void_pointer_type);
7311ecb34f4SJim Ingham   arguments.PushValue(value);
7321ecb34f4SJim Ingham   value.SetCompilerType(clang_char_pointer_type);
7331ecb34f4SJim Ingham   arguments.PushValue(value);
7340d231f71SJim Ingham   arguments.PushValue(value);
7350d231f71SJim Ingham   arguments.PushValue(value);
7361ecb34f4SJim Ingham 
7371ecb34f4SJim Ingham   do_dlopen_function = dlopen_utility_func_up->MakeFunctionCaller(
7381ecb34f4SJim Ingham       clang_void_pointer_type, arguments, exe_ctx.GetThreadSP(), utility_error);
7391ecb34f4SJim Ingham   if (utility_error.Fail()) {
7401ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not make function"
7411ecb34f4SJim Ingham                                    "caller: %s", utility_error.AsCString());
7421ecb34f4SJim Ingham     return nullptr;
7431ecb34f4SJim Ingham   }
7441ecb34f4SJim Ingham 
7451ecb34f4SJim Ingham   do_dlopen_function = dlopen_utility_func_up->GetFunctionCaller();
7461ecb34f4SJim Ingham   if (!do_dlopen_function) {
7471ecb34f4SJim Ingham     error.SetErrorString("dlopen error: could not get function caller.");
7481ecb34f4SJim Ingham     return nullptr;
7491ecb34f4SJim Ingham   }
7501ecb34f4SJim Ingham 
7511ecb34f4SJim Ingham   // We made a good utility function, so cache it in the process:
752d10d3795SFrederic Riss   return dlopen_utility_func_up;
7531ecb34f4SJim Ingham }
7541ecb34f4SJim Ingham 
755b9c1b51eSKate Stone uint32_t PlatformPOSIX::DoLoadImage(lldb_private::Process *process,
7564fbd67acSTamas Berghammer                                     const lldb_private::FileSpec &remote_file,
7570d231f71SJim Ingham                                     const std::vector<std::string> *paths,
7580d231f71SJim Ingham                                     lldb_private::Status &error,
7590d231f71SJim Ingham                                     lldb_private::FileSpec *loaded_image) {
7600d231f71SJim Ingham   if (loaded_image)
7610d231f71SJim Ingham     loaded_image->Clear();
7620d231f71SJim Ingham 
7631ecb34f4SJim Ingham   std::string path;
7641ecb34f4SJim Ingham   path = remote_file.GetPath();
7653cb132a0STamas Berghammer 
7661ecb34f4SJim Ingham   ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
7671ecb34f4SJim Ingham   if (!thread_sp) {
7681ecb34f4SJim Ingham     error.SetErrorString("dlopen error: no thread available to call dlopen.");
7693cb132a0STamas Berghammer     return LLDB_INVALID_IMAGE_TOKEN;
7703cb132a0STamas Berghammer   }
7713cb132a0STamas Berghammer 
7721ecb34f4SJim Ingham   DiagnosticManager diagnostics;
7733cb132a0STamas Berghammer 
7741ecb34f4SJim Ingham   ExecutionContext exe_ctx;
7751ecb34f4SJim Ingham   thread_sp->CalculateExecutionContext(exe_ctx);
7761ecb34f4SJim Ingham 
7771ecb34f4SJim Ingham   Status utility_error;
778d10d3795SFrederic Riss   UtilityFunction *dlopen_utility_func;
7791ecb34f4SJim Ingham   ValueList arguments;
7801ecb34f4SJim Ingham   FunctionCaller *do_dlopen_function = nullptr;
7811ecb34f4SJim Ingham 
782d10d3795SFrederic Riss   // The UtilityFunction is held in the Process.  Platforms don't track the
783d10d3795SFrederic Riss   // lifespan of the Targets that use them, we can't put this in the Platform.
784d10d3795SFrederic Riss   dlopen_utility_func = process->GetLoadImageUtilityFunction(
785d10d3795SFrederic Riss       this, [&]() -> std::unique_ptr<UtilityFunction> {
786d10d3795SFrederic Riss         return MakeLoadImageUtilityFunction(exe_ctx, error);
787d10d3795SFrederic Riss       });
7881ecb34f4SJim Ingham   // If we couldn't make it, the error will be in error, so we can exit here.
7891ecb34f4SJim Ingham   if (!dlopen_utility_func)
7901ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
7911ecb34f4SJim Ingham 
7921ecb34f4SJim Ingham   do_dlopen_function = dlopen_utility_func->GetFunctionCaller();
7931ecb34f4SJim Ingham   if (!do_dlopen_function) {
7941ecb34f4SJim Ingham     error.SetErrorString("dlopen error: could not get function caller.");
7951ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
7961ecb34f4SJim Ingham   }
7971ecb34f4SJim Ingham   arguments = do_dlopen_function->GetArgumentValues();
7981ecb34f4SJim Ingham 
79905097246SAdrian Prantl   // Now insert the path we are searching for and the result structure into the
80005097246SAdrian Prantl   // target.
8011ecb34f4SJim Ingham   uint32_t permissions = ePermissionsReadable|ePermissionsWritable;
8021ecb34f4SJim Ingham   size_t path_len = path.size() + 1;
8031ecb34f4SJim Ingham   lldb::addr_t path_addr = process->AllocateMemory(path_len,
8041ecb34f4SJim Ingham                                                    permissions,
8051ecb34f4SJim Ingham                                                    utility_error);
8061ecb34f4SJim Ingham   if (path_addr == LLDB_INVALID_ADDRESS) {
8071ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
8081ecb34f4SJim Ingham                                     "for path: %s", utility_error.AsCString());
8091ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
8101ecb34f4SJim Ingham   }
8111ecb34f4SJim Ingham 
8121ecb34f4SJim Ingham   // Make sure we deallocate the input string memory:
8131ecb34f4SJim Ingham   CleanUp path_cleanup([process, path_addr] {
8141ecb34f4SJim Ingham       process->DeallocateMemory(path_addr);
8151ecb34f4SJim Ingham   });
8161ecb34f4SJim Ingham 
8171ecb34f4SJim Ingham   process->WriteMemory(path_addr, path.c_str(), path_len, utility_error);
8181ecb34f4SJim Ingham   if (utility_error.Fail()) {
8191ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not write path string:"
8201ecb34f4SJim Ingham                                     " %s", utility_error.AsCString());
8211ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
8221ecb34f4SJim Ingham   }
8231ecb34f4SJim Ingham 
82405097246SAdrian Prantl   // Make space for our return structure.  It is two pointers big: the token
82505097246SAdrian Prantl   // and the error string.
8261ecb34f4SJim Ingham   const uint32_t addr_size = process->GetAddressByteSize();
8271ecb34f4SJim Ingham   lldb::addr_t return_addr = process->CallocateMemory(2*addr_size,
8281ecb34f4SJim Ingham                                                       permissions,
8291ecb34f4SJim Ingham                                                       utility_error);
8301ecb34f4SJim Ingham   if (utility_error.Fail()) {
8311ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
8321ecb34f4SJim Ingham                                     "for path: %s", utility_error.AsCString());
8331ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
8341ecb34f4SJim Ingham   }
8351ecb34f4SJim Ingham 
8361ecb34f4SJim Ingham   // Make sure we deallocate the result structure memory
8371ecb34f4SJim Ingham   CleanUp return_cleanup([process, return_addr] {
8381ecb34f4SJim Ingham       process->DeallocateMemory(return_addr);
8391ecb34f4SJim Ingham   });
8401ecb34f4SJim Ingham 
8410d231f71SJim Ingham   // This will be the address of the storage for paths, if we are using them,
8420d231f71SJim Ingham   // or nullptr to signal we aren't.
8430d231f71SJim Ingham   lldb::addr_t path_array_addr = 0x0;
8440d231f71SJim Ingham   llvm::Optional<CleanUp> path_array_cleanup;
8450d231f71SJim Ingham 
8460d231f71SJim Ingham   // This is the address to a buffer large enough to hold the largest path
8470d231f71SJim Ingham   // conjoined with the library name we're passing in.  This is a convenience
8480d231f71SJim Ingham   // to avoid having to call malloc in the dlopen function.
8490d231f71SJim Ingham   lldb::addr_t buffer_addr = 0x0;
8500d231f71SJim Ingham   llvm::Optional<CleanUp> buffer_cleanup;
8510d231f71SJim Ingham 
8521ecb34f4SJim Ingham   // Set the values into our args and write them to the target:
8530d231f71SJim Ingham   if (paths != nullptr) {
8540d231f71SJim Ingham     // First insert the paths into the target.  This is expected to be a
8550d231f71SJim Ingham     // continuous buffer with the strings laid out null terminated and
8560d231f71SJim Ingham     // end to end with an empty string terminating the buffer.
8570d231f71SJim Ingham     // We also compute the buffer's required size as we go.
8580d231f71SJim Ingham     size_t buffer_size = 0;
8590d231f71SJim Ingham     std::string path_array;
8600d231f71SJim Ingham     for (auto path : *paths) {
86140fa4a1aSJim Ingham       // Don't insert empty paths, they will make us abort the path
86240fa4a1aSJim Ingham       // search prematurely.
86340fa4a1aSJim Ingham       if (path.empty())
86440fa4a1aSJim Ingham         continue;
8650d231f71SJim Ingham       size_t path_size = path.size();
8660d231f71SJim Ingham       path_array.append(path);
8670d231f71SJim Ingham       path_array.push_back('\0');
8680d231f71SJim Ingham       if (path_size > buffer_size)
8690d231f71SJim Ingham         buffer_size = path_size;
8700d231f71SJim Ingham     }
8710d231f71SJim Ingham     path_array.push_back('\0');
8720d231f71SJim Ingham 
8730d231f71SJim Ingham     path_array_addr = process->AllocateMemory(path_array.size(),
8740d231f71SJim Ingham                                               permissions,
8750d231f71SJim Ingham                                               utility_error);
8760d231f71SJim Ingham     if (path_array_addr == LLDB_INVALID_ADDRESS) {
8770d231f71SJim Ingham       error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
8780d231f71SJim Ingham                                       "for path array: %s",
8790d231f71SJim Ingham                                       utility_error.AsCString());
8800d231f71SJim Ingham       return LLDB_INVALID_IMAGE_TOKEN;
8810d231f71SJim Ingham     }
8820d231f71SJim Ingham 
8830d231f71SJim Ingham     // Make sure we deallocate the paths array.
8840d231f71SJim Ingham     path_array_cleanup.emplace([process, path_array_addr] {
8850d231f71SJim Ingham         process->DeallocateMemory(path_array_addr);
8860d231f71SJim Ingham     });
8870d231f71SJim Ingham 
8880d231f71SJim Ingham     process->WriteMemory(path_array_addr, path_array.data(),
8890d231f71SJim Ingham                          path_array.size(), utility_error);
8900d231f71SJim Ingham 
8910d231f71SJim Ingham     if (utility_error.Fail()) {
8920d231f71SJim Ingham       error.SetErrorStringWithFormat("dlopen error: could not write path array:"
8930d231f71SJim Ingham                                      " %s", utility_error.AsCString());
8940d231f71SJim Ingham       return LLDB_INVALID_IMAGE_TOKEN;
8950d231f71SJim Ingham     }
8960d231f71SJim Ingham     // Now make spaces in the target for the buffer.  We need to add one for
8970d231f71SJim Ingham     // the '/' that the utility function will insert and one for the '\0':
8980d231f71SJim Ingham     buffer_size += path.size() + 2;
8990d231f71SJim Ingham 
9000d231f71SJim Ingham     buffer_addr = process->AllocateMemory(buffer_size,
9010d231f71SJim Ingham                                           permissions,
9020d231f71SJim Ingham                                           utility_error);
9030d231f71SJim Ingham     if (buffer_addr == LLDB_INVALID_ADDRESS) {
9040d231f71SJim Ingham       error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
9050d231f71SJim Ingham                                       "for buffer: %s",
9060d231f71SJim Ingham                                       utility_error.AsCString());
9070d231f71SJim Ingham       return LLDB_INVALID_IMAGE_TOKEN;
9080d231f71SJim Ingham     }
9090d231f71SJim Ingham 
9100d231f71SJim Ingham     // Make sure we deallocate the buffer memory:
9110d231f71SJim Ingham     buffer_cleanup.emplace([process, buffer_addr] {
9120d231f71SJim Ingham         process->DeallocateMemory(buffer_addr);
9130d231f71SJim Ingham     });
9140d231f71SJim Ingham   }
9150d231f71SJim Ingham 
9161ecb34f4SJim Ingham   arguments.GetValueAtIndex(0)->GetScalar() = path_addr;
9170d231f71SJim Ingham   arguments.GetValueAtIndex(1)->GetScalar() = path_array_addr;
9180d231f71SJim Ingham   arguments.GetValueAtIndex(2)->GetScalar() = buffer_addr;
9190d231f71SJim Ingham   arguments.GetValueAtIndex(3)->GetScalar() = return_addr;
9201ecb34f4SJim Ingham 
9211ecb34f4SJim Ingham   lldb::addr_t func_args_addr = LLDB_INVALID_ADDRESS;
9221ecb34f4SJim Ingham 
9231ecb34f4SJim Ingham   diagnostics.Clear();
9241ecb34f4SJim Ingham   if (!do_dlopen_function->WriteFunctionArguments(exe_ctx,
9251ecb34f4SJim Ingham                                                  func_args_addr,
9261ecb34f4SJim Ingham                                                  arguments,
9271ecb34f4SJim Ingham                                                  diagnostics)) {
9281ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not write function "
9291ecb34f4SJim Ingham                                    "arguments: %s",
9301ecb34f4SJim Ingham                                    diagnostics.GetString().c_str());
9311ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9321ecb34f4SJim Ingham   }
9331ecb34f4SJim Ingham 
9341ecb34f4SJim Ingham   // Make sure we clean up the args structure.  We can't reuse it because the
9351ecb34f4SJim Ingham   // Platform lives longer than the process and the Platforms don't get a
9361ecb34f4SJim Ingham   // signal to clean up cached data when a process goes away.
9371ecb34f4SJim Ingham   CleanUp args_cleanup([do_dlopen_function, &exe_ctx, func_args_addr] {
9381ecb34f4SJim Ingham     do_dlopen_function->DeallocateFunctionResults(exe_ctx, func_args_addr);
9391ecb34f4SJim Ingham   });
9401ecb34f4SJim Ingham 
9411ecb34f4SJim Ingham   // Now run the caller:
9421ecb34f4SJim Ingham   EvaluateExpressionOptions options;
9431ecb34f4SJim Ingham   options.SetExecutionPolicy(eExecutionPolicyAlways);
9441ecb34f4SJim Ingham   options.SetLanguage(eLanguageTypeC_plus_plus);
9451ecb34f4SJim Ingham   options.SetIgnoreBreakpoints(true);
9461ecb34f4SJim Ingham   options.SetUnwindOnError(true);
9471ecb34f4SJim Ingham   options.SetTrapExceptions(false); // dlopen can't throw exceptions, so
9481ecb34f4SJim Ingham                                     // don't do the work to trap them.
949*4c03ea14SAdrian Prantl   options.SetTimeout(process->GetUtilityExpressionTimeout());
950c01783a8SRaphael Isemann   options.SetIsForUtilityExpr(true);
9511ecb34f4SJim Ingham 
9521ecb34f4SJim Ingham   Value return_value;
9531ecb34f4SJim Ingham   // Fetch the clang types we will need:
9541ecb34f4SJim Ingham   ClangASTContext *ast = process->GetTarget().GetScratchClangASTContext();
9551ecb34f4SJim Ingham 
9561ecb34f4SJim Ingham   CompilerType clang_void_pointer_type
9571ecb34f4SJim Ingham       = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
9581ecb34f4SJim Ingham 
9591ecb34f4SJim Ingham   return_value.SetCompilerType(clang_void_pointer_type);
9601ecb34f4SJim Ingham 
9611ecb34f4SJim Ingham   ExpressionResults results = do_dlopen_function->ExecuteFunction(
9621ecb34f4SJim Ingham       exe_ctx, &func_args_addr, options, diagnostics, return_value);
9631ecb34f4SJim Ingham   if (results != eExpressionCompleted) {
9640d231f71SJim Ingham     error.SetErrorStringWithFormat("dlopen error: failed executing "
9651ecb34f4SJim Ingham                                    "dlopen wrapper function: %s",
9661ecb34f4SJim Ingham                                    diagnostics.GetString().c_str());
9671ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9681ecb34f4SJim Ingham   }
9691ecb34f4SJim Ingham 
9701ecb34f4SJim Ingham   // Read the dlopen token from the return area:
9711ecb34f4SJim Ingham   lldb::addr_t token = process->ReadPointerFromMemory(return_addr,
9721ecb34f4SJim Ingham                                                       utility_error);
9731ecb34f4SJim Ingham   if (utility_error.Fail()) {
9741ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not read the return "
9751ecb34f4SJim Ingham                                     "struct: %s", utility_error.AsCString());
9761ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9771ecb34f4SJim Ingham   }
9781ecb34f4SJim Ingham 
9791ecb34f4SJim Ingham   // The dlopen succeeded!
9800d231f71SJim Ingham   if (token != 0x0) {
9810d231f71SJim Ingham     if (loaded_image && buffer_addr != 0x0)
9820d231f71SJim Ingham     {
9830d231f71SJim Ingham       // Capture the image which was loaded.  We leave it in the buffer on
9840d231f71SJim Ingham       // exit from the dlopen function, so we can just read it from there:
9850d231f71SJim Ingham       std::string name_string;
9860d231f71SJim Ingham       process->ReadCStringFromMemory(buffer_addr, name_string, utility_error);
9870d231f71SJim Ingham       if (utility_error.Success())
9888f3be7a3SJonas Devlieghere         loaded_image->SetFile(name_string, llvm::sys::path::Style::posix);
9890d231f71SJim Ingham     }
9901ecb34f4SJim Ingham     return process->AddImageToken(token);
9910d231f71SJim Ingham   }
9921ecb34f4SJim Ingham 
9931ecb34f4SJim Ingham   // We got an error, lets read in the error string:
9941ecb34f4SJim Ingham   std::string dlopen_error_str;
9951ecb34f4SJim Ingham   lldb::addr_t error_addr
9961ecb34f4SJim Ingham     = process->ReadPointerFromMemory(return_addr + addr_size, utility_error);
9971ecb34f4SJim Ingham   if (utility_error.Fail()) {
9981ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not read error string: "
9991ecb34f4SJim Ingham                                     "%s", utility_error.AsCString());
10001ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
10011ecb34f4SJim Ingham   }
10021ecb34f4SJim Ingham 
10031ecb34f4SJim Ingham   size_t num_chars = process->ReadCStringFromMemory(error_addr + addr_size,
10041ecb34f4SJim Ingham                                                     dlopen_error_str,
10051ecb34f4SJim Ingham                                                     utility_error);
10061ecb34f4SJim Ingham   if (utility_error.Success() && num_chars > 0)
1007b9c1b51eSKate Stone     error.SetErrorStringWithFormat("dlopen error: %s",
10081ecb34f4SJim Ingham                                    dlopen_error_str.c_str());
10093cb132a0STamas Berghammer   else
10103cb132a0STamas Berghammer     error.SetErrorStringWithFormat("dlopen failed for unknown reasons.");
10111ecb34f4SJim Ingham 
10123cb132a0STamas Berghammer   return LLDB_INVALID_IMAGE_TOKEN;
10133cb132a0STamas Berghammer }
10143cb132a0STamas Berghammer 
101597206d57SZachary Turner Status PlatformPOSIX::UnloadImage(lldb_private::Process *process,
1016b9c1b51eSKate Stone                                   uint32_t image_token) {
10173cb132a0STamas Berghammer   const addr_t image_addr = process->GetImagePtrFromToken(image_token);
10183cb132a0STamas Berghammer   if (image_addr == LLDB_INVALID_ADDRESS)
101997206d57SZachary Turner     return Status("Invalid image token");
10203cb132a0STamas Berghammer 
10213cb132a0STamas Berghammer   StreamString expr;
10223cb132a0STamas Berghammer   expr.Printf("dlclose((void *)0x%" PRIx64 ")", image_addr);
1023f7a5851dSNitesh Jain   llvm::StringRef prefix = GetLibdlFunctionDeclarations(process);
10243cb132a0STamas Berghammer   lldb::ValueObjectSP result_valobj_sp;
102597206d57SZachary Turner   Status error = EvaluateLibdlExpression(process, expr.GetData(), prefix,
1026b9c1b51eSKate Stone                                          result_valobj_sp);
10273cb132a0STamas Berghammer   if (error.Fail())
10283cb132a0STamas Berghammer     return error;
10293cb132a0STamas Berghammer 
10303cb132a0STamas Berghammer   if (result_valobj_sp->GetError().Fail())
10313cb132a0STamas Berghammer     return result_valobj_sp->GetError();
10323cb132a0STamas Berghammer 
10333cb132a0STamas Berghammer   Scalar scalar;
1034b9c1b51eSKate Stone   if (result_valobj_sp->ResolveValue(scalar)) {
10353cb132a0STamas Berghammer     if (scalar.UInt(1))
103697206d57SZachary Turner       return Status("expression failed: \"%s\"", expr.GetData());
10373cb132a0STamas Berghammer     process->ResetImageToken(image_token);
10383cb132a0STamas Berghammer   }
103997206d57SZachary Turner   return Status();
10403cb132a0STamas Berghammer }
1041fcdde63fSTamas Berghammer 
1042f7a5851dSNitesh Jain llvm::StringRef
1043f7a5851dSNitesh Jain PlatformPOSIX::GetLibdlFunctionDeclarations(lldb_private::Process *process) {
1044fcdde63fSTamas Berghammer   return R"(
1045fcdde63fSTamas Berghammer               extern "C" void* dlopen(const char*, int);
1046fcdde63fSTamas Berghammer               extern "C" void* dlsym(void*, const char*);
1047fcdde63fSTamas Berghammer               extern "C" int   dlclose(void*);
1048fcdde63fSTamas Berghammer               extern "C" char* dlerror(void);
1049fcdde63fSTamas Berghammer              )";
1050fcdde63fSTamas Berghammer }
1051ccd6cffbSTamas Berghammer 
1052b9c1b51eSKate Stone size_t PlatformPOSIX::ConnectToWaitingProcesses(Debugger &debugger,
105397206d57SZachary Turner                                                 Status &error) {
1054ccd6cffbSTamas Berghammer   if (m_remote_platform_sp)
1055ccd6cffbSTamas Berghammer     return m_remote_platform_sp->ConnectToWaitingProcesses(debugger, error);
1056ccd6cffbSTamas Berghammer   return Platform::ConnectToWaitingProcesses(debugger, error);
1057ccd6cffbSTamas Berghammer }
1058ec9bc8ccSPavel Labath 
1059ec9bc8ccSPavel Labath ConstString PlatformPOSIX::GetFullNameForDylib(ConstString basename) {
1060ec9bc8ccSPavel Labath   if (basename.IsEmpty())
1061ec9bc8ccSPavel Labath     return basename;
1062ec9bc8ccSPavel Labath 
1063ec9bc8ccSPavel Labath   StreamString stream;
1064ec9bc8ccSPavel Labath   stream.Printf("lib%s.so", basename.GetCString());
1065ec9bc8ccSPavel Labath   return ConstString(stream.GetString());
1066ec9bc8ccSPavel Labath }
1067