1e996fd30SGreg Clayton //===-- PlatformLinux.cpp ---------------------------------------*- C++ -*-===//
2e996fd30SGreg Clayton //
3e996fd30SGreg Clayton //                     The LLVM Compiler Infrastructure
4e996fd30SGreg Clayton //
5e996fd30SGreg Clayton // This file is distributed under the University of Illinois Open Source
6e996fd30SGreg Clayton // License. See LICENSE.TXT for details.
7e996fd30SGreg Clayton //
8e996fd30SGreg Clayton //===----------------------------------------------------------------------===//
9e996fd30SGreg Clayton 
10e996fd30SGreg Clayton #include "PlatformLinux.h"
11b2f1fb29SVirgile Bello #include "lldb/Host/Config.h"
12e996fd30SGreg Clayton 
13e996fd30SGreg Clayton // C Includes
14ecc11474SStephen Wilson #include <stdio.h>
15b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
16ecc11474SStephen Wilson #include <sys/utsname.h>
17b2f1fb29SVirgile Bello #endif
18ecc11474SStephen Wilson 
19e996fd30SGreg Clayton // C++ Includes
20e996fd30SGreg Clayton // Other libraries and framework includes
21e996fd30SGreg Clayton // Project includes
2228041352SGreg Clayton #include "lldb/Core/Debugger.h"
23015d818bSTodd Fiala #include "lldb/Core/Log.h"
24ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
25348fb385STodd Fiala #include "lldb/Core/State.h"
26e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h"
2713b18261SZachary Turner #include "lldb/Host/HostInfo.h"
28e996fd30SGreg Clayton #include "lldb/Target/Process.h"
29b9c1b51eSKate Stone #include "lldb/Target/Target.h"
30bf9a7730SZachary Turner #include "lldb/Utility/Error.h"
31bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
32e996fd30SGreg Clayton 
33a868c13cSBruce Mitchener // Define these constants from Linux mman.h for use when targeting
3496ad3de5SRobert Flack // remote linux systems even when host has different values.
3596ad3de5SRobert Flack #define MAP_PRIVATE 2
3696ad3de5SRobert Flack #define MAP_ANON 0x20
3796ad3de5SRobert Flack 
38e996fd30SGreg Clayton using namespace lldb;
39e996fd30SGreg Clayton using namespace lldb_private;
40db264a6dSTamas Berghammer using namespace lldb_private::platform_linux;
41e996fd30SGreg Clayton 
4228041352SGreg Clayton static uint32_t g_initialize_count = 0;
4328041352SGreg Clayton 
44281961a8STodd Fiala //------------------------------------------------------------------
45281961a8STodd Fiala 
46b9c1b51eSKate Stone PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) {
47db264a6dSTamas Berghammer   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
48b9c1b51eSKate Stone   if (log) {
49015d818bSTodd Fiala     const char *arch_name;
50015d818bSTodd Fiala     if (arch && arch->GetArchitectureName())
51015d818bSTodd Fiala       arch_name = arch->GetArchitectureName();
52015d818bSTodd Fiala     else
53015d818bSTodd Fiala       arch_name = "<null>";
54015d818bSTodd Fiala 
55b9c1b51eSKate Stone     const char *triple_cstr =
56b9c1b51eSKate Stone         arch ? arch->GetTriple().getTriple().c_str() : "<null>";
57015d818bSTodd Fiala 
58b9c1b51eSKate Stone     log->Printf("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__,
59b9c1b51eSKate Stone                 force ? "true" : "false", arch_name, triple_cstr);
60015d818bSTodd Fiala   }
61015d818bSTodd Fiala 
62b3a40ba8SGreg Clayton   bool create = force;
63b9c1b51eSKate Stone   if (create == false && arch && arch->IsValid()) {
64b3a40ba8SGreg Clayton     const llvm::Triple &triple = arch->GetTriple();
65b9c1b51eSKate Stone     switch (triple.getOS()) {
6670512317SGreg Clayton     case llvm::Triple::Linux:
67b1da257aSTed Woodward       create = true;
6870512317SGreg Clayton       break;
6970512317SGreg Clayton 
70dbc6c0bbSGreg Clayton #if defined(__linux__)
71dbc6c0bbSGreg Clayton     // Only accept "unknown" for the OS if the host is linux and
726a7f3338SBruce Mitchener     // it "unknown" wasn't specified (it was just returned because it
73dbc6c0bbSGreg Clayton     // was NOT specified)
74015d818bSTodd Fiala     case llvm::Triple::OSType::UnknownOS:
7570512317SGreg Clayton       create = !arch->TripleOSWasSpecified();
7670512317SGreg Clayton       break;
77dbc6c0bbSGreg Clayton #endif
7870512317SGreg Clayton     default:
7970512317SGreg Clayton       break;
8070512317SGreg Clayton     }
8170512317SGreg Clayton   }
82015d818bSTodd Fiala 
83b9c1b51eSKate Stone   if (create) {
84015d818bSTodd Fiala     if (log)
85b9c1b51eSKate Stone       log->Printf("PlatformLinux::%s() creating remote-linux platform",
86b9c1b51eSKate Stone                   __FUNCTION__);
87615eb7e6SGreg Clayton     return PlatformSP(new PlatformLinux(false));
88015d818bSTodd Fiala   }
89015d818bSTodd Fiala 
90015d818bSTodd Fiala   if (log)
91b9c1b51eSKate Stone     log->Printf(
92b9c1b51eSKate Stone         "PlatformLinux::%s() aborting creation of remote-linux platform",
93b9c1b51eSKate Stone         __FUNCTION__);
94015d818bSTodd Fiala 
95615eb7e6SGreg Clayton   return PlatformSP();
96ecc11474SStephen Wilson }
97ecc11474SStephen Wilson 
98b9c1b51eSKate Stone ConstString PlatformLinux::GetPluginNameStatic(bool is_host) {
99b9c1b51eSKate Stone   if (is_host) {
10057abc5d6SGreg Clayton     static ConstString g_host_name(Platform::GetHostPlatformName());
10157abc5d6SGreg Clayton     return g_host_name;
102b9c1b51eSKate Stone   } else {
10357abc5d6SGreg Clayton     static ConstString g_remote_name("remote-linux");
10457abc5d6SGreg Clayton     return g_remote_name;
10557abc5d6SGreg Clayton   }
10628041352SGreg Clayton }
10728041352SGreg Clayton 
108b9c1b51eSKate Stone const char *PlatformLinux::GetPluginDescriptionStatic(bool is_host) {
10928041352SGreg Clayton   if (is_host)
11028041352SGreg Clayton     return "Local Linux user platform plug-in.";
11128041352SGreg Clayton   else
11228041352SGreg Clayton     return "Remote Linux user platform plug-in.";
113ecc11474SStephen Wilson }
114ecc11474SStephen Wilson 
115b9c1b51eSKate Stone ConstString PlatformLinux::GetPluginName() {
11657abc5d6SGreg Clayton   return GetPluginNameStatic(IsHost());
11757abc5d6SGreg Clayton }
11857abc5d6SGreg Clayton 
119b9c1b51eSKate Stone void PlatformLinux::Initialize() {
1203c4f89d7STamas Berghammer   PlatformPOSIX::Initialize();
1213c4f89d7STamas Berghammer 
122b9c1b51eSKate Stone   if (g_initialize_count++ == 0) {
1231c6a1ea9STamas Berghammer #if defined(__linux__) && !defined(__ANDROID__)
12428041352SGreg Clayton     PlatformSP default_platform_sp(new PlatformLinux(true));
12513b18261SZachary Turner     default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
126615eb7e6SGreg Clayton     Platform::SetHostPlatform(default_platform_sp);
12728041352SGreg Clayton #endif
128b9c1b51eSKate Stone     PluginManager::RegisterPlugin(
129b9c1b51eSKate Stone         PlatformLinux::GetPluginNameStatic(false),
13028041352SGreg Clayton         PlatformLinux::GetPluginDescriptionStatic(false),
131*d65ac229SPavel Labath         PlatformLinux::CreateInstance, nullptr);
132ecc11474SStephen Wilson   }
133e996fd30SGreg Clayton }
134e996fd30SGreg Clayton 
135b9c1b51eSKate Stone void PlatformLinux::Terminate() {
136b9c1b51eSKate Stone   if (g_initialize_count > 0) {
137b9c1b51eSKate Stone     if (--g_initialize_count == 0) {
13828041352SGreg Clayton       PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance);
139e996fd30SGreg Clayton     }
14028041352SGreg Clayton   }
1413c4f89d7STamas Berghammer 
1423c4f89d7STamas Berghammer   PlatformPOSIX::Terminate();
14328041352SGreg Clayton }
144e996fd30SGreg Clayton 
145e996fd30SGreg Clayton //------------------------------------------------------------------
146e996fd30SGreg Clayton /// Default Constructor
147e996fd30SGreg Clayton //------------------------------------------------------------------
148b9c1b51eSKate Stone PlatformLinux::PlatformLinux(bool is_host)
149b9c1b51eSKate Stone     : PlatformPOSIX(is_host) // This is the local host platform
150b9c1b51eSKate Stone {}
151e996fd30SGreg Clayton 
152222b937cSEugene Zelenko PlatformLinux::~PlatformLinux() = default;
153e996fd30SGreg Clayton 
154b9c1b51eSKate Stone bool PlatformLinux::GetSupportedArchitectureAtIndex(uint32_t idx,
155b9c1b51eSKate Stone                                                     ArchSpec &arch) {
156b9c1b51eSKate Stone   if (IsHost()) {
157e49b8e06SRobert Flack     ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
158b9c1b51eSKate Stone     if (hostArch.GetTriple().isOSLinux()) {
159b9c1b51eSKate Stone       if (idx == 0) {
160e49b8e06SRobert Flack         arch = hostArch;
161e49b8e06SRobert Flack         return arch.IsValid();
162b9c1b51eSKate Stone       } else if (idx == 1) {
163e49b8e06SRobert Flack         // If the default host architecture is 64-bit, look for a 32-bit variant
164b9c1b51eSKate Stone         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
165e49b8e06SRobert Flack           arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
166e49b8e06SRobert Flack           return arch.IsValid();
167e49b8e06SRobert Flack         }
168e49b8e06SRobert Flack       }
169e49b8e06SRobert Flack     }
170b9c1b51eSKate Stone   } else {
171e49b8e06SRobert Flack     if (m_remote_platform_sp)
172e49b8e06SRobert Flack       return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
173bb1e283cSTed Woodward 
174bb1e283cSTed Woodward     llvm::Triple triple;
175bb1e283cSTed Woodward     // Set the OS to linux
176bb1e283cSTed Woodward     triple.setOS(llvm::Triple::Linux);
177bb1e283cSTed Woodward     // Set the architecture
178b9c1b51eSKate Stone     switch (idx) {
179b9c1b51eSKate Stone     case 0:
180b9c1b51eSKate Stone       triple.setArchName("x86_64");
181b9c1b51eSKate Stone       break;
182b9c1b51eSKate Stone     case 1:
183b9c1b51eSKate Stone       triple.setArchName("i386");
184b9c1b51eSKate Stone       break;
185b9c1b51eSKate Stone     case 2:
186b9c1b51eSKate Stone       triple.setArchName("arm");
187b9c1b51eSKate Stone       break;
188b9c1b51eSKate Stone     case 3:
189b9c1b51eSKate Stone       triple.setArchName("aarch64");
190b9c1b51eSKate Stone       break;
191b9c1b51eSKate Stone     case 4:
192b9c1b51eSKate Stone       triple.setArchName("mips64");
193b9c1b51eSKate Stone       break;
194b9c1b51eSKate Stone     case 5:
195b9c1b51eSKate Stone       triple.setArchName("hexagon");
196b9c1b51eSKate Stone       break;
197b9c1b51eSKate Stone     case 6:
198b9c1b51eSKate Stone       triple.setArchName("mips");
199b9c1b51eSKate Stone       break;
200b9c1b51eSKate Stone     case 7:
201b9c1b51eSKate Stone       triple.setArchName("mips64el");
202b9c1b51eSKate Stone       break;
203b9c1b51eSKate Stone     case 8:
204b9c1b51eSKate Stone       triple.setArchName("mipsel");
205b9c1b51eSKate Stone       break;
206b9c1b51eSKate Stone     case 9:
207b9c1b51eSKate Stone       triple.setArchName("s390x");
208b9c1b51eSKate Stone       break;
209b9c1b51eSKate Stone     default:
210b9c1b51eSKate Stone       return false;
211bb1e283cSTed Woodward     }
212b9c1b51eSKate Stone     // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
213b9c1b51eSKate Stone     // vendor by
214b9c1b51eSKate Stone     // calling triple.SetVendorName("unknown") so that it is a "unspecified
215b9c1b51eSKate Stone     // unknown".
216b9c1b51eSKate Stone     // This means when someone calls triple.GetVendorName() it will return an
217b9c1b51eSKate Stone     // empty string
218b9c1b51eSKate Stone     // which indicates that the vendor can be set when two architectures are
219b9c1b51eSKate Stone     // merged
220bb1e283cSTed Woodward 
221bb1e283cSTed Woodward     // Now set the triple into "arch" and return true
222bb1e283cSTed Woodward     arch.SetTriple(triple);
223bb1e283cSTed Woodward     return true;
224e49b8e06SRobert Flack   }
225e996fd30SGreg Clayton   return false;
226e996fd30SGreg Clayton }
227ecc11474SStephen Wilson 
228b9c1b51eSKate Stone void PlatformLinux::GetStatus(Stream &strm) {
2293be69dacSDaniel Malea   Platform::GetStatus(strm);
230ecc11474SStephen Wilson 
231b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
232b743a803SStephane Sezer   // Display local kernel information only when we are running in host mode.
233b743a803SStephane Sezer   // Otherwise, we would end up printing non-Linux information (when running
23417b45390SJason Molenda   // on Mac OS for example).
235b9c1b51eSKate Stone   if (IsHost()) {
236b2f1fb29SVirgile Bello     struct utsname un;
237b2f1fb29SVirgile Bello 
2383be69dacSDaniel Malea     if (uname(&un))
2393be69dacSDaniel Malea       return;
2403be69dacSDaniel Malea 
2413be69dacSDaniel Malea     strm.Printf("    Kernel: %s\n", un.sysname);
2423be69dacSDaniel Malea     strm.Printf("   Release: %s\n", un.release);
2433be69dacSDaniel Malea     strm.Printf("   Version: %s\n", un.version);
244b743a803SStephane Sezer   }
245b2f1fb29SVirgile Bello #endif
246ecc11474SStephen Wilson }
247ecc11474SStephen Wilson 
248348fb385STodd Fiala int32_t
249b9c1b51eSKate Stone PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
250348fb385STodd Fiala   int32_t resume_count = 0;
25128041352SGreg Clayton 
252348fb385STodd Fiala   // Always resume past the initial stop when we use eLaunchFlagDebug
253b9c1b51eSKate Stone   if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
254348fb385STodd Fiala     // Resume past the stop for the final exec into the true inferior.
255348fb385STodd Fiala     ++resume_count;
256348fb385STodd Fiala   }
257015d818bSTodd Fiala 
258348fb385STodd Fiala   // If we're not launching a shell, we're done.
25910687b0eSZachary Turner   const FileSpec &shell = launch_info.GetShell();
26010687b0eSZachary Turner   if (!shell)
261348fb385STodd Fiala     return resume_count;
262348fb385STodd Fiala 
26310687b0eSZachary Turner   std::string shell_string = shell.GetPath();
264348fb385STodd Fiala   // We're in a shell, so for sure we have to resume past the shell exec.
265348fb385STodd Fiala   ++resume_count;
266348fb385STodd Fiala 
267348fb385STodd Fiala   // Figure out what shell we're planning on using.
26810687b0eSZachary Turner   const char *shell_name = strrchr(shell_string.c_str(), '/');
269348fb385STodd Fiala   if (shell_name == NULL)
27010687b0eSZachary Turner     shell_name = shell_string.c_str();
27128041352SGreg Clayton   else
272348fb385STodd Fiala     shell_name++;
273348fb385STodd Fiala 
274b9c1b51eSKate Stone   if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
275b9c1b51eSKate Stone       strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
276348fb385STodd Fiala     // These shells seem to re-exec themselves.  Add another resume.
277348fb385STodd Fiala     ++resume_count;
278ecc11474SStephen Wilson   }
27913e8e1c3SJohnny Chen 
280348fb385STodd Fiala   return resume_count;
281348fb385STodd Fiala }
282348fb385STodd Fiala 
283b9c1b51eSKate Stone bool PlatformLinux::CanDebugProcess() {
284b9c1b51eSKate Stone   if (IsHost()) {
285b36f9178SPavel Labath     return true;
286b9c1b51eSKate Stone   } else {
287015d818bSTodd Fiala     // If we're connected, we can debug.
288015d818bSTodd Fiala     return IsConnected();
289015d818bSTodd Fiala   }
290348fb385STodd Fiala }
291015d818bSTodd Fiala 
292b9c1b51eSKate Stone // For local debugging, Linux will override the debug logic to use llgs-launch
293b9c1b51eSKate Stone // rather than
294b9c1b51eSKate Stone // lldb-launch, llgs-attach.  This differs from current lldb-launch,
295b9c1b51eSKate Stone // debugserver-attach
296348fb385STodd Fiala // approach on MacOSX.
29713e8e1c3SJohnny Chen lldb::ProcessSP
298b9c1b51eSKate Stone PlatformLinux::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
299b9c1b51eSKate Stone                             Target *target, // Can be NULL, if NULL create a new
300b9c1b51eSKate Stone                                             // target, else use existing one
301b9c1b51eSKate Stone                             Error &error) {
302db264a6dSTamas Berghammer   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
303348fb385STodd Fiala   if (log)
304b9c1b51eSKate Stone     log->Printf("PlatformLinux::%s entered (target %p)", __FUNCTION__,
305b9c1b51eSKate Stone                 static_cast<void *>(target));
30628041352SGreg Clayton 
307348fb385STodd Fiala   // If we're a remote host, use standard behavior from parent class.
308348fb385STodd Fiala   if (!IsHost())
3098012cadbSGreg Clayton     return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
310348fb385STodd Fiala 
311348fb385STodd Fiala   //
312b9c1b51eSKate Stone   // For local debugging, we'll insist on having ProcessGDBRemote create the
313b9c1b51eSKate Stone   // process.
314348fb385STodd Fiala   //
315348fb385STodd Fiala 
316348fb385STodd Fiala   ProcessSP process_sp;
317348fb385STodd Fiala 
318348fb385STodd Fiala   // Make sure we stop at the entry point
319348fb385STodd Fiala   launch_info.GetFlags().Set(eLaunchFlagDebug);
320348fb385STodd Fiala 
321348fb385STodd Fiala   // We always launch the process we are going to debug in a separate process
322348fb385STodd Fiala   // group, since then we can handle ^C interrupts ourselves w/o having to worry
323348fb385STodd Fiala   // about the target getting them as well.
324348fb385STodd Fiala   launch_info.SetLaunchInSeparateProcessGroup(true);
325348fb385STodd Fiala 
326348fb385STodd Fiala   // Ensure we have a target.
327b9c1b51eSKate Stone   if (target == nullptr) {
328348fb385STodd Fiala     if (log)
329348fb385STodd Fiala       log->Printf("PlatformLinux::%s creating new target", __FUNCTION__);
330348fb385STodd Fiala 
331348fb385STodd Fiala     TargetSP new_target_sp;
332a47464b2SZachary Turner     error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
333a47464b2SZachary Turner                                                   nullptr, new_target_sp);
334b9c1b51eSKate Stone     if (error.Fail()) {
335348fb385STodd Fiala       if (log)
336b9c1b51eSKate Stone         log->Printf("PlatformLinux::%s failed to create new target: %s",
337b9c1b51eSKate Stone                     __FUNCTION__, error.AsCString());
338348fb385STodd Fiala       return process_sp;
339348fb385STodd Fiala     }
340348fb385STodd Fiala 
34128041352SGreg Clayton     target = new_target_sp.get();
342b9c1b51eSKate Stone     if (!target) {
343348fb385STodd Fiala       error.SetErrorString("CreateTarget() returned nullptr");
344348fb385STodd Fiala       if (log)
345b9c1b51eSKate Stone         log->Printf("PlatformLinux::%s failed: %s", __FUNCTION__,
346b9c1b51eSKate Stone                     error.AsCString());
347348fb385STodd Fiala       return process_sp;
348348fb385STodd Fiala     }
349b9c1b51eSKate Stone   } else {
350348fb385STodd Fiala     if (log)
351348fb385STodd Fiala       log->Printf("PlatformLinux::%s using provided target", __FUNCTION__);
352348fb385STodd Fiala   }
353348fb385STodd Fiala 
354348fb385STodd Fiala   // Mark target as currently selected target.
35528041352SGreg Clayton   debugger.GetTargetList().SetSelectedTarget(target);
35628041352SGreg Clayton 
357348fb385STodd Fiala   // Now create the gdb-remote process.
358348fb385STodd Fiala   if (log)
359b9c1b51eSKate Stone     log->Printf(
360b9c1b51eSKate Stone         "PlatformLinux::%s having target create process with gdb-remote plugin",
361b9c1b51eSKate Stone         __FUNCTION__);
362b9c1b51eSKate Stone   process_sp = target->CreateProcess(
363b9c1b51eSKate Stone       launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
36428041352SGreg Clayton 
365b9c1b51eSKate Stone   if (!process_sp) {
366348fb385STodd Fiala     error.SetErrorString("CreateProcess() failed for gdb-remote process");
367348fb385STodd Fiala     if (log)
368b9c1b51eSKate Stone       log->Printf("PlatformLinux::%s failed: %s", __FUNCTION__,
369b9c1b51eSKate Stone                   error.AsCString());
370348fb385STodd Fiala     return process_sp;
371b9c1b51eSKate Stone   } else {
372348fb385STodd Fiala     if (log)
373b9c1b51eSKate Stone       log->Printf("PlatformLinux::%s successfully created process",
374b9c1b51eSKate Stone                   __FUNCTION__);
375348fb385STodd Fiala   }
376348fb385STodd Fiala 
377348fb385STodd Fiala   // Adjust launch for a hijacker.
378348fb385STodd Fiala   ListenerSP listener_sp;
379b9c1b51eSKate Stone   if (!launch_info.GetHijackListener()) {
380348fb385STodd Fiala     if (log)
381348fb385STodd Fiala       log->Printf("PlatformLinux::%s setting up hijacker", __FUNCTION__);
382348fb385STodd Fiala 
383b9c1b51eSKate Stone     listener_sp =
384b9c1b51eSKate Stone         Listener::MakeListener("lldb.PlatformLinux.DebugProcess.hijack");
385348fb385STodd Fiala     launch_info.SetHijackListener(listener_sp);
386583bbb1dSJim Ingham     process_sp->HijackProcessEvents(listener_sp);
387348fb385STodd Fiala   }
388348fb385STodd Fiala 
389348fb385STodd Fiala   // Log file actions.
390b9c1b51eSKate Stone   if (log) {
391b9c1b51eSKate Stone     log->Printf(
392b9c1b51eSKate Stone         "PlatformLinux::%s launching process with the following file actions:",
393b9c1b51eSKate Stone         __FUNCTION__);
394348fb385STodd Fiala 
395348fb385STodd Fiala     StreamString stream;
396348fb385STodd Fiala     size_t i = 0;
397348fb385STodd Fiala     const FileAction *file_action;
398b9c1b51eSKate Stone     while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) {
399348fb385STodd Fiala       file_action->Dump(stream);
400c156427dSZachary Turner       log->PutCString(stream.GetData());
401348fb385STodd Fiala       stream.Clear();
402348fb385STodd Fiala     }
403348fb385STodd Fiala   }
404348fb385STodd Fiala 
405348fb385STodd Fiala   // Do the launch.
406348fb385STodd Fiala   error = process_sp->Launch(launch_info);
407b9c1b51eSKate Stone   if (error.Success()) {
408348fb385STodd Fiala     // Handle the hijacking of process events.
409b9c1b51eSKate Stone     if (listener_sp) {
410b9c1b51eSKate Stone       const StateType state = process_sp->WaitForProcessToStop(
411e3e21cfcSPavel Labath           llvm::None, NULL, false, listener_sp);
412348fb385STodd Fiala 
413b9c1b51eSKate Stone       if (state == eStateStopped) {
414348fb385STodd Fiala         if (log)
415348fb385STodd Fiala           log->Printf("PlatformLinux::%s pid %" PRIu64 " state %s\n",
416348fb385STodd Fiala                       __FUNCTION__, process_sp->GetID(), StateAsCString(state));
417b9c1b51eSKate Stone       } else {
418348fb385STodd Fiala         if (log)
419b9c1b51eSKate Stone           log->Printf("PlatformLinux::%s pid %" PRIu64
420b9c1b51eSKate Stone                       " state is not stopped - %s\n",
421348fb385STodd Fiala                       __FUNCTION__, process_sp->GetID(), StateAsCString(state));
422348fb385STodd Fiala       }
423348fb385STodd Fiala     }
424348fb385STodd Fiala 
425b9c1b51eSKate Stone     // Hook up process PTY if we have one (which we should for local debugging
426b9c1b51eSKate Stone     // with llgs).
427348fb385STodd Fiala     int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
428b9c1b51eSKate Stone     if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) {
429348fb385STodd Fiala       process_sp->SetSTDIOFileDescriptor(pty_fd);
430348fb385STodd Fiala       if (log)
431b9c1b51eSKate Stone         log->Printf("PlatformLinux::%s pid %" PRIu64
432b9c1b51eSKate Stone                     " hooked up STDIO pty to process",
433b9c1b51eSKate Stone                     __FUNCTION__, process_sp->GetID());
434b9c1b51eSKate Stone     } else {
435348fb385STodd Fiala       if (log)
436b9c1b51eSKate Stone         log->Printf("PlatformLinux::%s pid %" PRIu64
437b9c1b51eSKate Stone                     " not using process STDIO pty",
438b9c1b51eSKate Stone                     __FUNCTION__, process_sp->GetID());
43928041352SGreg Clayton     }
440b9c1b51eSKate Stone   } else {
441348fb385STodd Fiala     if (log)
442b9c1b51eSKate Stone       log->Printf("PlatformLinux::%s process launch failed: %s", __FUNCTION__,
443b9c1b51eSKate Stone                   error.AsCString());
444b9c1b51eSKate Stone     // FIXME figure out appropriate cleanup here.  Do we delete the target? Do
445b9c1b51eSKate Stone     // we delete the process?  Does our caller do that?
44628041352SGreg Clayton   }
447348fb385STodd Fiala 
44828041352SGreg Clayton   return process_sp;
44913e8e1c3SJohnny Chen }
4502094dbf4SJason Molenda 
451b9c1b51eSKate Stone void PlatformLinux::CalculateTrapHandlerSymbolNames() {
4522094dbf4SJason Molenda   m_trap_handlers.push_back(ConstString("_sigtramp"));
4532094dbf4SJason Molenda }
454af245d11STodd Fiala 
455b9c1b51eSKate Stone uint64_t PlatformLinux::ConvertMmapFlagsToPlatform(const ArchSpec &arch,
456b9c1b51eSKate Stone                                                    unsigned flags) {
45796ad3de5SRobert Flack   uint64_t flags_platform = 0;
458e0d8c422SMohit K. Bhakkad   uint64_t map_anon = MAP_ANON;
459e0d8c422SMohit K. Bhakkad 
460e0d8c422SMohit K. Bhakkad   // To get correct flags for MIPS Architecture
461b9c1b51eSKate Stone   if (arch.GetTriple().getArch() == llvm::Triple::mips64 ||
462b9c1b51eSKate Stone       arch.GetTriple().getArch() == llvm::Triple::mips64el ||
463b9c1b51eSKate Stone       arch.GetTriple().getArch() == llvm::Triple::mips ||
464b9c1b51eSKate Stone       arch.GetTriple().getArch() == llvm::Triple::mipsel)
465e0d8c422SMohit K. Bhakkad     map_anon = 0x800;
466e0d8c422SMohit K. Bhakkad 
46796ad3de5SRobert Flack   if (flags & eMmapFlagsPrivate)
46896ad3de5SRobert Flack     flags_platform |= MAP_PRIVATE;
46996ad3de5SRobert Flack   if (flags & eMmapFlagsAnon)
470e0d8c422SMohit K. Bhakkad     flags_platform |= map_anon;
47196ad3de5SRobert Flack   return flags_platform;
47296ad3de5SRobert Flack }
4736e25aeeaSEnrico Granata 
474