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 /// Default Constructor
40b9c1b51eSKate Stone PlatformPOSIX::PlatformPOSIX(bool is_host)
4152d9c62aSPavel Labath     : RemoteAwarePlatform(is_host), // This is the local host platform
420a66e2f1SEnrico Granata       m_option_group_platform_rsync(new OptionGroupPlatformRSync()),
430a66e2f1SEnrico Granata       m_option_group_platform_ssh(new OptionGroupPlatformSSH()),
4452d9c62aSPavel Labath       m_option_group_platform_caching(new OptionGroupPlatformCaching()) {}
45e0f8f574SDaniel Malea 
46e0f8f574SDaniel Malea /// Destructor.
47e0f8f574SDaniel Malea ///
48e0f8f574SDaniel Malea /// The destructor is virtual since this class is designed to be
49e0f8f574SDaniel Malea /// inherited from by the plug-in instance.
50b9c1b51eSKate Stone PlatformPOSIX::~PlatformPOSIX() {}
51e0f8f574SDaniel Malea 
52b9c1b51eSKate Stone lldb_private::OptionGroupOptions *PlatformPOSIX::GetConnectionOptions(
53b9c1b51eSKate Stone     lldb_private::CommandInterpreter &interpreter) {
540a66e2f1SEnrico Granata   auto iter = m_options.find(&interpreter), end = m_options.end();
55b9c1b51eSKate Stone   if (iter == end) {
56b9c1b51eSKate Stone     std::unique_ptr<lldb_private::OptionGroupOptions> options(
57b9c1b51eSKate Stone         new OptionGroupOptions());
580a66e2f1SEnrico Granata     options->Append(m_option_group_platform_rsync.get());
590a66e2f1SEnrico Granata     options->Append(m_option_group_platform_ssh.get());
600a66e2f1SEnrico Granata     options->Append(m_option_group_platform_caching.get());
610a66e2f1SEnrico Granata     m_options[&interpreter] = std::move(options);
62e0f8f574SDaniel Malea   }
630a66e2f1SEnrico Granata 
640a66e2f1SEnrico Granata   return m_options.at(&interpreter).get();
65e0f8f574SDaniel Malea }
66e0f8f574SDaniel Malea 
6797206d57SZachary Turner Status
6897206d57SZachary Turner PlatformPOSIX::ResolveExecutable(const ModuleSpec &module_spec,
69fc89bbccSPavel Labath                                  lldb::ModuleSP &exe_module_sp,
70fc89bbccSPavel Labath                                  const FileSpecList *module_search_paths_ptr) {
7197206d57SZachary Turner   Status error;
72fc89bbccSPavel Labath   // Nothing special to do here, just use the actual file and architecture
73fc89bbccSPavel Labath 
74fc89bbccSPavel Labath   char exe_path[PATH_MAX];
75fc89bbccSPavel Labath   ModuleSpec resolved_module_spec(module_spec);
76fc89bbccSPavel Labath 
77fc89bbccSPavel Labath   if (IsHost()) {
7805097246SAdrian Prantl     // If we have "ls" as the exe_file, resolve the executable location based
7905097246SAdrian Prantl     // on the current path variables
80dbd7fabaSJonas Devlieghere     if (!FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec())) {
81fc89bbccSPavel Labath       resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path));
828f3be7a3SJonas Devlieghere       resolved_module_spec.GetFileSpec().SetFile(exe_path,
83937348cdSJonas Devlieghere                                                  FileSpec::Style::native);
848f3be7a3SJonas Devlieghere       FileSystem::Instance().Resolve(resolved_module_spec.GetFileSpec());
85fc89bbccSPavel Labath     }
86fc89bbccSPavel Labath 
87dbd7fabaSJonas Devlieghere     if (!FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()))
882c22c800SJonas Devlieghere       FileSystem::Instance().ResolveExecutableLocation(
892c22c800SJonas Devlieghere           resolved_module_spec.GetFileSpec());
90fc89bbccSPavel Labath 
91fc89bbccSPavel Labath     // Resolve any executable within a bundle on MacOSX
92fc89bbccSPavel Labath     Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec());
93fc89bbccSPavel Labath 
94dbd7fabaSJonas Devlieghere     if (FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()))
95fc89bbccSPavel Labath       error.Clear();
96fc89bbccSPavel Labath     else {
977c5310bbSJonas Devlieghere       const uint32_t permissions = FileSystem::Instance().GetPermissions(
987c5310bbSJonas Devlieghere           resolved_module_spec.GetFileSpec());
99fc89bbccSPavel Labath       if (permissions && (permissions & eFilePermissionsEveryoneR) == 0)
100fc89bbccSPavel Labath         error.SetErrorStringWithFormat(
101fc89bbccSPavel Labath             "executable '%s' is not readable",
102fc89bbccSPavel Labath             resolved_module_spec.GetFileSpec().GetPath().c_str());
103fc89bbccSPavel Labath       else
104fc89bbccSPavel Labath         error.SetErrorStringWithFormat(
105fc89bbccSPavel Labath             "unable to find executable for '%s'",
106fc89bbccSPavel Labath             resolved_module_spec.GetFileSpec().GetPath().c_str());
107fc89bbccSPavel Labath     }
108fc89bbccSPavel Labath   } else {
109fc89bbccSPavel Labath     if (m_remote_platform_sp) {
110fc89bbccSPavel Labath       error =
111fc89bbccSPavel Labath           GetCachedExecutable(resolved_module_spec, exe_module_sp,
112fc89bbccSPavel Labath                               module_search_paths_ptr, *m_remote_platform_sp);
113fc89bbccSPavel Labath     } else {
114fc89bbccSPavel Labath       // We may connect to a process and use the provided executable (Don't use
115fc89bbccSPavel Labath       // local $PATH).
116fc89bbccSPavel Labath 
117fc89bbccSPavel Labath       // Resolve any executable within a bundle on MacOSX
118fc89bbccSPavel Labath       Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec());
119fc89bbccSPavel Labath 
120dbd7fabaSJonas Devlieghere       if (FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()))
121fc89bbccSPavel Labath         error.Clear();
122fc89bbccSPavel Labath       else
123fc89bbccSPavel Labath         error.SetErrorStringWithFormat("the platform is not currently "
124fc89bbccSPavel Labath                                        "connected, and '%s' doesn't exist in "
125fc89bbccSPavel Labath                                        "the system root.",
126fc89bbccSPavel Labath                                        exe_path);
127fc89bbccSPavel Labath     }
128fc89bbccSPavel Labath   }
129fc89bbccSPavel Labath 
130fc89bbccSPavel Labath   if (error.Success()) {
131fc89bbccSPavel Labath     if (resolved_module_spec.GetArchitecture().IsValid()) {
132fc89bbccSPavel Labath       error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
133fc89bbccSPavel Labath                                           module_search_paths_ptr, nullptr, nullptr);
134fc89bbccSPavel Labath       if (error.Fail()) {
135fc89bbccSPavel Labath         // If we failed, it may be because the vendor and os aren't known. If
136fc89bbccSPavel Labath 	// that is the case, try setting them to the host architecture and give
137fc89bbccSPavel Labath 	// it another try.
138fc89bbccSPavel Labath         llvm::Triple &module_triple =
139fc89bbccSPavel Labath             resolved_module_spec.GetArchitecture().GetTriple();
140fc89bbccSPavel Labath         bool is_vendor_specified =
141fc89bbccSPavel Labath             (module_triple.getVendor() != llvm::Triple::UnknownVendor);
142fc89bbccSPavel Labath         bool is_os_specified =
143fc89bbccSPavel Labath             (module_triple.getOS() != llvm::Triple::UnknownOS);
144fc89bbccSPavel Labath         if (!is_vendor_specified || !is_os_specified) {
145fc89bbccSPavel Labath           const llvm::Triple &host_triple =
146fc89bbccSPavel Labath               HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple();
147fc89bbccSPavel Labath 
148fc89bbccSPavel Labath           if (!is_vendor_specified)
149fc89bbccSPavel Labath             module_triple.setVendorName(host_triple.getVendorName());
150fc89bbccSPavel Labath           if (!is_os_specified)
151fc89bbccSPavel Labath             module_triple.setOSName(host_triple.getOSName());
152fc89bbccSPavel Labath 
153fc89bbccSPavel Labath           error = ModuleList::GetSharedModule(resolved_module_spec,
154fc89bbccSPavel Labath                                               exe_module_sp, module_search_paths_ptr, nullptr, nullptr);
155fc89bbccSPavel Labath         }
156fc89bbccSPavel Labath       }
157fc89bbccSPavel Labath 
158fc89bbccSPavel Labath       // TODO find out why exe_module_sp might be NULL
159fc89bbccSPavel Labath       if (error.Fail() || !exe_module_sp || !exe_module_sp->GetObjectFile()) {
160fc89bbccSPavel Labath         exe_module_sp.reset();
161fc89bbccSPavel Labath         error.SetErrorStringWithFormat(
162fc89bbccSPavel Labath             "'%s' doesn't contain the architecture %s",
163fc89bbccSPavel Labath             resolved_module_spec.GetFileSpec().GetPath().c_str(),
164fc89bbccSPavel Labath             resolved_module_spec.GetArchitecture().GetArchitectureName());
165fc89bbccSPavel Labath       }
166fc89bbccSPavel Labath     } else {
16705097246SAdrian Prantl       // No valid architecture was specified, ask the platform for the
16805097246SAdrian Prantl       // architectures that we should be using (in the correct order) and see
16905097246SAdrian Prantl       // if we can find a match that way
170fc89bbccSPavel Labath       StreamString arch_names;
171fc89bbccSPavel Labath       for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
172fc89bbccSPavel Labath                idx, resolved_module_spec.GetArchitecture());
173fc89bbccSPavel Labath            ++idx) {
174fc89bbccSPavel Labath         error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
175fc89bbccSPavel Labath                                             module_search_paths_ptr, nullptr, nullptr);
176fc89bbccSPavel Labath         // Did we find an executable using one of the
177fc89bbccSPavel Labath         if (error.Success()) {
178fc89bbccSPavel Labath           if (exe_module_sp && exe_module_sp->GetObjectFile())
179fc89bbccSPavel Labath             break;
180fc89bbccSPavel Labath           else
181fc89bbccSPavel Labath             error.SetErrorToGenericError();
182fc89bbccSPavel Labath         }
183fc89bbccSPavel Labath 
184fc89bbccSPavel Labath         if (idx > 0)
185fc89bbccSPavel Labath           arch_names.PutCString(", ");
186fc89bbccSPavel Labath         arch_names.PutCString(
187fc89bbccSPavel Labath             resolved_module_spec.GetArchitecture().GetArchitectureName());
188fc89bbccSPavel Labath       }
189fc89bbccSPavel Labath 
190fc89bbccSPavel Labath       if (error.Fail() || !exe_module_sp) {
1917c5310bbSJonas Devlieghere         if (FileSystem::Instance().Readable(
1927c5310bbSJonas Devlieghere                 resolved_module_spec.GetFileSpec())) {
193fc89bbccSPavel Labath           error.SetErrorStringWithFormat(
194fc89bbccSPavel Labath               "'%s' doesn't contain any '%s' platform architectures: %s",
195fc89bbccSPavel Labath               resolved_module_spec.GetFileSpec().GetPath().c_str(),
196fc89bbccSPavel Labath               GetPluginName().GetCString(), arch_names.GetData());
197fc89bbccSPavel Labath         } else {
198fc89bbccSPavel Labath           error.SetErrorStringWithFormat(
199fc89bbccSPavel Labath               "'%s' is not readable",
200fc89bbccSPavel Labath               resolved_module_spec.GetFileSpec().GetPath().c_str());
201fc89bbccSPavel Labath         }
202fc89bbccSPavel Labath       }
203fc89bbccSPavel Labath     }
204fc89bbccSPavel Labath   }
205fc89bbccSPavel Labath 
206fc89bbccSPavel Labath   return error;
207fc89bbccSPavel Labath }
208fc89bbccSPavel Labath 
209b9c1b51eSKate Stone static uint32_t chown_file(Platform *platform, const char *path,
210e0f8f574SDaniel Malea                            uint32_t uid = UINT32_MAX,
211b9c1b51eSKate Stone                            uint32_t gid = UINT32_MAX) {
212e0f8f574SDaniel Malea   if (!platform || !path || *path == 0)
213e0f8f574SDaniel Malea     return UINT32_MAX;
214e0f8f574SDaniel Malea 
215e0f8f574SDaniel Malea   if (uid == UINT32_MAX && gid == UINT32_MAX)
216e0f8f574SDaniel Malea     return 0; // pretend I did chown correctly - actually I just didn't care
217e0f8f574SDaniel Malea 
218e0f8f574SDaniel Malea   StreamString command;
219e0f8f574SDaniel Malea   command.PutCString("chown ");
220e0f8f574SDaniel Malea   if (uid != UINT32_MAX)
221e0f8f574SDaniel Malea     command.Printf("%d", uid);
222e0f8f574SDaniel Malea   if (gid != UINT32_MAX)
223e0f8f574SDaniel Malea     command.Printf(":%d", gid);
224e0f8f574SDaniel Malea   command.Printf("%s", path);
225e0f8f574SDaniel Malea   int status;
226*248a1305SKonrad Kleine   platform->RunShellCommand(command.GetData(), nullptr, &status, nullptr,
227*248a1305SKonrad Kleine                             nullptr, std::chrono::seconds(10));
228e0f8f574SDaniel Malea   return status;
229e0f8f574SDaniel Malea }
230e0f8f574SDaniel Malea 
23197206d57SZachary Turner lldb_private::Status
232e0f8f574SDaniel Malea PlatformPOSIX::PutFile(const lldb_private::FileSpec &source,
233b9c1b51eSKate Stone                        const lldb_private::FileSpec &destination, uint32_t uid,
234b9c1b51eSKate Stone                        uint32_t gid) {
235fbb76349SGreg Clayton   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
236fbb76349SGreg Clayton 
237b9c1b51eSKate Stone   if (IsHost()) {
238e0f8f574SDaniel Malea     if (FileSpec::Equal(source, destination, true))
23997206d57SZachary Turner       return Status();
240e0f8f574SDaniel Malea     // cp src dst
241e0f8f574SDaniel Malea     // chown uid:gid dst
242e0f8f574SDaniel Malea     std::string src_path(source.GetPath());
243e0f8f574SDaniel Malea     if (src_path.empty())
24497206d57SZachary Turner       return Status("unable to get file path for source");
245e0f8f574SDaniel Malea     std::string dst_path(destination.GetPath());
246e0f8f574SDaniel Malea     if (dst_path.empty())
24797206d57SZachary Turner       return Status("unable to get file path for destination");
248e0f8f574SDaniel Malea     StreamString command;
249e0f8f574SDaniel Malea     command.Printf("cp %s %s", src_path.c_str(), dst_path.c_str());
250e0f8f574SDaniel Malea     int status;
251*248a1305SKonrad Kleine     RunShellCommand(command.GetData(), nullptr, &status, nullptr, nullptr,
25219dd1a0eSPavel Labath                     std::chrono::seconds(10));
253e0f8f574SDaniel Malea     if (status != 0)
25497206d57SZachary Turner       return Status("unable to perform copy");
255e0f8f574SDaniel Malea     if (uid == UINT32_MAX && gid == UINT32_MAX)
25697206d57SZachary Turner       return Status();
257e0f8f574SDaniel Malea     if (chown_file(this, dst_path.c_str(), uid, gid) != 0)
25897206d57SZachary Turner       return Status("unable to perform chown");
25997206d57SZachary Turner     return Status();
260b9c1b51eSKate Stone   } else if (m_remote_platform_sp) {
261b9c1b51eSKate Stone     if (GetSupportsRSync()) {
262e0f8f574SDaniel Malea       std::string src_path(source.GetPath());
263e0f8f574SDaniel Malea       if (src_path.empty())
26497206d57SZachary Turner         return Status("unable to get file path for source");
265e0f8f574SDaniel Malea       std::string dst_path(destination.GetPath());
266e0f8f574SDaniel Malea       if (dst_path.empty())
26797206d57SZachary Turner         return Status("unable to get file path for destination");
268e0f8f574SDaniel Malea       StreamString command;
269b9c1b51eSKate Stone       if (GetIgnoresRemoteHostname()) {
270e0f8f574SDaniel Malea         if (!GetRSyncPrefix())
271b9c1b51eSKate Stone           command.Printf("rsync %s %s %s", GetRSyncOpts(), src_path.c_str(),
272e0f8f574SDaniel Malea                          dst_path.c_str());
273e0f8f574SDaniel Malea         else
274b9c1b51eSKate Stone           command.Printf("rsync %s %s %s%s", GetRSyncOpts(), src_path.c_str(),
275b9c1b51eSKate Stone                          GetRSyncPrefix(), dst_path.c_str());
276b9c1b51eSKate Stone       } else
277b9c1b51eSKate Stone         command.Printf("rsync %s %s %s:%s", GetRSyncOpts(), src_path.c_str(),
278b9c1b51eSKate Stone                        GetHostname(), dst_path.c_str());
279e0f8f574SDaniel Malea       if (log)
280e0f8f574SDaniel Malea         log->Printf("[PutFile] Running command: %s\n", command.GetData());
281e0f8f574SDaniel Malea       int retcode;
282*248a1305SKonrad Kleine       Host::RunShellCommand(command.GetData(), nullptr, &retcode, nullptr,
283*248a1305SKonrad Kleine                             nullptr, std::chrono::minutes(1));
284b9c1b51eSKate Stone       if (retcode == 0) {
285e0f8f574SDaniel Malea         // Don't chown a local file for a remote system
286e0f8f574SDaniel Malea         //                if (chown_file(this,dst_path.c_str(),uid,gid) != 0)
28797206d57SZachary Turner         //                    return Status("unable to perform chown");
28897206d57SZachary Turner         return Status();
289e0f8f574SDaniel Malea       }
290b9c1b51eSKate Stone       // if we are still here rsync has failed - let's try the slow way before
291b9c1b51eSKate Stone       // giving up
292e0f8f574SDaniel Malea     }
293e0f8f574SDaniel Malea   }
294e0f8f574SDaniel Malea   return Platform::PutFile(source, destination, uid, gid);
295e0f8f574SDaniel Malea }
296e0f8f574SDaniel Malea 
29797206d57SZachary Turner lldb_private::Status PlatformPOSIX::GetFile(
298b9c1b51eSKate Stone     const lldb_private::FileSpec &source,      // remote file path
299d3173f34SChaoren Lin     const lldb_private::FileSpec &destination) // local file path
300e0f8f574SDaniel Malea {
301fbb76349SGreg Clayton   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
302fbb76349SGreg Clayton 
303e0f8f574SDaniel Malea   // Check the args, first.
304e0f8f574SDaniel Malea   std::string src_path(source.GetPath());
305e0f8f574SDaniel Malea   if (src_path.empty())
30697206d57SZachary Turner     return Status("unable to get file path for source");
307e0f8f574SDaniel Malea   std::string dst_path(destination.GetPath());
308e0f8f574SDaniel Malea   if (dst_path.empty())
30997206d57SZachary Turner     return Status("unable to get file path for destination");
310b9c1b51eSKate Stone   if (IsHost()) {
311e0f8f574SDaniel Malea     if (FileSpec::Equal(source, destination, true))
31297206d57SZachary Turner       return Status("local scenario->source and destination are the same file "
313b9c1b51eSKate Stone                     "path: no operation performed");
314e0f8f574SDaniel Malea     // cp src dst
315e0f8f574SDaniel Malea     StreamString cp_command;
316e0f8f574SDaniel Malea     cp_command.Printf("cp %s %s", src_path.c_str(), dst_path.c_str());
317e0f8f574SDaniel Malea     int status;
318*248a1305SKonrad Kleine     RunShellCommand(cp_command.GetData(), nullptr, &status, nullptr, nullptr,
31919dd1a0eSPavel Labath                     std::chrono::seconds(10));
320e0f8f574SDaniel Malea     if (status != 0)
32197206d57SZachary Turner       return Status("unable to perform copy");
32297206d57SZachary Turner     return Status();
323b9c1b51eSKate Stone   } else if (m_remote_platform_sp) {
324b9c1b51eSKate Stone     if (GetSupportsRSync()) {
325e0f8f574SDaniel Malea       StreamString command;
326b9c1b51eSKate Stone       if (GetIgnoresRemoteHostname()) {
327e0f8f574SDaniel Malea         if (!GetRSyncPrefix())
328b9c1b51eSKate Stone           command.Printf("rsync %s %s %s", GetRSyncOpts(), src_path.c_str(),
329e0f8f574SDaniel Malea                          dst_path.c_str());
330e0f8f574SDaniel Malea         else
331b9c1b51eSKate Stone           command.Printf("rsync %s %s%s %s", GetRSyncOpts(), GetRSyncPrefix(),
332b9c1b51eSKate Stone                          src_path.c_str(), dst_path.c_str());
333b9c1b51eSKate Stone       } else
334b9c1b51eSKate Stone         command.Printf("rsync %s %s:%s %s", GetRSyncOpts(),
335b9c1b51eSKate Stone                        m_remote_platform_sp->GetHostname(), src_path.c_str(),
336e0f8f574SDaniel Malea                        dst_path.c_str());
337e0f8f574SDaniel Malea       if (log)
338e0f8f574SDaniel Malea         log->Printf("[GetFile] Running command: %s\n", command.GetData());
339e0f8f574SDaniel Malea       int retcode;
340*248a1305SKonrad Kleine       Host::RunShellCommand(command.GetData(), nullptr, &retcode, nullptr,
341*248a1305SKonrad Kleine                             nullptr, std::chrono::minutes(1));
342e0f8f574SDaniel Malea       if (retcode == 0)
34397206d57SZachary Turner         return Status();
34405097246SAdrian Prantl       // If we are here, rsync has failed - let's try the slow way before
34505097246SAdrian Prantl       // giving up
346e0f8f574SDaniel Malea     }
347e0f8f574SDaniel Malea     // open src and dst
348e0f8f574SDaniel Malea     // read/write, read/write, read/write, ...
349e0f8f574SDaniel Malea     // close src
350e0f8f574SDaniel Malea     // close dst
351e0f8f574SDaniel Malea     if (log)
352e0f8f574SDaniel Malea       log->Printf("[GetFile] Using block by block transfer....\n");
35397206d57SZachary Turner     Status error;
354b9c1b51eSKate Stone     user_id_t fd_src = OpenFile(source, File::eOpenOptionRead,
355b9c1b51eSKate Stone                                 lldb::eFilePermissionsFileDefault, error);
356e0f8f574SDaniel Malea 
357e0f8f574SDaniel Malea     if (fd_src == UINT64_MAX)
35897206d57SZachary Turner       return Status("unable to open source file");
359e0f8f574SDaniel Malea 
360fbb76349SGreg Clayton     uint32_t permissions = 0;
361d3173f34SChaoren Lin     error = GetFilePermissions(source, permissions);
362e0f8f574SDaniel Malea 
363e0f8f574SDaniel Malea     if (permissions == 0)
364fbb76349SGreg Clayton       permissions = lldb::eFilePermissionsFileDefault;
365e0f8f574SDaniel Malea 
366c00cf4a0SZachary Turner     user_id_t fd_dst = FileCache::GetInstance().OpenFile(
367b9c1b51eSKate Stone         destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
368b9c1b51eSKate Stone                          File::eOpenOptionTruncate,
369b9c1b51eSKate Stone         permissions, error);
370e0f8f574SDaniel Malea 
371b9c1b51eSKate Stone     if (fd_dst == UINT64_MAX) {
372e0f8f574SDaniel Malea       if (error.Success())
373e0f8f574SDaniel Malea         error.SetErrorString("unable to open destination file");
374e0f8f574SDaniel Malea     }
375e0f8f574SDaniel Malea 
376b9c1b51eSKate Stone     if (error.Success()) {
377e0f8f574SDaniel Malea       lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
378e0f8f574SDaniel Malea       uint64_t offset = 0;
379e0f8f574SDaniel Malea       error.Clear();
380b9c1b51eSKate Stone       while (error.Success()) {
381b9c1b51eSKate Stone         const uint64_t n_read = ReadFile(fd_src, offset, buffer_sp->GetBytes(),
382b9c1b51eSKate Stone                                          buffer_sp->GetByteSize(), error);
383e0f8f574SDaniel Malea         if (error.Fail())
384e0f8f574SDaniel Malea           break;
385e0f8f574SDaniel Malea         if (n_read == 0)
386e0f8f574SDaniel Malea           break;
387b9c1b51eSKate Stone         if (FileCache::GetInstance().WriteFile(fd_dst, offset,
388b9c1b51eSKate Stone                                                buffer_sp->GetBytes(), n_read,
389b9c1b51eSKate Stone                                                error) != n_read) {
390e0f8f574SDaniel Malea           if (!error.Fail())
391e0f8f574SDaniel Malea             error.SetErrorString("unable to write to destination file");
392e0f8f574SDaniel Malea           break;
393e0f8f574SDaniel Malea         }
394e0f8f574SDaniel Malea         offset += n_read;
395e0f8f574SDaniel Malea       }
396e0f8f574SDaniel Malea     }
397e0f8f574SDaniel Malea     // Ignore the close error of src.
398e0f8f574SDaniel Malea     if (fd_src != UINT64_MAX)
399e0f8f574SDaniel Malea       CloseFile(fd_src, error);
400e0f8f574SDaniel Malea     // And close the dst file descriptot.
401b9c1b51eSKate Stone     if (fd_dst != UINT64_MAX &&
402b9c1b51eSKate Stone         !FileCache::GetInstance().CloseFile(fd_dst, error)) {
403e0f8f574SDaniel Malea       if (!error.Fail())
404e0f8f574SDaniel Malea         error.SetErrorString("unable to close destination file");
405e0f8f574SDaniel Malea     }
406e0f8f574SDaniel Malea     return error;
407e0f8f574SDaniel Malea   }
408e0f8f574SDaniel Malea   return Platform::GetFile(source, destination);
409e0f8f574SDaniel Malea }
410e0f8f574SDaniel Malea 
411b9c1b51eSKate Stone std::string PlatformPOSIX::GetPlatformSpecificConnectionInformation() {
412e0f8f574SDaniel Malea   StreamString stream;
413b9c1b51eSKate Stone   if (GetSupportsRSync()) {
414e0f8f574SDaniel Malea     stream.PutCString("rsync");
415e0f8f574SDaniel Malea     if ((GetRSyncOpts() && *GetRSyncOpts()) ||
416b9c1b51eSKate Stone         (GetRSyncPrefix() && *GetRSyncPrefix()) || GetIgnoresRemoteHostname()) {
417e0f8f574SDaniel Malea       stream.Printf(", options: ");
418e0f8f574SDaniel Malea       if (GetRSyncOpts() && *GetRSyncOpts())
419e0f8f574SDaniel Malea         stream.Printf("'%s' ", GetRSyncOpts());
420e0f8f574SDaniel Malea       stream.Printf(", prefix: ");
421e0f8f574SDaniel Malea       if (GetRSyncPrefix() && *GetRSyncPrefix())
422e0f8f574SDaniel Malea         stream.Printf("'%s' ", GetRSyncPrefix());
423e0f8f574SDaniel Malea       if (GetIgnoresRemoteHostname())
424e0f8f574SDaniel Malea         stream.Printf("ignore remote-hostname ");
425e0f8f574SDaniel Malea     }
426e0f8f574SDaniel Malea   }
427b9c1b51eSKate Stone   if (GetSupportsSSH()) {
428e0f8f574SDaniel Malea     stream.PutCString("ssh");
429e0f8f574SDaniel Malea     if (GetSSHOpts() && *GetSSHOpts())
430e0f8f574SDaniel Malea       stream.Printf(", options: '%s' ", GetSSHOpts());
431e0f8f574SDaniel Malea   }
432e0f8f574SDaniel Malea   if (GetLocalCacheDirectory() && *GetLocalCacheDirectory())
433e0f8f574SDaniel Malea     stream.Printf("cache dir: %s", GetLocalCacheDirectory());
434e0f8f574SDaniel Malea   if (stream.GetSize())
435c156427dSZachary Turner     return stream.GetString();
436e0f8f574SDaniel Malea   else
437e0f8f574SDaniel Malea     return "";
438e0f8f574SDaniel Malea }
439e0f8f574SDaniel Malea 
440b9c1b51eSKate Stone const lldb::UnixSignalsSP &PlatformPOSIX::GetRemoteUnixSignals() {
44198d0a4b3SChaoren Lin   if (IsRemote() && m_remote_platform_sp)
44298d0a4b3SChaoren Lin     return m_remote_platform_sp->GetRemoteUnixSignals();
44398d0a4b3SChaoren Lin   return Platform::GetRemoteUnixSignals();
44498d0a4b3SChaoren Lin }
44598d0a4b3SChaoren Lin 
44697206d57SZachary Turner Status PlatformPOSIX::ConnectRemote(Args &args) {
44797206d57SZachary Turner   Status error;
448b9c1b51eSKate Stone   if (IsHost()) {
449b9c1b51eSKate Stone     error.SetErrorStringWithFormat(
450b9c1b51eSKate Stone         "can't connect to the host platform '%s', always connected",
451b9c1b51eSKate Stone         GetPluginName().GetCString());
452b9c1b51eSKate Stone   } else {
453015d818bSTodd Fiala     if (!m_remote_platform_sp)
454b9c1b51eSKate Stone       m_remote_platform_sp =
455b9c1b51eSKate Stone           Platform::Create(ConstString("remote-gdb-server"), error);
456015d818bSTodd Fiala 
457015d818bSTodd Fiala     if (m_remote_platform_sp && error.Success())
458015d818bSTodd Fiala       error = m_remote_platform_sp->ConnectRemote(args);
459015d818bSTodd Fiala     else
460015d818bSTodd Fiala       error.SetErrorString("failed to create a 'remote-gdb-server' platform");
461015d818bSTodd Fiala 
462015d818bSTodd Fiala     if (error.Fail())
463015d818bSTodd Fiala       m_remote_platform_sp.reset();
464015d818bSTodd Fiala   }
465015d818bSTodd Fiala 
466b9c1b51eSKate Stone   if (error.Success() && m_remote_platform_sp) {
467b9c1b51eSKate Stone     if (m_option_group_platform_rsync.get() &&
468b9c1b51eSKate Stone         m_option_group_platform_ssh.get() &&
469b9c1b51eSKate Stone         m_option_group_platform_caching.get()) {
470b9c1b51eSKate Stone       if (m_option_group_platform_rsync->m_rsync) {
471015d818bSTodd Fiala         SetSupportsRSync(true);
4720a66e2f1SEnrico Granata         SetRSyncOpts(m_option_group_platform_rsync->m_rsync_opts.c_str());
4730a66e2f1SEnrico Granata         SetRSyncPrefix(m_option_group_platform_rsync->m_rsync_prefix.c_str());
474b9c1b51eSKate Stone         SetIgnoresRemoteHostname(
475b9c1b51eSKate Stone             m_option_group_platform_rsync->m_ignores_remote_hostname);
476015d818bSTodd Fiala       }
477b9c1b51eSKate Stone       if (m_option_group_platform_ssh->m_ssh) {
478015d818bSTodd Fiala         SetSupportsSSH(true);
4790a66e2f1SEnrico Granata         SetSSHOpts(m_option_group_platform_ssh->m_ssh_opts.c_str());
480015d818bSTodd Fiala       }
481b9c1b51eSKate Stone       SetLocalCacheDirectory(
482b9c1b51eSKate Stone           m_option_group_platform_caching->m_cache_dir.c_str());
483015d818bSTodd Fiala     }
484015d818bSTodd Fiala   }
485015d818bSTodd Fiala 
486015d818bSTodd Fiala   return error;
487015d818bSTodd Fiala }
488015d818bSTodd Fiala 
48997206d57SZachary Turner Status PlatformPOSIX::DisconnectRemote() {
49097206d57SZachary Turner   Status error;
491015d818bSTodd Fiala 
492b9c1b51eSKate Stone   if (IsHost()) {
493b9c1b51eSKate Stone     error.SetErrorStringWithFormat(
494b9c1b51eSKate Stone         "can't disconnect from the host platform '%s', always connected",
495b9c1b51eSKate Stone         GetPluginName().GetCString());
496b9c1b51eSKate Stone   } else {
497015d818bSTodd Fiala     if (m_remote_platform_sp)
498015d818bSTodd Fiala       error = m_remote_platform_sp->DisconnectRemote();
499015d818bSTodd Fiala     else
500015d818bSTodd Fiala       error.SetErrorString("the platform is not currently connected");
501015d818bSTodd Fiala   }
502015d818bSTodd Fiala   return error;
503015d818bSTodd Fiala }
504015d818bSTodd Fiala 
505b9c1b51eSKate Stone lldb::ProcessSP PlatformPOSIX::Attach(ProcessAttachInfo &attach_info,
506b9c1b51eSKate Stone                                       Debugger &debugger, Target *target,
50797206d57SZachary Turner                                       Status &error) {
508348fb385STodd Fiala   lldb::ProcessSP process_sp;
509348fb385STodd Fiala   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
510348fb385STodd Fiala 
511b9c1b51eSKate Stone   if (IsHost()) {
512*248a1305SKonrad Kleine     if (target == nullptr) {
513348fb385STodd Fiala       TargetSP new_target_sp;
514348fb385STodd Fiala 
515f9a07e9fSJonas Devlieghere       error = debugger.GetTargetList().CreateTarget(
516*248a1305SKonrad Kleine           debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp);
517348fb385STodd Fiala       target = new_target_sp.get();
518348fb385STodd Fiala       if (log)
519348fb385STodd Fiala         log->Printf("PlatformPOSIX::%s created new target", __FUNCTION__);
520b9c1b51eSKate Stone     } else {
521348fb385STodd Fiala       error.Clear();
522348fb385STodd Fiala       if (log)
523b9c1b51eSKate Stone         log->Printf("PlatformPOSIX::%s target already existed, setting target",
524b9c1b51eSKate Stone                     __FUNCTION__);
525348fb385STodd Fiala     }
526348fb385STodd Fiala 
527b9c1b51eSKate Stone     if (target && error.Success()) {
528348fb385STodd Fiala       debugger.GetTargetList().SetSelectedTarget(target);
529b9c1b51eSKate Stone       if (log) {
530348fb385STodd Fiala         ModuleSP exe_module_sp = target->GetExecutableModule();
531b9c1b51eSKate Stone         log->Printf("PlatformPOSIX::%s set selected target to %p %s",
532b9c1b51eSKate Stone                     __FUNCTION__, (void *)target,
533b9c1b51eSKate Stone                     exe_module_sp
534b9c1b51eSKate Stone                         ? exe_module_sp->GetFileSpec().GetPath().c_str()
535b9c1b51eSKate Stone                         : "<null>");
536348fb385STodd Fiala       }
537348fb385STodd Fiala 
538b9c1b51eSKate Stone       process_sp =
539b9c1b51eSKate Stone           target->CreateProcess(attach_info.GetListenerForProcess(debugger),
540*248a1305SKonrad Kleine                                 attach_info.GetProcessPluginName(), nullptr);
541348fb385STodd Fiala 
542b9c1b51eSKate Stone       if (process_sp) {
543583bbb1dSJim Ingham         ListenerSP listener_sp = attach_info.GetHijackListener();
544b9c1b51eSKate Stone         if (listener_sp == nullptr) {
545b9c1b51eSKate Stone           listener_sp =
546b9c1b51eSKate Stone               Listener::MakeListener("lldb.PlatformPOSIX.attach.hijack");
547348fb385STodd Fiala           attach_info.SetHijackListener(listener_sp);
548926af0cdSOleksiy Vyalov         }
549583bbb1dSJim Ingham         process_sp->HijackProcessEvents(listener_sp);
550348fb385STodd Fiala         error = process_sp->Attach(attach_info);
551348fb385STodd Fiala       }
552348fb385STodd Fiala     }
553b9c1b51eSKate Stone   } else {
554348fb385STodd Fiala     if (m_remote_platform_sp)
555b9c1b51eSKate Stone       process_sp =
556b9c1b51eSKate Stone           m_remote_platform_sp->Attach(attach_info, debugger, target, error);
557348fb385STodd Fiala     else
558348fb385STodd Fiala       error.SetErrorString("the platform is not currently connected");
559348fb385STodd Fiala   }
560348fb385STodd Fiala   return process_sp;
561348fb385STodd Fiala }
562348fb385STodd Fiala 
563015d818bSTodd Fiala lldb::ProcessSP
564b9c1b51eSKate Stone PlatformPOSIX::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
565b9c1b51eSKate Stone                             Target *target, // Can be NULL, if NULL create a new
566b9c1b51eSKate Stone                                             // target, else use existing one
56797206d57SZachary Turner                             Status &error) {
568015d818bSTodd Fiala   ProcessSP process_sp;
569015d818bSTodd Fiala 
570b9c1b51eSKate Stone   if (IsHost()) {
571b9c1b51eSKate Stone     // We are going to hand this process off to debugserver which will be in
572245dd2e0SPavel Labath     // charge of setting the exit status.  However, we still need to reap it
573245dd2e0SPavel Labath     // from lldb. So, make sure we use a exit callback which does not set exit
574245dd2e0SPavel Labath     // status.
575245dd2e0SPavel Labath     const bool monitor_signals = false;
576245dd2e0SPavel Labath     launch_info.SetMonitorProcessCallback(
577245dd2e0SPavel Labath         &ProcessLaunchInfo::NoOpMonitorCallback, monitor_signals);
5788012cadbSGreg Clayton     process_sp = Platform::DebugProcess(launch_info, debugger, target, error);
579b9c1b51eSKate Stone   } else {
580015d818bSTodd Fiala     if (m_remote_platform_sp)
581b9c1b51eSKate Stone       process_sp = m_remote_platform_sp->DebugProcess(launch_info, debugger,
582b9c1b51eSKate Stone                                                       target, error);
583015d818bSTodd Fiala     else
584015d818bSTodd Fiala       error.SetErrorString("the platform is not currently connected");
585015d818bSTodd Fiala   }
586015d818bSTodd Fiala   return process_sp;
587015d818bSTodd Fiala }
588015d818bSTodd Fiala 
589b9c1b51eSKate Stone void PlatformPOSIX::CalculateTrapHandlerSymbolNames() {
5902094dbf4SJason Molenda   m_trap_handlers.push_back(ConstString("_sigtramp"));
5912094dbf4SJason Molenda }
5923cb132a0STamas Berghammer 
59397206d57SZachary Turner Status PlatformPOSIX::EvaluateLibdlExpression(
594b9c1b51eSKate Stone     lldb_private::Process *process, const char *expr_cstr,
595d37c9469SPavel Labath     llvm::StringRef expr_prefix, lldb::ValueObjectSP &result_valobj_sp) {
5963cb132a0STamas Berghammer   DynamicLoader *loader = process->GetDynamicLoader();
597b9c1b51eSKate Stone   if (loader) {
59897206d57SZachary Turner     Status error = loader->CanLoadImage();
5993cb132a0STamas Berghammer     if (error.Fail())
6003cb132a0STamas Berghammer       return error;
6013cb132a0STamas Berghammer   }
6023cb132a0STamas Berghammer 
6038d94ba0fSJim Ingham   ThreadSP thread_sp(process->GetThreadList().GetExpressionExecutionThread());
6043cb132a0STamas Berghammer   if (!thread_sp)
60597206d57SZachary Turner     return Status("Selected thread isn't valid");
6063cb132a0STamas Berghammer 
6073cb132a0STamas Berghammer   StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
6083cb132a0STamas Berghammer   if (!frame_sp)
60997206d57SZachary Turner     return Status("Frame 0 isn't valid");
6103cb132a0STamas Berghammer 
6113cb132a0STamas Berghammer   ExecutionContext exe_ctx;
6123cb132a0STamas Berghammer   frame_sp->CalculateExecutionContext(exe_ctx);
6133cb132a0STamas Berghammer   EvaluateExpressionOptions expr_options;
6143cb132a0STamas Berghammer   expr_options.SetUnwindOnError(true);
6153cb132a0STamas Berghammer   expr_options.SetIgnoreBreakpoints(true);
6163cb132a0STamas Berghammer   expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
6173cb132a0STamas Berghammer   expr_options.SetLanguage(eLanguageTypeC_plus_plus);
618b9c1b51eSKate Stone   expr_options.SetTrapExceptions(false); // dlopen can't throw exceptions, so
619b9c1b51eSKate Stone                                          // don't do the work to trap them.
6204c03ea14SAdrian Prantl   expr_options.SetTimeout(process->GetUtilityExpressionTimeout());
6213cb132a0STamas Berghammer 
62297206d57SZachary Turner   Status expr_error;
62302980825SJim Ingham   ExpressionResults result =
624b9c1b51eSKate Stone       UserExpression::Evaluate(exe_ctx, expr_options, expr_cstr, expr_prefix,
625b9c1b51eSKate Stone                                result_valobj_sp, expr_error);
62602980825SJim Ingham   if (result != eExpressionCompleted)
62702980825SJim Ingham     return expr_error;
62802980825SJim Ingham 
6293cb132a0STamas Berghammer   if (result_valobj_sp->GetError().Fail())
6303cb132a0STamas Berghammer     return result_valobj_sp->GetError();
63197206d57SZachary Turner   return Status();
6323cb132a0STamas Berghammer }
6333cb132a0STamas Berghammer 
634d10d3795SFrederic Riss std::unique_ptr<UtilityFunction>
6351ecb34f4SJim Ingham PlatformPOSIX::MakeLoadImageUtilityFunction(ExecutionContext &exe_ctx,
636d10d3795SFrederic Riss                                             Status &error) {
63705097246SAdrian Prantl   // Remember to prepend this with the prefix from
63805097246SAdrian Prantl   // GetLibdlFunctionDeclarations. The returned values are all in
63905097246SAdrian Prantl   // __lldb_dlopen_result for consistency. The wrapper returns a void * but
64005097246SAdrian Prantl   // doesn't use it because UtilityFunctions don't work with void returns at
64105097246SAdrian Prantl   // present.
6421ecb34f4SJim Ingham   static const char *dlopen_wrapper_code = R"(
6431ecb34f4SJim Ingham   struct __lldb_dlopen_result {
6441ecb34f4SJim Ingham     void *image_ptr;
6451ecb34f4SJim Ingham     const char *error_str;
6461ecb34f4SJim Ingham   };
6471ecb34f4SJim Ingham 
64803856dd9SJim Ingham   extern void *memcpy(void *, const void *, size_t size);
6490d231f71SJim Ingham   extern size_t strlen(const char *);
6500d231f71SJim Ingham 
6510d231f71SJim Ingham 
6520d231f71SJim Ingham   void * __lldb_dlopen_wrapper (const char *name,
6530d231f71SJim Ingham                                 const char *path_strings,
6540d231f71SJim Ingham                                 char *buffer,
6551ecb34f4SJim Ingham                                 __lldb_dlopen_result *result_ptr)
6561ecb34f4SJim Ingham   {
6570d231f71SJim Ingham     // This is the case where the name is the full path:
65803856dd9SJim Ingham     if (!path_strings) {
6590d231f71SJim Ingham       result_ptr->image_ptr = dlopen(name, 2);
66003856dd9SJim Ingham       if (result_ptr->image_ptr)
6610d231f71SJim Ingham         result_ptr->error_str = nullptr;
6620d231f71SJim Ingham       return nullptr;
6630d231f71SJim Ingham     }
6640d231f71SJim Ingham 
6650d231f71SJim Ingham     // This is the case where we have a list of paths:
6660d231f71SJim Ingham     size_t name_len = strlen(name);
66703856dd9SJim Ingham     while (path_strings && path_strings[0] != '\0') {
6680d231f71SJim Ingham       size_t path_len = strlen(path_strings);
6690d231f71SJim Ingham       memcpy((void *) buffer, (void *) path_strings, path_len);
6700d231f71SJim Ingham       buffer[path_len] = '/';
6710d231f71SJim Ingham       char *target_ptr = buffer+path_len+1;
6720d231f71SJim Ingham       memcpy((void *) target_ptr, (void *) name, name_len + 1);
6730d231f71SJim Ingham       result_ptr->image_ptr = dlopen(buffer, 2);
67403856dd9SJim Ingham       if (result_ptr->image_ptr) {
6750d231f71SJim Ingham         result_ptr->error_str = nullptr;
6760d231f71SJim Ingham         break;
6770d231f71SJim Ingham       }
6781ecb34f4SJim Ingham       result_ptr->error_str = dlerror();
6790d231f71SJim Ingham       path_strings = path_strings + path_len + 1;
6800d231f71SJim Ingham     }
6811ecb34f4SJim Ingham     return nullptr;
6821ecb34f4SJim Ingham   }
6831ecb34f4SJim Ingham   )";
6841ecb34f4SJim Ingham 
6851ecb34f4SJim Ingham   static const char *dlopen_wrapper_name = "__lldb_dlopen_wrapper";
6861ecb34f4SJim Ingham   Process *process = exe_ctx.GetProcessSP().get();
6871ecb34f4SJim Ingham   // Insert the dlopen shim defines into our generic expression:
6881ecb34f4SJim Ingham   std::string expr(GetLibdlFunctionDeclarations(process));
6891ecb34f4SJim Ingham   expr.append(dlopen_wrapper_code);
6901ecb34f4SJim Ingham   Status utility_error;
6911ecb34f4SJim Ingham   DiagnosticManager diagnostics;
6921ecb34f4SJim Ingham 
6931ecb34f4SJim Ingham   std::unique_ptr<UtilityFunction> dlopen_utility_func_up(process
6941ecb34f4SJim Ingham       ->GetTarget().GetUtilityFunctionForLanguage(expr.c_str(),
6951ecb34f4SJim Ingham                                                   eLanguageTypeObjC,
6961ecb34f4SJim Ingham                                                   dlopen_wrapper_name,
6971ecb34f4SJim Ingham                                                   utility_error));
6981ecb34f4SJim Ingham   if (utility_error.Fail()) {
6991ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not make utility"
7001ecb34f4SJim Ingham                                    "function: %s", utility_error.AsCString());
7011ecb34f4SJim Ingham     return nullptr;
7021ecb34f4SJim Ingham   }
7031ecb34f4SJim Ingham   if (!dlopen_utility_func_up->Install(diagnostics, exe_ctx)) {
7041ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not install utility"
7051ecb34f4SJim Ingham                                    "function: %s",
7061ecb34f4SJim Ingham                                    diagnostics.GetString().c_str());
7071ecb34f4SJim Ingham     return nullptr;
7081ecb34f4SJim Ingham   }
7091ecb34f4SJim Ingham 
7101ecb34f4SJim Ingham   Value value;
7111ecb34f4SJim Ingham   ValueList arguments;
7121ecb34f4SJim Ingham   FunctionCaller *do_dlopen_function = nullptr;
7131ecb34f4SJim Ingham 
7141ecb34f4SJim Ingham   // Fetch the clang types we will need:
7151ecb34f4SJim Ingham   ClangASTContext *ast = process->GetTarget().GetScratchClangASTContext();
7161ecb34f4SJim Ingham 
7171ecb34f4SJim Ingham   CompilerType clang_void_pointer_type
7181ecb34f4SJim Ingham       = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
7191ecb34f4SJim Ingham   CompilerType clang_char_pointer_type
7201ecb34f4SJim Ingham         = ast->GetBasicType(eBasicTypeChar).GetPointerType();
7211ecb34f4SJim Ingham 
7220d231f71SJim Ingham   // We are passing four arguments, the basename, the list of places to look,
7230d231f71SJim Ingham   // a buffer big enough for all the path + name combos, and
7240d231f71SJim Ingham   // a pointer to the storage we've made for the result:
7251ecb34f4SJim Ingham   value.SetValueType(Value::eValueTypeScalar);
7261ecb34f4SJim Ingham   value.SetCompilerType(clang_void_pointer_type);
7271ecb34f4SJim Ingham   arguments.PushValue(value);
7281ecb34f4SJim Ingham   value.SetCompilerType(clang_char_pointer_type);
7291ecb34f4SJim Ingham   arguments.PushValue(value);
7300d231f71SJim Ingham   arguments.PushValue(value);
7310d231f71SJim Ingham   arguments.PushValue(value);
7321ecb34f4SJim Ingham 
7331ecb34f4SJim Ingham   do_dlopen_function = dlopen_utility_func_up->MakeFunctionCaller(
7341ecb34f4SJim Ingham       clang_void_pointer_type, arguments, exe_ctx.GetThreadSP(), utility_error);
7351ecb34f4SJim Ingham   if (utility_error.Fail()) {
7361ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not make function"
7371ecb34f4SJim Ingham                                    "caller: %s", utility_error.AsCString());
7381ecb34f4SJim Ingham     return nullptr;
7391ecb34f4SJim Ingham   }
7401ecb34f4SJim Ingham 
7411ecb34f4SJim Ingham   do_dlopen_function = dlopen_utility_func_up->GetFunctionCaller();
7421ecb34f4SJim Ingham   if (!do_dlopen_function) {
7431ecb34f4SJim Ingham     error.SetErrorString("dlopen error: could not get function caller.");
7441ecb34f4SJim Ingham     return nullptr;
7451ecb34f4SJim Ingham   }
7461ecb34f4SJim Ingham 
7471ecb34f4SJim Ingham   // We made a good utility function, so cache it in the process:
748d10d3795SFrederic Riss   return dlopen_utility_func_up;
7491ecb34f4SJim Ingham }
7501ecb34f4SJim Ingham 
751b9c1b51eSKate Stone uint32_t PlatformPOSIX::DoLoadImage(lldb_private::Process *process,
7524fbd67acSTamas Berghammer                                     const lldb_private::FileSpec &remote_file,
7530d231f71SJim Ingham                                     const std::vector<std::string> *paths,
7540d231f71SJim Ingham                                     lldb_private::Status &error,
7550d231f71SJim Ingham                                     lldb_private::FileSpec *loaded_image) {
7560d231f71SJim Ingham   if (loaded_image)
7570d231f71SJim Ingham     loaded_image->Clear();
7580d231f71SJim Ingham 
7591ecb34f4SJim Ingham   std::string path;
7601ecb34f4SJim Ingham   path = remote_file.GetPath();
7613cb132a0STamas Berghammer 
7621ecb34f4SJim Ingham   ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
7631ecb34f4SJim Ingham   if (!thread_sp) {
7641ecb34f4SJim Ingham     error.SetErrorString("dlopen error: no thread available to call dlopen.");
7653cb132a0STamas Berghammer     return LLDB_INVALID_IMAGE_TOKEN;
7663cb132a0STamas Berghammer   }
7673cb132a0STamas Berghammer 
7681ecb34f4SJim Ingham   DiagnosticManager diagnostics;
7693cb132a0STamas Berghammer 
7701ecb34f4SJim Ingham   ExecutionContext exe_ctx;
7711ecb34f4SJim Ingham   thread_sp->CalculateExecutionContext(exe_ctx);
7721ecb34f4SJim Ingham 
7731ecb34f4SJim Ingham   Status utility_error;
774d10d3795SFrederic Riss   UtilityFunction *dlopen_utility_func;
7751ecb34f4SJim Ingham   ValueList arguments;
7761ecb34f4SJim Ingham   FunctionCaller *do_dlopen_function = nullptr;
7771ecb34f4SJim Ingham 
778d10d3795SFrederic Riss   // The UtilityFunction is held in the Process.  Platforms don't track the
779d10d3795SFrederic Riss   // lifespan of the Targets that use them, we can't put this in the Platform.
780d10d3795SFrederic Riss   dlopen_utility_func = process->GetLoadImageUtilityFunction(
781d10d3795SFrederic Riss       this, [&]() -> std::unique_ptr<UtilityFunction> {
782d10d3795SFrederic Riss         return MakeLoadImageUtilityFunction(exe_ctx, error);
783d10d3795SFrederic Riss       });
7841ecb34f4SJim Ingham   // If we couldn't make it, the error will be in error, so we can exit here.
7851ecb34f4SJim Ingham   if (!dlopen_utility_func)
7861ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
7871ecb34f4SJim Ingham 
7881ecb34f4SJim Ingham   do_dlopen_function = dlopen_utility_func->GetFunctionCaller();
7891ecb34f4SJim Ingham   if (!do_dlopen_function) {
7901ecb34f4SJim Ingham     error.SetErrorString("dlopen error: could not get function caller.");
7911ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
7921ecb34f4SJim Ingham   }
7931ecb34f4SJim Ingham   arguments = do_dlopen_function->GetArgumentValues();
7941ecb34f4SJim Ingham 
79505097246SAdrian Prantl   // Now insert the path we are searching for and the result structure into the
79605097246SAdrian Prantl   // target.
7971ecb34f4SJim Ingham   uint32_t permissions = ePermissionsReadable|ePermissionsWritable;
7981ecb34f4SJim Ingham   size_t path_len = path.size() + 1;
7991ecb34f4SJim Ingham   lldb::addr_t path_addr = process->AllocateMemory(path_len,
8001ecb34f4SJim Ingham                                                    permissions,
8011ecb34f4SJim Ingham                                                    utility_error);
8021ecb34f4SJim Ingham   if (path_addr == LLDB_INVALID_ADDRESS) {
8031ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
8041ecb34f4SJim Ingham                                     "for path: %s", utility_error.AsCString());
8051ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
8061ecb34f4SJim Ingham   }
8071ecb34f4SJim Ingham 
8081ecb34f4SJim Ingham   // Make sure we deallocate the input string memory:
8091ecb34f4SJim Ingham   CleanUp path_cleanup([process, path_addr] {
8101ecb34f4SJim Ingham       process->DeallocateMemory(path_addr);
8111ecb34f4SJim Ingham   });
8121ecb34f4SJim Ingham 
8131ecb34f4SJim Ingham   process->WriteMemory(path_addr, path.c_str(), path_len, utility_error);
8141ecb34f4SJim Ingham   if (utility_error.Fail()) {
8151ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not write path string:"
8161ecb34f4SJim Ingham                                     " %s", utility_error.AsCString());
8171ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
8181ecb34f4SJim Ingham   }
8191ecb34f4SJim Ingham 
82005097246SAdrian Prantl   // Make space for our return structure.  It is two pointers big: the token
82105097246SAdrian Prantl   // and the error string.
8221ecb34f4SJim Ingham   const uint32_t addr_size = process->GetAddressByteSize();
8231ecb34f4SJim Ingham   lldb::addr_t return_addr = process->CallocateMemory(2*addr_size,
8241ecb34f4SJim Ingham                                                       permissions,
8251ecb34f4SJim Ingham                                                       utility_error);
8261ecb34f4SJim Ingham   if (utility_error.Fail()) {
8271ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
8281ecb34f4SJim Ingham                                     "for path: %s", utility_error.AsCString());
8291ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
8301ecb34f4SJim Ingham   }
8311ecb34f4SJim Ingham 
8321ecb34f4SJim Ingham   // Make sure we deallocate the result structure memory
8331ecb34f4SJim Ingham   CleanUp return_cleanup([process, return_addr] {
8341ecb34f4SJim Ingham       process->DeallocateMemory(return_addr);
8351ecb34f4SJim Ingham   });
8361ecb34f4SJim Ingham 
8370d231f71SJim Ingham   // This will be the address of the storage for paths, if we are using them,
8380d231f71SJim Ingham   // or nullptr to signal we aren't.
8390d231f71SJim Ingham   lldb::addr_t path_array_addr = 0x0;
8400d231f71SJim Ingham   llvm::Optional<CleanUp> path_array_cleanup;
8410d231f71SJim Ingham 
8420d231f71SJim Ingham   // This is the address to a buffer large enough to hold the largest path
8430d231f71SJim Ingham   // conjoined with the library name we're passing in.  This is a convenience
8440d231f71SJim Ingham   // to avoid having to call malloc in the dlopen function.
8450d231f71SJim Ingham   lldb::addr_t buffer_addr = 0x0;
8460d231f71SJim Ingham   llvm::Optional<CleanUp> buffer_cleanup;
8470d231f71SJim Ingham 
8481ecb34f4SJim Ingham   // Set the values into our args and write them to the target:
8490d231f71SJim Ingham   if (paths != nullptr) {
8500d231f71SJim Ingham     // First insert the paths into the target.  This is expected to be a
8510d231f71SJim Ingham     // continuous buffer with the strings laid out null terminated and
8520d231f71SJim Ingham     // end to end with an empty string terminating the buffer.
8530d231f71SJim Ingham     // We also compute the buffer's required size as we go.
8540d231f71SJim Ingham     size_t buffer_size = 0;
8550d231f71SJim Ingham     std::string path_array;
8560d231f71SJim Ingham     for (auto path : *paths) {
85740fa4a1aSJim Ingham       // Don't insert empty paths, they will make us abort the path
85840fa4a1aSJim Ingham       // search prematurely.
85940fa4a1aSJim Ingham       if (path.empty())
86040fa4a1aSJim Ingham         continue;
8610d231f71SJim Ingham       size_t path_size = path.size();
8620d231f71SJim Ingham       path_array.append(path);
8630d231f71SJim Ingham       path_array.push_back('\0');
8640d231f71SJim Ingham       if (path_size > buffer_size)
8650d231f71SJim Ingham         buffer_size = path_size;
8660d231f71SJim Ingham     }
8670d231f71SJim Ingham     path_array.push_back('\0');
8680d231f71SJim Ingham 
8690d231f71SJim Ingham     path_array_addr = process->AllocateMemory(path_array.size(),
8700d231f71SJim Ingham                                               permissions,
8710d231f71SJim Ingham                                               utility_error);
8720d231f71SJim Ingham     if (path_array_addr == LLDB_INVALID_ADDRESS) {
8730d231f71SJim Ingham       error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
8740d231f71SJim Ingham                                       "for path array: %s",
8750d231f71SJim Ingham                                       utility_error.AsCString());
8760d231f71SJim Ingham       return LLDB_INVALID_IMAGE_TOKEN;
8770d231f71SJim Ingham     }
8780d231f71SJim Ingham 
8790d231f71SJim Ingham     // Make sure we deallocate the paths array.
8800d231f71SJim Ingham     path_array_cleanup.emplace([process, path_array_addr] {
8810d231f71SJim Ingham         process->DeallocateMemory(path_array_addr);
8820d231f71SJim Ingham     });
8830d231f71SJim Ingham 
8840d231f71SJim Ingham     process->WriteMemory(path_array_addr, path_array.data(),
8850d231f71SJim Ingham                          path_array.size(), utility_error);
8860d231f71SJim Ingham 
8870d231f71SJim Ingham     if (utility_error.Fail()) {
8880d231f71SJim Ingham       error.SetErrorStringWithFormat("dlopen error: could not write path array:"
8890d231f71SJim Ingham                                      " %s", utility_error.AsCString());
8900d231f71SJim Ingham       return LLDB_INVALID_IMAGE_TOKEN;
8910d231f71SJim Ingham     }
8920d231f71SJim Ingham     // Now make spaces in the target for the buffer.  We need to add one for
8930d231f71SJim Ingham     // the '/' that the utility function will insert and one for the '\0':
8940d231f71SJim Ingham     buffer_size += path.size() + 2;
8950d231f71SJim Ingham 
8960d231f71SJim Ingham     buffer_addr = process->AllocateMemory(buffer_size,
8970d231f71SJim Ingham                                           permissions,
8980d231f71SJim Ingham                                           utility_error);
8990d231f71SJim Ingham     if (buffer_addr == LLDB_INVALID_ADDRESS) {
9000d231f71SJim Ingham       error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
9010d231f71SJim Ingham                                       "for buffer: %s",
9020d231f71SJim Ingham                                       utility_error.AsCString());
9030d231f71SJim Ingham       return LLDB_INVALID_IMAGE_TOKEN;
9040d231f71SJim Ingham     }
9050d231f71SJim Ingham 
9060d231f71SJim Ingham     // Make sure we deallocate the buffer memory:
9070d231f71SJim Ingham     buffer_cleanup.emplace([process, buffer_addr] {
9080d231f71SJim Ingham         process->DeallocateMemory(buffer_addr);
9090d231f71SJim Ingham     });
9100d231f71SJim Ingham   }
9110d231f71SJim Ingham 
9121ecb34f4SJim Ingham   arguments.GetValueAtIndex(0)->GetScalar() = path_addr;
9130d231f71SJim Ingham   arguments.GetValueAtIndex(1)->GetScalar() = path_array_addr;
9140d231f71SJim Ingham   arguments.GetValueAtIndex(2)->GetScalar() = buffer_addr;
9150d231f71SJim Ingham   arguments.GetValueAtIndex(3)->GetScalar() = return_addr;
9161ecb34f4SJim Ingham 
9171ecb34f4SJim Ingham   lldb::addr_t func_args_addr = LLDB_INVALID_ADDRESS;
9181ecb34f4SJim Ingham 
9191ecb34f4SJim Ingham   diagnostics.Clear();
9201ecb34f4SJim Ingham   if (!do_dlopen_function->WriteFunctionArguments(exe_ctx,
9211ecb34f4SJim Ingham                                                  func_args_addr,
9221ecb34f4SJim Ingham                                                  arguments,
9231ecb34f4SJim Ingham                                                  diagnostics)) {
9241ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not write function "
9251ecb34f4SJim Ingham                                    "arguments: %s",
9261ecb34f4SJim Ingham                                    diagnostics.GetString().c_str());
9271ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9281ecb34f4SJim Ingham   }
9291ecb34f4SJim Ingham 
9301ecb34f4SJim Ingham   // Make sure we clean up the args structure.  We can't reuse it because the
9311ecb34f4SJim Ingham   // Platform lives longer than the process and the Platforms don't get a
9321ecb34f4SJim Ingham   // signal to clean up cached data when a process goes away.
9331ecb34f4SJim Ingham   CleanUp args_cleanup([do_dlopen_function, &exe_ctx, func_args_addr] {
9341ecb34f4SJim Ingham     do_dlopen_function->DeallocateFunctionResults(exe_ctx, func_args_addr);
9351ecb34f4SJim Ingham   });
9361ecb34f4SJim Ingham 
9371ecb34f4SJim Ingham   // Now run the caller:
9381ecb34f4SJim Ingham   EvaluateExpressionOptions options;
9391ecb34f4SJim Ingham   options.SetExecutionPolicy(eExecutionPolicyAlways);
9401ecb34f4SJim Ingham   options.SetLanguage(eLanguageTypeC_plus_plus);
9411ecb34f4SJim Ingham   options.SetIgnoreBreakpoints(true);
9421ecb34f4SJim Ingham   options.SetUnwindOnError(true);
9431ecb34f4SJim Ingham   options.SetTrapExceptions(false); // dlopen can't throw exceptions, so
9441ecb34f4SJim Ingham                                     // don't do the work to trap them.
9454c03ea14SAdrian Prantl   options.SetTimeout(process->GetUtilityExpressionTimeout());
946c01783a8SRaphael Isemann   options.SetIsForUtilityExpr(true);
9471ecb34f4SJim Ingham 
9481ecb34f4SJim Ingham   Value return_value;
9491ecb34f4SJim Ingham   // Fetch the clang types we will need:
9501ecb34f4SJim Ingham   ClangASTContext *ast = process->GetTarget().GetScratchClangASTContext();
9511ecb34f4SJim Ingham 
9521ecb34f4SJim Ingham   CompilerType clang_void_pointer_type
9531ecb34f4SJim Ingham       = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
9541ecb34f4SJim Ingham 
9551ecb34f4SJim Ingham   return_value.SetCompilerType(clang_void_pointer_type);
9561ecb34f4SJim Ingham 
9571ecb34f4SJim Ingham   ExpressionResults results = do_dlopen_function->ExecuteFunction(
9581ecb34f4SJim Ingham       exe_ctx, &func_args_addr, options, diagnostics, return_value);
9591ecb34f4SJim Ingham   if (results != eExpressionCompleted) {
9600d231f71SJim Ingham     error.SetErrorStringWithFormat("dlopen error: failed executing "
9611ecb34f4SJim Ingham                                    "dlopen wrapper function: %s",
9621ecb34f4SJim Ingham                                    diagnostics.GetString().c_str());
9631ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9641ecb34f4SJim Ingham   }
9651ecb34f4SJim Ingham 
9661ecb34f4SJim Ingham   // Read the dlopen token from the return area:
9671ecb34f4SJim Ingham   lldb::addr_t token = process->ReadPointerFromMemory(return_addr,
9681ecb34f4SJim Ingham                                                       utility_error);
9691ecb34f4SJim Ingham   if (utility_error.Fail()) {
9701ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not read the return "
9711ecb34f4SJim Ingham                                     "struct: %s", utility_error.AsCString());
9721ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9731ecb34f4SJim Ingham   }
9741ecb34f4SJim Ingham 
9751ecb34f4SJim Ingham   // The dlopen succeeded!
9760d231f71SJim Ingham   if (token != 0x0) {
9770d231f71SJim Ingham     if (loaded_image && buffer_addr != 0x0)
9780d231f71SJim Ingham     {
9790d231f71SJim Ingham       // Capture the image which was loaded.  We leave it in the buffer on
9800d231f71SJim Ingham       // exit from the dlopen function, so we can just read it from there:
9810d231f71SJim Ingham       std::string name_string;
9820d231f71SJim Ingham       process->ReadCStringFromMemory(buffer_addr, name_string, utility_error);
9830d231f71SJim Ingham       if (utility_error.Success())
9848f3be7a3SJonas Devlieghere         loaded_image->SetFile(name_string, llvm::sys::path::Style::posix);
9850d231f71SJim Ingham     }
9861ecb34f4SJim Ingham     return process->AddImageToken(token);
9870d231f71SJim Ingham   }
9881ecb34f4SJim Ingham 
9891ecb34f4SJim Ingham   // We got an error, lets read in the error string:
9901ecb34f4SJim Ingham   std::string dlopen_error_str;
9911ecb34f4SJim Ingham   lldb::addr_t error_addr
9921ecb34f4SJim Ingham     = process->ReadPointerFromMemory(return_addr + addr_size, utility_error);
9931ecb34f4SJim Ingham   if (utility_error.Fail()) {
9941ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not read error string: "
9951ecb34f4SJim Ingham                                     "%s", utility_error.AsCString());
9961ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9971ecb34f4SJim Ingham   }
9981ecb34f4SJim Ingham 
9991ecb34f4SJim Ingham   size_t num_chars = process->ReadCStringFromMemory(error_addr + addr_size,
10001ecb34f4SJim Ingham                                                     dlopen_error_str,
10011ecb34f4SJim Ingham                                                     utility_error);
10021ecb34f4SJim Ingham   if (utility_error.Success() && num_chars > 0)
1003b9c1b51eSKate Stone     error.SetErrorStringWithFormat("dlopen error: %s",
10041ecb34f4SJim Ingham                                    dlopen_error_str.c_str());
10053cb132a0STamas Berghammer   else
10063cb132a0STamas Berghammer     error.SetErrorStringWithFormat("dlopen failed for unknown reasons.");
10071ecb34f4SJim Ingham 
10083cb132a0STamas Berghammer   return LLDB_INVALID_IMAGE_TOKEN;
10093cb132a0STamas Berghammer }
10103cb132a0STamas Berghammer 
101197206d57SZachary Turner Status PlatformPOSIX::UnloadImage(lldb_private::Process *process,
1012b9c1b51eSKate Stone                                   uint32_t image_token) {
10133cb132a0STamas Berghammer   const addr_t image_addr = process->GetImagePtrFromToken(image_token);
10143cb132a0STamas Berghammer   if (image_addr == LLDB_INVALID_ADDRESS)
101597206d57SZachary Turner     return Status("Invalid image token");
10163cb132a0STamas Berghammer 
10173cb132a0STamas Berghammer   StreamString expr;
10183cb132a0STamas Berghammer   expr.Printf("dlclose((void *)0x%" PRIx64 ")", image_addr);
1019f7a5851dSNitesh Jain   llvm::StringRef prefix = GetLibdlFunctionDeclarations(process);
10203cb132a0STamas Berghammer   lldb::ValueObjectSP result_valobj_sp;
102197206d57SZachary Turner   Status error = EvaluateLibdlExpression(process, expr.GetData(), prefix,
1022b9c1b51eSKate Stone                                          result_valobj_sp);
10233cb132a0STamas Berghammer   if (error.Fail())
10243cb132a0STamas Berghammer     return error;
10253cb132a0STamas Berghammer 
10263cb132a0STamas Berghammer   if (result_valobj_sp->GetError().Fail())
10273cb132a0STamas Berghammer     return result_valobj_sp->GetError();
10283cb132a0STamas Berghammer 
10293cb132a0STamas Berghammer   Scalar scalar;
1030b9c1b51eSKate Stone   if (result_valobj_sp->ResolveValue(scalar)) {
10313cb132a0STamas Berghammer     if (scalar.UInt(1))
103297206d57SZachary Turner       return Status("expression failed: \"%s\"", expr.GetData());
10333cb132a0STamas Berghammer     process->ResetImageToken(image_token);
10343cb132a0STamas Berghammer   }
103597206d57SZachary Turner   return Status();
10363cb132a0STamas Berghammer }
1037fcdde63fSTamas Berghammer 
1038f7a5851dSNitesh Jain llvm::StringRef
1039f7a5851dSNitesh Jain PlatformPOSIX::GetLibdlFunctionDeclarations(lldb_private::Process *process) {
1040fcdde63fSTamas Berghammer   return R"(
1041fcdde63fSTamas Berghammer               extern "C" void* dlopen(const char*, int);
1042fcdde63fSTamas Berghammer               extern "C" void* dlsym(void*, const char*);
1043fcdde63fSTamas Berghammer               extern "C" int   dlclose(void*);
1044fcdde63fSTamas Berghammer               extern "C" char* dlerror(void);
1045fcdde63fSTamas Berghammer              )";
1046fcdde63fSTamas Berghammer }
1047ccd6cffbSTamas Berghammer 
1048b9c1b51eSKate Stone size_t PlatformPOSIX::ConnectToWaitingProcesses(Debugger &debugger,
104997206d57SZachary Turner                                                 Status &error) {
1050ccd6cffbSTamas Berghammer   if (m_remote_platform_sp)
1051ccd6cffbSTamas Berghammer     return m_remote_platform_sp->ConnectToWaitingProcesses(debugger, error);
1052ccd6cffbSTamas Berghammer   return Platform::ConnectToWaitingProcesses(debugger, error);
1053ccd6cffbSTamas Berghammer }
1054ec9bc8ccSPavel Labath 
1055ec9bc8ccSPavel Labath ConstString PlatformPOSIX::GetFullNameForDylib(ConstString basename) {
1056ec9bc8ccSPavel Labath   if (basename.IsEmpty())
1057ec9bc8ccSPavel Labath     return basename;
1058ec9bc8ccSPavel Labath 
1059ec9bc8ccSPavel Labath   StreamString stream;
1060ec9bc8ccSPavel Labath   stream.Printf("lib%s.so", basename.GetCString());
1061ec9bc8ccSPavel Labath   return ConstString(stream.GetString());
1062ec9bc8ccSPavel Labath }
1063