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
222586e94bSJason Molenda #include "lldb/Breakpoint/BreakpointLocation.h"
2328041352SGreg Clayton #include "lldb/Core/Debugger.h"
24348fb385STodd Fiala #include "lldb/Core/Error.h"
25015d818bSTodd Fiala #include "lldb/Core/Log.h"
26e996fd30SGreg Clayton #include "lldb/Core/Module.h"
27e996fd30SGreg Clayton #include "lldb/Core/ModuleList.h"
281f746071SGreg Clayton #include "lldb/Core/ModuleSpec.h"
29ecc11474SStephen Wilson #include "lldb/Core/PluginManager.h"
30348fb385STodd Fiala #include "lldb/Core/State.h"
31e996fd30SGreg Clayton #include "lldb/Core/StreamString.h"
32e996fd30SGreg Clayton #include "lldb/Host/FileSpec.h"
3313b18261SZachary Turner #include "lldb/Host/HostInfo.h"
34348fb385STodd Fiala #include "lldb/Interpreter/OptionValueProperties.h"
35348fb385STodd Fiala #include "lldb/Interpreter/Property.h"
36e996fd30SGreg Clayton #include "lldb/Target/Process.h"
37b9c1b51eSKate Stone #include "lldb/Target/Target.h"
38e996fd30SGreg Clayton 
39a868c13cSBruce Mitchener // Define these constants from Linux mman.h for use when targeting
4096ad3de5SRobert Flack // remote linux systems even when host has different values.
4196ad3de5SRobert Flack #define MAP_PRIVATE 2
4296ad3de5SRobert Flack #define MAP_ANON 0x20
4396ad3de5SRobert Flack 
44e996fd30SGreg Clayton using namespace lldb;
45e996fd30SGreg Clayton using namespace lldb_private;
46db264a6dSTamas Berghammer using namespace lldb_private::platform_linux;
47e996fd30SGreg Clayton 
4828041352SGreg Clayton static uint32_t g_initialize_count = 0;
4928041352SGreg Clayton 
50281961a8STodd Fiala //------------------------------------------------------------------
51281961a8STodd Fiala /// Code to handle the PlatformLinux settings
52281961a8STodd Fiala //------------------------------------------------------------------
53281961a8STodd Fiala 
54b9c1b51eSKate Stone namespace {
55b9c1b51eSKate Stone class PlatformLinuxProperties : public Properties {
56db264a6dSTamas Berghammer public:
57db264a6dSTamas Berghammer   PlatformLinuxProperties();
58db264a6dSTamas Berghammer 
59222b937cSEugene Zelenko   ~PlatformLinuxProperties() override = default;
60222b937cSEugene Zelenko 
61b9c1b51eSKate Stone   static ConstString &GetSettingName();
62db264a6dSTamas Berghammer 
63db264a6dSTamas Berghammer private:
64b9c1b51eSKate Stone   static const PropertyDefinition *GetStaticPropertyDefinitions();
65db264a6dSTamas Berghammer };
66db264a6dSTamas Berghammer 
67db264a6dSTamas Berghammer typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP;
68db264a6dSTamas Berghammer 
69db264a6dSTamas Berghammer } // anonymous namespace
70db264a6dSTamas Berghammer 
71b9c1b51eSKate Stone PlatformLinuxProperties::PlatformLinuxProperties() : Properties() {
72db264a6dSTamas Berghammer   m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
73db264a6dSTamas Berghammer   m_collection_sp->Initialize(GetStaticPropertyDefinitions());
74db264a6dSTamas Berghammer }
75db264a6dSTamas Berghammer 
76b9c1b51eSKate Stone ConstString &PlatformLinuxProperties::GetSettingName() {
77db264a6dSTamas Berghammer   static ConstString g_setting_name("linux");
78db264a6dSTamas Berghammer   return g_setting_name;
79db264a6dSTamas Berghammer }
80db264a6dSTamas Berghammer 
81348fb385STodd Fiala const PropertyDefinition *
82b9c1b51eSKate Stone PlatformLinuxProperties::GetStaticPropertyDefinitions() {
83b9c1b51eSKate Stone   static PropertyDefinition g_properties[] = {
84b9c1b51eSKate Stone       {NULL, OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL}};
85281961a8STodd Fiala 
86348fb385STodd Fiala   return g_properties;
87348fb385STodd Fiala }
88281961a8STodd Fiala 
89b9c1b51eSKate Stone static const PlatformLinuxPropertiesSP &GetGlobalProperties() {
90281961a8STodd Fiala   static PlatformLinuxPropertiesSP g_settings_sp;
91281961a8STodd Fiala   if (!g_settings_sp)
92281961a8STodd Fiala     g_settings_sp.reset(new PlatformLinuxProperties());
93281961a8STodd Fiala   return g_settings_sp;
94281961a8STodd Fiala }
95281961a8STodd Fiala 
96b9c1b51eSKate Stone void PlatformLinux::DebuggerInitialize(Debugger &debugger) {
97b9c1b51eSKate Stone   if (!PluginManager::GetSettingForPlatformPlugin(
98b9c1b51eSKate Stone           debugger, PlatformLinuxProperties::GetSettingName())) {
99281961a8STodd Fiala     const bool is_global_setting = true;
100b9c1b51eSKate Stone     PluginManager::CreateSettingForPlatformPlugin(
101b9c1b51eSKate Stone         debugger, GetGlobalProperties()->GetValueProperties(),
102281961a8STodd Fiala         ConstString("Properties for the PlatformLinux plug-in."),
103281961a8STodd Fiala         is_global_setting);
104281961a8STodd Fiala   }
105281961a8STodd Fiala }
106281961a8STodd Fiala 
107281961a8STodd Fiala //------------------------------------------------------------------
108281961a8STodd Fiala 
109b9c1b51eSKate Stone PlatformSP PlatformLinux::CreateInstance(bool force, const ArchSpec *arch) {
110db264a6dSTamas Berghammer   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
111b9c1b51eSKate Stone   if (log) {
112015d818bSTodd Fiala     const char *arch_name;
113015d818bSTodd Fiala     if (arch && arch->GetArchitectureName())
114015d818bSTodd Fiala       arch_name = arch->GetArchitectureName();
115015d818bSTodd Fiala     else
116015d818bSTodd Fiala       arch_name = "<null>";
117015d818bSTodd Fiala 
118b9c1b51eSKate Stone     const char *triple_cstr =
119b9c1b51eSKate Stone         arch ? arch->GetTriple().getTriple().c_str() : "<null>";
120015d818bSTodd Fiala 
121b9c1b51eSKate Stone     log->Printf("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__,
122b9c1b51eSKate Stone                 force ? "true" : "false", arch_name, triple_cstr);
123015d818bSTodd Fiala   }
124015d818bSTodd Fiala 
125b3a40ba8SGreg Clayton   bool create = force;
126b9c1b51eSKate Stone   if (create == false && arch && arch->IsValid()) {
127b3a40ba8SGreg Clayton     const llvm::Triple &triple = arch->GetTriple();
128b9c1b51eSKate Stone     switch (triple.getOS()) {
12970512317SGreg Clayton     case llvm::Triple::Linux:
130b1da257aSTed Woodward       create = true;
13170512317SGreg Clayton       break;
13270512317SGreg Clayton 
133dbc6c0bbSGreg Clayton #if defined(__linux__)
134dbc6c0bbSGreg Clayton     // Only accept "unknown" for the OS if the host is linux and
1356a7f3338SBruce Mitchener     // it "unknown" wasn't specified (it was just returned because it
136dbc6c0bbSGreg Clayton     // was NOT specified)
137015d818bSTodd Fiala     case llvm::Triple::OSType::UnknownOS:
13870512317SGreg Clayton       create = !arch->TripleOSWasSpecified();
13970512317SGreg Clayton       break;
140dbc6c0bbSGreg Clayton #endif
14170512317SGreg Clayton     default:
14270512317SGreg Clayton       break;
14370512317SGreg Clayton     }
14470512317SGreg Clayton   }
145015d818bSTodd Fiala 
146b9c1b51eSKate Stone   if (create) {
147015d818bSTodd Fiala     if (log)
148b9c1b51eSKate Stone       log->Printf("PlatformLinux::%s() creating remote-linux platform",
149b9c1b51eSKate Stone                   __FUNCTION__);
150615eb7e6SGreg Clayton     return PlatformSP(new PlatformLinux(false));
151015d818bSTodd Fiala   }
152015d818bSTodd Fiala 
153015d818bSTodd Fiala   if (log)
154b9c1b51eSKate Stone     log->Printf(
155b9c1b51eSKate Stone         "PlatformLinux::%s() aborting creation of remote-linux platform",
156b9c1b51eSKate Stone         __FUNCTION__);
157015d818bSTodd Fiala 
158615eb7e6SGreg Clayton   return PlatformSP();
159ecc11474SStephen Wilson }
160ecc11474SStephen Wilson 
161b9c1b51eSKate Stone ConstString PlatformLinux::GetPluginNameStatic(bool is_host) {
162b9c1b51eSKate Stone   if (is_host) {
16357abc5d6SGreg Clayton     static ConstString g_host_name(Platform::GetHostPlatformName());
16457abc5d6SGreg Clayton     return g_host_name;
165b9c1b51eSKate Stone   } else {
16657abc5d6SGreg Clayton     static ConstString g_remote_name("remote-linux");
16757abc5d6SGreg Clayton     return g_remote_name;
16857abc5d6SGreg Clayton   }
16928041352SGreg Clayton }
17028041352SGreg Clayton 
171b9c1b51eSKate Stone const char *PlatformLinux::GetPluginDescriptionStatic(bool is_host) {
17228041352SGreg Clayton   if (is_host)
17328041352SGreg Clayton     return "Local Linux user platform plug-in.";
17428041352SGreg Clayton   else
17528041352SGreg Clayton     return "Remote Linux user platform plug-in.";
176ecc11474SStephen Wilson }
177ecc11474SStephen Wilson 
178b9c1b51eSKate Stone ConstString PlatformLinux::GetPluginName() {
17957abc5d6SGreg Clayton   return GetPluginNameStatic(IsHost());
18057abc5d6SGreg Clayton }
18157abc5d6SGreg Clayton 
182b9c1b51eSKate Stone void PlatformLinux::Initialize() {
1833c4f89d7STamas Berghammer   PlatformPOSIX::Initialize();
1843c4f89d7STamas Berghammer 
185b9c1b51eSKate Stone   if (g_initialize_count++ == 0) {
1861c6a1ea9STamas Berghammer #if defined(__linux__) && !defined(__ANDROID__)
18728041352SGreg Clayton     PlatformSP default_platform_sp(new PlatformLinux(true));
18813b18261SZachary Turner     default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
189615eb7e6SGreg Clayton     Platform::SetHostPlatform(default_platform_sp);
19028041352SGreg Clayton #endif
191b9c1b51eSKate Stone     PluginManager::RegisterPlugin(
192b9c1b51eSKate Stone         PlatformLinux::GetPluginNameStatic(false),
19328041352SGreg Clayton         PlatformLinux::GetPluginDescriptionStatic(false),
194b9c1b51eSKate Stone         PlatformLinux::CreateInstance, PlatformLinux::DebuggerInitialize);
195ecc11474SStephen Wilson   }
196e996fd30SGreg Clayton }
197e996fd30SGreg Clayton 
198b9c1b51eSKate Stone void PlatformLinux::Terminate() {
199b9c1b51eSKate Stone   if (g_initialize_count > 0) {
200b9c1b51eSKate Stone     if (--g_initialize_count == 0) {
20128041352SGreg Clayton       PluginManager::UnregisterPlugin(PlatformLinux::CreateInstance);
202e996fd30SGreg Clayton     }
20328041352SGreg Clayton   }
2043c4f89d7STamas Berghammer 
2053c4f89d7STamas Berghammer   PlatformPOSIX::Terminate();
20628041352SGreg Clayton }
207e996fd30SGreg Clayton 
208b9c1b51eSKate Stone Error PlatformLinux::ResolveExecutable(
209b9c1b51eSKate Stone     const ModuleSpec &ms, lldb::ModuleSP &exe_module_sp,
210b9c1b51eSKate Stone     const FileSpecList *module_search_paths_ptr) {
211e996fd30SGreg Clayton   Error error;
212e996fd30SGreg Clayton   // Nothing special to do here, just use the actual file and architecture
213e996fd30SGreg Clayton 
21428041352SGreg Clayton   char exe_path[PATH_MAX];
2158012cadbSGreg Clayton   ModuleSpec resolved_module_spec(ms);
216e996fd30SGreg Clayton 
217b9c1b51eSKate Stone   if (IsHost()) {
21828041352SGreg Clayton     // If we have "ls" as the exe_file, resolve the executable location based on
219e996fd30SGreg Clayton     // the current path variables
220b9c1b51eSKate Stone     if (!resolved_module_spec.GetFileSpec().Exists()) {
2218012cadbSGreg Clayton       resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path));
2228012cadbSGreg Clayton       resolved_module_spec.GetFileSpec().SetFile(exe_path, true);
22328041352SGreg Clayton     }
22428041352SGreg Clayton 
2258012cadbSGreg Clayton     if (!resolved_module_spec.GetFileSpec().Exists())
2268012cadbSGreg Clayton       resolved_module_spec.GetFileSpec().ResolveExecutableLocation();
227e996fd30SGreg Clayton 
2288012cadbSGreg Clayton     if (resolved_module_spec.GetFileSpec().Exists())
22928041352SGreg Clayton       error.Clear();
230b9c1b51eSKate Stone     else {
231b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
232b9c1b51eSKate Stone           "unable to find executable for '%s'",
233b9c1b51eSKate Stone           resolved_module_spec.GetFileSpec().GetPath().c_str());
23428041352SGreg Clayton     }
235b9c1b51eSKate Stone   } else {
236b9c1b51eSKate Stone     if (m_remote_platform_sp) {
237b9c1b51eSKate Stone       error =
238b9c1b51eSKate Stone           GetCachedExecutable(resolved_module_spec, exe_module_sp,
239b9c1b51eSKate Stone                               module_search_paths_ptr, *m_remote_platform_sp);
240b9c1b51eSKate Stone     } else {
241b9c1b51eSKate Stone       // We may connect to a process and use the provided executable (Don't use
242b9c1b51eSKate Stone       // local $PATH).
243e996fd30SGreg Clayton 
2448012cadbSGreg Clayton       if (resolved_module_spec.GetFileSpec().Exists())
24528041352SGreg Clayton         error.Clear();
24628041352SGreg Clayton       else
247b9c1b51eSKate Stone         error.SetErrorStringWithFormat("the platform is not currently "
248b9c1b51eSKate Stone                                        "connected, and '%s' doesn't exist in "
249b9c1b51eSKate Stone                                        "the system root.",
250b9c1b51eSKate Stone                                        exe_path);
25128041352SGreg Clayton     }
25228041352SGreg Clayton   }
25328041352SGreg Clayton 
254b9c1b51eSKate Stone   if (error.Success()) {
255b9c1b51eSKate Stone     if (resolved_module_spec.GetArchitecture().IsValid()) {
256b9c1b51eSKate Stone       error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
257b9c1b51eSKate Stone                                           NULL, NULL, NULL);
258b9c1b51eSKate Stone       if (error.Fail()) {
259b9c1b51eSKate Stone         // If we failed, it may be because the vendor and os aren't known. If
260b9c1b51eSKate Stone         // that is the
261b9c1b51eSKate Stone         // case, try setting them to the host architecture and give it another
262b9c1b51eSKate Stone         // try.
263b9c1b51eSKate Stone         llvm::Triple &module_triple =
264b9c1b51eSKate Stone             resolved_module_spec.GetArchitecture().GetTriple();
265b9c1b51eSKate Stone         bool is_vendor_specified =
266b9c1b51eSKate Stone             (module_triple.getVendor() != llvm::Triple::UnknownVendor);
267b9c1b51eSKate Stone         bool is_os_specified =
268b9c1b51eSKate Stone             (module_triple.getOS() != llvm::Triple::UnknownOS);
269b9c1b51eSKate Stone         if (!is_vendor_specified || !is_os_specified) {
270b9c1b51eSKate Stone           const llvm::Triple &host_triple =
271b9c1b51eSKate Stone               HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple();
2729f0013d8SMichael Sartain 
2739f0013d8SMichael Sartain           if (!is_vendor_specified)
2749f0013d8SMichael Sartain             module_triple.setVendorName(host_triple.getVendorName());
2759f0013d8SMichael Sartain           if (!is_os_specified)
2769f0013d8SMichael Sartain             module_triple.setOSName(host_triple.getOSName());
2779f0013d8SMichael Sartain 
2788012cadbSGreg Clayton           error = ModuleList::GetSharedModule(resolved_module_spec,
279b9c1b51eSKate Stone                                               exe_module_sp, NULL, NULL, NULL);
2809f0013d8SMichael Sartain         }
2819f0013d8SMichael Sartain       }
282e996fd30SGreg Clayton 
283e635db49SSean Callanan       // TODO find out why exe_module_sp might be NULL
284b9c1b51eSKate Stone       if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL) {
285e996fd30SGreg Clayton         exe_module_sp.reset();
286b9c1b51eSKate Stone         error.SetErrorStringWithFormat(
287b9c1b51eSKate Stone             "'%s' doesn't contain the architecture %s",
2888012cadbSGreg Clayton             resolved_module_spec.GetFileSpec().GetPath().c_str(),
2898012cadbSGreg Clayton             resolved_module_spec.GetArchitecture().GetArchitectureName());
290e996fd30SGreg Clayton       }
291b9c1b51eSKate Stone     } else {
292e996fd30SGreg Clayton       // No valid architecture was specified, ask the platform for
293e996fd30SGreg Clayton       // the architectures that we should be using (in the correct order)
294e996fd30SGreg Clayton       // and see if we can find a match that way
295e996fd30SGreg Clayton       StreamString arch_names;
296b9c1b51eSKate Stone       for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
297b9c1b51eSKate Stone                idx, resolved_module_spec.GetArchitecture());
298b9c1b51eSKate Stone            ++idx) {
299b9c1b51eSKate Stone         error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
300b9c1b51eSKate Stone                                             NULL, NULL, NULL);
301e996fd30SGreg Clayton         // Did we find an executable using one of the
302b9c1b51eSKate Stone         if (error.Success()) {
303e996fd30SGreg Clayton           if (exe_module_sp && exe_module_sp->GetObjectFile())
304e996fd30SGreg Clayton             break;
305e996fd30SGreg Clayton           else
306e996fd30SGreg Clayton             error.SetErrorToGenericError();
307e996fd30SGreg Clayton         }
308e996fd30SGreg Clayton 
309e996fd30SGreg Clayton         if (idx > 0)
310e996fd30SGreg Clayton           arch_names.PutCString(", ");
311b9c1b51eSKate Stone         arch_names.PutCString(
312b9c1b51eSKate Stone             resolved_module_spec.GetArchitecture().GetArchitectureName());
313e996fd30SGreg Clayton       }
314e996fd30SGreg Clayton 
315b9c1b51eSKate Stone       if (error.Fail() || !exe_module_sp) {
316b9c1b51eSKate Stone         if (resolved_module_spec.GetFileSpec().Readable()) {
317b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
318b9c1b51eSKate Stone               "'%s' doesn't contain any '%s' platform architectures: %s",
3198012cadbSGreg Clayton               resolved_module_spec.GetFileSpec().GetPath().c_str(),
320c156427dSZachary Turner               GetPluginName().GetCString(), arch_names.GetData());
321b9c1b51eSKate Stone         } else {
322b9c1b51eSKate Stone           error.SetErrorStringWithFormat(
323b9c1b51eSKate Stone               "'%s' is not readable",
324b9c1b51eSKate Stone               resolved_module_spec.GetFileSpec().GetPath().c_str());
32539945dccSGreg Clayton         }
32639945dccSGreg Clayton       }
327e996fd30SGreg Clayton     }
328e996fd30SGreg Clayton   }
329e996fd30SGreg Clayton 
330e996fd30SGreg Clayton   return error;
331e996fd30SGreg Clayton }
332e996fd30SGreg Clayton 
333b9c1b51eSKate Stone Error PlatformLinux::GetFileWithUUID(const FileSpec &platform_file,
334b9c1b51eSKate Stone                                      const UUID *uuid_ptr,
335b9c1b51eSKate Stone                                      FileSpec &local_file) {
336b9c1b51eSKate Stone   if (IsRemote()) {
33728041352SGreg Clayton     if (m_remote_platform_sp)
338b9c1b51eSKate Stone       return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr,
339b9c1b51eSKate Stone                                                    local_file);
34028041352SGreg Clayton   }
34128041352SGreg Clayton 
342e996fd30SGreg Clayton   // Default to the local case
343e996fd30SGreg Clayton   local_file = platform_file;
344c1edf566SMehdi Amini   return Error();
345e996fd30SGreg Clayton }
346e996fd30SGreg Clayton 
347e996fd30SGreg Clayton //------------------------------------------------------------------
348e996fd30SGreg Clayton /// Default Constructor
349e996fd30SGreg Clayton //------------------------------------------------------------------
350b9c1b51eSKate Stone PlatformLinux::PlatformLinux(bool is_host)
351b9c1b51eSKate Stone     : PlatformPOSIX(is_host) // This is the local host platform
352b9c1b51eSKate Stone {}
353e996fd30SGreg Clayton 
354e996fd30SGreg Clayton //------------------------------------------------------------------
355e996fd30SGreg Clayton /// Destructor.
356e996fd30SGreg Clayton ///
357e996fd30SGreg Clayton /// The destructor is virtual since this class is designed to be
358e996fd30SGreg Clayton /// inherited from by the plug-in instance.
359e996fd30SGreg Clayton //------------------------------------------------------------------
360222b937cSEugene Zelenko PlatformLinux::~PlatformLinux() = default;
361e996fd30SGreg Clayton 
362b9c1b51eSKate Stone bool PlatformLinux::GetProcessInfo(lldb::pid_t pid,
363b9c1b51eSKate Stone                                    ProcessInstanceInfo &process_info) {
36428041352SGreg Clayton   bool success = false;
365b9c1b51eSKate Stone   if (IsHost()) {
36628041352SGreg Clayton     success = Platform::GetProcessInfo(pid, process_info);
367b9c1b51eSKate Stone   } else {
36828041352SGreg Clayton     if (m_remote_platform_sp)
36928041352SGreg Clayton       success = m_remote_platform_sp->GetProcessInfo(pid, process_info);
37028041352SGreg Clayton   }
37128041352SGreg Clayton   return success;
372e996fd30SGreg Clayton }
373e996fd30SGreg Clayton 
3748e6ec453SStephane Sezer uint32_t
3758e6ec453SStephane Sezer PlatformLinux::FindProcesses(const ProcessInstanceInfoMatch &match_info,
376b9c1b51eSKate Stone                              ProcessInstanceInfoList &process_infos) {
3778e6ec453SStephane Sezer   uint32_t match_count = 0;
378b9c1b51eSKate Stone   if (IsHost()) {
3798e6ec453SStephane Sezer     // Let the base class figure out the host details
3808e6ec453SStephane Sezer     match_count = Platform::FindProcesses(match_info, process_infos);
381b9c1b51eSKate Stone   } else {
3828e6ec453SStephane Sezer     // If we are remote, we can only return results if we are connected
3838e6ec453SStephane Sezer     if (m_remote_platform_sp)
384b9c1b51eSKate Stone       match_count =
385b9c1b51eSKate Stone           m_remote_platform_sp->FindProcesses(match_info, process_infos);
3868e6ec453SStephane Sezer   }
3878e6ec453SStephane Sezer   return match_count;
3888e6ec453SStephane Sezer }
3898e6ec453SStephane Sezer 
390b9c1b51eSKate Stone bool PlatformLinux::GetSupportedArchitectureAtIndex(uint32_t idx,
391b9c1b51eSKate Stone                                                     ArchSpec &arch) {
392b9c1b51eSKate Stone   if (IsHost()) {
393e49b8e06SRobert Flack     ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
394b9c1b51eSKate Stone     if (hostArch.GetTriple().isOSLinux()) {
395b9c1b51eSKate Stone       if (idx == 0) {
396e49b8e06SRobert Flack         arch = hostArch;
397e49b8e06SRobert Flack         return arch.IsValid();
398b9c1b51eSKate Stone       } else if (idx == 1) {
399e49b8e06SRobert Flack         // If the default host architecture is 64-bit, look for a 32-bit variant
400b9c1b51eSKate Stone         if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) {
401e49b8e06SRobert Flack           arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
402e49b8e06SRobert Flack           return arch.IsValid();
403e49b8e06SRobert Flack         }
404e49b8e06SRobert Flack       }
405e49b8e06SRobert Flack     }
406b9c1b51eSKate Stone   } else {
407e49b8e06SRobert Flack     if (m_remote_platform_sp)
408e49b8e06SRobert Flack       return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch);
409bb1e283cSTed Woodward 
410bb1e283cSTed Woodward     llvm::Triple triple;
411bb1e283cSTed Woodward     // Set the OS to linux
412bb1e283cSTed Woodward     triple.setOS(llvm::Triple::Linux);
413bb1e283cSTed Woodward     // Set the architecture
414b9c1b51eSKate Stone     switch (idx) {
415b9c1b51eSKate Stone     case 0:
416b9c1b51eSKate Stone       triple.setArchName("x86_64");
417b9c1b51eSKate Stone       break;
418b9c1b51eSKate Stone     case 1:
419b9c1b51eSKate Stone       triple.setArchName("i386");
420b9c1b51eSKate Stone       break;
421b9c1b51eSKate Stone     case 2:
422b9c1b51eSKate Stone       triple.setArchName("arm");
423b9c1b51eSKate Stone       break;
424b9c1b51eSKate Stone     case 3:
425b9c1b51eSKate Stone       triple.setArchName("aarch64");
426b9c1b51eSKate Stone       break;
427b9c1b51eSKate Stone     case 4:
428b9c1b51eSKate Stone       triple.setArchName("mips64");
429b9c1b51eSKate Stone       break;
430b9c1b51eSKate Stone     case 5:
431b9c1b51eSKate Stone       triple.setArchName("hexagon");
432b9c1b51eSKate Stone       break;
433b9c1b51eSKate Stone     case 6:
434b9c1b51eSKate Stone       triple.setArchName("mips");
435b9c1b51eSKate Stone       break;
436b9c1b51eSKate Stone     case 7:
437b9c1b51eSKate Stone       triple.setArchName("mips64el");
438b9c1b51eSKate Stone       break;
439b9c1b51eSKate Stone     case 8:
440b9c1b51eSKate Stone       triple.setArchName("mipsel");
441b9c1b51eSKate Stone       break;
442b9c1b51eSKate Stone     case 9:
443b9c1b51eSKate Stone       triple.setArchName("s390x");
444b9c1b51eSKate Stone       break;
445b9c1b51eSKate Stone     default:
446b9c1b51eSKate Stone       return false;
447bb1e283cSTed Woodward     }
448b9c1b51eSKate Stone     // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the
449b9c1b51eSKate Stone     // vendor by
450b9c1b51eSKate Stone     // calling triple.SetVendorName("unknown") so that it is a "unspecified
451b9c1b51eSKate Stone     // unknown".
452b9c1b51eSKate Stone     // This means when someone calls triple.GetVendorName() it will return an
453b9c1b51eSKate Stone     // empty string
454b9c1b51eSKate Stone     // which indicates that the vendor can be set when two architectures are
455b9c1b51eSKate Stone     // merged
456bb1e283cSTed Woodward 
457bb1e283cSTed Woodward     // Now set the triple into "arch" and return true
458bb1e283cSTed Woodward     arch.SetTriple(triple);
459bb1e283cSTed Woodward     return true;
460e49b8e06SRobert Flack   }
461e996fd30SGreg Clayton   return false;
462e996fd30SGreg Clayton }
463ecc11474SStephen Wilson 
464b9c1b51eSKate Stone void PlatformLinux::GetStatus(Stream &strm) {
4653be69dacSDaniel Malea   Platform::GetStatus(strm);
466ecc11474SStephen Wilson 
467b2f1fb29SVirgile Bello #ifndef LLDB_DISABLE_POSIX
468b743a803SStephane Sezer   // Display local kernel information only when we are running in host mode.
469b743a803SStephane Sezer   // Otherwise, we would end up printing non-Linux information (when running
47017b45390SJason Molenda   // on Mac OS for example).
471b9c1b51eSKate Stone   if (IsHost()) {
472b2f1fb29SVirgile Bello     struct utsname un;
473b2f1fb29SVirgile Bello 
4743be69dacSDaniel Malea     if (uname(&un))
4753be69dacSDaniel Malea       return;
4763be69dacSDaniel Malea 
4773be69dacSDaniel Malea     strm.Printf("    Kernel: %s\n", un.sysname);
4783be69dacSDaniel Malea     strm.Printf("   Release: %s\n", un.release);
4793be69dacSDaniel Malea     strm.Printf("   Version: %s\n", un.version);
480b743a803SStephane Sezer   }
481b2f1fb29SVirgile Bello #endif
482ecc11474SStephen Wilson }
483ecc11474SStephen Wilson 
484348fb385STodd Fiala int32_t
485b9c1b51eSKate Stone PlatformLinux::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
486348fb385STodd Fiala   int32_t resume_count = 0;
48728041352SGreg Clayton 
488348fb385STodd Fiala   // Always resume past the initial stop when we use eLaunchFlagDebug
489b9c1b51eSKate Stone   if (launch_info.GetFlags().Test(eLaunchFlagDebug)) {
490348fb385STodd Fiala     // Resume past the stop for the final exec into the true inferior.
491348fb385STodd Fiala     ++resume_count;
492348fb385STodd Fiala   }
493015d818bSTodd Fiala 
494348fb385STodd Fiala   // If we're not launching a shell, we're done.
49510687b0eSZachary Turner   const FileSpec &shell = launch_info.GetShell();
49610687b0eSZachary Turner   if (!shell)
497348fb385STodd Fiala     return resume_count;
498348fb385STodd Fiala 
49910687b0eSZachary Turner   std::string shell_string = shell.GetPath();
500348fb385STodd Fiala   // We're in a shell, so for sure we have to resume past the shell exec.
501348fb385STodd Fiala   ++resume_count;
502348fb385STodd Fiala 
503348fb385STodd Fiala   // Figure out what shell we're planning on using.
50410687b0eSZachary Turner   const char *shell_name = strrchr(shell_string.c_str(), '/');
505348fb385STodd Fiala   if (shell_name == NULL)
50610687b0eSZachary Turner     shell_name = shell_string.c_str();
50728041352SGreg Clayton   else
508348fb385STodd Fiala     shell_name++;
509348fb385STodd Fiala 
510b9c1b51eSKate Stone   if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 ||
511b9c1b51eSKate Stone       strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) {
512348fb385STodd Fiala     // These shells seem to re-exec themselves.  Add another resume.
513348fb385STodd Fiala     ++resume_count;
514ecc11474SStephen Wilson   }
51513e8e1c3SJohnny Chen 
516348fb385STodd Fiala   return resume_count;
517348fb385STodd Fiala }
518348fb385STodd Fiala 
519b9c1b51eSKate Stone bool PlatformLinux::CanDebugProcess() {
520b9c1b51eSKate Stone   if (IsHost()) {
521b36f9178SPavel Labath     return true;
522b9c1b51eSKate Stone   } else {
523015d818bSTodd Fiala     // If we're connected, we can debug.
524015d818bSTodd Fiala     return IsConnected();
525015d818bSTodd Fiala   }
526348fb385STodd Fiala }
527015d818bSTodd Fiala 
528b9c1b51eSKate Stone // For local debugging, Linux will override the debug logic to use llgs-launch
529b9c1b51eSKate Stone // rather than
530b9c1b51eSKate Stone // lldb-launch, llgs-attach.  This differs from current lldb-launch,
531b9c1b51eSKate Stone // debugserver-attach
532348fb385STodd Fiala // approach on MacOSX.
53313e8e1c3SJohnny Chen lldb::ProcessSP
534b9c1b51eSKate Stone PlatformLinux::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger,
535b9c1b51eSKate Stone                             Target *target, // Can be NULL, if NULL create a new
536b9c1b51eSKate Stone                                             // target, else use existing one
537b9c1b51eSKate Stone                             Error &error) {
538db264a6dSTamas Berghammer   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
539348fb385STodd Fiala   if (log)
540b9c1b51eSKate Stone     log->Printf("PlatformLinux::%s entered (target %p)", __FUNCTION__,
541b9c1b51eSKate Stone                 static_cast<void *>(target));
54228041352SGreg Clayton 
543348fb385STodd Fiala   // If we're a remote host, use standard behavior from parent class.
544348fb385STodd Fiala   if (!IsHost())
5458012cadbSGreg Clayton     return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error);
546348fb385STodd Fiala 
547348fb385STodd Fiala   //
548b9c1b51eSKate Stone   // For local debugging, we'll insist on having ProcessGDBRemote create the
549b9c1b51eSKate Stone   // process.
550348fb385STodd Fiala   //
551348fb385STodd Fiala 
552348fb385STodd Fiala   ProcessSP process_sp;
553348fb385STodd Fiala 
554348fb385STodd Fiala   // Make sure we stop at the entry point
555348fb385STodd Fiala   launch_info.GetFlags().Set(eLaunchFlagDebug);
556348fb385STodd Fiala 
557348fb385STodd Fiala   // We always launch the process we are going to debug in a separate process
558348fb385STodd Fiala   // group, since then we can handle ^C interrupts ourselves w/o having to worry
559348fb385STodd Fiala   // about the target getting them as well.
560348fb385STodd Fiala   launch_info.SetLaunchInSeparateProcessGroup(true);
561348fb385STodd Fiala 
562348fb385STodd Fiala   // Ensure we have a target.
563b9c1b51eSKate Stone   if (target == nullptr) {
564348fb385STodd Fiala     if (log)
565348fb385STodd Fiala       log->Printf("PlatformLinux::%s creating new target", __FUNCTION__);
566348fb385STodd Fiala 
567348fb385STodd Fiala     TargetSP new_target_sp;
568a47464b2SZachary Turner     error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
569a47464b2SZachary Turner                                                   nullptr, new_target_sp);
570b9c1b51eSKate Stone     if (error.Fail()) {
571348fb385STodd Fiala       if (log)
572b9c1b51eSKate Stone         log->Printf("PlatformLinux::%s failed to create new target: %s",
573b9c1b51eSKate Stone                     __FUNCTION__, error.AsCString());
574348fb385STodd Fiala       return process_sp;
575348fb385STodd Fiala     }
576348fb385STodd Fiala 
57728041352SGreg Clayton     target = new_target_sp.get();
578b9c1b51eSKate Stone     if (!target) {
579348fb385STodd Fiala       error.SetErrorString("CreateTarget() returned nullptr");
580348fb385STodd Fiala       if (log)
581b9c1b51eSKate Stone         log->Printf("PlatformLinux::%s failed: %s", __FUNCTION__,
582b9c1b51eSKate Stone                     error.AsCString());
583348fb385STodd Fiala       return process_sp;
584348fb385STodd Fiala     }
585b9c1b51eSKate Stone   } else {
586348fb385STodd Fiala     if (log)
587348fb385STodd Fiala       log->Printf("PlatformLinux::%s using provided target", __FUNCTION__);
588348fb385STodd Fiala   }
589348fb385STodd Fiala 
590348fb385STodd Fiala   // Mark target as currently selected target.
59128041352SGreg Clayton   debugger.GetTargetList().SetSelectedTarget(target);
59228041352SGreg Clayton 
593348fb385STodd Fiala   // Now create the gdb-remote process.
594348fb385STodd Fiala   if (log)
595b9c1b51eSKate Stone     log->Printf(
596b9c1b51eSKate Stone         "PlatformLinux::%s having target create process with gdb-remote plugin",
597b9c1b51eSKate Stone         __FUNCTION__);
598b9c1b51eSKate Stone   process_sp = target->CreateProcess(
599b9c1b51eSKate Stone       launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr);
60028041352SGreg Clayton 
601b9c1b51eSKate Stone   if (!process_sp) {
602348fb385STodd Fiala     error.SetErrorString("CreateProcess() failed for gdb-remote process");
603348fb385STodd Fiala     if (log)
604b9c1b51eSKate Stone       log->Printf("PlatformLinux::%s failed: %s", __FUNCTION__,
605b9c1b51eSKate Stone                   error.AsCString());
606348fb385STodd Fiala     return process_sp;
607b9c1b51eSKate Stone   } else {
608348fb385STodd Fiala     if (log)
609b9c1b51eSKate Stone       log->Printf("PlatformLinux::%s successfully created process",
610b9c1b51eSKate Stone                   __FUNCTION__);
611348fb385STodd Fiala   }
612348fb385STodd Fiala 
613348fb385STodd Fiala   // Adjust launch for a hijacker.
614348fb385STodd Fiala   ListenerSP listener_sp;
615b9c1b51eSKate Stone   if (!launch_info.GetHijackListener()) {
616348fb385STodd Fiala     if (log)
617348fb385STodd Fiala       log->Printf("PlatformLinux::%s setting up hijacker", __FUNCTION__);
618348fb385STodd Fiala 
619b9c1b51eSKate Stone     listener_sp =
620b9c1b51eSKate Stone         Listener::MakeListener("lldb.PlatformLinux.DebugProcess.hijack");
621348fb385STodd Fiala     launch_info.SetHijackListener(listener_sp);
622583bbb1dSJim Ingham     process_sp->HijackProcessEvents(listener_sp);
623348fb385STodd Fiala   }
624348fb385STodd Fiala 
625348fb385STodd Fiala   // Log file actions.
626b9c1b51eSKate Stone   if (log) {
627b9c1b51eSKate Stone     log->Printf(
628b9c1b51eSKate Stone         "PlatformLinux::%s launching process with the following file actions:",
629b9c1b51eSKate Stone         __FUNCTION__);
630348fb385STodd Fiala 
631348fb385STodd Fiala     StreamString stream;
632348fb385STodd Fiala     size_t i = 0;
633348fb385STodd Fiala     const FileAction *file_action;
634b9c1b51eSKate Stone     while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) {
635348fb385STodd Fiala       file_action->Dump(stream);
636c156427dSZachary Turner       log->PutCString(stream.GetData());
637348fb385STodd Fiala       stream.Clear();
638348fb385STodd Fiala     }
639348fb385STodd Fiala   }
640348fb385STodd Fiala 
641348fb385STodd Fiala   // Do the launch.
642348fb385STodd Fiala   error = process_sp->Launch(launch_info);
643b9c1b51eSKate Stone   if (error.Success()) {
644348fb385STodd Fiala     // Handle the hijacking of process events.
645b9c1b51eSKate Stone     if (listener_sp) {
646b9c1b51eSKate Stone       const StateType state = process_sp->WaitForProcessToStop(
647*e3e21cfcSPavel Labath           llvm::None, NULL, false, listener_sp);
648348fb385STodd Fiala 
649b9c1b51eSKate Stone       if (state == eStateStopped) {
650348fb385STodd Fiala         if (log)
651348fb385STodd Fiala           log->Printf("PlatformLinux::%s pid %" PRIu64 " state %s\n",
652348fb385STodd Fiala                       __FUNCTION__, process_sp->GetID(), StateAsCString(state));
653b9c1b51eSKate Stone       } else {
654348fb385STodd Fiala         if (log)
655b9c1b51eSKate Stone           log->Printf("PlatformLinux::%s pid %" PRIu64
656b9c1b51eSKate Stone                       " state is not stopped - %s\n",
657348fb385STodd Fiala                       __FUNCTION__, process_sp->GetID(), StateAsCString(state));
658348fb385STodd Fiala       }
659348fb385STodd Fiala     }
660348fb385STodd Fiala 
661b9c1b51eSKate Stone     // Hook up process PTY if we have one (which we should for local debugging
662b9c1b51eSKate Stone     // with llgs).
663348fb385STodd Fiala     int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor();
664b9c1b51eSKate Stone     if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) {
665348fb385STodd Fiala       process_sp->SetSTDIOFileDescriptor(pty_fd);
666348fb385STodd Fiala       if (log)
667b9c1b51eSKate Stone         log->Printf("PlatformLinux::%s pid %" PRIu64
668b9c1b51eSKate Stone                     " hooked up STDIO pty to process",
669b9c1b51eSKate Stone                     __FUNCTION__, process_sp->GetID());
670b9c1b51eSKate Stone     } else {
671348fb385STodd Fiala       if (log)
672b9c1b51eSKate Stone         log->Printf("PlatformLinux::%s pid %" PRIu64
673b9c1b51eSKate Stone                     " not using process STDIO pty",
674b9c1b51eSKate Stone                     __FUNCTION__, process_sp->GetID());
67528041352SGreg Clayton     }
676b9c1b51eSKate Stone   } else {
677348fb385STodd Fiala     if (log)
678b9c1b51eSKate Stone       log->Printf("PlatformLinux::%s process launch failed: %s", __FUNCTION__,
679b9c1b51eSKate Stone                   error.AsCString());
680b9c1b51eSKate Stone     // FIXME figure out appropriate cleanup here.  Do we delete the target? Do
681b9c1b51eSKate Stone     // we delete the process?  Does our caller do that?
68228041352SGreg Clayton   }
683348fb385STodd Fiala 
68428041352SGreg Clayton   return process_sp;
68513e8e1c3SJohnny Chen }
6862094dbf4SJason Molenda 
687b9c1b51eSKate Stone void PlatformLinux::CalculateTrapHandlerSymbolNames() {
6882094dbf4SJason Molenda   m_trap_handlers.push_back(ConstString("_sigtramp"));
6892094dbf4SJason Molenda }
690af245d11STodd Fiala 
691b9c1b51eSKate Stone uint64_t PlatformLinux::ConvertMmapFlagsToPlatform(const ArchSpec &arch,
692b9c1b51eSKate Stone                                                    unsigned flags) {
69396ad3de5SRobert Flack   uint64_t flags_platform = 0;
694e0d8c422SMohit K. Bhakkad   uint64_t map_anon = MAP_ANON;
695e0d8c422SMohit K. Bhakkad 
696e0d8c422SMohit K. Bhakkad   // To get correct flags for MIPS Architecture
697b9c1b51eSKate Stone   if (arch.GetTriple().getArch() == llvm::Triple::mips64 ||
698b9c1b51eSKate Stone       arch.GetTriple().getArch() == llvm::Triple::mips64el ||
699b9c1b51eSKate Stone       arch.GetTriple().getArch() == llvm::Triple::mips ||
700b9c1b51eSKate Stone       arch.GetTriple().getArch() == llvm::Triple::mipsel)
701e0d8c422SMohit K. Bhakkad     map_anon = 0x800;
702e0d8c422SMohit K. Bhakkad 
70396ad3de5SRobert Flack   if (flags & eMmapFlagsPrivate)
70496ad3de5SRobert Flack     flags_platform |= MAP_PRIVATE;
70596ad3de5SRobert Flack   if (flags & eMmapFlagsAnon)
706e0d8c422SMohit K. Bhakkad     flags_platform |= map_anon;
70796ad3de5SRobert Flack   return flags_platform;
70896ad3de5SRobert Flack }
7096e25aeeaSEnrico Granata 
710b9c1b51eSKate Stone ConstString PlatformLinux::GetFullNameForDylib(ConstString basename) {
7116e25aeeaSEnrico Granata   if (basename.IsEmpty())
7126e25aeeaSEnrico Granata     return basename;
7136e25aeeaSEnrico Granata 
7146e25aeeaSEnrico Granata   StreamString stream;
7156e25aeeaSEnrico Granata   stream.Printf("lib%s.so", basename.GetCString());
716c156427dSZachary Turner   return ConstString(stream.GetString());
7176e25aeeaSEnrico Granata }
718