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;
226248a1305SKonrad Kleine   platform->RunShellCommand(command.GetData(), nullptr, &status, nullptr,
227248a1305SKonrad 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;
251248a1305SKonrad 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());
279*63e5fb76SJonas Devlieghere       LLDB_LOGF(log, "[PutFile] Running command: %s\n", command.GetData());
280e0f8f574SDaniel Malea       int retcode;
281248a1305SKonrad Kleine       Host::RunShellCommand(command.GetData(), nullptr, &retcode, nullptr,
282248a1305SKonrad Kleine                             nullptr, std::chrono::minutes(1));
283b9c1b51eSKate Stone       if (retcode == 0) {
284e0f8f574SDaniel Malea         // Don't chown a local file for a remote system
285e0f8f574SDaniel Malea         //                if (chown_file(this,dst_path.c_str(),uid,gid) != 0)
28697206d57SZachary Turner         //                    return Status("unable to perform chown");
28797206d57SZachary Turner         return Status();
288e0f8f574SDaniel Malea       }
289b9c1b51eSKate Stone       // if we are still here rsync has failed - let's try the slow way before
290b9c1b51eSKate Stone       // giving up
291e0f8f574SDaniel Malea     }
292e0f8f574SDaniel Malea   }
293e0f8f574SDaniel Malea   return Platform::PutFile(source, destination, uid, gid);
294e0f8f574SDaniel Malea }
295e0f8f574SDaniel Malea 
29697206d57SZachary Turner lldb_private::Status PlatformPOSIX::GetFile(
297b9c1b51eSKate Stone     const lldb_private::FileSpec &source,      // remote file path
298d3173f34SChaoren Lin     const lldb_private::FileSpec &destination) // local file path
299e0f8f574SDaniel Malea {
300fbb76349SGreg Clayton   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
301fbb76349SGreg Clayton 
302e0f8f574SDaniel Malea   // Check the args, first.
303e0f8f574SDaniel Malea   std::string src_path(source.GetPath());
304e0f8f574SDaniel Malea   if (src_path.empty())
30597206d57SZachary Turner     return Status("unable to get file path for source");
306e0f8f574SDaniel Malea   std::string dst_path(destination.GetPath());
307e0f8f574SDaniel Malea   if (dst_path.empty())
30897206d57SZachary Turner     return Status("unable to get file path for destination");
309b9c1b51eSKate Stone   if (IsHost()) {
310e0f8f574SDaniel Malea     if (FileSpec::Equal(source, destination, true))
31197206d57SZachary Turner       return Status("local scenario->source and destination are the same file "
312b9c1b51eSKate Stone                     "path: no operation performed");
313e0f8f574SDaniel Malea     // cp src dst
314e0f8f574SDaniel Malea     StreamString cp_command;
315e0f8f574SDaniel Malea     cp_command.Printf("cp %s %s", src_path.c_str(), dst_path.c_str());
316e0f8f574SDaniel Malea     int status;
317248a1305SKonrad Kleine     RunShellCommand(cp_command.GetData(), nullptr, &status, nullptr, nullptr,
31819dd1a0eSPavel Labath                     std::chrono::seconds(10));
319e0f8f574SDaniel Malea     if (status != 0)
32097206d57SZachary Turner       return Status("unable to perform copy");
32197206d57SZachary Turner     return Status();
322b9c1b51eSKate Stone   } else if (m_remote_platform_sp) {
323b9c1b51eSKate Stone     if (GetSupportsRSync()) {
324e0f8f574SDaniel Malea       StreamString command;
325b9c1b51eSKate Stone       if (GetIgnoresRemoteHostname()) {
326e0f8f574SDaniel Malea         if (!GetRSyncPrefix())
327b9c1b51eSKate Stone           command.Printf("rsync %s %s %s", GetRSyncOpts(), src_path.c_str(),
328e0f8f574SDaniel Malea                          dst_path.c_str());
329e0f8f574SDaniel Malea         else
330b9c1b51eSKate Stone           command.Printf("rsync %s %s%s %s", GetRSyncOpts(), GetRSyncPrefix(),
331b9c1b51eSKate Stone                          src_path.c_str(), dst_path.c_str());
332b9c1b51eSKate Stone       } else
333b9c1b51eSKate Stone         command.Printf("rsync %s %s:%s %s", GetRSyncOpts(),
334b9c1b51eSKate Stone                        m_remote_platform_sp->GetHostname(), src_path.c_str(),
335e0f8f574SDaniel Malea                        dst_path.c_str());
336*63e5fb76SJonas Devlieghere       LLDB_LOGF(log, "[GetFile] Running command: %s\n", command.GetData());
337e0f8f574SDaniel Malea       int retcode;
338248a1305SKonrad Kleine       Host::RunShellCommand(command.GetData(), nullptr, &retcode, nullptr,
339248a1305SKonrad Kleine                             nullptr, std::chrono::minutes(1));
340e0f8f574SDaniel Malea       if (retcode == 0)
34197206d57SZachary Turner         return Status();
34205097246SAdrian Prantl       // If we are here, rsync has failed - let's try the slow way before
34305097246SAdrian Prantl       // giving up
344e0f8f574SDaniel Malea     }
345e0f8f574SDaniel Malea     // open src and dst
346e0f8f574SDaniel Malea     // read/write, read/write, read/write, ...
347e0f8f574SDaniel Malea     // close src
348e0f8f574SDaniel Malea     // close dst
349*63e5fb76SJonas Devlieghere     LLDB_LOGF(log, "[GetFile] Using block by block transfer....\n");
35097206d57SZachary Turner     Status error;
351b9c1b51eSKate Stone     user_id_t fd_src = OpenFile(source, File::eOpenOptionRead,
352b9c1b51eSKate Stone                                 lldb::eFilePermissionsFileDefault, error);
353e0f8f574SDaniel Malea 
354e0f8f574SDaniel Malea     if (fd_src == UINT64_MAX)
35597206d57SZachary Turner       return Status("unable to open source file");
356e0f8f574SDaniel Malea 
357fbb76349SGreg Clayton     uint32_t permissions = 0;
358d3173f34SChaoren Lin     error = GetFilePermissions(source, permissions);
359e0f8f574SDaniel Malea 
360e0f8f574SDaniel Malea     if (permissions == 0)
361fbb76349SGreg Clayton       permissions = lldb::eFilePermissionsFileDefault;
362e0f8f574SDaniel Malea 
363c00cf4a0SZachary Turner     user_id_t fd_dst = FileCache::GetInstance().OpenFile(
364b9c1b51eSKate Stone         destination, File::eOpenOptionCanCreate | File::eOpenOptionWrite |
365b9c1b51eSKate Stone                          File::eOpenOptionTruncate,
366b9c1b51eSKate Stone         permissions, error);
367e0f8f574SDaniel Malea 
368b9c1b51eSKate Stone     if (fd_dst == UINT64_MAX) {
369e0f8f574SDaniel Malea       if (error.Success())
370e0f8f574SDaniel Malea         error.SetErrorString("unable to open destination file");
371e0f8f574SDaniel Malea     }
372e0f8f574SDaniel Malea 
373b9c1b51eSKate Stone     if (error.Success()) {
374e0f8f574SDaniel Malea       lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0));
375e0f8f574SDaniel Malea       uint64_t offset = 0;
376e0f8f574SDaniel Malea       error.Clear();
377b9c1b51eSKate Stone       while (error.Success()) {
378b9c1b51eSKate Stone         const uint64_t n_read = ReadFile(fd_src, offset, buffer_sp->GetBytes(),
379b9c1b51eSKate Stone                                          buffer_sp->GetByteSize(), error);
380e0f8f574SDaniel Malea         if (error.Fail())
381e0f8f574SDaniel Malea           break;
382e0f8f574SDaniel Malea         if (n_read == 0)
383e0f8f574SDaniel Malea           break;
384b9c1b51eSKate Stone         if (FileCache::GetInstance().WriteFile(fd_dst, offset,
385b9c1b51eSKate Stone                                                buffer_sp->GetBytes(), n_read,
386b9c1b51eSKate Stone                                                error) != n_read) {
387e0f8f574SDaniel Malea           if (!error.Fail())
388e0f8f574SDaniel Malea             error.SetErrorString("unable to write to destination file");
389e0f8f574SDaniel Malea           break;
390e0f8f574SDaniel Malea         }
391e0f8f574SDaniel Malea         offset += n_read;
392e0f8f574SDaniel Malea       }
393e0f8f574SDaniel Malea     }
394e0f8f574SDaniel Malea     // Ignore the close error of src.
395e0f8f574SDaniel Malea     if (fd_src != UINT64_MAX)
396e0f8f574SDaniel Malea       CloseFile(fd_src, error);
397e0f8f574SDaniel Malea     // And close the dst file descriptot.
398b9c1b51eSKate Stone     if (fd_dst != UINT64_MAX &&
399b9c1b51eSKate Stone         !FileCache::GetInstance().CloseFile(fd_dst, error)) {
400e0f8f574SDaniel Malea       if (!error.Fail())
401e0f8f574SDaniel Malea         error.SetErrorString("unable to close destination file");
402e0f8f574SDaniel Malea     }
403e0f8f574SDaniel Malea     return error;
404e0f8f574SDaniel Malea   }
405e0f8f574SDaniel Malea   return Platform::GetFile(source, destination);
406e0f8f574SDaniel Malea }
407e0f8f574SDaniel Malea 
408b9c1b51eSKate Stone std::string PlatformPOSIX::GetPlatformSpecificConnectionInformation() {
409e0f8f574SDaniel Malea   StreamString stream;
410b9c1b51eSKate Stone   if (GetSupportsRSync()) {
411e0f8f574SDaniel Malea     stream.PutCString("rsync");
412e0f8f574SDaniel Malea     if ((GetRSyncOpts() && *GetRSyncOpts()) ||
413b9c1b51eSKate Stone         (GetRSyncPrefix() && *GetRSyncPrefix()) || GetIgnoresRemoteHostname()) {
414e0f8f574SDaniel Malea       stream.Printf(", options: ");
415e0f8f574SDaniel Malea       if (GetRSyncOpts() && *GetRSyncOpts())
416e0f8f574SDaniel Malea         stream.Printf("'%s' ", GetRSyncOpts());
417e0f8f574SDaniel Malea       stream.Printf(", prefix: ");
418e0f8f574SDaniel Malea       if (GetRSyncPrefix() && *GetRSyncPrefix())
419e0f8f574SDaniel Malea         stream.Printf("'%s' ", GetRSyncPrefix());
420e0f8f574SDaniel Malea       if (GetIgnoresRemoteHostname())
421e0f8f574SDaniel Malea         stream.Printf("ignore remote-hostname ");
422e0f8f574SDaniel Malea     }
423e0f8f574SDaniel Malea   }
424b9c1b51eSKate Stone   if (GetSupportsSSH()) {
425e0f8f574SDaniel Malea     stream.PutCString("ssh");
426e0f8f574SDaniel Malea     if (GetSSHOpts() && *GetSSHOpts())
427e0f8f574SDaniel Malea       stream.Printf(", options: '%s' ", GetSSHOpts());
428e0f8f574SDaniel Malea   }
429e0f8f574SDaniel Malea   if (GetLocalCacheDirectory() && *GetLocalCacheDirectory())
430e0f8f574SDaniel Malea     stream.Printf("cache dir: %s", GetLocalCacheDirectory());
431e0f8f574SDaniel Malea   if (stream.GetSize())
432c156427dSZachary Turner     return stream.GetString();
433e0f8f574SDaniel Malea   else
434e0f8f574SDaniel Malea     return "";
435e0f8f574SDaniel Malea }
436e0f8f574SDaniel Malea 
437b9c1b51eSKate Stone const lldb::UnixSignalsSP &PlatformPOSIX::GetRemoteUnixSignals() {
43898d0a4b3SChaoren Lin   if (IsRemote() && m_remote_platform_sp)
43998d0a4b3SChaoren Lin     return m_remote_platform_sp->GetRemoteUnixSignals();
44098d0a4b3SChaoren Lin   return Platform::GetRemoteUnixSignals();
44198d0a4b3SChaoren Lin }
44298d0a4b3SChaoren Lin 
44397206d57SZachary Turner Status PlatformPOSIX::ConnectRemote(Args &args) {
44497206d57SZachary Turner   Status error;
445b9c1b51eSKate Stone   if (IsHost()) {
446b9c1b51eSKate Stone     error.SetErrorStringWithFormat(
447b9c1b51eSKate Stone         "can't connect to the host platform '%s', always connected",
448b9c1b51eSKate Stone         GetPluginName().GetCString());
449b9c1b51eSKate Stone   } else {
450015d818bSTodd Fiala     if (!m_remote_platform_sp)
451b9c1b51eSKate Stone       m_remote_platform_sp =
452b9c1b51eSKate Stone           Platform::Create(ConstString("remote-gdb-server"), error);
453015d818bSTodd Fiala 
454015d818bSTodd Fiala     if (m_remote_platform_sp && error.Success())
455015d818bSTodd Fiala       error = m_remote_platform_sp->ConnectRemote(args);
456015d818bSTodd Fiala     else
457015d818bSTodd Fiala       error.SetErrorString("failed to create a 'remote-gdb-server' platform");
458015d818bSTodd Fiala 
459015d818bSTodd Fiala     if (error.Fail())
460015d818bSTodd Fiala       m_remote_platform_sp.reset();
461015d818bSTodd Fiala   }
462015d818bSTodd Fiala 
463b9c1b51eSKate Stone   if (error.Success() && m_remote_platform_sp) {
464b9c1b51eSKate Stone     if (m_option_group_platform_rsync.get() &&
465b9c1b51eSKate Stone         m_option_group_platform_ssh.get() &&
466b9c1b51eSKate Stone         m_option_group_platform_caching.get()) {
467b9c1b51eSKate Stone       if (m_option_group_platform_rsync->m_rsync) {
468015d818bSTodd Fiala         SetSupportsRSync(true);
4690a66e2f1SEnrico Granata         SetRSyncOpts(m_option_group_platform_rsync->m_rsync_opts.c_str());
4700a66e2f1SEnrico Granata         SetRSyncPrefix(m_option_group_platform_rsync->m_rsync_prefix.c_str());
471b9c1b51eSKate Stone         SetIgnoresRemoteHostname(
472b9c1b51eSKate Stone             m_option_group_platform_rsync->m_ignores_remote_hostname);
473015d818bSTodd Fiala       }
474b9c1b51eSKate Stone       if (m_option_group_platform_ssh->m_ssh) {
475015d818bSTodd Fiala         SetSupportsSSH(true);
4760a66e2f1SEnrico Granata         SetSSHOpts(m_option_group_platform_ssh->m_ssh_opts.c_str());
477015d818bSTodd Fiala       }
478b9c1b51eSKate Stone       SetLocalCacheDirectory(
479b9c1b51eSKate Stone           m_option_group_platform_caching->m_cache_dir.c_str());
480015d818bSTodd Fiala     }
481015d818bSTodd Fiala   }
482015d818bSTodd Fiala 
483015d818bSTodd Fiala   return error;
484015d818bSTodd Fiala }
485015d818bSTodd Fiala 
48697206d57SZachary Turner Status PlatformPOSIX::DisconnectRemote() {
48797206d57SZachary Turner   Status error;
488015d818bSTodd Fiala 
489b9c1b51eSKate Stone   if (IsHost()) {
490b9c1b51eSKate Stone     error.SetErrorStringWithFormat(
491b9c1b51eSKate Stone         "can't disconnect from the host platform '%s', always connected",
492b9c1b51eSKate Stone         GetPluginName().GetCString());
493b9c1b51eSKate Stone   } else {
494015d818bSTodd Fiala     if (m_remote_platform_sp)
495015d818bSTodd Fiala       error = m_remote_platform_sp->DisconnectRemote();
496015d818bSTodd Fiala     else
497015d818bSTodd Fiala       error.SetErrorString("the platform is not currently connected");
498015d818bSTodd Fiala   }
499015d818bSTodd Fiala   return error;
500015d818bSTodd Fiala }
501015d818bSTodd Fiala 
502b9c1b51eSKate Stone lldb::ProcessSP PlatformPOSIX::Attach(ProcessAttachInfo &attach_info,
503b9c1b51eSKate Stone                                       Debugger &debugger, Target *target,
50497206d57SZachary Turner                                       Status &error) {
505348fb385STodd Fiala   lldb::ProcessSP process_sp;
506348fb385STodd Fiala   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
507348fb385STodd Fiala 
508b9c1b51eSKate Stone   if (IsHost()) {
509248a1305SKonrad Kleine     if (target == nullptr) {
510348fb385STodd Fiala       TargetSP new_target_sp;
511348fb385STodd Fiala 
512f9a07e9fSJonas Devlieghere       error = debugger.GetTargetList().CreateTarget(
513248a1305SKonrad Kleine           debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp);
514348fb385STodd Fiala       target = new_target_sp.get();
515*63e5fb76SJonas Devlieghere       LLDB_LOGF(log, "PlatformPOSIX::%s created new target", __FUNCTION__);
516b9c1b51eSKate Stone     } else {
517348fb385STodd Fiala       error.Clear();
518*63e5fb76SJonas Devlieghere       LLDB_LOGF(log, "PlatformPOSIX::%s target already existed, setting target",
519b9c1b51eSKate Stone                 __FUNCTION__);
520348fb385STodd Fiala     }
521348fb385STodd Fiala 
522b9c1b51eSKate Stone     if (target && error.Success()) {
523348fb385STodd Fiala       debugger.GetTargetList().SetSelectedTarget(target);
524b9c1b51eSKate Stone       if (log) {
525348fb385STodd Fiala         ModuleSP exe_module_sp = target->GetExecutableModule();
526*63e5fb76SJonas Devlieghere         LLDB_LOGF(log, "PlatformPOSIX::%s set selected target to %p %s",
527b9c1b51eSKate Stone                   __FUNCTION__, (void *)target,
528*63e5fb76SJonas Devlieghere                   exe_module_sp ? exe_module_sp->GetFileSpec().GetPath().c_str()
529b9c1b51eSKate Stone                                 : "<null>");
530348fb385STodd Fiala       }
531348fb385STodd Fiala 
532b9c1b51eSKate Stone       process_sp =
533b9c1b51eSKate Stone           target->CreateProcess(attach_info.GetListenerForProcess(debugger),
534248a1305SKonrad Kleine                                 attach_info.GetProcessPluginName(), nullptr);
535348fb385STodd Fiala 
536b9c1b51eSKate Stone       if (process_sp) {
537583bbb1dSJim Ingham         ListenerSP listener_sp = attach_info.GetHijackListener();
538b9c1b51eSKate Stone         if (listener_sp == nullptr) {
539b9c1b51eSKate Stone           listener_sp =
540b9c1b51eSKate Stone               Listener::MakeListener("lldb.PlatformPOSIX.attach.hijack");
541348fb385STodd Fiala           attach_info.SetHijackListener(listener_sp);
542926af0cdSOleksiy Vyalov         }
543583bbb1dSJim Ingham         process_sp->HijackProcessEvents(listener_sp);
544348fb385STodd Fiala         error = process_sp->Attach(attach_info);
545348fb385STodd Fiala       }
546348fb385STodd Fiala     }
547b9c1b51eSKate Stone   } else {
548348fb385STodd Fiala     if (m_remote_platform_sp)
549b9c1b51eSKate Stone       process_sp =
550b9c1b51eSKate Stone           m_remote_platform_sp->Attach(attach_info, debugger, target, error);
551348fb385STodd Fiala     else
552348fb385STodd Fiala       error.SetErrorString("the platform is not currently connected");
553348fb385STodd Fiala   }
554348fb385STodd Fiala   return process_sp;
555348fb385STodd Fiala }
556348fb385STodd Fiala 
557015d818bSTodd Fiala lldb::ProcessSP
558b9c1b51eSKate Stone PlatformPOSIX::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
559b9c1b51eSKate Stone                             Target *target, // Can be NULL, if NULL create a new
560b9c1b51eSKate Stone                                             // target, else use existing one
56197206d57SZachary Turner                             Status &error) {
562015d818bSTodd Fiala   ProcessSP process_sp;
563015d818bSTodd Fiala 
564b9c1b51eSKate Stone   if (IsHost()) {
565b9c1b51eSKate Stone     // We are going to hand this process off to debugserver which will be in
566245dd2e0SPavel Labath     // charge of setting the exit status.  However, we still need to reap it
567245dd2e0SPavel Labath     // from lldb. So, make sure we use a exit callback which does not set exit
568245dd2e0SPavel Labath     // status.
569245dd2e0SPavel Labath     const bool monitor_signals = false;
570245dd2e0SPavel Labath     launch_info.SetMonitorProcessCallback(
571245dd2e0SPavel Labath         &ProcessLaunchInfo::NoOpMonitorCallback, monitor_signals);
5728012cadbSGreg Clayton     process_sp = Platform::DebugProcess(launch_info, debugger, target, error);
573b9c1b51eSKate Stone   } else {
574015d818bSTodd Fiala     if (m_remote_platform_sp)
575b9c1b51eSKate Stone       process_sp = m_remote_platform_sp->DebugProcess(launch_info, debugger,
576b9c1b51eSKate Stone                                                       target, error);
577015d818bSTodd Fiala     else
578015d818bSTodd Fiala       error.SetErrorString("the platform is not currently connected");
579015d818bSTodd Fiala   }
580015d818bSTodd Fiala   return process_sp;
581015d818bSTodd Fiala }
582015d818bSTodd Fiala 
583b9c1b51eSKate Stone void PlatformPOSIX::CalculateTrapHandlerSymbolNames() {
5842094dbf4SJason Molenda   m_trap_handlers.push_back(ConstString("_sigtramp"));
5852094dbf4SJason Molenda }
5863cb132a0STamas Berghammer 
58797206d57SZachary Turner Status PlatformPOSIX::EvaluateLibdlExpression(
588b9c1b51eSKate Stone     lldb_private::Process *process, const char *expr_cstr,
589d37c9469SPavel Labath     llvm::StringRef expr_prefix, lldb::ValueObjectSP &result_valobj_sp) {
5903cb132a0STamas Berghammer   DynamicLoader *loader = process->GetDynamicLoader();
591b9c1b51eSKate Stone   if (loader) {
59297206d57SZachary Turner     Status error = loader->CanLoadImage();
5933cb132a0STamas Berghammer     if (error.Fail())
5943cb132a0STamas Berghammer       return error;
5953cb132a0STamas Berghammer   }
5963cb132a0STamas Berghammer 
5978d94ba0fSJim Ingham   ThreadSP thread_sp(process->GetThreadList().GetExpressionExecutionThread());
5983cb132a0STamas Berghammer   if (!thread_sp)
59997206d57SZachary Turner     return Status("Selected thread isn't valid");
6003cb132a0STamas Berghammer 
6013cb132a0STamas Berghammer   StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0));
6023cb132a0STamas Berghammer   if (!frame_sp)
60397206d57SZachary Turner     return Status("Frame 0 isn't valid");
6043cb132a0STamas Berghammer 
6053cb132a0STamas Berghammer   ExecutionContext exe_ctx;
6063cb132a0STamas Berghammer   frame_sp->CalculateExecutionContext(exe_ctx);
6073cb132a0STamas Berghammer   EvaluateExpressionOptions expr_options;
6083cb132a0STamas Berghammer   expr_options.SetUnwindOnError(true);
6093cb132a0STamas Berghammer   expr_options.SetIgnoreBreakpoints(true);
6103cb132a0STamas Berghammer   expr_options.SetExecutionPolicy(eExecutionPolicyAlways);
6113cb132a0STamas Berghammer   expr_options.SetLanguage(eLanguageTypeC_plus_plus);
612b9c1b51eSKate Stone   expr_options.SetTrapExceptions(false); // dlopen can't throw exceptions, so
613b9c1b51eSKate Stone                                          // don't do the work to trap them.
6144c03ea14SAdrian Prantl   expr_options.SetTimeout(process->GetUtilityExpressionTimeout());
6153cb132a0STamas Berghammer 
61697206d57SZachary Turner   Status expr_error;
61702980825SJim Ingham   ExpressionResults result =
618b9c1b51eSKate Stone       UserExpression::Evaluate(exe_ctx, expr_options, expr_cstr, expr_prefix,
619b9c1b51eSKate Stone                                result_valobj_sp, expr_error);
62002980825SJim Ingham   if (result != eExpressionCompleted)
62102980825SJim Ingham     return expr_error;
62202980825SJim Ingham 
6233cb132a0STamas Berghammer   if (result_valobj_sp->GetError().Fail())
6243cb132a0STamas Berghammer     return result_valobj_sp->GetError();
62597206d57SZachary Turner   return Status();
6263cb132a0STamas Berghammer }
6273cb132a0STamas Berghammer 
628d10d3795SFrederic Riss std::unique_ptr<UtilityFunction>
6291ecb34f4SJim Ingham PlatformPOSIX::MakeLoadImageUtilityFunction(ExecutionContext &exe_ctx,
630d10d3795SFrederic Riss                                             Status &error) {
63105097246SAdrian Prantl   // Remember to prepend this with the prefix from
63205097246SAdrian Prantl   // GetLibdlFunctionDeclarations. The returned values are all in
63305097246SAdrian Prantl   // __lldb_dlopen_result for consistency. The wrapper returns a void * but
63405097246SAdrian Prantl   // doesn't use it because UtilityFunctions don't work with void returns at
63505097246SAdrian Prantl   // present.
6361ecb34f4SJim Ingham   static const char *dlopen_wrapper_code = R"(
6371ecb34f4SJim Ingham   struct __lldb_dlopen_result {
6381ecb34f4SJim Ingham     void *image_ptr;
6391ecb34f4SJim Ingham     const char *error_str;
6401ecb34f4SJim Ingham   };
6411ecb34f4SJim Ingham 
64203856dd9SJim Ingham   extern void *memcpy(void *, const void *, size_t size);
6430d231f71SJim Ingham   extern size_t strlen(const char *);
6440d231f71SJim Ingham 
6450d231f71SJim Ingham 
6460d231f71SJim Ingham   void * __lldb_dlopen_wrapper (const char *name,
6470d231f71SJim Ingham                                 const char *path_strings,
6480d231f71SJim Ingham                                 char *buffer,
6491ecb34f4SJim Ingham                                 __lldb_dlopen_result *result_ptr)
6501ecb34f4SJim Ingham   {
6510d231f71SJim Ingham     // This is the case where the name is the full path:
65203856dd9SJim Ingham     if (!path_strings) {
6530d231f71SJim Ingham       result_ptr->image_ptr = dlopen(name, 2);
65403856dd9SJim Ingham       if (result_ptr->image_ptr)
6550d231f71SJim Ingham         result_ptr->error_str = nullptr;
6560d231f71SJim Ingham       return nullptr;
6570d231f71SJim Ingham     }
6580d231f71SJim Ingham 
6590d231f71SJim Ingham     // This is the case where we have a list of paths:
6600d231f71SJim Ingham     size_t name_len = strlen(name);
66103856dd9SJim Ingham     while (path_strings && path_strings[0] != '\0') {
6620d231f71SJim Ingham       size_t path_len = strlen(path_strings);
6630d231f71SJim Ingham       memcpy((void *) buffer, (void *) path_strings, path_len);
6640d231f71SJim Ingham       buffer[path_len] = '/';
6650d231f71SJim Ingham       char *target_ptr = buffer+path_len+1;
6660d231f71SJim Ingham       memcpy((void *) target_ptr, (void *) name, name_len + 1);
6670d231f71SJim Ingham       result_ptr->image_ptr = dlopen(buffer, 2);
66803856dd9SJim Ingham       if (result_ptr->image_ptr) {
6690d231f71SJim Ingham         result_ptr->error_str = nullptr;
6700d231f71SJim Ingham         break;
6710d231f71SJim Ingham       }
6721ecb34f4SJim Ingham       result_ptr->error_str = dlerror();
6730d231f71SJim Ingham       path_strings = path_strings + path_len + 1;
6740d231f71SJim Ingham     }
6751ecb34f4SJim Ingham     return nullptr;
6761ecb34f4SJim Ingham   }
6771ecb34f4SJim Ingham   )";
6781ecb34f4SJim Ingham 
6791ecb34f4SJim Ingham   static const char *dlopen_wrapper_name = "__lldb_dlopen_wrapper";
6801ecb34f4SJim Ingham   Process *process = exe_ctx.GetProcessSP().get();
6811ecb34f4SJim Ingham   // Insert the dlopen shim defines into our generic expression:
6821ecb34f4SJim Ingham   std::string expr(GetLibdlFunctionDeclarations(process));
6831ecb34f4SJim Ingham   expr.append(dlopen_wrapper_code);
6841ecb34f4SJim Ingham   Status utility_error;
6851ecb34f4SJim Ingham   DiagnosticManager diagnostics;
6861ecb34f4SJim Ingham 
6871ecb34f4SJim Ingham   std::unique_ptr<UtilityFunction> dlopen_utility_func_up(process
6881ecb34f4SJim Ingham       ->GetTarget().GetUtilityFunctionForLanguage(expr.c_str(),
6891ecb34f4SJim Ingham                                                   eLanguageTypeObjC,
6901ecb34f4SJim Ingham                                                   dlopen_wrapper_name,
6911ecb34f4SJim Ingham                                                   utility_error));
6921ecb34f4SJim Ingham   if (utility_error.Fail()) {
6931ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not make utility"
6941ecb34f4SJim Ingham                                    "function: %s", utility_error.AsCString());
6951ecb34f4SJim Ingham     return nullptr;
6961ecb34f4SJim Ingham   }
6971ecb34f4SJim Ingham   if (!dlopen_utility_func_up->Install(diagnostics, exe_ctx)) {
6981ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not install utility"
6991ecb34f4SJim Ingham                                    "function: %s",
7001ecb34f4SJim Ingham                                    diagnostics.GetString().c_str());
7011ecb34f4SJim Ingham     return nullptr;
7021ecb34f4SJim Ingham   }
7031ecb34f4SJim Ingham 
7041ecb34f4SJim Ingham   Value value;
7051ecb34f4SJim Ingham   ValueList arguments;
7061ecb34f4SJim Ingham   FunctionCaller *do_dlopen_function = nullptr;
7071ecb34f4SJim Ingham 
7081ecb34f4SJim Ingham   // Fetch the clang types we will need:
7091ecb34f4SJim Ingham   ClangASTContext *ast = process->GetTarget().GetScratchClangASTContext();
7101ecb34f4SJim Ingham 
7111ecb34f4SJim Ingham   CompilerType clang_void_pointer_type
7121ecb34f4SJim Ingham       = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
7131ecb34f4SJim Ingham   CompilerType clang_char_pointer_type
7141ecb34f4SJim Ingham         = ast->GetBasicType(eBasicTypeChar).GetPointerType();
7151ecb34f4SJim Ingham 
7160d231f71SJim Ingham   // We are passing four arguments, the basename, the list of places to look,
7170d231f71SJim Ingham   // a buffer big enough for all the path + name combos, and
7180d231f71SJim Ingham   // a pointer to the storage we've made for the result:
7191ecb34f4SJim Ingham   value.SetValueType(Value::eValueTypeScalar);
7201ecb34f4SJim Ingham   value.SetCompilerType(clang_void_pointer_type);
7211ecb34f4SJim Ingham   arguments.PushValue(value);
7221ecb34f4SJim Ingham   value.SetCompilerType(clang_char_pointer_type);
7231ecb34f4SJim Ingham   arguments.PushValue(value);
7240d231f71SJim Ingham   arguments.PushValue(value);
7250d231f71SJim Ingham   arguments.PushValue(value);
7261ecb34f4SJim Ingham 
7271ecb34f4SJim Ingham   do_dlopen_function = dlopen_utility_func_up->MakeFunctionCaller(
7281ecb34f4SJim Ingham       clang_void_pointer_type, arguments, exe_ctx.GetThreadSP(), utility_error);
7291ecb34f4SJim Ingham   if (utility_error.Fail()) {
7301ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not make function"
7311ecb34f4SJim Ingham                                    "caller: %s", utility_error.AsCString());
7321ecb34f4SJim Ingham     return nullptr;
7331ecb34f4SJim Ingham   }
7341ecb34f4SJim Ingham 
7351ecb34f4SJim Ingham   do_dlopen_function = dlopen_utility_func_up->GetFunctionCaller();
7361ecb34f4SJim Ingham   if (!do_dlopen_function) {
7371ecb34f4SJim Ingham     error.SetErrorString("dlopen error: could not get function caller.");
7381ecb34f4SJim Ingham     return nullptr;
7391ecb34f4SJim Ingham   }
7401ecb34f4SJim Ingham 
7411ecb34f4SJim Ingham   // We made a good utility function, so cache it in the process:
742d10d3795SFrederic Riss   return dlopen_utility_func_up;
7431ecb34f4SJim Ingham }
7441ecb34f4SJim Ingham 
745b9c1b51eSKate Stone uint32_t PlatformPOSIX::DoLoadImage(lldb_private::Process *process,
7464fbd67acSTamas Berghammer                                     const lldb_private::FileSpec &remote_file,
7470d231f71SJim Ingham                                     const std::vector<std::string> *paths,
7480d231f71SJim Ingham                                     lldb_private::Status &error,
7490d231f71SJim Ingham                                     lldb_private::FileSpec *loaded_image) {
7500d231f71SJim Ingham   if (loaded_image)
7510d231f71SJim Ingham     loaded_image->Clear();
7520d231f71SJim Ingham 
7531ecb34f4SJim Ingham   std::string path;
7541ecb34f4SJim Ingham   path = remote_file.GetPath();
7553cb132a0STamas Berghammer 
7561ecb34f4SJim Ingham   ThreadSP thread_sp = process->GetThreadList().GetExpressionExecutionThread();
7571ecb34f4SJim Ingham   if (!thread_sp) {
7581ecb34f4SJim Ingham     error.SetErrorString("dlopen error: no thread available to call dlopen.");
7593cb132a0STamas Berghammer     return LLDB_INVALID_IMAGE_TOKEN;
7603cb132a0STamas Berghammer   }
7613cb132a0STamas Berghammer 
7621ecb34f4SJim Ingham   DiagnosticManager diagnostics;
7633cb132a0STamas Berghammer 
7641ecb34f4SJim Ingham   ExecutionContext exe_ctx;
7651ecb34f4SJim Ingham   thread_sp->CalculateExecutionContext(exe_ctx);
7661ecb34f4SJim Ingham 
7671ecb34f4SJim Ingham   Status utility_error;
768d10d3795SFrederic Riss   UtilityFunction *dlopen_utility_func;
7691ecb34f4SJim Ingham   ValueList arguments;
7701ecb34f4SJim Ingham   FunctionCaller *do_dlopen_function = nullptr;
7711ecb34f4SJim Ingham 
772d10d3795SFrederic Riss   // The UtilityFunction is held in the Process.  Platforms don't track the
773d10d3795SFrederic Riss   // lifespan of the Targets that use them, we can't put this in the Platform.
774d10d3795SFrederic Riss   dlopen_utility_func = process->GetLoadImageUtilityFunction(
775d10d3795SFrederic Riss       this, [&]() -> std::unique_ptr<UtilityFunction> {
776d10d3795SFrederic Riss         return MakeLoadImageUtilityFunction(exe_ctx, error);
777d10d3795SFrederic Riss       });
7781ecb34f4SJim Ingham   // If we couldn't make it, the error will be in error, so we can exit here.
7791ecb34f4SJim Ingham   if (!dlopen_utility_func)
7801ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
7811ecb34f4SJim Ingham 
7821ecb34f4SJim Ingham   do_dlopen_function = dlopen_utility_func->GetFunctionCaller();
7831ecb34f4SJim Ingham   if (!do_dlopen_function) {
7841ecb34f4SJim Ingham     error.SetErrorString("dlopen error: could not get function caller.");
7851ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
7861ecb34f4SJim Ingham   }
7871ecb34f4SJim Ingham   arguments = do_dlopen_function->GetArgumentValues();
7881ecb34f4SJim Ingham 
78905097246SAdrian Prantl   // Now insert the path we are searching for and the result structure into the
79005097246SAdrian Prantl   // target.
7911ecb34f4SJim Ingham   uint32_t permissions = ePermissionsReadable|ePermissionsWritable;
7921ecb34f4SJim Ingham   size_t path_len = path.size() + 1;
7931ecb34f4SJim Ingham   lldb::addr_t path_addr = process->AllocateMemory(path_len,
7941ecb34f4SJim Ingham                                                    permissions,
7951ecb34f4SJim Ingham                                                    utility_error);
7961ecb34f4SJim Ingham   if (path_addr == LLDB_INVALID_ADDRESS) {
7971ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
7981ecb34f4SJim Ingham                                     "for path: %s", utility_error.AsCString());
7991ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
8001ecb34f4SJim Ingham   }
8011ecb34f4SJim Ingham 
8021ecb34f4SJim Ingham   // Make sure we deallocate the input string memory:
8031ecb34f4SJim Ingham   CleanUp path_cleanup([process, path_addr] {
8041ecb34f4SJim Ingham       process->DeallocateMemory(path_addr);
8051ecb34f4SJim Ingham   });
8061ecb34f4SJim Ingham 
8071ecb34f4SJim Ingham   process->WriteMemory(path_addr, path.c_str(), path_len, utility_error);
8081ecb34f4SJim Ingham   if (utility_error.Fail()) {
8091ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not write path string:"
8101ecb34f4SJim Ingham                                     " %s", utility_error.AsCString());
8111ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
8121ecb34f4SJim Ingham   }
8131ecb34f4SJim Ingham 
81405097246SAdrian Prantl   // Make space for our return structure.  It is two pointers big: the token
81505097246SAdrian Prantl   // and the error string.
8161ecb34f4SJim Ingham   const uint32_t addr_size = process->GetAddressByteSize();
8171ecb34f4SJim Ingham   lldb::addr_t return_addr = process->CallocateMemory(2*addr_size,
8181ecb34f4SJim Ingham                                                       permissions,
8191ecb34f4SJim Ingham                                                       utility_error);
8201ecb34f4SJim Ingham   if (utility_error.Fail()) {
8211ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
8221ecb34f4SJim Ingham                                     "for path: %s", utility_error.AsCString());
8231ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
8241ecb34f4SJim Ingham   }
8251ecb34f4SJim Ingham 
8261ecb34f4SJim Ingham   // Make sure we deallocate the result structure memory
8271ecb34f4SJim Ingham   CleanUp return_cleanup([process, return_addr] {
8281ecb34f4SJim Ingham       process->DeallocateMemory(return_addr);
8291ecb34f4SJim Ingham   });
8301ecb34f4SJim Ingham 
8310d231f71SJim Ingham   // This will be the address of the storage for paths, if we are using them,
8320d231f71SJim Ingham   // or nullptr to signal we aren't.
8330d231f71SJim Ingham   lldb::addr_t path_array_addr = 0x0;
8340d231f71SJim Ingham   llvm::Optional<CleanUp> path_array_cleanup;
8350d231f71SJim Ingham 
8360d231f71SJim Ingham   // This is the address to a buffer large enough to hold the largest path
8370d231f71SJim Ingham   // conjoined with the library name we're passing in.  This is a convenience
8380d231f71SJim Ingham   // to avoid having to call malloc in the dlopen function.
8390d231f71SJim Ingham   lldb::addr_t buffer_addr = 0x0;
8400d231f71SJim Ingham   llvm::Optional<CleanUp> buffer_cleanup;
8410d231f71SJim Ingham 
8421ecb34f4SJim Ingham   // Set the values into our args and write them to the target:
8430d231f71SJim Ingham   if (paths != nullptr) {
8440d231f71SJim Ingham     // First insert the paths into the target.  This is expected to be a
8450d231f71SJim Ingham     // continuous buffer with the strings laid out null terminated and
8460d231f71SJim Ingham     // end to end with an empty string terminating the buffer.
8470d231f71SJim Ingham     // We also compute the buffer's required size as we go.
8480d231f71SJim Ingham     size_t buffer_size = 0;
8490d231f71SJim Ingham     std::string path_array;
8500d231f71SJim Ingham     for (auto path : *paths) {
85140fa4a1aSJim Ingham       // Don't insert empty paths, they will make us abort the path
85240fa4a1aSJim Ingham       // search prematurely.
85340fa4a1aSJim Ingham       if (path.empty())
85440fa4a1aSJim Ingham         continue;
8550d231f71SJim Ingham       size_t path_size = path.size();
8560d231f71SJim Ingham       path_array.append(path);
8570d231f71SJim Ingham       path_array.push_back('\0');
8580d231f71SJim Ingham       if (path_size > buffer_size)
8590d231f71SJim Ingham         buffer_size = path_size;
8600d231f71SJim Ingham     }
8610d231f71SJim Ingham     path_array.push_back('\0');
8620d231f71SJim Ingham 
8630d231f71SJim Ingham     path_array_addr = process->AllocateMemory(path_array.size(),
8640d231f71SJim Ingham                                               permissions,
8650d231f71SJim Ingham                                               utility_error);
8660d231f71SJim Ingham     if (path_array_addr == LLDB_INVALID_ADDRESS) {
8670d231f71SJim Ingham       error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
8680d231f71SJim Ingham                                       "for path array: %s",
8690d231f71SJim Ingham                                       utility_error.AsCString());
8700d231f71SJim Ingham       return LLDB_INVALID_IMAGE_TOKEN;
8710d231f71SJim Ingham     }
8720d231f71SJim Ingham 
8730d231f71SJim Ingham     // Make sure we deallocate the paths array.
8740d231f71SJim Ingham     path_array_cleanup.emplace([process, path_array_addr] {
8750d231f71SJim Ingham         process->DeallocateMemory(path_array_addr);
8760d231f71SJim Ingham     });
8770d231f71SJim Ingham 
8780d231f71SJim Ingham     process->WriteMemory(path_array_addr, path_array.data(),
8790d231f71SJim Ingham                          path_array.size(), utility_error);
8800d231f71SJim Ingham 
8810d231f71SJim Ingham     if (utility_error.Fail()) {
8820d231f71SJim Ingham       error.SetErrorStringWithFormat("dlopen error: could not write path array:"
8830d231f71SJim Ingham                                      " %s", utility_error.AsCString());
8840d231f71SJim Ingham       return LLDB_INVALID_IMAGE_TOKEN;
8850d231f71SJim Ingham     }
8860d231f71SJim Ingham     // Now make spaces in the target for the buffer.  We need to add one for
8870d231f71SJim Ingham     // the '/' that the utility function will insert and one for the '\0':
8880d231f71SJim Ingham     buffer_size += path.size() + 2;
8890d231f71SJim Ingham 
8900d231f71SJim Ingham     buffer_addr = process->AllocateMemory(buffer_size,
8910d231f71SJim Ingham                                           permissions,
8920d231f71SJim Ingham                                           utility_error);
8930d231f71SJim Ingham     if (buffer_addr == LLDB_INVALID_ADDRESS) {
8940d231f71SJim Ingham       error.SetErrorStringWithFormat("dlopen error: could not allocate memory"
8950d231f71SJim Ingham                                       "for buffer: %s",
8960d231f71SJim Ingham                                       utility_error.AsCString());
8970d231f71SJim Ingham       return LLDB_INVALID_IMAGE_TOKEN;
8980d231f71SJim Ingham     }
8990d231f71SJim Ingham 
9000d231f71SJim Ingham     // Make sure we deallocate the buffer memory:
9010d231f71SJim Ingham     buffer_cleanup.emplace([process, buffer_addr] {
9020d231f71SJim Ingham         process->DeallocateMemory(buffer_addr);
9030d231f71SJim Ingham     });
9040d231f71SJim Ingham   }
9050d231f71SJim Ingham 
9061ecb34f4SJim Ingham   arguments.GetValueAtIndex(0)->GetScalar() = path_addr;
9070d231f71SJim Ingham   arguments.GetValueAtIndex(1)->GetScalar() = path_array_addr;
9080d231f71SJim Ingham   arguments.GetValueAtIndex(2)->GetScalar() = buffer_addr;
9090d231f71SJim Ingham   arguments.GetValueAtIndex(3)->GetScalar() = return_addr;
9101ecb34f4SJim Ingham 
9111ecb34f4SJim Ingham   lldb::addr_t func_args_addr = LLDB_INVALID_ADDRESS;
9121ecb34f4SJim Ingham 
9131ecb34f4SJim Ingham   diagnostics.Clear();
9141ecb34f4SJim Ingham   if (!do_dlopen_function->WriteFunctionArguments(exe_ctx,
9151ecb34f4SJim Ingham                                                  func_args_addr,
9161ecb34f4SJim Ingham                                                  arguments,
9171ecb34f4SJim Ingham                                                  diagnostics)) {
9181ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not write function "
9191ecb34f4SJim Ingham                                    "arguments: %s",
9201ecb34f4SJim Ingham                                    diagnostics.GetString().c_str());
9211ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9221ecb34f4SJim Ingham   }
9231ecb34f4SJim Ingham 
9241ecb34f4SJim Ingham   // Make sure we clean up the args structure.  We can't reuse it because the
9251ecb34f4SJim Ingham   // Platform lives longer than the process and the Platforms don't get a
9261ecb34f4SJim Ingham   // signal to clean up cached data when a process goes away.
9271ecb34f4SJim Ingham   CleanUp args_cleanup([do_dlopen_function, &exe_ctx, func_args_addr] {
9281ecb34f4SJim Ingham     do_dlopen_function->DeallocateFunctionResults(exe_ctx, func_args_addr);
9291ecb34f4SJim Ingham   });
9301ecb34f4SJim Ingham 
9311ecb34f4SJim Ingham   // Now run the caller:
9321ecb34f4SJim Ingham   EvaluateExpressionOptions options;
9331ecb34f4SJim Ingham   options.SetExecutionPolicy(eExecutionPolicyAlways);
9341ecb34f4SJim Ingham   options.SetLanguage(eLanguageTypeC_plus_plus);
9351ecb34f4SJim Ingham   options.SetIgnoreBreakpoints(true);
9361ecb34f4SJim Ingham   options.SetUnwindOnError(true);
9371ecb34f4SJim Ingham   options.SetTrapExceptions(false); // dlopen can't throw exceptions, so
9381ecb34f4SJim Ingham                                     // don't do the work to trap them.
9394c03ea14SAdrian Prantl   options.SetTimeout(process->GetUtilityExpressionTimeout());
940c01783a8SRaphael Isemann   options.SetIsForUtilityExpr(true);
9411ecb34f4SJim Ingham 
9421ecb34f4SJim Ingham   Value return_value;
9431ecb34f4SJim Ingham   // Fetch the clang types we will need:
9441ecb34f4SJim Ingham   ClangASTContext *ast = process->GetTarget().GetScratchClangASTContext();
9451ecb34f4SJim Ingham 
9461ecb34f4SJim Ingham   CompilerType clang_void_pointer_type
9471ecb34f4SJim Ingham       = ast->GetBasicType(eBasicTypeVoid).GetPointerType();
9481ecb34f4SJim Ingham 
9491ecb34f4SJim Ingham   return_value.SetCompilerType(clang_void_pointer_type);
9501ecb34f4SJim Ingham 
9511ecb34f4SJim Ingham   ExpressionResults results = do_dlopen_function->ExecuteFunction(
9521ecb34f4SJim Ingham       exe_ctx, &func_args_addr, options, diagnostics, return_value);
9531ecb34f4SJim Ingham   if (results != eExpressionCompleted) {
9540d231f71SJim Ingham     error.SetErrorStringWithFormat("dlopen error: failed executing "
9551ecb34f4SJim Ingham                                    "dlopen wrapper function: %s",
9561ecb34f4SJim Ingham                                    diagnostics.GetString().c_str());
9571ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9581ecb34f4SJim Ingham   }
9591ecb34f4SJim Ingham 
9601ecb34f4SJim Ingham   // Read the dlopen token from the return area:
9611ecb34f4SJim Ingham   lldb::addr_t token = process->ReadPointerFromMemory(return_addr,
9621ecb34f4SJim Ingham                                                       utility_error);
9631ecb34f4SJim Ingham   if (utility_error.Fail()) {
9641ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not read the return "
9651ecb34f4SJim Ingham                                     "struct: %s", utility_error.AsCString());
9661ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9671ecb34f4SJim Ingham   }
9681ecb34f4SJim Ingham 
9691ecb34f4SJim Ingham   // The dlopen succeeded!
9700d231f71SJim Ingham   if (token != 0x0) {
9710d231f71SJim Ingham     if (loaded_image && buffer_addr != 0x0)
9720d231f71SJim Ingham     {
9730d231f71SJim Ingham       // Capture the image which was loaded.  We leave it in the buffer on
9740d231f71SJim Ingham       // exit from the dlopen function, so we can just read it from there:
9750d231f71SJim Ingham       std::string name_string;
9760d231f71SJim Ingham       process->ReadCStringFromMemory(buffer_addr, name_string, utility_error);
9770d231f71SJim Ingham       if (utility_error.Success())
9788f3be7a3SJonas Devlieghere         loaded_image->SetFile(name_string, llvm::sys::path::Style::posix);
9790d231f71SJim Ingham     }
9801ecb34f4SJim Ingham     return process->AddImageToken(token);
9810d231f71SJim Ingham   }
9821ecb34f4SJim Ingham 
9831ecb34f4SJim Ingham   // We got an error, lets read in the error string:
9841ecb34f4SJim Ingham   std::string dlopen_error_str;
9851ecb34f4SJim Ingham   lldb::addr_t error_addr
9861ecb34f4SJim Ingham     = process->ReadPointerFromMemory(return_addr + addr_size, utility_error);
9871ecb34f4SJim Ingham   if (utility_error.Fail()) {
9881ecb34f4SJim Ingham     error.SetErrorStringWithFormat("dlopen error: could not read error string: "
9891ecb34f4SJim Ingham                                     "%s", utility_error.AsCString());
9901ecb34f4SJim Ingham     return LLDB_INVALID_IMAGE_TOKEN;
9911ecb34f4SJim Ingham   }
9921ecb34f4SJim Ingham 
9931ecb34f4SJim Ingham   size_t num_chars = process->ReadCStringFromMemory(error_addr + addr_size,
9941ecb34f4SJim Ingham                                                     dlopen_error_str,
9951ecb34f4SJim Ingham                                                     utility_error);
9961ecb34f4SJim Ingham   if (utility_error.Success() && num_chars > 0)
997b9c1b51eSKate Stone     error.SetErrorStringWithFormat("dlopen error: %s",
9981ecb34f4SJim Ingham                                    dlopen_error_str.c_str());
9993cb132a0STamas Berghammer   else
10003cb132a0STamas Berghammer     error.SetErrorStringWithFormat("dlopen failed for unknown reasons.");
10011ecb34f4SJim Ingham 
10023cb132a0STamas Berghammer   return LLDB_INVALID_IMAGE_TOKEN;
10033cb132a0STamas Berghammer }
10043cb132a0STamas Berghammer 
100597206d57SZachary Turner Status PlatformPOSIX::UnloadImage(lldb_private::Process *process,
1006b9c1b51eSKate Stone                                   uint32_t image_token) {
10073cb132a0STamas Berghammer   const addr_t image_addr = process->GetImagePtrFromToken(image_token);
10083cb132a0STamas Berghammer   if (image_addr == LLDB_INVALID_ADDRESS)
100997206d57SZachary Turner     return Status("Invalid image token");
10103cb132a0STamas Berghammer 
10113cb132a0STamas Berghammer   StreamString expr;
10123cb132a0STamas Berghammer   expr.Printf("dlclose((void *)0x%" PRIx64 ")", image_addr);
1013f7a5851dSNitesh Jain   llvm::StringRef prefix = GetLibdlFunctionDeclarations(process);
10143cb132a0STamas Berghammer   lldb::ValueObjectSP result_valobj_sp;
101597206d57SZachary Turner   Status error = EvaluateLibdlExpression(process, expr.GetData(), prefix,
1016b9c1b51eSKate Stone                                          result_valobj_sp);
10173cb132a0STamas Berghammer   if (error.Fail())
10183cb132a0STamas Berghammer     return error;
10193cb132a0STamas Berghammer 
10203cb132a0STamas Berghammer   if (result_valobj_sp->GetError().Fail())
10213cb132a0STamas Berghammer     return result_valobj_sp->GetError();
10223cb132a0STamas Berghammer 
10233cb132a0STamas Berghammer   Scalar scalar;
1024b9c1b51eSKate Stone   if (result_valobj_sp->ResolveValue(scalar)) {
10253cb132a0STamas Berghammer     if (scalar.UInt(1))
102697206d57SZachary Turner       return Status("expression failed: \"%s\"", expr.GetData());
10273cb132a0STamas Berghammer     process->ResetImageToken(image_token);
10283cb132a0STamas Berghammer   }
102997206d57SZachary Turner   return Status();
10303cb132a0STamas Berghammer }
1031fcdde63fSTamas Berghammer 
1032f7a5851dSNitesh Jain llvm::StringRef
1033f7a5851dSNitesh Jain PlatformPOSIX::GetLibdlFunctionDeclarations(lldb_private::Process *process) {
1034fcdde63fSTamas Berghammer   return R"(
1035fcdde63fSTamas Berghammer               extern "C" void* dlopen(const char*, int);
1036fcdde63fSTamas Berghammer               extern "C" void* dlsym(void*, const char*);
1037fcdde63fSTamas Berghammer               extern "C" int   dlclose(void*);
1038fcdde63fSTamas Berghammer               extern "C" char* dlerror(void);
1039fcdde63fSTamas Berghammer              )";
1040fcdde63fSTamas Berghammer }
1041ccd6cffbSTamas Berghammer 
1042b9c1b51eSKate Stone size_t PlatformPOSIX::ConnectToWaitingProcesses(Debugger &debugger,
104397206d57SZachary Turner                                                 Status &error) {
1044ccd6cffbSTamas Berghammer   if (m_remote_platform_sp)
1045ccd6cffbSTamas Berghammer     return m_remote_platform_sp->ConnectToWaitingProcesses(debugger, error);
1046ccd6cffbSTamas Berghammer   return Platform::ConnectToWaitingProcesses(debugger, error);
1047ccd6cffbSTamas Berghammer }
1048ec9bc8ccSPavel Labath 
1049ec9bc8ccSPavel Labath ConstString PlatformPOSIX::GetFullNameForDylib(ConstString basename) {
1050ec9bc8ccSPavel Labath   if (basename.IsEmpty())
1051ec9bc8ccSPavel Labath     return basename;
1052ec9bc8ccSPavel Labath 
1053ec9bc8ccSPavel Labath   StreamString stream;
1054ec9bc8ccSPavel Labath   stream.Printf("lib%s.so", basename.GetCString());
1055ec9bc8ccSPavel Labath   return ConstString(stream.GetString());
1056ec9bc8ccSPavel Labath }
1057