1af245d11STodd Fiala //===-- NativeProcessLinux.cpp -------------------------------- -*- C++ -*-===//
2af245d11STodd Fiala //
3af245d11STodd Fiala //                     The LLVM Compiler Infrastructure
4af245d11STodd Fiala //
5af245d11STodd Fiala // This file is distributed under the University of Illinois Open Source
6af245d11STodd Fiala // License. See LICENSE.TXT for details.
7af245d11STodd Fiala //
8af245d11STodd Fiala //===----------------------------------------------------------------------===//
9af245d11STodd Fiala 
10af245d11STodd Fiala #include "NativeProcessLinux.h"
11af245d11STodd Fiala 
12af245d11STodd Fiala // C Includes
13af245d11STodd Fiala #include <errno.h>
14af245d11STodd Fiala #include <string.h>
15af245d11STodd Fiala #include <stdint.h>
16af245d11STodd Fiala #include <unistd.h>
17af245d11STodd Fiala 
18af245d11STodd Fiala // C++ Includes
19af245d11STodd Fiala #include <fstream>
20df7c6995SPavel Labath #include <mutex>
21c076559aSPavel Labath #include <sstream>
22af245d11STodd Fiala #include <string>
235b981ab9SPavel Labath #include <unordered_map>
24af245d11STodd Fiala 
25af245d11STodd Fiala // Other libraries and framework includes
26d8c338d4STamas Berghammer #include "lldb/Core/EmulateInstruction.h"
27af245d11STodd Fiala #include "lldb/Core/Error.h"
28af245d11STodd Fiala #include "lldb/Core/Module.h"
296edef204SOleksiy Vyalov #include "lldb/Core/ModuleSpec.h"
30af245d11STodd Fiala #include "lldb/Core/RegisterValue.h"
31af245d11STodd Fiala #include "lldb/Core/State.h"
321e209fccSTamas Berghammer #include "lldb/Host/common/NativeBreakpoint.h"
330cbf0b13STamas Berghammer #include "lldb/Host/common/NativeRegisterContext.h"
34af245d11STodd Fiala #include "lldb/Host/Host.h"
3539de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h"
365b981ab9SPavel Labath #include "lldb/Target/Platform.h"
3790aff47cSZachary Turner #include "lldb/Target/Process.h"
38af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h"
395b981ab9SPavel Labath #include "lldb/Target/Target.h"
40c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h"
41af245d11STodd Fiala #include "lldb/Utility/PseudoTerminal.h"
42f805e190SPavel Labath #include "lldb/Utility/StringExtractor.h"
43af245d11STodd Fiala 
441e209fccSTamas Berghammer #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
45af245d11STodd Fiala #include "NativeThreadLinux.h"
46af245d11STodd Fiala #include "ProcFileReader.h"
471e209fccSTamas Berghammer #include "Procfs.h"
48cacde7dfSTodd Fiala 
49d858487eSTamas Berghammer // System includes - They have to be included after framework includes because they define some
50d858487eSTamas Berghammer // macros which collide with variable names in other modules
51d858487eSTamas Berghammer #include <linux/unistd.h>
52d858487eSTamas Berghammer #include <sys/socket.h>
538b335671SVince Harron 
54df7c6995SPavel Labath #include <sys/syscall.h>
55d858487eSTamas Berghammer #include <sys/types.h>
56d858487eSTamas Berghammer #include <sys/user.h>
57d858487eSTamas Berghammer #include <sys/wait.h>
58d858487eSTamas Berghammer 
598b335671SVince Harron #include "lldb/Host/linux/Personality.h"
608b335671SVince Harron #include "lldb/Host/linux/Ptrace.h"
618b335671SVince Harron #include "lldb/Host/linux/Signalfd.h"
62df7c6995SPavel Labath #include "lldb/Host/linux/Uio.h"
638b335671SVince Harron #include "lldb/Host/android/Android.h"
64af245d11STodd Fiala 
650bce1b67STodd Fiala #define LLDB_PERSONALITY_GET_CURRENT_SETTINGS  0xffffffff
66af245d11STodd Fiala 
67af245d11STodd Fiala // Support hardware breakpoints in case it has not been defined
68af245d11STodd Fiala #ifndef TRAP_HWBKPT
69af245d11STodd Fiala   #define TRAP_HWBKPT 4
70af245d11STodd Fiala #endif
71af245d11STodd Fiala 
727cb18bf5STamas Berghammer using namespace lldb;
737cb18bf5STamas Berghammer using namespace lldb_private;
74db264a6dSTamas Berghammer using namespace lldb_private::process_linux;
757cb18bf5STamas Berghammer using namespace llvm;
767cb18bf5STamas Berghammer 
77af245d11STodd Fiala // Private bits we only need internally.
78df7c6995SPavel Labath 
79df7c6995SPavel Labath static bool ProcessVmReadvSupported()
80df7c6995SPavel Labath {
81df7c6995SPavel Labath     static bool is_supported;
82df7c6995SPavel Labath     static std::once_flag flag;
83df7c6995SPavel Labath 
84df7c6995SPavel Labath     std::call_once(flag, [] {
85df7c6995SPavel Labath         Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
86df7c6995SPavel Labath 
87df7c6995SPavel Labath         uint32_t source = 0x47424742;
88df7c6995SPavel Labath         uint32_t dest = 0;
89df7c6995SPavel Labath 
90df7c6995SPavel Labath         struct iovec local, remote;
91df7c6995SPavel Labath         remote.iov_base = &source;
92df7c6995SPavel Labath         local.iov_base = &dest;
93df7c6995SPavel Labath         remote.iov_len = local.iov_len = sizeof source;
94df7c6995SPavel Labath 
95df7c6995SPavel Labath         // We shall try if cross-process-memory reads work by attempting to read a value from our own process.
96df7c6995SPavel Labath         ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0);
97df7c6995SPavel Labath         is_supported = (res == sizeof(source) && source == dest);
98df7c6995SPavel Labath         if (log)
99df7c6995SPavel Labath         {
100df7c6995SPavel Labath             if (is_supported)
101df7c6995SPavel Labath                 log->Printf("%s: Detected kernel support for process_vm_readv syscall. Fast memory reads enabled.",
102df7c6995SPavel Labath                         __FUNCTION__);
103df7c6995SPavel Labath             else
104df7c6995SPavel Labath                 log->Printf("%s: syscall process_vm_readv failed (error: %s). Fast memory reads disabled.",
105df7c6995SPavel Labath                         __FUNCTION__, strerror(errno));
106df7c6995SPavel Labath         }
107df7c6995SPavel Labath     });
108df7c6995SPavel Labath 
109df7c6995SPavel Labath     return is_supported;
110df7c6995SPavel Labath }
111df7c6995SPavel Labath 
112af245d11STodd Fiala namespace
113af245d11STodd Fiala {
114af245d11STodd Fiala     Error
115af245d11STodd Fiala     ResolveProcessArchitecture (lldb::pid_t pid, Platform &platform, ArchSpec &arch)
116af245d11STodd Fiala     {
117af245d11STodd Fiala         // Grab process info for the running process.
118af245d11STodd Fiala         ProcessInstanceInfo process_info;
119af245d11STodd Fiala         if (!platform.GetProcessInfo (pid, process_info))
120db264a6dSTamas Berghammer             return Error("failed to get process info");
121af245d11STodd Fiala 
122af245d11STodd Fiala         // Resolve the executable module.
123af245d11STodd Fiala         ModuleSP exe_module_sp;
124e56f6dceSChaoren Lin         ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture());
125af245d11STodd Fiala         FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths ());
126af245d11STodd Fiala         Error error = platform.ResolveExecutable(
12754539338SOleksiy Vyalov             exe_module_spec,
128af245d11STodd Fiala             exe_module_sp,
129af245d11STodd Fiala             executable_search_paths.GetSize () ? &executable_search_paths : NULL);
130af245d11STodd Fiala 
131af245d11STodd Fiala         if (!error.Success ())
132af245d11STodd Fiala             return error;
133af245d11STodd Fiala 
134af245d11STodd Fiala         // Check if we've got our architecture from the exe_module.
135af245d11STodd Fiala         arch = exe_module_sp->GetArchitecture ();
136af245d11STodd Fiala         if (arch.IsValid ())
137af245d11STodd Fiala             return Error();
138af245d11STodd Fiala         else
139af245d11STodd Fiala             return Error("failed to retrieve a valid architecture from the exe module");
140af245d11STodd Fiala     }
141af245d11STodd Fiala 
142af245d11STodd Fiala     void
143db264a6dSTamas Berghammer     DisplayBytes (StreamString &s, void *bytes, uint32_t count)
144af245d11STodd Fiala     {
145af245d11STodd Fiala         uint8_t *ptr = (uint8_t *)bytes;
146af245d11STodd Fiala         const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
147af245d11STodd Fiala         for(uint32_t i=0; i<loop_count; i++)
148af245d11STodd Fiala         {
149af245d11STodd Fiala             s.Printf ("[%x]", *ptr);
150af245d11STodd Fiala             ptr++;
151af245d11STodd Fiala         }
152af245d11STodd Fiala     }
153af245d11STodd Fiala 
154af245d11STodd Fiala     void
155af245d11STodd Fiala     PtraceDisplayBytes(int &req, void *data, size_t data_size)
156af245d11STodd Fiala     {
157af245d11STodd Fiala         StreamString buf;
158af245d11STodd Fiala         Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
159af245d11STodd Fiala                     POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
160af245d11STodd Fiala 
161af245d11STodd Fiala         if (verbose_log)
162af245d11STodd Fiala         {
163af245d11STodd Fiala             switch(req)
164af245d11STodd Fiala             {
165af245d11STodd Fiala             case PTRACE_POKETEXT:
166af245d11STodd Fiala             {
167af245d11STodd Fiala                 DisplayBytes(buf, &data, 8);
168af245d11STodd Fiala                 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
169af245d11STodd Fiala                 break;
170af245d11STodd Fiala             }
171af245d11STodd Fiala             case PTRACE_POKEDATA:
172af245d11STodd Fiala             {
173af245d11STodd Fiala                 DisplayBytes(buf, &data, 8);
174af245d11STodd Fiala                 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
175af245d11STodd Fiala                 break;
176af245d11STodd Fiala             }
177af245d11STodd Fiala             case PTRACE_POKEUSER:
178af245d11STodd Fiala             {
179af245d11STodd Fiala                 DisplayBytes(buf, &data, 8);
180af245d11STodd Fiala                 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
181af245d11STodd Fiala                 break;
182af245d11STodd Fiala             }
183af245d11STodd Fiala             case PTRACE_SETREGS:
184af245d11STodd Fiala             {
185af245d11STodd Fiala                 DisplayBytes(buf, data, data_size);
186af245d11STodd Fiala                 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
187af245d11STodd Fiala                 break;
188af245d11STodd Fiala             }
189af245d11STodd Fiala             case PTRACE_SETFPREGS:
190af245d11STodd Fiala             {
191af245d11STodd Fiala                 DisplayBytes(buf, data, data_size);
192af245d11STodd Fiala                 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
193af245d11STodd Fiala                 break;
194af245d11STodd Fiala             }
195af245d11STodd Fiala             case PTRACE_SETSIGINFO:
196af245d11STodd Fiala             {
197af245d11STodd Fiala                 DisplayBytes(buf, data, sizeof(siginfo_t));
198af245d11STodd Fiala                 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
199af245d11STodd Fiala                 break;
200af245d11STodd Fiala             }
201af245d11STodd Fiala             case PTRACE_SETREGSET:
202af245d11STodd Fiala             {
203af245d11STodd Fiala                 // Extract iov_base from data, which is a pointer to the struct IOVEC
204af245d11STodd Fiala                 DisplayBytes(buf, *(void **)data, data_size);
205af245d11STodd Fiala                 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
206af245d11STodd Fiala                 break;
207af245d11STodd Fiala             }
208af245d11STodd Fiala             default:
209af245d11STodd Fiala             {
210af245d11STodd Fiala             }
211af245d11STodd Fiala             }
212af245d11STodd Fiala         }
213af245d11STodd Fiala     }
214af245d11STodd Fiala 
21519cbe96aSPavel Labath     static constexpr unsigned k_ptrace_word_size = sizeof(void*);
21619cbe96aSPavel Labath     static_assert(sizeof(long) >= k_ptrace_word_size, "Size of long must be larger than ptrace word size");
2171107b5a5SPavel Labath } // end of anonymous namespace
2181107b5a5SPavel Labath 
219bd7cbc5aSPavel Labath // Simple helper function to ensure flags are enabled on the given file
220bd7cbc5aSPavel Labath // descriptor.
221bd7cbc5aSPavel Labath static Error
222bd7cbc5aSPavel Labath EnsureFDFlags(int fd, int flags)
223bd7cbc5aSPavel Labath {
224bd7cbc5aSPavel Labath     Error error;
225bd7cbc5aSPavel Labath 
226bd7cbc5aSPavel Labath     int status = fcntl(fd, F_GETFL);
227bd7cbc5aSPavel Labath     if (status == -1)
228bd7cbc5aSPavel Labath     {
229bd7cbc5aSPavel Labath         error.SetErrorToErrno();
230bd7cbc5aSPavel Labath         return error;
231bd7cbc5aSPavel Labath     }
232bd7cbc5aSPavel Labath 
233bd7cbc5aSPavel Labath     if (fcntl(fd, F_SETFL, status | flags) == -1)
234bd7cbc5aSPavel Labath     {
235bd7cbc5aSPavel Labath         error.SetErrorToErrno();
236bd7cbc5aSPavel Labath         return error;
237bd7cbc5aSPavel Labath     }
238bd7cbc5aSPavel Labath 
239bd7cbc5aSPavel Labath     return error;
240bd7cbc5aSPavel Labath }
241bd7cbc5aSPavel Labath 
242bd7cbc5aSPavel Labath NativeProcessLinux::LaunchArgs::LaunchArgs(Module *module,
243af245d11STodd Fiala                                        char const **argv,
244af245d11STodd Fiala                                        char const **envp,
245d3173f34SChaoren Lin                                        const FileSpec &stdin_file_spec,
246d3173f34SChaoren Lin                                        const FileSpec &stdout_file_spec,
247d3173f34SChaoren Lin                                        const FileSpec &stderr_file_spec,
248d3173f34SChaoren Lin                                        const FileSpec &working_dir,
249db264a6dSTamas Berghammer                                        const ProcessLaunchInfo &launch_info)
250bd7cbc5aSPavel Labath     : m_module(module),
251af245d11STodd Fiala       m_argv(argv),
252af245d11STodd Fiala       m_envp(envp),
253d3173f34SChaoren Lin       m_stdin_file_spec(stdin_file_spec),
254d3173f34SChaoren Lin       m_stdout_file_spec(stdout_file_spec),
255d3173f34SChaoren Lin       m_stderr_file_spec(stderr_file_spec),
2560bce1b67STodd Fiala       m_working_dir(working_dir),
2570bce1b67STodd Fiala       m_launch_info(launch_info)
2580bce1b67STodd Fiala {
2590bce1b67STodd Fiala }
260af245d11STodd Fiala 
261af245d11STodd Fiala NativeProcessLinux::LaunchArgs::~LaunchArgs()
262af245d11STodd Fiala { }
263af245d11STodd Fiala 
264af245d11STodd Fiala // -----------------------------------------------------------------------------
265af245d11STodd Fiala // Public Static Methods
266af245d11STodd Fiala // -----------------------------------------------------------------------------
267af245d11STodd Fiala 
268db264a6dSTamas Berghammer Error
269d5b310f2SPavel Labath NativeProcessProtocol::Launch (
270db264a6dSTamas Berghammer     ProcessLaunchInfo &launch_info,
271db264a6dSTamas Berghammer     NativeProcessProtocol::NativeDelegate &native_delegate,
27219cbe96aSPavel Labath     MainLoop &mainloop,
273af245d11STodd Fiala     NativeProcessProtocolSP &native_process_sp)
274af245d11STodd Fiala {
275af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
276af245d11STodd Fiala 
277d5b310f2SPavel Labath     lldb::ModuleSP exe_module_sp;
278d5b310f2SPavel Labath     PlatformSP platform_sp (Platform::GetHostPlatform ());
279d5b310f2SPavel Labath     Error error = platform_sp->ResolveExecutable(
280d5b310f2SPavel Labath             ModuleSpec(launch_info.GetExecutableFile(), launch_info.GetArchitecture()),
281d5b310f2SPavel Labath             exe_module_sp,
282d5b310f2SPavel Labath             nullptr);
283d5b310f2SPavel Labath 
284d5b310f2SPavel Labath     if (! error.Success())
285d5b310f2SPavel Labath         return error;
286af245d11STodd Fiala 
287af245d11STodd Fiala     // Verify the working directory is valid if one was specified.
288d3173f34SChaoren Lin     FileSpec working_dir{launch_info.GetWorkingDirectory()};
289d3173f34SChaoren Lin     if (working_dir &&
290d3173f34SChaoren Lin             (!working_dir.ResolvePath() ||
291d3173f34SChaoren Lin              working_dir.GetFileType() != FileSpec::eFileTypeDirectory))
292af245d11STodd Fiala     {
293d3173f34SChaoren Lin         error.SetErrorStringWithFormat ("No such file or directory: %s",
294d3173f34SChaoren Lin                 working_dir.GetCString());
295af245d11STodd Fiala         return error;
296af245d11STodd Fiala     }
297af245d11STodd Fiala 
298db264a6dSTamas Berghammer     const FileAction *file_action;
299af245d11STodd Fiala 
300d3173f34SChaoren Lin     // Default of empty will mean to use existing open file descriptors.
301d3173f34SChaoren Lin     FileSpec stdin_file_spec{};
302d3173f34SChaoren Lin     FileSpec stdout_file_spec{};
303d3173f34SChaoren Lin     FileSpec stderr_file_spec{};
304af245d11STodd Fiala 
305af245d11STodd Fiala     file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
30675f47c3aSTodd Fiala     if (file_action)
307d3173f34SChaoren Lin         stdin_file_spec = file_action->GetFileSpec();
308af245d11STodd Fiala 
309af245d11STodd Fiala     file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
31075f47c3aSTodd Fiala     if (file_action)
311d3173f34SChaoren Lin         stdout_file_spec = file_action->GetFileSpec();
312af245d11STodd Fiala 
313af245d11STodd Fiala     file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
31475f47c3aSTodd Fiala     if (file_action)
315d3173f34SChaoren Lin         stderr_file_spec = file_action->GetFileSpec();
31675f47c3aSTodd Fiala 
31775f47c3aSTodd Fiala     if (log)
31875f47c3aSTodd Fiala     {
319d3173f34SChaoren Lin         if (stdin_file_spec)
320d3173f34SChaoren Lin             log->Printf ("NativeProcessLinux::%s setting STDIN to '%s'",
321d3173f34SChaoren Lin                     __FUNCTION__, stdin_file_spec.GetCString());
32275f47c3aSTodd Fiala         else
32375f47c3aSTodd Fiala             log->Printf ("NativeProcessLinux::%s leaving STDIN as is", __FUNCTION__);
32475f47c3aSTodd Fiala 
325d3173f34SChaoren Lin         if (stdout_file_spec)
326d3173f34SChaoren Lin             log->Printf ("NativeProcessLinux::%s setting STDOUT to '%s'",
327d3173f34SChaoren Lin                     __FUNCTION__, stdout_file_spec.GetCString());
32875f47c3aSTodd Fiala         else
32975f47c3aSTodd Fiala             log->Printf ("NativeProcessLinux::%s leaving STDOUT as is", __FUNCTION__);
33075f47c3aSTodd Fiala 
331d3173f34SChaoren Lin         if (stderr_file_spec)
332d3173f34SChaoren Lin             log->Printf ("NativeProcessLinux::%s setting STDERR to '%s'",
333d3173f34SChaoren Lin                     __FUNCTION__, stderr_file_spec.GetCString());
33475f47c3aSTodd Fiala         else
33575f47c3aSTodd Fiala             log->Printf ("NativeProcessLinux::%s leaving STDERR as is", __FUNCTION__);
33675f47c3aSTodd Fiala     }
337af245d11STodd Fiala 
338af245d11STodd Fiala     // Create the NativeProcessLinux in launch mode.
339af245d11STodd Fiala     native_process_sp.reset (new NativeProcessLinux ());
340af245d11STodd Fiala 
341af245d11STodd Fiala     if (log)
342af245d11STodd Fiala     {
343af245d11STodd Fiala         int i = 0;
344af245d11STodd Fiala         for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i)
345af245d11STodd Fiala         {
346af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr");
347af245d11STodd Fiala             ++i;
348af245d11STodd Fiala         }
349af245d11STodd Fiala     }
350af245d11STodd Fiala 
351af245d11STodd Fiala     if (!native_process_sp->RegisterNativeDelegate (native_delegate))
352af245d11STodd Fiala     {
353af245d11STodd Fiala         native_process_sp.reset ();
354af245d11STodd Fiala         error.SetErrorStringWithFormat ("failed to register the native delegate");
355af245d11STodd Fiala         return error;
356af245d11STodd Fiala     }
357af245d11STodd Fiala 
358cb84eebbSTamas Berghammer     std::static_pointer_cast<NativeProcessLinux> (native_process_sp)->LaunchInferior (
35919cbe96aSPavel Labath             mainloop,
360d5b310f2SPavel Labath             exe_module_sp.get(),
361af245d11STodd Fiala             launch_info.GetArguments ().GetConstArgumentVector (),
362af245d11STodd Fiala             launch_info.GetEnvironmentEntries ().GetConstArgumentVector (),
363d3173f34SChaoren Lin             stdin_file_spec,
364d3173f34SChaoren Lin             stdout_file_spec,
365d3173f34SChaoren Lin             stderr_file_spec,
366af245d11STodd Fiala             working_dir,
3670bce1b67STodd Fiala             launch_info,
368af245d11STodd Fiala             error);
369af245d11STodd Fiala 
370af245d11STodd Fiala     if (error.Fail ())
371af245d11STodd Fiala     {
372af245d11STodd Fiala         native_process_sp.reset ();
373af245d11STodd Fiala         if (log)
374af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ());
375af245d11STodd Fiala         return error;
376af245d11STodd Fiala     }
377af245d11STodd Fiala 
378af245d11STodd Fiala     launch_info.SetProcessID (native_process_sp->GetID ());
379af245d11STodd Fiala 
380af245d11STodd Fiala     return error;
381af245d11STodd Fiala }
382af245d11STodd Fiala 
383db264a6dSTamas Berghammer Error
384d5b310f2SPavel Labath NativeProcessProtocol::Attach (
385af245d11STodd Fiala     lldb::pid_t pid,
386db264a6dSTamas Berghammer     NativeProcessProtocol::NativeDelegate &native_delegate,
38719cbe96aSPavel Labath     MainLoop &mainloop,
388af245d11STodd Fiala     NativeProcessProtocolSP &native_process_sp)
389af245d11STodd Fiala {
390af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
391af245d11STodd Fiala     if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE))
392af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid);
393af245d11STodd Fiala 
394af245d11STodd Fiala     // Grab the current platform architecture.  This should be Linux,
395af245d11STodd Fiala     // since this code is only intended to run on a Linux host.
396615eb7e6SGreg Clayton     PlatformSP platform_sp (Platform::GetHostPlatform ());
397af245d11STodd Fiala     if (!platform_sp)
398af245d11STodd Fiala         return Error("failed to get a valid default platform");
399af245d11STodd Fiala 
400af245d11STodd Fiala     // Retrieve the architecture for the running process.
401af245d11STodd Fiala     ArchSpec process_arch;
402af245d11STodd Fiala     Error error = ResolveProcessArchitecture (pid, *platform_sp.get (), process_arch);
403af245d11STodd Fiala     if (!error.Success ())
404af245d11STodd Fiala         return error;
405af245d11STodd Fiala 
4061339b5e8SOleksiy Vyalov     std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ());
407af245d11STodd Fiala 
4081339b5e8SOleksiy Vyalov     if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate))
409af245d11STodd Fiala     {
410af245d11STodd Fiala         error.SetErrorStringWithFormat ("failed to register the native delegate");
411af245d11STodd Fiala         return error;
412af245d11STodd Fiala     }
413af245d11STodd Fiala 
41419cbe96aSPavel Labath     native_process_linux_sp->AttachToInferior (mainloop, pid, error);
415af245d11STodd Fiala     if (!error.Success ())
416af245d11STodd Fiala         return error;
417af245d11STodd Fiala 
4181339b5e8SOleksiy Vyalov     native_process_sp = native_process_linux_sp;
419af245d11STodd Fiala     return error;
420af245d11STodd Fiala }
421af245d11STodd Fiala 
422af245d11STodd Fiala // -----------------------------------------------------------------------------
423af245d11STodd Fiala // Public Instance Methods
424af245d11STodd Fiala // -----------------------------------------------------------------------------
425af245d11STodd Fiala 
426af245d11STodd Fiala NativeProcessLinux::NativeProcessLinux () :
427af245d11STodd Fiala     NativeProcessProtocol (LLDB_INVALID_PROCESS_ID),
428af245d11STodd Fiala     m_arch (),
429af245d11STodd Fiala     m_supports_mem_region (eLazyBoolCalculate),
430af245d11STodd Fiala     m_mem_region_cache (),
4318c8ff7afSPavel Labath     m_mem_region_cache_mutex ()
432af245d11STodd Fiala {
433af245d11STodd Fiala }
434af245d11STodd Fiala 
435af245d11STodd Fiala void
436af245d11STodd Fiala NativeProcessLinux::LaunchInferior (
43719cbe96aSPavel Labath     MainLoop &mainloop,
438af245d11STodd Fiala     Module *module,
439af245d11STodd Fiala     const char *argv[],
440af245d11STodd Fiala     const char *envp[],
441d3173f34SChaoren Lin     const FileSpec &stdin_file_spec,
442d3173f34SChaoren Lin     const FileSpec &stdout_file_spec,
443d3173f34SChaoren Lin     const FileSpec &stderr_file_spec,
444d3173f34SChaoren Lin     const FileSpec &working_dir,
445db264a6dSTamas Berghammer     const ProcessLaunchInfo &launch_info,
446db264a6dSTamas Berghammer     Error &error)
447af245d11STodd Fiala {
44819cbe96aSPavel Labath     m_sigchld_handle = mainloop.RegisterSignal(SIGCHLD,
44919cbe96aSPavel Labath             [this] (MainLoopBase &) { SigchldHandler(); }, error);
45019cbe96aSPavel Labath     if (! m_sigchld_handle)
45119cbe96aSPavel Labath         return;
45219cbe96aSPavel Labath 
453af245d11STodd Fiala     if (module)
454af245d11STodd Fiala         m_arch = module->GetArchitecture ();
455af245d11STodd Fiala 
456af245d11STodd Fiala     SetState (eStateLaunching);
457af245d11STodd Fiala 
458af245d11STodd Fiala     std::unique_ptr<LaunchArgs> args(
459d3173f34SChaoren Lin         new LaunchArgs(module, argv, envp,
460d3173f34SChaoren Lin                        stdin_file_spec,
461d3173f34SChaoren Lin                        stdout_file_spec,
462d3173f34SChaoren Lin                        stderr_file_spec,
463d3173f34SChaoren Lin                        working_dir,
464d3173f34SChaoren Lin                        launch_info));
465af245d11STodd Fiala 
46619cbe96aSPavel Labath     Launch(args.get(), error);
467af245d11STodd Fiala }
468af245d11STodd Fiala 
469af245d11STodd Fiala void
47019cbe96aSPavel Labath NativeProcessLinux::AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error)
471af245d11STodd Fiala {
472af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
473af245d11STodd Fiala     if (log)
474af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid);
475af245d11STodd Fiala 
47619cbe96aSPavel Labath     m_sigchld_handle = mainloop.RegisterSignal(SIGCHLD,
47719cbe96aSPavel Labath             [this] (MainLoopBase &) { SigchldHandler(); }, error);
47819cbe96aSPavel Labath     if (! m_sigchld_handle)
47919cbe96aSPavel Labath         return;
48019cbe96aSPavel Labath 
481af245d11STodd Fiala     // We can use the Host for everything except the ResolveExecutable portion.
482615eb7e6SGreg Clayton     PlatformSP platform_sp = Platform::GetHostPlatform ();
483af245d11STodd Fiala     if (!platform_sp)
484af245d11STodd Fiala     {
485af245d11STodd Fiala         if (log)
486af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): no default platform set", __FUNCTION__, pid);
487af245d11STodd Fiala         error.SetErrorString ("no default platform available");
48850d60be3SShawn Best         return;
489af245d11STodd Fiala     }
490af245d11STodd Fiala 
491af245d11STodd Fiala     // Gather info about the process.
492af245d11STodd Fiala     ProcessInstanceInfo process_info;
49350d60be3SShawn Best     if (!platform_sp->GetProcessInfo (pid, process_info))
49450d60be3SShawn Best     {
49550d60be3SShawn Best         if (log)
49650d60be3SShawn Best             log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): failed to get process info", __FUNCTION__, pid);
49750d60be3SShawn Best         error.SetErrorString ("failed to get process info");
49850d60be3SShawn Best         return;
49950d60be3SShawn Best     }
500af245d11STodd Fiala 
501af245d11STodd Fiala     // Resolve the executable module
502af245d11STodd Fiala     ModuleSP exe_module_sp;
503af245d11STodd Fiala     FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
504e56f6dceSChaoren Lin     ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture());
5056edef204SOleksiy Vyalov     error = platform_sp->ResolveExecutable(exe_module_spec, exe_module_sp,
506af245d11STodd Fiala                                            executable_search_paths.GetSize() ? &executable_search_paths : NULL);
507af245d11STodd Fiala     if (!error.Success())
508af245d11STodd Fiala         return;
509af245d11STodd Fiala 
510af245d11STodd Fiala     // Set the architecture to the exe architecture.
511af245d11STodd Fiala     m_arch = exe_module_sp->GetArchitecture();
512af245d11STodd Fiala     if (log)
513af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ());
514af245d11STodd Fiala 
515af245d11STodd Fiala     m_pid = pid;
516af245d11STodd Fiala     SetState(eStateAttaching);
517af245d11STodd Fiala 
51819cbe96aSPavel Labath     Attach(pid, error);
519af245d11STodd Fiala }
520af245d11STodd Fiala 
521bd7cbc5aSPavel Labath ::pid_t
522bd7cbc5aSPavel Labath NativeProcessLinux::Launch(LaunchArgs *args, Error &error)
523af245d11STodd Fiala {
5240bce1b67STodd Fiala     assert (args && "null args");
525af245d11STodd Fiala 
526af245d11STodd Fiala     const char **argv = args->m_argv;
527af245d11STodd Fiala     const char **envp = args->m_envp;
528d3173f34SChaoren Lin     const FileSpec working_dir = args->m_working_dir;
529af245d11STodd Fiala 
530af245d11STodd Fiala     lldb_utility::PseudoTerminal terminal;
531af245d11STodd Fiala     const size_t err_len = 1024;
532af245d11STodd Fiala     char err_str[err_len];
533af245d11STodd Fiala     lldb::pid_t pid;
534af245d11STodd Fiala     NativeThreadProtocolSP thread_sp;
535af245d11STodd Fiala 
536af245d11STodd Fiala     lldb::ThreadSP inferior;
537af245d11STodd Fiala 
538af245d11STodd Fiala     // Propagate the environment if one is not supplied.
539af245d11STodd Fiala     if (envp == NULL || envp[0] == NULL)
540af245d11STodd Fiala         envp = const_cast<const char **>(environ);
541af245d11STodd Fiala 
542af245d11STodd Fiala     if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1))
543af245d11STodd Fiala     {
544bd7cbc5aSPavel Labath         error.SetErrorToGenericError();
545bd7cbc5aSPavel Labath         error.SetErrorStringWithFormat("Process fork failed: %s", err_str);
546bd7cbc5aSPavel Labath         return -1;
547af245d11STodd Fiala     }
548af245d11STodd Fiala 
549af245d11STodd Fiala     // Recognized child exit status codes.
550af245d11STodd Fiala     enum {
551af245d11STodd Fiala         ePtraceFailed = 1,
552af245d11STodd Fiala         eDupStdinFailed,
553af245d11STodd Fiala         eDupStdoutFailed,
554af245d11STodd Fiala         eDupStderrFailed,
555af245d11STodd Fiala         eChdirFailed,
556af245d11STodd Fiala         eExecFailed,
557*78856474SPavel Labath         eSetGidFailed,
558*78856474SPavel Labath         eSetSigMaskFailed
559af245d11STodd Fiala     };
560af245d11STodd Fiala 
561af245d11STodd Fiala     // Child process.
562af245d11STodd Fiala     if (pid == 0)
563af245d11STodd Fiala     {
564d2c4c9b1SPavel Labath         // First, make sure we disable all logging. If we are logging to stdout, our logs can be
565d2c4c9b1SPavel Labath         // mistaken for inferior output.
566d2c4c9b1SPavel Labath         Log::DisableAllLogChannels(nullptr);
56775f47c3aSTodd Fiala         // FIXME consider opening a pipe between parent/child and have this forked child
568d2c4c9b1SPavel Labath         // send log info to parent re: launch status.
569af245d11STodd Fiala 
57075f47c3aSTodd Fiala         // Start tracing this child that is about to exec.
5714a9babb2SPavel Labath         error = PtraceWrapper(PTRACE_TRACEME, 0);
572bd7cbc5aSPavel Labath         if (error.Fail())
573af245d11STodd Fiala             exit(ePtraceFailed);
574af245d11STodd Fiala 
575493c3a12SPavel Labath         // terminal has already dupped the tty descriptors to stdin/out/err.
576493c3a12SPavel Labath         // This closes original fd from which they were copied (and avoids
577493c3a12SPavel Labath         // leaking descriptors to the debugged process.
578493c3a12SPavel Labath         terminal.CloseSlaveFileDescriptor();
579493c3a12SPavel Labath 
580af245d11STodd Fiala         // Do not inherit setgid powers.
581af245d11STodd Fiala         if (setgid(getgid()) != 0)
582af245d11STodd Fiala             exit(eSetGidFailed);
583af245d11STodd Fiala 
584af245d11STodd Fiala         // Attempt to have our own process group.
585af245d11STodd Fiala         if (setpgid(0, 0) != 0)
586af245d11STodd Fiala         {
58775f47c3aSTodd Fiala             // FIXME log that this failed. This is common.
588af245d11STodd Fiala             // Don't allow this to prevent an inferior exec.
589af245d11STodd Fiala         }
590af245d11STodd Fiala 
591af245d11STodd Fiala         // Dup file descriptors if needed.
592d3173f34SChaoren Lin         if (args->m_stdin_file_spec)
593d3173f34SChaoren Lin             if (!DupDescriptor(args->m_stdin_file_spec, STDIN_FILENO, O_RDONLY))
594af245d11STodd Fiala                 exit(eDupStdinFailed);
595af245d11STodd Fiala 
596d3173f34SChaoren Lin         if (args->m_stdout_file_spec)
597d3173f34SChaoren Lin             if (!DupDescriptor(args->m_stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC))
598af245d11STodd Fiala                 exit(eDupStdoutFailed);
599af245d11STodd Fiala 
600d3173f34SChaoren Lin         if (args->m_stderr_file_spec)
601d3173f34SChaoren Lin             if (!DupDescriptor(args->m_stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC))
602af245d11STodd Fiala                 exit(eDupStderrFailed);
603af245d11STodd Fiala 
6049cf4f2c2SChaoren Lin         // Close everything besides stdin, stdout, and stderr that has no file
6059cf4f2c2SChaoren Lin         // action to avoid leaking
6069cf4f2c2SChaoren Lin         for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
6079cf4f2c2SChaoren Lin             if (!args->m_launch_info.GetFileActionForFD(fd))
6089cf4f2c2SChaoren Lin                 close(fd);
6099cf4f2c2SChaoren Lin 
610af245d11STodd Fiala         // Change working directory
611d3173f34SChaoren Lin         if (working_dir && 0 != ::chdir(working_dir.GetCString()))
612af245d11STodd Fiala               exit(eChdirFailed);
613af245d11STodd Fiala 
6140bce1b67STodd Fiala         // Disable ASLR if requested.
6150bce1b67STodd Fiala         if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
6160bce1b67STodd Fiala         {
6170bce1b67STodd Fiala             const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
6180bce1b67STodd Fiala             if (old_personality == -1)
6190bce1b67STodd Fiala             {
62075f47c3aSTodd Fiala                 // Can't retrieve Linux personality.  Cannot disable ASLR.
6210bce1b67STodd Fiala             }
6220bce1b67STodd Fiala             else
6230bce1b67STodd Fiala             {
6240bce1b67STodd Fiala                 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
6250bce1b67STodd Fiala                 if (new_personality == -1)
6260bce1b67STodd Fiala                 {
62775f47c3aSTodd Fiala                     // Disabling ASLR failed.
6280bce1b67STodd Fiala                 }
6290bce1b67STodd Fiala                 else
6300bce1b67STodd Fiala                 {
63175f47c3aSTodd Fiala                     // Disabling ASLR succeeded.
6320bce1b67STodd Fiala                 }
6330bce1b67STodd Fiala             }
6340bce1b67STodd Fiala         }
6350bce1b67STodd Fiala 
636*78856474SPavel Labath         // Clear the signal mask to prevent the child from being affected by
637*78856474SPavel Labath         // any masking done by the parent.
638*78856474SPavel Labath         sigset_t set;
639*78856474SPavel Labath         if (sigemptyset(&set) != 0 || pthread_sigmask(SIG_SETMASK, &set, nullptr) != 0)
640*78856474SPavel Labath             exit(eSetSigMaskFailed);
641*78856474SPavel Labath 
64275f47c3aSTodd Fiala         // Execute.  We should never return...
643af245d11STodd Fiala         execve(argv[0],
644af245d11STodd Fiala                const_cast<char *const *>(argv),
645af245d11STodd Fiala                const_cast<char *const *>(envp));
64675f47c3aSTodd Fiala 
64775f47c3aSTodd Fiala         // ...unless exec fails.  In which case we definitely need to end the child here.
648af245d11STodd Fiala         exit(eExecFailed);
649af245d11STodd Fiala     }
650af245d11STodd Fiala 
65175f47c3aSTodd Fiala     //
65275f47c3aSTodd Fiala     // This is the parent code here.
65375f47c3aSTodd Fiala     //
65475f47c3aSTodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
65575f47c3aSTodd Fiala 
656af245d11STodd Fiala     // Wait for the child process to trap on its call to execve.
657af245d11STodd Fiala     ::pid_t wpid;
658af245d11STodd Fiala     int status;
659af245d11STodd Fiala     if ((wpid = waitpid(pid, &status, 0)) < 0)
660af245d11STodd Fiala     {
661bd7cbc5aSPavel Labath         error.SetErrorToErrno();
662af245d11STodd Fiala         if (log)
663bd7cbc5aSPavel Labath             log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s",
664bd7cbc5aSPavel Labath                     __FUNCTION__, error.AsCString ());
665af245d11STodd Fiala 
666af245d11STodd Fiala         // Mark the inferior as invalid.
667af245d11STodd Fiala         // FIXME this could really use a new state - eStateLaunchFailure.  For now, using eStateInvalid.
668bd7cbc5aSPavel Labath         SetState (StateType::eStateInvalid);
669af245d11STodd Fiala 
670bd7cbc5aSPavel Labath         return -1;
671af245d11STodd Fiala     }
672af245d11STodd Fiala     else if (WIFEXITED(status))
673af245d11STodd Fiala     {
674af245d11STodd Fiala         // open, dup or execve likely failed for some reason.
675bd7cbc5aSPavel Labath         error.SetErrorToGenericError();
676af245d11STodd Fiala         switch (WEXITSTATUS(status))
677af245d11STodd Fiala         {
678af245d11STodd Fiala             case ePtraceFailed:
679bd7cbc5aSPavel Labath                 error.SetErrorString("Child ptrace failed.");
680af245d11STodd Fiala                 break;
681af245d11STodd Fiala             case eDupStdinFailed:
682bd7cbc5aSPavel Labath                 error.SetErrorString("Child open stdin failed.");
683af245d11STodd Fiala                 break;
684af245d11STodd Fiala             case eDupStdoutFailed:
685bd7cbc5aSPavel Labath                 error.SetErrorString("Child open stdout failed.");
686af245d11STodd Fiala                 break;
687af245d11STodd Fiala             case eDupStderrFailed:
688bd7cbc5aSPavel Labath                 error.SetErrorString("Child open stderr failed.");
689af245d11STodd Fiala                 break;
690af245d11STodd Fiala             case eChdirFailed:
691bd7cbc5aSPavel Labath                 error.SetErrorString("Child failed to set working directory.");
692af245d11STodd Fiala                 break;
693af245d11STodd Fiala             case eExecFailed:
694bd7cbc5aSPavel Labath                 error.SetErrorString("Child exec failed.");
695af245d11STodd Fiala                 break;
696af245d11STodd Fiala             case eSetGidFailed:
697bd7cbc5aSPavel Labath                 error.SetErrorString("Child setgid failed.");
698af245d11STodd Fiala                 break;
699*78856474SPavel Labath             case eSetSigMaskFailed:
700*78856474SPavel Labath                 error.SetErrorString("Child failed to set signal mask.");
701*78856474SPavel Labath                 break;
702af245d11STodd Fiala             default:
703bd7cbc5aSPavel Labath                 error.SetErrorString("Child returned unknown exit status.");
704af245d11STodd Fiala                 break;
705af245d11STodd Fiala         }
706af245d11STodd Fiala 
707af245d11STodd Fiala         if (log)
708af245d11STodd Fiala         {
709af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP",
710af245d11STodd Fiala                     __FUNCTION__,
711af245d11STodd Fiala                     WEXITSTATUS(status));
712af245d11STodd Fiala         }
713af245d11STodd Fiala 
714af245d11STodd Fiala         // Mark the inferior as invalid.
715af245d11STodd Fiala         // FIXME this could really use a new state - eStateLaunchFailure.  For now, using eStateInvalid.
716bd7cbc5aSPavel Labath         SetState (StateType::eStateInvalid);
717af245d11STodd Fiala 
718bd7cbc5aSPavel Labath         return -1;
719af245d11STodd Fiala     }
720af245d11STodd Fiala     assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) &&
721af245d11STodd Fiala            "Could not sync with inferior process.");
722af245d11STodd Fiala 
723af245d11STodd Fiala     if (log)
724af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__);
725af245d11STodd Fiala 
726bd7cbc5aSPavel Labath     error = SetDefaultPtraceOpts(pid);
727bd7cbc5aSPavel Labath     if (error.Fail())
728af245d11STodd Fiala     {
729af245d11STodd Fiala         if (log)
730af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s",
731bd7cbc5aSPavel Labath                     __FUNCTION__, error.AsCString ());
732af245d11STodd Fiala 
733af245d11STodd Fiala         // Mark the inferior as invalid.
734af245d11STodd Fiala         // FIXME this could really use a new state - eStateLaunchFailure.  For now, using eStateInvalid.
735bd7cbc5aSPavel Labath         SetState (StateType::eStateInvalid);
736af245d11STodd Fiala 
737bd7cbc5aSPavel Labath         return -1;
738af245d11STodd Fiala     }
739af245d11STodd Fiala 
740af245d11STodd Fiala     // Release the master terminal descriptor and pass it off to the
741af245d11STodd Fiala     // NativeProcessLinux instance.  Similarly stash the inferior pid.
742bd7cbc5aSPavel Labath     m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
743bd7cbc5aSPavel Labath     m_pid = pid;
744af245d11STodd Fiala 
745af245d11STodd Fiala     // Set the terminal fd to be in non blocking mode (it simplifies the
746af245d11STodd Fiala     // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
747af245d11STodd Fiala     // descriptor to read from).
748bd7cbc5aSPavel Labath     error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK);
749bd7cbc5aSPavel Labath     if (error.Fail())
750af245d11STodd Fiala     {
751af245d11STodd Fiala         if (log)
752af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s",
753bd7cbc5aSPavel Labath                     __FUNCTION__, error.AsCString ());
754af245d11STodd Fiala 
755af245d11STodd Fiala         // Mark the inferior as invalid.
756af245d11STodd Fiala         // FIXME this could really use a new state - eStateLaunchFailure.  For now, using eStateInvalid.
757bd7cbc5aSPavel Labath         SetState (StateType::eStateInvalid);
758af245d11STodd Fiala 
759bd7cbc5aSPavel Labath         return -1;
760af245d11STodd Fiala     }
761af245d11STodd Fiala 
762af245d11STodd Fiala     if (log)
763af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
764af245d11STodd Fiala 
765bd7cbc5aSPavel Labath     thread_sp = AddThread (pid);
766af245d11STodd Fiala     assert (thread_sp && "AddThread() returned a nullptr thread");
767cb84eebbSTamas Berghammer     std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP);
7681dbc6c9cSPavel Labath     ThreadWasCreated(pid);
769af245d11STodd Fiala 
770af245d11STodd Fiala     // Let our process instance know the thread has stopped.
771bd7cbc5aSPavel Labath     SetCurrentThreadID (thread_sp->GetID ());
772bd7cbc5aSPavel Labath     SetState (StateType::eStateStopped);
773af245d11STodd Fiala 
774af245d11STodd Fiala     if (log)
775af245d11STodd Fiala     {
776bd7cbc5aSPavel Labath         if (error.Success ())
777af245d11STodd Fiala         {
778af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__);
779af245d11STodd Fiala         }
780af245d11STodd Fiala         else
781af245d11STodd Fiala         {
782af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s inferior launching failed: %s",
783bd7cbc5aSPavel Labath                 __FUNCTION__, error.AsCString ());
784bd7cbc5aSPavel Labath             return -1;
785af245d11STodd Fiala         }
786af245d11STodd Fiala     }
787bd7cbc5aSPavel Labath     return pid;
788af245d11STodd Fiala }
789af245d11STodd Fiala 
790bd7cbc5aSPavel Labath ::pid_t
791bd7cbc5aSPavel Labath NativeProcessLinux::Attach(lldb::pid_t pid, Error &error)
792af245d11STodd Fiala {
793af245d11STodd Fiala     lldb::ThreadSP inferior;
794af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
795af245d11STodd Fiala 
796af245d11STodd Fiala     // Use a map to keep track of the threads which we have attached/need to attach.
797af245d11STodd Fiala     Host::TidMap tids_to_attach;
798af245d11STodd Fiala     if (pid <= 1)
799af245d11STodd Fiala     {
800bd7cbc5aSPavel Labath         error.SetErrorToGenericError();
801bd7cbc5aSPavel Labath         error.SetErrorString("Attaching to process 1 is not allowed.");
802bd7cbc5aSPavel Labath         return -1;
803af245d11STodd Fiala     }
804af245d11STodd Fiala 
805af245d11STodd Fiala     while (Host::FindProcessThreads(pid, tids_to_attach))
806af245d11STodd Fiala     {
807af245d11STodd Fiala         for (Host::TidMap::iterator it = tids_to_attach.begin();
808af245d11STodd Fiala              it != tids_to_attach.end();)
809af245d11STodd Fiala         {
810af245d11STodd Fiala             if (it->second == false)
811af245d11STodd Fiala             {
812af245d11STodd Fiala                 lldb::tid_t tid = it->first;
813af245d11STodd Fiala 
814af245d11STodd Fiala                 // Attach to the requested process.
815af245d11STodd Fiala                 // An attach will cause the thread to stop with a SIGSTOP.
8164a9babb2SPavel Labath                 error = PtraceWrapper(PTRACE_ATTACH, tid);
817bd7cbc5aSPavel Labath                 if (error.Fail())
818af245d11STodd Fiala                 {
819af245d11STodd Fiala                     // No such thread. The thread may have exited.
820af245d11STodd Fiala                     // More error handling may be needed.
821bd7cbc5aSPavel Labath                     if (error.GetError() == ESRCH)
822af245d11STodd Fiala                     {
823af245d11STodd Fiala                         it = tids_to_attach.erase(it);
824af245d11STodd Fiala                         continue;
825af245d11STodd Fiala                     }
826af245d11STodd Fiala                     else
827bd7cbc5aSPavel Labath                         return -1;
828af245d11STodd Fiala                 }
829af245d11STodd Fiala 
830af245d11STodd Fiala                 int status;
831af245d11STodd Fiala                 // Need to use __WALL otherwise we receive an error with errno=ECHLD
832af245d11STodd Fiala                 // At this point we should have a thread stopped if waitpid succeeds.
833af245d11STodd Fiala                 if ((status = waitpid(tid, NULL, __WALL)) < 0)
834af245d11STodd Fiala                 {
835af245d11STodd Fiala                     // No such thread. The thread may have exited.
836af245d11STodd Fiala                     // More error handling may be needed.
837af245d11STodd Fiala                     if (errno == ESRCH)
838af245d11STodd Fiala                     {
839af245d11STodd Fiala                         it = tids_to_attach.erase(it);
840af245d11STodd Fiala                         continue;
841af245d11STodd Fiala                     }
842af245d11STodd Fiala                     else
843af245d11STodd Fiala                     {
844bd7cbc5aSPavel Labath                         error.SetErrorToErrno();
845bd7cbc5aSPavel Labath                         return -1;
846af245d11STodd Fiala                     }
847af245d11STodd Fiala                 }
848af245d11STodd Fiala 
849bd7cbc5aSPavel Labath                 error = SetDefaultPtraceOpts(tid);
850bd7cbc5aSPavel Labath                 if (error.Fail())
851bd7cbc5aSPavel Labath                     return -1;
852af245d11STodd Fiala 
853af245d11STodd Fiala                 if (log)
854af245d11STodd Fiala                     log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
855af245d11STodd Fiala 
856af245d11STodd Fiala                 it->second = true;
857af245d11STodd Fiala 
858af245d11STodd Fiala                 // Create the thread, mark it as stopped.
859bd7cbc5aSPavel Labath                 NativeThreadProtocolSP thread_sp (AddThread (static_cast<lldb::tid_t> (tid)));
860af245d11STodd Fiala                 assert (thread_sp && "AddThread() returned a nullptr");
861fa03ad2eSChaoren Lin 
862fa03ad2eSChaoren Lin                 // This will notify this is a new thread and tell the system it is stopped.
863cb84eebbSTamas Berghammer                 std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP);
8641dbc6c9cSPavel Labath                 ThreadWasCreated(tid);
865bd7cbc5aSPavel Labath                 SetCurrentThreadID (thread_sp->GetID ());
866af245d11STodd Fiala             }
867af245d11STodd Fiala 
868af245d11STodd Fiala             // move the loop forward
869af245d11STodd Fiala             ++it;
870af245d11STodd Fiala         }
871af245d11STodd Fiala     }
872af245d11STodd Fiala 
873af245d11STodd Fiala     if (tids_to_attach.size() > 0)
874af245d11STodd Fiala     {
875bd7cbc5aSPavel Labath         m_pid = pid;
876af245d11STodd Fiala         // Let our process instance know the thread has stopped.
877bd7cbc5aSPavel Labath         SetState (StateType::eStateStopped);
878af245d11STodd Fiala     }
879af245d11STodd Fiala     else
880af245d11STodd Fiala     {
881bd7cbc5aSPavel Labath         error.SetErrorToGenericError();
882bd7cbc5aSPavel Labath         error.SetErrorString("No such process.");
883bd7cbc5aSPavel Labath         return -1;
884af245d11STodd Fiala     }
885af245d11STodd Fiala 
886bd7cbc5aSPavel Labath     return pid;
887af245d11STodd Fiala }
888af245d11STodd Fiala 
88997ccc294SChaoren Lin Error
890af245d11STodd Fiala NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid)
891af245d11STodd Fiala {
892af245d11STodd Fiala     long ptrace_opts = 0;
893af245d11STodd Fiala 
894af245d11STodd Fiala     // Have the child raise an event on exit.  This is used to keep the child in
895af245d11STodd Fiala     // limbo until it is destroyed.
896af245d11STodd Fiala     ptrace_opts |= PTRACE_O_TRACEEXIT;
897af245d11STodd Fiala 
898af245d11STodd Fiala     // Have the tracer trace threads which spawn in the inferior process.
899af245d11STodd Fiala     // TODO: if we want to support tracing the inferiors' child, add the
900af245d11STodd Fiala     // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
901af245d11STodd Fiala     ptrace_opts |= PTRACE_O_TRACECLONE;
902af245d11STodd Fiala 
903af245d11STodd Fiala     // Have the tracer notify us before execve returns
904af245d11STodd Fiala     // (needed to disable legacy SIGTRAP generation)
905af245d11STodd Fiala     ptrace_opts |= PTRACE_O_TRACEEXEC;
906af245d11STodd Fiala 
9074a9babb2SPavel Labath     return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts);
908af245d11STodd Fiala }
909af245d11STodd Fiala 
910af245d11STodd Fiala static ExitType convert_pid_status_to_exit_type (int status)
911af245d11STodd Fiala {
912af245d11STodd Fiala     if (WIFEXITED (status))
913af245d11STodd Fiala         return ExitType::eExitTypeExit;
914af245d11STodd Fiala     else if (WIFSIGNALED (status))
915af245d11STodd Fiala         return ExitType::eExitTypeSignal;
916af245d11STodd Fiala     else if (WIFSTOPPED (status))
917af245d11STodd Fiala         return ExitType::eExitTypeStop;
918af245d11STodd Fiala     else
919af245d11STodd Fiala     {
920af245d11STodd Fiala         // We don't know what this is.
921af245d11STodd Fiala         return ExitType::eExitTypeInvalid;
922af245d11STodd Fiala     }
923af245d11STodd Fiala }
924af245d11STodd Fiala 
925af245d11STodd Fiala static int convert_pid_status_to_return_code (int status)
926af245d11STodd Fiala {
927af245d11STodd Fiala     if (WIFEXITED (status))
928af245d11STodd Fiala         return WEXITSTATUS (status);
929af245d11STodd Fiala     else if (WIFSIGNALED (status))
930af245d11STodd Fiala         return WTERMSIG (status);
931af245d11STodd Fiala     else if (WIFSTOPPED (status))
932af245d11STodd Fiala         return WSTOPSIG (status);
933af245d11STodd Fiala     else
934af245d11STodd Fiala     {
935af245d11STodd Fiala         // We don't know what this is.
936af245d11STodd Fiala         return ExitType::eExitTypeInvalid;
937af245d11STodd Fiala     }
938af245d11STodd Fiala }
939af245d11STodd Fiala 
9401107b5a5SPavel Labath // Handles all waitpid events from the inferior process.
9411107b5a5SPavel Labath void
9421107b5a5SPavel Labath NativeProcessLinux::MonitorCallback(lldb::pid_t pid,
943af245d11STodd Fiala                                     bool exited,
944af245d11STodd Fiala                                     int signal,
945af245d11STodd Fiala                                     int status)
946af245d11STodd Fiala {
947af245d11STodd Fiala     Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS));
948af245d11STodd Fiala 
949af245d11STodd Fiala     // Certain activities differ based on whether the pid is the tid of the main thread.
9501107b5a5SPavel Labath     const bool is_main_thread = (pid == GetID ());
951af245d11STodd Fiala 
952af245d11STodd Fiala     // Handle when the thread exits.
953af245d11STodd Fiala     if (exited)
954af245d11STodd Fiala     {
955af245d11STodd Fiala         if (log)
95686fd8e45SChaoren Lin             log->Printf ("NativeProcessLinux::%s() got exit signal(%d) , tid = %"  PRIu64 " (%s main thread)", __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not");
957af245d11STodd Fiala 
958af245d11STodd Fiala         // This is a thread that exited.  Ensure we're not tracking it anymore.
9591107b5a5SPavel Labath         const bool thread_found = StopTrackingThread (pid);
960af245d11STodd Fiala 
961af245d11STodd Fiala         if (is_main_thread)
962af245d11STodd Fiala         {
963af245d11STodd Fiala             // We only set the exit status and notify the delegate if we haven't already set the process
964af245d11STodd Fiala             // state to an exited state.  We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8)
965af245d11STodd Fiala             // for the main thread.
9661107b5a5SPavel Labath             const bool already_notified = (GetState() == StateType::eStateExited) || (GetState () == StateType::eStateCrashed);
967af245d11STodd Fiala             if (!already_notified)
968af245d11STodd Fiala             {
969af245d11STodd Fiala                 if (log)
9701107b5a5SPavel Labath                     log->Printf ("NativeProcessLinux::%s() tid = %"  PRIu64 " handling main thread exit (%s), expected exit state already set but state was %s instead, setting exit state now", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found", StateAsCString (GetState ()));
971af245d11STodd Fiala                 // The main thread exited.  We're done monitoring.  Report to delegate.
9721107b5a5SPavel Labath                 SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true);
973af245d11STodd Fiala 
974af245d11STodd Fiala                 // Notify delegate that our process has exited.
9751107b5a5SPavel Labath                 SetState (StateType::eStateExited, true);
976af245d11STodd Fiala             }
977af245d11STodd Fiala             else
978af245d11STodd Fiala             {
979af245d11STodd Fiala                 if (log)
980af245d11STodd Fiala                     log->Printf ("NativeProcessLinux::%s() tid = %"  PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found");
981af245d11STodd Fiala             }
982af245d11STodd Fiala         }
983af245d11STodd Fiala         else
984af245d11STodd Fiala         {
985af245d11STodd Fiala             // Do we want to report to the delegate in this case?  I think not.  If this was an orderly
986af245d11STodd Fiala             // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal,
987af245d11STodd Fiala             // and we would have done an all-stop then.
988af245d11STodd Fiala             if (log)
989af245d11STodd Fiala                 log->Printf ("NativeProcessLinux::%s() tid = %"  PRIu64 " handling non-main thread exit (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found");
990af245d11STodd Fiala         }
9911107b5a5SPavel Labath         return;
992af245d11STodd Fiala     }
993af245d11STodd Fiala 
994af245d11STodd Fiala     // Get details on the signal raised.
995af245d11STodd Fiala     siginfo_t info;
9961107b5a5SPavel Labath     const auto err = GetSignalInfo(pid, &info);
99797ccc294SChaoren Lin     if (err.Success())
998fa03ad2eSChaoren Lin     {
999fa03ad2eSChaoren Lin         // We have retrieved the signal info.  Dispatch appropriately.
1000fa03ad2eSChaoren Lin         if (info.si_signo == SIGTRAP)
10011107b5a5SPavel Labath             MonitorSIGTRAP(&info, pid);
1002fa03ad2eSChaoren Lin         else
10031107b5a5SPavel Labath             MonitorSignal(&info, pid, exited);
1004fa03ad2eSChaoren Lin     }
1005fa03ad2eSChaoren Lin     else
1006af245d11STodd Fiala     {
100797ccc294SChaoren Lin         if (err.GetError() == EINVAL)
1008af245d11STodd Fiala         {
1009fa03ad2eSChaoren Lin             // This is a group stop reception for this tid.
101039036ac3SPavel Labath             // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU into the
101139036ac3SPavel Labath             // tracee, triggering the group-stop mechanism. Normally receiving these would stop
101239036ac3SPavel Labath             // the process, pending a SIGCONT. Simulating this state in a debugger is hard and is
101339036ac3SPavel Labath             // generally not needed (one use case is debugging background task being managed by a
101439036ac3SPavel Labath             // shell). For general use, it is sufficient to stop the process in a signal-delivery
101539036ac3SPavel Labath             // stop which happens before the group stop. This done by MonitorSignal and works
101639036ac3SPavel Labath             // correctly for all signals.
1017fa03ad2eSChaoren Lin             if (log)
101839036ac3SPavel Labath                 log->Printf("NativeProcessLinux::%s received a group stop for pid %" PRIu64 " tid %" PRIu64 ". Transparent handling of group stops not supported, resuming the thread.", __FUNCTION__, GetID (), pid);
101939036ac3SPavel Labath             Resume(pid, signal);
1020a9882ceeSTodd Fiala         }
1021a9882ceeSTodd Fiala         else
1022a9882ceeSTodd Fiala         {
1023af245d11STodd Fiala             // ptrace(GETSIGINFO) failed (but not due to group-stop).
1024af245d11STodd Fiala 
1025af245d11STodd Fiala             // A return value of ESRCH means the thread/process is no longer on the system,
1026af245d11STodd Fiala             // so it was killed somehow outside of our control.  Either way, we can't do anything
1027af245d11STodd Fiala             // with it anymore.
1028af245d11STodd Fiala 
1029af245d11STodd Fiala             // Stop tracking the metadata for the thread since it's entirely off the system now.
10301107b5a5SPavel Labath             const bool thread_found = StopTrackingThread (pid);
1031af245d11STodd Fiala 
1032af245d11STodd Fiala             if (log)
1033af245d11STodd Fiala                 log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)",
103497ccc294SChaoren Lin                              __FUNCTION__, err.AsCString(), pid, signal, status, err.GetError() == ESRCH ? "thread/process killed" : "unknown reason", is_main_thread ? "is main thread" : "is not main thread", thread_found ? "thread metadata removed" : "thread metadata not found");
1035af245d11STodd Fiala 
1036af245d11STodd Fiala             if (is_main_thread)
1037af245d11STodd Fiala             {
1038af245d11STodd Fiala                 // Notify the delegate - our process is not available but appears to have been killed outside
1039af245d11STodd Fiala                 // our control.  Is eStateExited the right exit state in this case?
10401107b5a5SPavel Labath                 SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true);
10411107b5a5SPavel Labath                 SetState (StateType::eStateExited, true);
1042af245d11STodd Fiala             }
1043af245d11STodd Fiala             else
1044af245d11STodd Fiala             {
1045af245d11STodd Fiala                 // This thread was pulled out from underneath us.  Anything to do here? Do we want to do an all stop?
1046af245d11STodd Fiala                 if (log)
10471107b5a5SPavel Labath                     log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 " non-main thread exit occurred, didn't tell delegate anything since thread disappeared out from underneath us", __FUNCTION__, GetID (), pid);
1048af245d11STodd Fiala             }
1049af245d11STodd Fiala         }
1050af245d11STodd Fiala     }
1051af245d11STodd Fiala }
1052af245d11STodd Fiala 
1053af245d11STodd Fiala void
1054426bdf88SPavel Labath NativeProcessLinux::WaitForNewThread(::pid_t tid)
1055426bdf88SPavel Labath {
1056426bdf88SPavel Labath     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1057426bdf88SPavel Labath 
1058426bdf88SPavel Labath     NativeThreadProtocolSP new_thread_sp = GetThreadByID(tid);
1059426bdf88SPavel Labath 
1060426bdf88SPavel Labath     if (new_thread_sp)
1061426bdf88SPavel Labath     {
1062426bdf88SPavel Labath         // We are already tracking the thread - we got the event on the new thread (see
1063426bdf88SPavel Labath         // MonitorSignal) before this one. We are done.
1064426bdf88SPavel Labath         return;
1065426bdf88SPavel Labath     }
1066426bdf88SPavel Labath 
1067426bdf88SPavel Labath     // The thread is not tracked yet, let's wait for it to appear.
1068426bdf88SPavel Labath     int status = -1;
1069426bdf88SPavel Labath     ::pid_t wait_pid;
1070426bdf88SPavel Labath     do
1071426bdf88SPavel Labath     {
1072426bdf88SPavel Labath         if (log)
1073426bdf88SPavel Labath             log->Printf ("NativeProcessLinux::%s() received thread creation event for tid %" PRIu32 ". tid not tracked yet, waiting for thread to appear...", __FUNCTION__, tid);
1074426bdf88SPavel Labath         wait_pid = waitpid(tid, &status, __WALL);
1075426bdf88SPavel Labath     }
1076426bdf88SPavel Labath     while (wait_pid == -1 && errno == EINTR);
1077426bdf88SPavel Labath     // Since we are waiting on a specific tid, this must be the creation event. But let's do
1078426bdf88SPavel Labath     // some checks just in case.
1079426bdf88SPavel Labath     if (wait_pid != tid) {
1080426bdf88SPavel Labath         if (log)
1081426bdf88SPavel Labath             log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime", __FUNCTION__, tid);
1082426bdf88SPavel Labath         // The only way I know of this could happen is if the whole process was
1083426bdf88SPavel Labath         // SIGKILLed in the mean time. In any case, we can't do anything about that now.
1084426bdf88SPavel Labath         return;
1085426bdf88SPavel Labath     }
1086426bdf88SPavel Labath     if (WIFEXITED(status))
1087426bdf88SPavel Labath     {
1088426bdf88SPavel Labath         if (log)
1089426bdf88SPavel Labath             log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " returned an 'exited' event. Not tracking the thread.", __FUNCTION__, tid);
1090426bdf88SPavel Labath         // Also a very improbable event.
1091426bdf88SPavel Labath         return;
1092426bdf88SPavel Labath     }
1093426bdf88SPavel Labath 
1094426bdf88SPavel Labath     siginfo_t info;
1095426bdf88SPavel Labath     Error error = GetSignalInfo(tid, &info);
1096426bdf88SPavel Labath     if (error.Fail())
1097426bdf88SPavel Labath     {
1098426bdf88SPavel Labath         if (log)
1099426bdf88SPavel Labath             log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime.", __FUNCTION__, tid);
1100426bdf88SPavel Labath         return;
1101426bdf88SPavel Labath     }
1102426bdf88SPavel Labath 
1103426bdf88SPavel Labath     if (((info.si_pid != 0) || (info.si_code != SI_USER)) && log)
1104426bdf88SPavel Labath     {
1105426bdf88SPavel Labath         // We should be getting a thread creation signal here, but we received something
1106426bdf88SPavel Labath         // else. There isn't much we can do about it now, so we will just log that. Since the
1107426bdf88SPavel Labath         // thread is alive and we are receiving events from it, we shall pretend that it was
1108426bdf88SPavel Labath         // created properly.
1109426bdf88SPavel Labath         log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " received unexpected signal with code %d from pid %d.", __FUNCTION__, tid, info.si_code, info.si_pid);
1110426bdf88SPavel Labath     }
1111426bdf88SPavel Labath 
1112426bdf88SPavel Labath     if (log)
1113426bdf88SPavel Labath         log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 ": tracking new thread tid %" PRIu32,
1114426bdf88SPavel Labath                  __FUNCTION__, GetID (), tid);
1115426bdf88SPavel Labath 
1116426bdf88SPavel Labath     new_thread_sp = AddThread(tid);
1117426bdf88SPavel Labath     std::static_pointer_cast<NativeThreadLinux> (new_thread_sp)->SetRunning ();
1118426bdf88SPavel Labath     Resume (tid, LLDB_INVALID_SIGNAL_NUMBER);
11191dbc6c9cSPavel Labath     ThreadWasCreated(tid);
1120426bdf88SPavel Labath }
1121426bdf88SPavel Labath 
1122426bdf88SPavel Labath void
1123af245d11STodd Fiala NativeProcessLinux::MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid)
1124af245d11STodd Fiala {
1125af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1126af245d11STodd Fiala     const bool is_main_thread = (pid == GetID ());
1127af245d11STodd Fiala 
1128af245d11STodd Fiala     assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
1129af245d11STodd Fiala     if (!info)
1130af245d11STodd Fiala         return;
1131af245d11STodd Fiala 
11325830aa75STamas Berghammer     Mutex::Locker locker (m_threads_mutex);
11335830aa75STamas Berghammer 
1134af245d11STodd Fiala     // See if we can find a thread for this signal.
1135af245d11STodd Fiala     NativeThreadProtocolSP thread_sp = GetThreadByID (pid);
1136af245d11STodd Fiala     if (!thread_sp)
1137af245d11STodd Fiala     {
1138af245d11STodd Fiala         if (log)
1139af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid);
1140af245d11STodd Fiala     }
1141af245d11STodd Fiala 
1142af245d11STodd Fiala     switch (info->si_code)
1143af245d11STodd Fiala     {
1144af245d11STodd Fiala     // TODO: these two cases are required if we want to support tracing of the inferiors' children.  We'd need this to debug a monitor.
1145af245d11STodd Fiala     // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1146af245d11STodd Fiala     // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1147af245d11STodd Fiala 
1148af245d11STodd Fiala     case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1149af245d11STodd Fiala     {
11505fd24c67SPavel Labath         // This is the notification on the parent thread which informs us of new thread
1151426bdf88SPavel Labath         // creation.
1152426bdf88SPavel Labath         // We don't want to do anything with the parent thread so we just resume it. In case we
1153426bdf88SPavel Labath         // want to implement "break on thread creation" functionality, we would need to stop
1154426bdf88SPavel Labath         // here.
1155af245d11STodd Fiala 
1156af245d11STodd Fiala         unsigned long event_message = 0;
1157426bdf88SPavel Labath         if (GetEventMessage (pid, &event_message).Fail())
1158fa03ad2eSChaoren Lin         {
1159426bdf88SPavel Labath             if (log)
1160fa03ad2eSChaoren Lin                 log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event but GetEventMessage failed so we don't know the new tid", __FUNCTION__, pid);
1161426bdf88SPavel Labath         } else
1162426bdf88SPavel Labath             WaitForNewThread(event_message);
1163af245d11STodd Fiala 
11645fd24c67SPavel Labath         Resume (pid, LLDB_INVALID_SIGNAL_NUMBER);
1165af245d11STodd Fiala         break;
1166af245d11STodd Fiala     }
1167af245d11STodd Fiala 
1168af245d11STodd Fiala     case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
1169a9882ceeSTodd Fiala     {
1170a9882ceeSTodd Fiala         NativeThreadProtocolSP main_thread_sp;
1171af245d11STodd Fiala         if (log)
1172af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1173a9882ceeSTodd Fiala 
11741dbc6c9cSPavel Labath         // Exec clears any pending notifications.
11751dbc6c9cSPavel Labath         m_pending_notification_up.reset ();
1176fa03ad2eSChaoren Lin 
1177fa03ad2eSChaoren Lin         // Remove all but the main thread here.  Linux fork creates a new process which only copies the main thread.  Mutexes are in undefined state.
1178a9882ceeSTodd Fiala         if (log)
1179a9882ceeSTodd Fiala             log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__);
1180a9882ceeSTodd Fiala 
1181a9882ceeSTodd Fiala         for (auto thread_sp : m_threads)
1182a9882ceeSTodd Fiala         {
1183a9882ceeSTodd Fiala             const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID ();
1184a9882ceeSTodd Fiala             if (is_main_thread)
1185a9882ceeSTodd Fiala             {
1186a9882ceeSTodd Fiala                 main_thread_sp = thread_sp;
1187a9882ceeSTodd Fiala                 if (log)
1188a9882ceeSTodd Fiala                     log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ());
1189a9882ceeSTodd Fiala             }
1190a9882ceeSTodd Fiala             else
1191a9882ceeSTodd Fiala             {
1192fa03ad2eSChaoren Lin                 // Tell thread coordinator this thread is dead.
1193a9882ceeSTodd Fiala                 if (log)
1194a9882ceeSTodd Fiala                     log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ());
1195a9882ceeSTodd Fiala             }
1196a9882ceeSTodd Fiala         }
1197a9882ceeSTodd Fiala 
1198a9882ceeSTodd Fiala         m_threads.clear ();
1199a9882ceeSTodd Fiala 
1200a9882ceeSTodd Fiala         if (main_thread_sp)
1201a9882ceeSTodd Fiala         {
1202a9882ceeSTodd Fiala             m_threads.push_back (main_thread_sp);
1203a9882ceeSTodd Fiala             SetCurrentThreadID (main_thread_sp->GetID ());
1204cb84eebbSTamas Berghammer             std::static_pointer_cast<NativeThreadLinux> (main_thread_sp)->SetStoppedByExec ();
1205a9882ceeSTodd Fiala         }
1206a9882ceeSTodd Fiala         else
1207a9882ceeSTodd Fiala         {
1208a9882ceeSTodd Fiala             SetCurrentThreadID (LLDB_INVALID_THREAD_ID);
1209a9882ceeSTodd Fiala             if (log)
1210a9882ceeSTodd Fiala                 log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ());
1211a9882ceeSTodd Fiala         }
1212a9882ceeSTodd Fiala 
1213fa03ad2eSChaoren Lin         // Tell coordinator about about the "new" (since exec) stopped main thread.
1214fa03ad2eSChaoren Lin         const lldb::tid_t main_thread_tid = GetID ();
12151dbc6c9cSPavel Labath         ThreadWasCreated(main_thread_tid);
1216fa03ad2eSChaoren Lin 
1217fa03ad2eSChaoren Lin         // NOTE: ideally these next statements would execute at the same time as the coordinator thread create was executed.
1218fa03ad2eSChaoren Lin         // Consider a handler that can execute when that happens.
1219a9882ceeSTodd Fiala         // Let our delegate know we have just exec'd.
1220a9882ceeSTodd Fiala         NotifyDidExec ();
1221a9882ceeSTodd Fiala 
1222a9882ceeSTodd Fiala         // If we have a main thread, indicate we are stopped.
1223a9882ceeSTodd Fiala         assert (main_thread_sp && "exec called during ptraced process but no main thread metadata tracked");
1224fa03ad2eSChaoren Lin 
1225fa03ad2eSChaoren Lin         // Let the process know we're stopped.
1226ed89c7feSPavel Labath         StopRunningThreads (pid);
1227a9882ceeSTodd Fiala 
1228af245d11STodd Fiala         break;
1229a9882ceeSTodd Fiala     }
1230af245d11STodd Fiala 
1231af245d11STodd Fiala     case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1232af245d11STodd Fiala     {
1233af245d11STodd Fiala         // The inferior process or one of its threads is about to exit.
12346e35163cSPavel Labath         // We don't want to do anything with the thread so we just resume it. In case we
12356e35163cSPavel Labath         // want to implement "break on thread exit" functionality, we would need to stop
12366e35163cSPavel Labath         // here.
1237fa03ad2eSChaoren Lin 
1238af245d11STodd Fiala         unsigned long data = 0;
123997ccc294SChaoren Lin         if (GetEventMessage(pid, &data).Fail())
1240af245d11STodd Fiala             data = -1;
1241af245d11STodd Fiala 
1242af245d11STodd Fiala         if (log)
1243af245d11STodd Fiala         {
1244af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)",
1245af245d11STodd Fiala                          __FUNCTION__,
1246af245d11STodd Fiala                          data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false",
1247af245d11STodd Fiala                          pid,
1248af245d11STodd Fiala                     is_main_thread ? "is main thread" : "not main thread");
1249af245d11STodd Fiala         }
1250af245d11STodd Fiala 
1251af245d11STodd Fiala         if (is_main_thread)
1252af245d11STodd Fiala         {
1253af245d11STodd Fiala             SetExitStatus (convert_pid_status_to_exit_type (data), convert_pid_status_to_return_code (data), nullptr, true);
125475f47c3aSTodd Fiala         }
125575f47c3aSTodd Fiala 
12566e35163cSPavel Labath         Resume(pid, LLDB_INVALID_SIGNAL_NUMBER);
1257af245d11STodd Fiala 
1258af245d11STodd Fiala         break;
1259af245d11STodd Fiala     }
1260af245d11STodd Fiala 
1261af245d11STodd Fiala     case 0:
1262c16f5dcaSChaoren Lin     case TRAP_TRACE:  // We receive this on single stepping.
1263c16f5dcaSChaoren Lin     case TRAP_HWBKPT: // We receive this on watchpoint hit
126486fd8e45SChaoren Lin         if (thread_sp)
126586fd8e45SChaoren Lin         {
1266c16f5dcaSChaoren Lin             // If a watchpoint was hit, report it
1267c16f5dcaSChaoren Lin             uint32_t wp_index;
1268ea8c25a8SOmair Javaid             Error error = thread_sp->GetRegisterContext()->GetWatchpointHitIndex(wp_index, (lldb::addr_t)info->si_addr);
1269c16f5dcaSChaoren Lin             if (error.Fail() && log)
1270c16f5dcaSChaoren Lin                 log->Printf("NativeProcessLinux::%s() "
1271c16f5dcaSChaoren Lin                             "received error while checking for watchpoint hits, "
1272c16f5dcaSChaoren Lin                             "pid = %" PRIu64 " error = %s",
1273c16f5dcaSChaoren Lin                             __FUNCTION__, pid, error.AsCString());
1274c16f5dcaSChaoren Lin             if (wp_index != LLDB_INVALID_INDEX32)
12755830aa75STamas Berghammer             {
1276c16f5dcaSChaoren Lin                 MonitorWatchpoint(pid, thread_sp, wp_index);
1277c16f5dcaSChaoren Lin                 break;
1278c16f5dcaSChaoren Lin             }
1279c16f5dcaSChaoren Lin         }
1280c16f5dcaSChaoren Lin         // Otherwise, report step over
1281c16f5dcaSChaoren Lin         MonitorTrace(pid, thread_sp);
1282af245d11STodd Fiala         break;
1283af245d11STodd Fiala 
1284af245d11STodd Fiala     case SI_KERNEL:
128535799963SMohit K. Bhakkad #if defined __mips__
128635799963SMohit K. Bhakkad         // For mips there is no special signal for watchpoint
128735799963SMohit K. Bhakkad         // So we check for watchpoint in kernel trap
128835799963SMohit K. Bhakkad         if (thread_sp)
128935799963SMohit K. Bhakkad         {
129035799963SMohit K. Bhakkad             // If a watchpoint was hit, report it
129135799963SMohit K. Bhakkad             uint32_t wp_index;
1292c60c9452SJaydeep Patil             Error error = thread_sp->GetRegisterContext()->GetWatchpointHitIndex(wp_index, LLDB_INVALID_ADDRESS);
129335799963SMohit K. Bhakkad             if (error.Fail() && log)
129435799963SMohit K. Bhakkad                 log->Printf("NativeProcessLinux::%s() "
129535799963SMohit K. Bhakkad                             "received error while checking for watchpoint hits, "
129635799963SMohit K. Bhakkad                             "pid = %" PRIu64 " error = %s",
129735799963SMohit K. Bhakkad                             __FUNCTION__, pid, error.AsCString());
129835799963SMohit K. Bhakkad             if (wp_index != LLDB_INVALID_INDEX32)
129935799963SMohit K. Bhakkad             {
130035799963SMohit K. Bhakkad                 MonitorWatchpoint(pid, thread_sp, wp_index);
130135799963SMohit K. Bhakkad                 break;
130235799963SMohit K. Bhakkad             }
130335799963SMohit K. Bhakkad         }
130435799963SMohit K. Bhakkad         // NO BREAK
130535799963SMohit K. Bhakkad #endif
1306af245d11STodd Fiala     case TRAP_BRKPT:
1307c16f5dcaSChaoren Lin         MonitorBreakpoint(pid, thread_sp);
1308af245d11STodd Fiala         break;
1309af245d11STodd Fiala 
1310af245d11STodd Fiala     case SIGTRAP:
1311af245d11STodd Fiala     case (SIGTRAP | 0x80):
1312af245d11STodd Fiala         if (log)
1313fa03ad2eSChaoren Lin             log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), pid);
1314fa03ad2eSChaoren Lin 
1315af245d11STodd Fiala         // Ignore these signals until we know more about them.
13166e35163cSPavel Labath         Resume(pid, LLDB_INVALID_SIGNAL_NUMBER);
1317af245d11STodd Fiala         break;
1318af245d11STodd Fiala 
1319af245d11STodd Fiala     default:
1320af245d11STodd Fiala         assert(false && "Unexpected SIGTRAP code!");
1321af245d11STodd Fiala         if (log)
13226e35163cSPavel Labath             log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 "tid %" PRIu64 " received unhandled SIGTRAP code: 0x%d",
13236e35163cSPavel Labath                     __FUNCTION__, GetID (), pid, info->si_code);
1324af245d11STodd Fiala         break;
1325af245d11STodd Fiala 
1326af245d11STodd Fiala     }
1327af245d11STodd Fiala }
1328af245d11STodd Fiala 
1329af245d11STodd Fiala void
1330c16f5dcaSChaoren Lin NativeProcessLinux::MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp)
1331c16f5dcaSChaoren Lin {
1332c16f5dcaSChaoren Lin     Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
1333c16f5dcaSChaoren Lin     if (log)
1334c16f5dcaSChaoren Lin         log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)",
1335c16f5dcaSChaoren Lin                 __FUNCTION__, pid);
1336c16f5dcaSChaoren Lin 
1337c16f5dcaSChaoren Lin     if (thread_sp)
1338c16f5dcaSChaoren Lin         std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace();
1339c16f5dcaSChaoren Lin 
1340c16f5dcaSChaoren Lin     // This thread is currently stopped.
13411dbc6c9cSPavel Labath     ThreadDidStop(pid, false);
1342c16f5dcaSChaoren Lin 
1343c16f5dcaSChaoren Lin     // Here we don't have to request the rest of the threads to stop or request a deferred stop.
1344c16f5dcaSChaoren Lin     // This would have already happened at the time the Resume() with step operation was signaled.
1345c16f5dcaSChaoren Lin     // At this point, we just need to say we stopped, and the deferred notifcation will fire off
1346c16f5dcaSChaoren Lin     // once all running threads have checked in as stopped.
1347c16f5dcaSChaoren Lin     SetCurrentThreadID(pid);
1348c16f5dcaSChaoren Lin     // Tell the process we have a stop (from software breakpoint).
1349ed89c7feSPavel Labath     StopRunningThreads(pid);
1350c16f5dcaSChaoren Lin }
1351c16f5dcaSChaoren Lin 
1352c16f5dcaSChaoren Lin void
1353c16f5dcaSChaoren Lin NativeProcessLinux::MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp)
1354c16f5dcaSChaoren Lin {
1355c16f5dcaSChaoren Lin     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS));
1356c16f5dcaSChaoren Lin     if (log)
1357c16f5dcaSChaoren Lin         log->Printf("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64,
1358c16f5dcaSChaoren Lin                 __FUNCTION__, pid);
1359c16f5dcaSChaoren Lin 
1360c16f5dcaSChaoren Lin     // This thread is currently stopped.
13611dbc6c9cSPavel Labath     ThreadDidStop(pid, false);
1362c16f5dcaSChaoren Lin 
1363c16f5dcaSChaoren Lin     // Mark the thread as stopped at breakpoint.
1364c16f5dcaSChaoren Lin     if (thread_sp)
1365c16f5dcaSChaoren Lin     {
1366c16f5dcaSChaoren Lin         std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByBreakpoint();
1367c16f5dcaSChaoren Lin         Error error = FixupBreakpointPCAsNeeded(thread_sp);
1368c16f5dcaSChaoren Lin         if (error.Fail())
1369c16f5dcaSChaoren Lin             if (log)
1370c16f5dcaSChaoren Lin                 log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s",
1371c16f5dcaSChaoren Lin                         __FUNCTION__, pid, error.AsCString());
1372d8c338d4STamas Berghammer 
13739eb1ecb9SPavel Labath         if (m_threads_stepping_with_breakpoint.find(pid) != m_threads_stepping_with_breakpoint.end())
1374d8c338d4STamas Berghammer             std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace();
1375d8c338d4STamas Berghammer     }
1376c16f5dcaSChaoren Lin     else
1377c16f5dcaSChaoren Lin         if (log)
1378c16f5dcaSChaoren Lin             log->Printf("NativeProcessLinux::%s()  pid = %" PRIu64 ": "
1379c16f5dcaSChaoren Lin                     "warning, cannot process software breakpoint since no thread metadata",
1380c16f5dcaSChaoren Lin                     __FUNCTION__, pid);
1381c16f5dcaSChaoren Lin 
1382c16f5dcaSChaoren Lin 
1383c16f5dcaSChaoren Lin     // We need to tell all other running threads before we notify the delegate about this stop.
1384ed89c7feSPavel Labath     StopRunningThreads(pid);
1385c16f5dcaSChaoren Lin }
1386c16f5dcaSChaoren Lin 
1387c16f5dcaSChaoren Lin void
1388c16f5dcaSChaoren Lin NativeProcessLinux::MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index)
1389c16f5dcaSChaoren Lin {
1390c16f5dcaSChaoren Lin     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS));
1391c16f5dcaSChaoren Lin     if (log)
1392c16f5dcaSChaoren Lin         log->Printf("NativeProcessLinux::%s() received watchpoint event, "
1393c16f5dcaSChaoren Lin                     "pid = %" PRIu64 ", wp_index = %" PRIu32,
1394c16f5dcaSChaoren Lin                     __FUNCTION__, pid, wp_index);
1395c16f5dcaSChaoren Lin 
1396c16f5dcaSChaoren Lin     // This thread is currently stopped.
13971dbc6c9cSPavel Labath     ThreadDidStop(pid, false);
1398c16f5dcaSChaoren Lin 
1399c16f5dcaSChaoren Lin     // Mark the thread as stopped at watchpoint.
1400c16f5dcaSChaoren Lin     // The address is at (lldb::addr_t)info->si_addr if we need it.
1401c16f5dcaSChaoren Lin     lldbassert(thread_sp && "thread_sp cannot be NULL");
1402c16f5dcaSChaoren Lin     std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByWatchpoint(wp_index);
1403c16f5dcaSChaoren Lin 
1404c16f5dcaSChaoren Lin     // We need to tell all other running threads before we notify the delegate about this stop.
1405ed89c7feSPavel Labath     StopRunningThreads(pid);
1406c16f5dcaSChaoren Lin }
1407c16f5dcaSChaoren Lin 
1408c16f5dcaSChaoren Lin void
1409af245d11STodd Fiala NativeProcessLinux::MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited)
1410af245d11STodd Fiala {
1411511e5cdcSTodd Fiala     assert (info && "null info");
1412511e5cdcSTodd Fiala     if (!info)
1413511e5cdcSTodd Fiala         return;
1414511e5cdcSTodd Fiala 
1415511e5cdcSTodd Fiala     const int signo = info->si_signo;
1416511e5cdcSTodd Fiala     const bool is_from_llgs = info->si_pid == getpid ();
1417af245d11STodd Fiala 
1418af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1419af245d11STodd Fiala 
1420af245d11STodd Fiala     // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1421af245d11STodd Fiala     // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1422af245d11STodd Fiala     // kill(2) or raise(3).  Similarly for tgkill(2) on Linux.
1423af245d11STodd Fiala     //
1424af245d11STodd Fiala     // IOW, user generated signals never generate what we consider to be a
1425af245d11STodd Fiala     // "crash".
1426af245d11STodd Fiala     //
1427af245d11STodd Fiala     // Similarly, ACK signals generated by this monitor.
1428af245d11STodd Fiala 
14295830aa75STamas Berghammer     Mutex::Locker locker (m_threads_mutex);
14305830aa75STamas Berghammer 
1431af245d11STodd Fiala     // See if we can find a thread for this signal.
1432af245d11STodd Fiala     NativeThreadProtocolSP thread_sp = GetThreadByID (pid);
1433af245d11STodd Fiala     if (!thread_sp)
1434af245d11STodd Fiala     {
1435af245d11STodd Fiala         if (log)
1436af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid);
1437af245d11STodd Fiala     }
1438af245d11STodd Fiala 
1439af245d11STodd Fiala     // Handle the signal.
1440af245d11STodd Fiala     if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1441af245d11STodd Fiala     {
1442af245d11STodd Fiala         if (log)
1443af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")",
1444af245d11STodd Fiala                             __FUNCTION__,
144598d0a4b3SChaoren Lin                             Host::GetSignalAsCString(signo),
1446af245d11STodd Fiala                             signo,
1447af245d11STodd Fiala                             (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1448af245d11STodd Fiala                             info->si_pid,
1449511e5cdcSTodd Fiala                             is_from_llgs ? "from llgs" : "not from llgs",
1450af245d11STodd Fiala                             pid);
145158a2f669STodd Fiala     }
1452af245d11STodd Fiala 
145358a2f669STodd Fiala     // Check for new thread notification.
145458a2f669STodd Fiala     if ((info->si_pid == 0) && (info->si_code == SI_USER))
1455af245d11STodd Fiala     {
1456af245d11STodd Fiala         // A new thread creation is being signaled. This is one of two parts that come in
1457426bdf88SPavel Labath         // a non-deterministic order. This code handles the case where the new thread event comes
1458426bdf88SPavel Labath         // before the event on the parent thread. For the opposite case see code in
1459426bdf88SPavel Labath         // MonitorSIGTRAP.
1460af245d11STodd Fiala         if (log)
1461af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " tid %" PRIu64 ": new thread notification",
1462af245d11STodd Fiala                      __FUNCTION__, GetID (), pid);
1463af245d11STodd Fiala 
14645fd24c67SPavel Labath         thread_sp = AddThread(pid);
14655fd24c67SPavel Labath         assert (thread_sp.get() && "failed to create the tracking data for newly created inferior thread");
14665fd24c67SPavel Labath         // We can now resume the newly created thread.
1467cb84eebbSTamas Berghammer         std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning ();
14685fd24c67SPavel Labath         Resume (pid, LLDB_INVALID_SIGNAL_NUMBER);
14691dbc6c9cSPavel Labath         ThreadWasCreated(pid);
147058a2f669STodd Fiala         // Done handling.
147158a2f669STodd Fiala         return;
1472af245d11STodd Fiala     }
147358a2f669STodd Fiala 
147458a2f669STodd Fiala     // Check for thread stop notification.
1475511e5cdcSTodd Fiala     if (is_from_llgs && (info->si_code == SI_TKILL) && (signo == SIGSTOP))
1476af245d11STodd Fiala     {
1477af245d11STodd Fiala         // This is a tgkill()-based stop.
1478af245d11STodd Fiala         if (thread_sp)
1479af245d11STodd Fiala         {
1480fa03ad2eSChaoren Lin             if (log)
1481fa03ad2eSChaoren Lin                 log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped",
1482fa03ad2eSChaoren Lin                              __FUNCTION__,
1483fa03ad2eSChaoren Lin                              GetID (),
1484fa03ad2eSChaoren Lin                              pid);
1485fa03ad2eSChaoren Lin 
1486aab58633SChaoren Lin             // Check that we're not already marked with a stop reason.
1487aab58633SChaoren Lin             // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that
1488aab58633SChaoren Lin             // the kernel signaled us with the thread stopping which we handled and marked as stopped,
1489aab58633SChaoren Lin             // and that, without an intervening resume, we received another stop.  It is more likely
1490aab58633SChaoren Lin             // that we are missing the marking of a run state somewhere if we find that the thread was
1491aab58633SChaoren Lin             // marked as stopped.
1492cb84eebbSTamas Berghammer             std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp);
1493cb84eebbSTamas Berghammer             assert (linux_thread_sp && "linux_thread_sp is null!");
1494aab58633SChaoren Lin 
1495cb84eebbSTamas Berghammer             const StateType thread_state = linux_thread_sp->GetState ();
1496aab58633SChaoren Lin             if (!StateIsStoppedState (thread_state, false))
1497aab58633SChaoren Lin             {
1498ed89c7feSPavel Labath                 // An inferior thread has stopped because of a SIGSTOP we have sent it.
1499ed89c7feSPavel Labath                 // Generally, these are not important stops and we don't want to report them as
1500ed89c7feSPavel Labath                 // they are just used to stop other threads when one thread (the one with the
1501ed89c7feSPavel Labath                 // *real* stop reason) hits a breakpoint (watchpoint, etc...). However, in the
1502ed89c7feSPavel Labath                 // case of an asynchronous Interrupt(), this *is* the real stop reason, so we
1503ed89c7feSPavel Labath                 // leave the signal intact if this is the thread that was chosen as the
1504ed89c7feSPavel Labath                 // triggering thread.
1505ed89c7feSPavel Labath                 if (m_pending_notification_up && m_pending_notification_up->triggering_tid == pid)
1506c4e25c96SPavel Labath                     linux_thread_sp->SetStoppedBySignal(SIGSTOP, info);
1507ed89c7feSPavel Labath                 else
150805569f67SPavel Labath                     linux_thread_sp->SetStoppedWithNoReason();
1509ed89c7feSPavel Labath 
1510af245d11STodd Fiala                 SetCurrentThreadID (thread_sp->GetID ());
15111dbc6c9cSPavel Labath                 ThreadDidStop (thread_sp->GetID (), true);
1512aab58633SChaoren Lin             }
1513aab58633SChaoren Lin             else
1514aab58633SChaoren Lin             {
1515aab58633SChaoren Lin                 if (log)
1516aab58633SChaoren Lin                 {
1517aab58633SChaoren Lin                     // Retrieve the signal name if the thread was stopped by a signal.
1518aab58633SChaoren Lin                     int stop_signo = 0;
1519cb84eebbSTamas Berghammer                     const bool stopped_by_signal = linux_thread_sp->IsStopped (&stop_signo);
152098d0a4b3SChaoren Lin                     const char *signal_name = stopped_by_signal ? Host::GetSignalAsCString(stop_signo) : "<not stopped by signal>";
1521aab58633SChaoren Lin                     if (!signal_name)
1522aab58633SChaoren Lin                         signal_name = "<no-signal-name>";
1523aab58633SChaoren Lin 
1524aab58633SChaoren Lin                     log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread was already marked as a stopped state (state=%s, signal=%d (%s)), leaving stop signal as is",
1525aab58633SChaoren Lin                                  __FUNCTION__,
1526aab58633SChaoren Lin                                  GetID (),
1527cb84eebbSTamas Berghammer                                  linux_thread_sp->GetID (),
1528aab58633SChaoren Lin                                  StateAsCString (thread_state),
1529aab58633SChaoren Lin                                  stop_signo,
1530aab58633SChaoren Lin                                  signal_name);
1531aab58633SChaoren Lin                 }
15321dbc6c9cSPavel Labath                 ThreadDidStop (thread_sp->GetID (), false);
1533af245d11STodd Fiala             }
153486fd8e45SChaoren Lin         }
1535af245d11STodd Fiala 
153658a2f669STodd Fiala         // Done handling.
1537af245d11STodd Fiala         return;
1538af245d11STodd Fiala     }
1539af245d11STodd Fiala 
1540af245d11STodd Fiala     if (log)
154198d0a4b3SChaoren Lin         log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, Host::GetSignalAsCString(signo));
1542af245d11STodd Fiala 
154386fd8e45SChaoren Lin     // This thread is stopped.
15441dbc6c9cSPavel Labath     ThreadDidStop (pid, false);
154586fd8e45SChaoren Lin 
154686fd8e45SChaoren Lin     if (thread_sp)
1547c4e25c96SPavel Labath         std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal(signo, info);
154886fd8e45SChaoren Lin 
154986fd8e45SChaoren Lin     // Send a stop to the debugger after we get all other threads to stop.
1550ed89c7feSPavel Labath     StopRunningThreads (pid);
1551511e5cdcSTodd Fiala }
1552af245d11STodd Fiala 
1553e7708688STamas Berghammer namespace {
1554e7708688STamas Berghammer 
1555e7708688STamas Berghammer struct EmulatorBaton
1556e7708688STamas Berghammer {
1557e7708688STamas Berghammer     NativeProcessLinux* m_process;
1558e7708688STamas Berghammer     NativeRegisterContext* m_reg_context;
15596648fcc3SPavel Labath 
15606648fcc3SPavel Labath     // eRegisterKindDWARF -> RegsiterValue
15616648fcc3SPavel Labath     std::unordered_map<uint32_t, RegisterValue> m_register_values;
1562e7708688STamas Berghammer 
1563e7708688STamas Berghammer     EmulatorBaton(NativeProcessLinux* process, NativeRegisterContext* reg_context) :
1564e7708688STamas Berghammer             m_process(process), m_reg_context(reg_context) {}
1565e7708688STamas Berghammer };
1566e7708688STamas Berghammer 
1567e7708688STamas Berghammer } // anonymous namespace
1568e7708688STamas Berghammer 
1569e7708688STamas Berghammer static size_t
1570e7708688STamas Berghammer ReadMemoryCallback (EmulateInstruction *instruction,
1571e7708688STamas Berghammer                     void *baton,
1572e7708688STamas Berghammer                     const EmulateInstruction::Context &context,
1573e7708688STamas Berghammer                     lldb::addr_t addr,
1574e7708688STamas Berghammer                     void *dst,
1575e7708688STamas Berghammer                     size_t length)
1576e7708688STamas Berghammer {
1577e7708688STamas Berghammer     EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton);
1578e7708688STamas Berghammer 
15793eb4b458SChaoren Lin     size_t bytes_read;
1580e7708688STamas Berghammer     emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read);
1581e7708688STamas Berghammer     return bytes_read;
1582e7708688STamas Berghammer }
1583e7708688STamas Berghammer 
1584e7708688STamas Berghammer static bool
1585e7708688STamas Berghammer ReadRegisterCallback (EmulateInstruction *instruction,
1586e7708688STamas Berghammer                       void *baton,
1587e7708688STamas Berghammer                       const RegisterInfo *reg_info,
1588e7708688STamas Berghammer                       RegisterValue &reg_value)
1589e7708688STamas Berghammer {
1590e7708688STamas Berghammer     EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton);
1591e7708688STamas Berghammer 
15926648fcc3SPavel Labath     auto it = emulator_baton->m_register_values.find(reg_info->kinds[eRegisterKindDWARF]);
15936648fcc3SPavel Labath     if (it != emulator_baton->m_register_values.end())
15946648fcc3SPavel Labath     {
15956648fcc3SPavel Labath         reg_value = it->second;
15966648fcc3SPavel Labath         return true;
15976648fcc3SPavel Labath     }
15986648fcc3SPavel Labath 
1599e7708688STamas Berghammer     // The emulator only fill in the dwarf regsiter numbers (and in some case
1600e7708688STamas Berghammer     // the generic register numbers). Get the full register info from the
1601e7708688STamas Berghammer     // register context based on the dwarf register numbers.
1602e7708688STamas Berghammer     const RegisterInfo* full_reg_info = emulator_baton->m_reg_context->GetRegisterInfo(
1603e7708688STamas Berghammer             eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]);
1604e7708688STamas Berghammer 
1605e7708688STamas Berghammer     Error error = emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value);
16066648fcc3SPavel Labath     if (error.Success())
16076648fcc3SPavel Labath         return true;
1608cdc22a88SMohit K. Bhakkad 
16096648fcc3SPavel Labath     return false;
1610e7708688STamas Berghammer }
1611e7708688STamas Berghammer 
1612e7708688STamas Berghammer static bool
1613e7708688STamas Berghammer WriteRegisterCallback (EmulateInstruction *instruction,
1614e7708688STamas Berghammer                        void *baton,
1615e7708688STamas Berghammer                        const EmulateInstruction::Context &context,
1616e7708688STamas Berghammer                        const RegisterInfo *reg_info,
1617e7708688STamas Berghammer                        const RegisterValue &reg_value)
1618e7708688STamas Berghammer {
1619e7708688STamas Berghammer     EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton);
16206648fcc3SPavel Labath     emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = reg_value;
1621e7708688STamas Berghammer     return true;
1622e7708688STamas Berghammer }
1623e7708688STamas Berghammer 
1624e7708688STamas Berghammer static size_t
1625e7708688STamas Berghammer WriteMemoryCallback (EmulateInstruction *instruction,
1626e7708688STamas Berghammer                      void *baton,
1627e7708688STamas Berghammer                      const EmulateInstruction::Context &context,
1628e7708688STamas Berghammer                      lldb::addr_t addr,
1629e7708688STamas Berghammer                      const void *dst,
1630e7708688STamas Berghammer                      size_t length)
1631e7708688STamas Berghammer {
1632e7708688STamas Berghammer     return length;
1633e7708688STamas Berghammer }
1634e7708688STamas Berghammer 
1635e7708688STamas Berghammer static lldb::addr_t
1636e7708688STamas Berghammer ReadFlags (NativeRegisterContext* regsiter_context)
1637e7708688STamas Berghammer {
1638e7708688STamas Berghammer     const RegisterInfo* flags_info = regsiter_context->GetRegisterInfo(
1639e7708688STamas Berghammer             eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS);
1640e7708688STamas Berghammer     return regsiter_context->ReadRegisterAsUnsigned(flags_info, LLDB_INVALID_ADDRESS);
1641e7708688STamas Berghammer }
1642e7708688STamas Berghammer 
1643e7708688STamas Berghammer Error
1644e7708688STamas Berghammer NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp)
1645e7708688STamas Berghammer {
1646e7708688STamas Berghammer     Error error;
1647e7708688STamas Berghammer     NativeRegisterContextSP register_context_sp = thread_sp->GetRegisterContext();
1648e7708688STamas Berghammer 
1649e7708688STamas Berghammer     std::unique_ptr<EmulateInstruction> emulator_ap(
1650e7708688STamas Berghammer         EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, nullptr));
1651e7708688STamas Berghammer 
1652e7708688STamas Berghammer     if (emulator_ap == nullptr)
1653e7708688STamas Berghammer         return Error("Instruction emulator not found!");
1654e7708688STamas Berghammer 
1655e7708688STamas Berghammer     EmulatorBaton baton(this, register_context_sp.get());
1656e7708688STamas Berghammer     emulator_ap->SetBaton(&baton);
1657e7708688STamas Berghammer     emulator_ap->SetReadMemCallback(&ReadMemoryCallback);
1658e7708688STamas Berghammer     emulator_ap->SetReadRegCallback(&ReadRegisterCallback);
1659e7708688STamas Berghammer     emulator_ap->SetWriteMemCallback(&WriteMemoryCallback);
1660e7708688STamas Berghammer     emulator_ap->SetWriteRegCallback(&WriteRegisterCallback);
1661e7708688STamas Berghammer 
1662e7708688STamas Berghammer     if (!emulator_ap->ReadInstruction())
1663e7708688STamas Berghammer         return Error("Read instruction failed!");
1664e7708688STamas Berghammer 
16656648fcc3SPavel Labath     bool emulation_result = emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC);
16666648fcc3SPavel Labath 
16676648fcc3SPavel Labath     const RegisterInfo* reg_info_pc = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
16686648fcc3SPavel Labath     const RegisterInfo* reg_info_flags = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS);
16696648fcc3SPavel Labath 
16706648fcc3SPavel Labath     auto pc_it = baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]);
16716648fcc3SPavel Labath     auto flags_it = baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]);
16726648fcc3SPavel Labath 
1673e7708688STamas Berghammer     lldb::addr_t next_pc;
1674e7708688STamas Berghammer     lldb::addr_t next_flags;
16756648fcc3SPavel Labath     if (emulation_result)
1676e7708688STamas Berghammer     {
16776648fcc3SPavel Labath         assert(pc_it != baton.m_register_values.end() && "Emulation was successfull but PC wasn't updated");
16786648fcc3SPavel Labath         next_pc = pc_it->second.GetAsUInt64();
16796648fcc3SPavel Labath 
16806648fcc3SPavel Labath         if (flags_it != baton.m_register_values.end())
16816648fcc3SPavel Labath             next_flags = flags_it->second.GetAsUInt64();
1682e7708688STamas Berghammer         else
1683e7708688STamas Berghammer             next_flags = ReadFlags (register_context_sp.get());
1684e7708688STamas Berghammer     }
16856648fcc3SPavel Labath     else if (pc_it == baton.m_register_values.end())
1686e7708688STamas Berghammer     {
1687e7708688STamas Berghammer         // Emulate instruction failed and it haven't changed PC. Advance PC
1688e7708688STamas Berghammer         // with the size of the current opcode because the emulation of all
1689e7708688STamas Berghammer         // PC modifying instruction should be successful. The failure most
1690e7708688STamas Berghammer         // likely caused by a not supported instruction which don't modify PC.
1691e7708688STamas Berghammer         next_pc = register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize();
1692e7708688STamas Berghammer         next_flags = ReadFlags (register_context_sp.get());
1693e7708688STamas Berghammer     }
1694e7708688STamas Berghammer     else
1695e7708688STamas Berghammer     {
1696e7708688STamas Berghammer         // The instruction emulation failed after it modified the PC. It is an
1697e7708688STamas Berghammer         // unknown error where we can't continue because the next instruction is
1698e7708688STamas Berghammer         // modifying the PC but we don't  know how.
1699e7708688STamas Berghammer         return Error ("Instruction emulation failed unexpectedly.");
1700e7708688STamas Berghammer     }
1701e7708688STamas Berghammer 
1702e7708688STamas Berghammer     if (m_arch.GetMachine() == llvm::Triple::arm)
1703e7708688STamas Berghammer     {
1704e7708688STamas Berghammer         if (next_flags & 0x20)
1705e7708688STamas Berghammer         {
1706e7708688STamas Berghammer             // Thumb mode
1707e7708688STamas Berghammer             error = SetSoftwareBreakpoint(next_pc, 2);
1708e7708688STamas Berghammer         }
1709e7708688STamas Berghammer         else
1710e7708688STamas Berghammer         {
1711e7708688STamas Berghammer             // Arm mode
1712e7708688STamas Berghammer             error = SetSoftwareBreakpoint(next_pc, 4);
1713e7708688STamas Berghammer         }
1714e7708688STamas Berghammer     }
1715cdc22a88SMohit K. Bhakkad     else if (m_arch.GetMachine() == llvm::Triple::mips64
1716c60c9452SJaydeep Patil             || m_arch.GetMachine() == llvm::Triple::mips64el
1717c60c9452SJaydeep Patil             || m_arch.GetMachine() == llvm::Triple::mips
1718c60c9452SJaydeep Patil             || m_arch.GetMachine() == llvm::Triple::mipsel)
1719cdc22a88SMohit K. Bhakkad         error = SetSoftwareBreakpoint(next_pc, 4);
1720e7708688STamas Berghammer     else
1721e7708688STamas Berghammer     {
1722e7708688STamas Berghammer         // No size hint is given for the next breakpoint
1723e7708688STamas Berghammer         error = SetSoftwareBreakpoint(next_pc, 0);
1724e7708688STamas Berghammer     }
1725e7708688STamas Berghammer 
1726e7708688STamas Berghammer     if (error.Fail())
1727e7708688STamas Berghammer         return error;
1728e7708688STamas Berghammer 
1729e7708688STamas Berghammer     m_threads_stepping_with_breakpoint.insert({thread_sp->GetID(), next_pc});
1730e7708688STamas Berghammer 
1731e7708688STamas Berghammer     return Error();
1732e7708688STamas Berghammer }
1733e7708688STamas Berghammer 
1734e7708688STamas Berghammer bool
1735e7708688STamas Berghammer NativeProcessLinux::SupportHardwareSingleStepping() const
1736e7708688STamas Berghammer {
1737cdc22a88SMohit K. Bhakkad     if (m_arch.GetMachine() == llvm::Triple::arm
1738c60c9452SJaydeep Patil         || m_arch.GetMachine() == llvm::Triple::mips64 || m_arch.GetMachine() == llvm::Triple::mips64el
1739c60c9452SJaydeep Patil         || m_arch.GetMachine() == llvm::Triple::mips || m_arch.GetMachine() == llvm::Triple::mipsel)
1740cdc22a88SMohit K. Bhakkad         return false;
1741cdc22a88SMohit K. Bhakkad     return true;
1742e7708688STamas Berghammer }
1743e7708688STamas Berghammer 
1744af245d11STodd Fiala Error
1745af245d11STodd Fiala NativeProcessLinux::Resume (const ResumeActionList &resume_actions)
1746af245d11STodd Fiala {
1747af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD));
1748af245d11STodd Fiala     if (log)
1749af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ());
1750af245d11STodd Fiala 
1751e7708688STamas Berghammer     bool software_single_step = !SupportHardwareSingleStepping();
1752af245d11STodd Fiala 
1753af245d11STodd Fiala     Mutex::Locker locker (m_threads_mutex);
17545830aa75STamas Berghammer 
1755e7708688STamas Berghammer     if (software_single_step)
1756e7708688STamas Berghammer     {
1757e7708688STamas Berghammer         for (auto thread_sp : m_threads)
1758e7708688STamas Berghammer         {
1759e7708688STamas Berghammer             assert (thread_sp && "thread list should not contain NULL threads");
1760e7708688STamas Berghammer 
1761e7708688STamas Berghammer             const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true);
1762e7708688STamas Berghammer             if (action == nullptr)
1763e7708688STamas Berghammer                 continue;
1764e7708688STamas Berghammer 
1765e7708688STamas Berghammer             if (action->state == eStateStepping)
1766e7708688STamas Berghammer             {
1767e7708688STamas Berghammer                 Error error = SetupSoftwareSingleStepping(thread_sp);
1768e7708688STamas Berghammer                 if (error.Fail())
1769e7708688STamas Berghammer                     return error;
1770e7708688STamas Berghammer             }
1771e7708688STamas Berghammer         }
1772e7708688STamas Berghammer     }
1773e7708688STamas Berghammer 
1774af245d11STodd Fiala     for (auto thread_sp : m_threads)
1775af245d11STodd Fiala     {
1776af245d11STodd Fiala         assert (thread_sp && "thread list should not contain NULL threads");
1777af245d11STodd Fiala 
1778af245d11STodd Fiala         const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true);
17796a196ce6SChaoren Lin 
17806a196ce6SChaoren Lin         if (action == nullptr)
17816a196ce6SChaoren Lin         {
17826a196ce6SChaoren Lin             if (log)
17836a196ce6SChaoren Lin                 log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64,
17846a196ce6SChaoren Lin                     __FUNCTION__, GetID (), thread_sp->GetID ());
17856a196ce6SChaoren Lin             continue;
17866a196ce6SChaoren Lin         }
1787af245d11STodd Fiala 
1788af245d11STodd Fiala         if (log)
1789af245d11STodd Fiala         {
1790af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64,
1791af245d11STodd Fiala                     __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ());
1792af245d11STodd Fiala         }
1793af245d11STodd Fiala 
1794af245d11STodd Fiala         switch (action->state)
1795af245d11STodd Fiala         {
1796af245d11STodd Fiala         case eStateRunning:
1797fa03ad2eSChaoren Lin         {
1798af245d11STodd Fiala             // Run the thread, possibly feeding it the signal.
1799fa03ad2eSChaoren Lin             const int signo = action->signal;
18001dbc6c9cSPavel Labath             ResumeThread(thread_sp->GetID (),
180186fd8e45SChaoren Lin                     [=](lldb::tid_t tid_to_resume, bool supress_signal)
1802af245d11STodd Fiala                     {
1803cb84eebbSTamas Berghammer                         std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning ();
1804fa03ad2eSChaoren Lin                         // Pass this signal number on to the inferior to handle.
18055830aa75STamas Berghammer                         const auto resume_result = Resume (tid_to_resume, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER);
18065830aa75STamas Berghammer                         if (resume_result.Success())
18075830aa75STamas Berghammer                             SetState(eStateRunning, true);
18085830aa75STamas Berghammer                         return resume_result;
18091dbc6c9cSPavel Labath                     },
18101dbc6c9cSPavel Labath                     false);
1811af245d11STodd Fiala             break;
1812fa03ad2eSChaoren Lin         }
1813af245d11STodd Fiala 
1814af245d11STodd Fiala         case eStateStepping:
1815af245d11STodd Fiala         {
1816ae29d395SChaoren Lin             // Request the step.
1817ae29d395SChaoren Lin             const int signo = action->signal;
18181dbc6c9cSPavel Labath             ResumeThread(thread_sp->GetID (),
181986fd8e45SChaoren Lin                     [=](lldb::tid_t tid_to_step, bool supress_signal)
1820af245d11STodd Fiala                     {
1821cb84eebbSTamas Berghammer                         std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStepping ();
1822e7708688STamas Berghammer 
1823e7708688STamas Berghammer                         Error step_result;
1824e7708688STamas Berghammer                         if (software_single_step)
1825e7708688STamas Berghammer                             step_result = Resume (tid_to_step, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER);
1826e7708688STamas Berghammer                         else
1827e7708688STamas Berghammer                             step_result = SingleStep (tid_to_step,(signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER);
1828e7708688STamas Berghammer 
182937c768caSChaoren Lin                         assert (step_result.Success() && "SingleStep() failed");
18305830aa75STamas Berghammer                         if (step_result.Success())
18315830aa75STamas Berghammer                             SetState(eStateStepping, true);
183237c768caSChaoren Lin                         return step_result;
18331dbc6c9cSPavel Labath                     },
18341dbc6c9cSPavel Labath                     false);
1835af245d11STodd Fiala             break;
1836ae29d395SChaoren Lin         }
1837af245d11STodd Fiala 
1838af245d11STodd Fiala         case eStateSuspended:
1839af245d11STodd Fiala         case eStateStopped:
1840108c325dSPavel Labath             lldbassert(0 && "Unexpected state");
1841af245d11STodd Fiala 
1842af245d11STodd Fiala         default:
1843af245d11STodd Fiala             return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64,
1844af245d11STodd Fiala                     __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ());
1845af245d11STodd Fiala         }
1846af245d11STodd Fiala     }
1847af245d11STodd Fiala 
18485830aa75STamas Berghammer     return Error();
1849af245d11STodd Fiala }
1850af245d11STodd Fiala 
1851af245d11STodd Fiala Error
1852af245d11STodd Fiala NativeProcessLinux::Halt ()
1853af245d11STodd Fiala {
1854af245d11STodd Fiala     Error error;
1855af245d11STodd Fiala 
1856af245d11STodd Fiala     if (kill (GetID (), SIGSTOP) != 0)
1857af245d11STodd Fiala         error.SetErrorToErrno ();
1858af245d11STodd Fiala 
1859af245d11STodd Fiala     return error;
1860af245d11STodd Fiala }
1861af245d11STodd Fiala 
1862af245d11STodd Fiala Error
1863af245d11STodd Fiala NativeProcessLinux::Detach ()
1864af245d11STodd Fiala {
1865af245d11STodd Fiala     Error error;
1866af245d11STodd Fiala 
1867af245d11STodd Fiala     // Tell ptrace to detach from the process.
1868af245d11STodd Fiala     if (GetID () != LLDB_INVALID_PROCESS_ID)
1869af245d11STodd Fiala         error = Detach (GetID ());
1870af245d11STodd Fiala 
1871af245d11STodd Fiala     // Stop monitoring the inferior.
187219cbe96aSPavel Labath     m_sigchld_handle.reset();
1873af245d11STodd Fiala 
1874af245d11STodd Fiala     // No error.
1875af245d11STodd Fiala     return error;
1876af245d11STodd Fiala }
1877af245d11STodd Fiala 
1878af245d11STodd Fiala Error
1879af245d11STodd Fiala NativeProcessLinux::Signal (int signo)
1880af245d11STodd Fiala {
1881af245d11STodd Fiala     Error error;
1882af245d11STodd Fiala 
1883af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1884af245d11STodd Fiala     if (log)
1885af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64,
188698d0a4b3SChaoren Lin                 __FUNCTION__, signo, Host::GetSignalAsCString(signo), GetID());
1887af245d11STodd Fiala 
1888af245d11STodd Fiala     if (kill(GetID(), signo))
1889af245d11STodd Fiala         error.SetErrorToErrno();
1890af245d11STodd Fiala 
1891af245d11STodd Fiala     return error;
1892af245d11STodd Fiala }
1893af245d11STodd Fiala 
1894af245d11STodd Fiala Error
1895e9547b80SChaoren Lin NativeProcessLinux::Interrupt ()
1896e9547b80SChaoren Lin {
1897e9547b80SChaoren Lin     // Pick a running thread (or if none, a not-dead stopped thread) as
1898e9547b80SChaoren Lin     // the chosen thread that will be the stop-reason thread.
1899e9547b80SChaoren Lin     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1900e9547b80SChaoren Lin 
1901e9547b80SChaoren Lin     NativeThreadProtocolSP running_thread_sp;
1902e9547b80SChaoren Lin     NativeThreadProtocolSP stopped_thread_sp;
1903e9547b80SChaoren Lin 
1904e9547b80SChaoren Lin     if (log)
1905e9547b80SChaoren Lin         log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__);
1906e9547b80SChaoren Lin 
19075830aa75STamas Berghammer     Mutex::Locker locker (m_threads_mutex);
19085830aa75STamas Berghammer 
1909e9547b80SChaoren Lin     for (auto thread_sp : m_threads)
1910e9547b80SChaoren Lin     {
1911e9547b80SChaoren Lin         // The thread shouldn't be null but lets just cover that here.
1912e9547b80SChaoren Lin         if (!thread_sp)
1913e9547b80SChaoren Lin             continue;
1914e9547b80SChaoren Lin 
1915e9547b80SChaoren Lin         // If we have a running or stepping thread, we'll call that the
1916e9547b80SChaoren Lin         // target of the interrupt.
1917e9547b80SChaoren Lin         const auto thread_state = thread_sp->GetState ();
1918e9547b80SChaoren Lin         if (thread_state == eStateRunning ||
1919e9547b80SChaoren Lin             thread_state == eStateStepping)
1920e9547b80SChaoren Lin         {
1921e9547b80SChaoren Lin             running_thread_sp = thread_sp;
1922e9547b80SChaoren Lin             break;
1923e9547b80SChaoren Lin         }
1924e9547b80SChaoren Lin         else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true))
1925e9547b80SChaoren Lin         {
1926e9547b80SChaoren Lin             // Remember the first non-dead stopped thread.  We'll use that as a backup if there are no running threads.
1927e9547b80SChaoren Lin             stopped_thread_sp = thread_sp;
1928e9547b80SChaoren Lin         }
1929e9547b80SChaoren Lin     }
1930e9547b80SChaoren Lin 
1931e9547b80SChaoren Lin     if (!running_thread_sp && !stopped_thread_sp)
1932e9547b80SChaoren Lin     {
19335830aa75STamas Berghammer         Error error("found no running/stepping or live stopped threads as target for interrupt");
1934e9547b80SChaoren Lin         if (log)
1935e9547b80SChaoren Lin             log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ());
19365830aa75STamas Berghammer 
1937e9547b80SChaoren Lin         return error;
1938e9547b80SChaoren Lin     }
1939e9547b80SChaoren Lin 
1940e9547b80SChaoren Lin     NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp;
1941e9547b80SChaoren Lin 
1942e9547b80SChaoren Lin     if (log)
1943e9547b80SChaoren Lin         log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target",
1944e9547b80SChaoren Lin                      __FUNCTION__,
1945e9547b80SChaoren Lin                      GetID (),
1946e9547b80SChaoren Lin                      running_thread_sp ? "running" : "stopped",
1947e9547b80SChaoren Lin                      deferred_signal_thread_sp->GetID ());
1948e9547b80SChaoren Lin 
1949ed89c7feSPavel Labath     StopRunningThreads(deferred_signal_thread_sp->GetID());
195045f5cb31SPavel Labath 
19515830aa75STamas Berghammer     return Error();
1952e9547b80SChaoren Lin }
1953e9547b80SChaoren Lin 
1954e9547b80SChaoren Lin Error
1955af245d11STodd Fiala NativeProcessLinux::Kill ()
1956af245d11STodd Fiala {
1957af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1958af245d11STodd Fiala     if (log)
1959af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ());
1960af245d11STodd Fiala 
1961af245d11STodd Fiala     Error error;
1962af245d11STodd Fiala 
1963af245d11STodd Fiala     switch (m_state)
1964af245d11STodd Fiala     {
1965af245d11STodd Fiala         case StateType::eStateInvalid:
1966af245d11STodd Fiala         case StateType::eStateExited:
1967af245d11STodd Fiala         case StateType::eStateCrashed:
1968af245d11STodd Fiala         case StateType::eStateDetached:
1969af245d11STodd Fiala         case StateType::eStateUnloaded:
1970af245d11STodd Fiala             // Nothing to do - the process is already dead.
1971af245d11STodd Fiala             if (log)
1972af245d11STodd Fiala                 log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state));
1973af245d11STodd Fiala             return error;
1974af245d11STodd Fiala 
1975af245d11STodd Fiala         case StateType::eStateConnected:
1976af245d11STodd Fiala         case StateType::eStateAttaching:
1977af245d11STodd Fiala         case StateType::eStateLaunching:
1978af245d11STodd Fiala         case StateType::eStateStopped:
1979af245d11STodd Fiala         case StateType::eStateRunning:
1980af245d11STodd Fiala         case StateType::eStateStepping:
1981af245d11STodd Fiala         case StateType::eStateSuspended:
1982af245d11STodd Fiala             // We can try to kill a process in these states.
1983af245d11STodd Fiala             break;
1984af245d11STodd Fiala     }
1985af245d11STodd Fiala 
1986af245d11STodd Fiala     if (kill (GetID (), SIGKILL) != 0)
1987af245d11STodd Fiala     {
1988af245d11STodd Fiala         error.SetErrorToErrno ();
1989af245d11STodd Fiala         return error;
1990af245d11STodd Fiala     }
1991af245d11STodd Fiala 
1992af245d11STodd Fiala     return error;
1993af245d11STodd Fiala }
1994af245d11STodd Fiala 
1995af245d11STodd Fiala static Error
1996af245d11STodd Fiala ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info)
1997af245d11STodd Fiala {
1998af245d11STodd Fiala     memory_region_info.Clear();
1999af245d11STodd Fiala 
2000af245d11STodd Fiala     StringExtractor line_extractor (maps_line.c_str ());
2001af245d11STodd Fiala 
2002af245d11STodd Fiala     // Format: {address_start_hex}-{address_end_hex} perms offset  dev   inode   pathname
2003af245d11STodd Fiala     // perms: rwxp   (letter is present if set, '-' if not, final character is p=private, s=shared).
2004af245d11STodd Fiala 
2005af245d11STodd Fiala     // Parse out the starting address
2006af245d11STodd Fiala     lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0);
2007af245d11STodd Fiala 
2008af245d11STodd Fiala     // Parse out hyphen separating start and end address from range.
2009af245d11STodd Fiala     if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-'))
2010af245d11STodd Fiala         return Error ("malformed /proc/{pid}/maps entry, missing dash between address range");
2011af245d11STodd Fiala 
2012af245d11STodd Fiala     // Parse out the ending address
2013af245d11STodd Fiala     lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address);
2014af245d11STodd Fiala 
2015af245d11STodd Fiala     // Parse out the space after the address.
2016af245d11STodd Fiala     if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' '))
2017af245d11STodd Fiala         return Error ("malformed /proc/{pid}/maps entry, missing space after range");
2018af245d11STodd Fiala 
2019af245d11STodd Fiala     // Save the range.
2020af245d11STodd Fiala     memory_region_info.GetRange ().SetRangeBase (start_address);
2021af245d11STodd Fiala     memory_region_info.GetRange ().SetRangeEnd (end_address);
2022af245d11STodd Fiala 
2023af245d11STodd Fiala     // Parse out each permission entry.
2024af245d11STodd Fiala     if (line_extractor.GetBytesLeft () < 4)
2025af245d11STodd Fiala         return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions");
2026af245d11STodd Fiala 
2027af245d11STodd Fiala     // Handle read permission.
2028af245d11STodd Fiala     const char read_perm_char = line_extractor.GetChar ();
2029af245d11STodd Fiala     if (read_perm_char == 'r')
2030af245d11STodd Fiala         memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes);
2031af245d11STodd Fiala     else
2032af245d11STodd Fiala     {
2033af245d11STodd Fiala         assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" );
2034af245d11STodd Fiala         memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo);
2035af245d11STodd Fiala     }
2036af245d11STodd Fiala 
2037af245d11STodd Fiala     // Handle write permission.
2038af245d11STodd Fiala     const char write_perm_char = line_extractor.GetChar ();
2039af245d11STodd Fiala     if (write_perm_char == 'w')
2040af245d11STodd Fiala         memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes);
2041af245d11STodd Fiala     else
2042af245d11STodd Fiala     {
2043af245d11STodd Fiala         assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" );
2044af245d11STodd Fiala         memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo);
2045af245d11STodd Fiala     }
2046af245d11STodd Fiala 
2047af245d11STodd Fiala     // Handle execute permission.
2048af245d11STodd Fiala     const char exec_perm_char = line_extractor.GetChar ();
2049af245d11STodd Fiala     if (exec_perm_char == 'x')
2050af245d11STodd Fiala         memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes);
2051af245d11STodd Fiala     else
2052af245d11STodd Fiala     {
2053af245d11STodd Fiala         assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" );
2054af245d11STodd Fiala         memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo);
2055af245d11STodd Fiala     }
2056af245d11STodd Fiala 
2057af245d11STodd Fiala     return Error ();
2058af245d11STodd Fiala }
2059af245d11STodd Fiala 
2060af245d11STodd Fiala Error
2061af245d11STodd Fiala NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info)
2062af245d11STodd Fiala {
2063af245d11STodd Fiala     // FIXME review that the final memory region returned extends to the end of the virtual address space,
2064af245d11STodd Fiala     // with no perms if it is not mapped.
2065af245d11STodd Fiala 
2066af245d11STodd Fiala     // Use an approach that reads memory regions from /proc/{pid}/maps.
2067af245d11STodd Fiala     // Assume proc maps entries are in ascending order.
2068af245d11STodd Fiala     // FIXME assert if we find differently.
2069af245d11STodd Fiala     Mutex::Locker locker (m_mem_region_cache_mutex);
2070af245d11STodd Fiala 
2071af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2072af245d11STodd Fiala     Error error;
2073af245d11STodd Fiala 
2074af245d11STodd Fiala     if (m_supports_mem_region == LazyBool::eLazyBoolNo)
2075af245d11STodd Fiala     {
2076af245d11STodd Fiala         // We're done.
2077af245d11STodd Fiala         error.SetErrorString ("unsupported");
2078af245d11STodd Fiala         return error;
2079af245d11STodd Fiala     }
2080af245d11STodd Fiala 
2081af245d11STodd Fiala     // If our cache is empty, pull the latest.  There should always be at least one memory region
2082af245d11STodd Fiala     // if memory region handling is supported.
2083af245d11STodd Fiala     if (m_mem_region_cache.empty ())
2084af245d11STodd Fiala     {
2085af245d11STodd Fiala         error = ProcFileReader::ProcessLineByLine (GetID (), "maps",
2086af245d11STodd Fiala              [&] (const std::string &line) -> bool
2087af245d11STodd Fiala              {
2088af245d11STodd Fiala                  MemoryRegionInfo info;
2089af245d11STodd Fiala                  const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info);
2090af245d11STodd Fiala                  if (parse_error.Success ())
2091af245d11STodd Fiala                  {
2092af245d11STodd Fiala                      m_mem_region_cache.push_back (info);
2093af245d11STodd Fiala                      return true;
2094af245d11STodd Fiala                  }
2095af245d11STodd Fiala                  else
2096af245d11STodd Fiala                  {
2097af245d11STodd Fiala                      if (log)
2098af245d11STodd Fiala                          log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ());
2099af245d11STodd Fiala                      return false;
2100af245d11STodd Fiala                  }
2101af245d11STodd Fiala              });
2102af245d11STodd Fiala 
2103af245d11STodd Fiala         // If we had an error, we'll mark unsupported.
2104af245d11STodd Fiala         if (error.Fail ())
2105af245d11STodd Fiala         {
2106af245d11STodd Fiala             m_supports_mem_region = LazyBool::eLazyBoolNo;
2107af245d11STodd Fiala             return error;
2108af245d11STodd Fiala         }
2109af245d11STodd Fiala         else if (m_mem_region_cache.empty ())
2110af245d11STodd Fiala         {
2111af245d11STodd Fiala             // No entries after attempting to read them.  This shouldn't happen if /proc/{pid}/maps
2112af245d11STodd Fiala             // is supported.  Assume we don't support map entries via procfs.
2113af245d11STodd Fiala             if (log)
2114af245d11STodd Fiala                 log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__);
2115af245d11STodd Fiala             m_supports_mem_region = LazyBool::eLazyBoolNo;
2116af245d11STodd Fiala             error.SetErrorString ("not supported");
2117af245d11STodd Fiala             return error;
2118af245d11STodd Fiala         }
2119af245d11STodd Fiala 
2120af245d11STodd Fiala         if (log)
2121af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s read %" PRIu64 " memory region entries from /proc/%" PRIu64 "/maps", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ()), GetID ());
2122af245d11STodd Fiala 
2123af245d11STodd Fiala         // We support memory retrieval, remember that.
2124af245d11STodd Fiala         m_supports_mem_region = LazyBool::eLazyBoolYes;
2125af245d11STodd Fiala     }
2126af245d11STodd Fiala     else
2127af245d11STodd Fiala     {
2128af245d11STodd Fiala         if (log)
2129af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ()));
2130af245d11STodd Fiala     }
2131af245d11STodd Fiala 
2132af245d11STodd Fiala     lldb::addr_t prev_base_address = 0;
2133af245d11STodd Fiala 
2134af245d11STodd Fiala     // FIXME start by finding the last region that is <= target address using binary search.  Data is sorted.
2135af245d11STodd Fiala     // There can be a ton of regions on pthreads apps with lots of threads.
2136af245d11STodd Fiala     for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it)
2137af245d11STodd Fiala     {
2138af245d11STodd Fiala         MemoryRegionInfo &proc_entry_info = *it;
2139af245d11STodd Fiala 
2140af245d11STodd Fiala         // Sanity check assumption that /proc/{pid}/maps entries are ascending.
2141af245d11STodd Fiala         assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected");
2142af245d11STodd Fiala         prev_base_address = proc_entry_info.GetRange ().GetRangeBase ();
2143af245d11STodd Fiala 
2144af245d11STodd Fiala         // If the target address comes before this entry, indicate distance to next region.
2145af245d11STodd Fiala         if (load_addr < proc_entry_info.GetRange ().GetRangeBase ())
2146af245d11STodd Fiala         {
2147af245d11STodd Fiala             range_info.GetRange ().SetRangeBase (load_addr);
2148af245d11STodd Fiala             range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr);
2149af245d11STodd Fiala             range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo);
2150af245d11STodd Fiala             range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo);
2151af245d11STodd Fiala             range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo);
2152af245d11STodd Fiala 
2153af245d11STodd Fiala             return error;
2154af245d11STodd Fiala         }
2155af245d11STodd Fiala         else if (proc_entry_info.GetRange ().Contains (load_addr))
2156af245d11STodd Fiala         {
2157af245d11STodd Fiala             // The target address is within the memory region we're processing here.
2158af245d11STodd Fiala             range_info = proc_entry_info;
2159af245d11STodd Fiala             return error;
2160af245d11STodd Fiala         }
2161af245d11STodd Fiala 
2162af245d11STodd Fiala         // The target memory address comes somewhere after the region we just parsed.
2163af245d11STodd Fiala     }
2164af245d11STodd Fiala 
216509839c33STamas Berghammer     // If we made it here, we didn't find an entry that contained the given address. Return the
216609839c33STamas Berghammer     // load_addr as start and the amount of bytes betwwen load address and the end of the memory as
216709839c33STamas Berghammer     // size.
216809839c33STamas Berghammer     range_info.GetRange ().SetRangeBase (load_addr);
216909839c33STamas Berghammer     switch (m_arch.GetAddressByteSize())
217009839c33STamas Berghammer     {
217109839c33STamas Berghammer         case 4:
217209839c33STamas Berghammer             range_info.GetRange ().SetByteSize (0x100000000ull - load_addr);
217309839c33STamas Berghammer             break;
217409839c33STamas Berghammer         case 8:
217509839c33STamas Berghammer             range_info.GetRange ().SetByteSize (0ull - load_addr);
217609839c33STamas Berghammer             break;
217709839c33STamas Berghammer         default:
217809839c33STamas Berghammer             assert(false && "Unrecognized data byte size");
217909839c33STamas Berghammer             break;
218009839c33STamas Berghammer     }
218109839c33STamas Berghammer     range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo);
218209839c33STamas Berghammer     range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo);
218309839c33STamas Berghammer     range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo);
2184af245d11STodd Fiala     return error;
2185af245d11STodd Fiala }
2186af245d11STodd Fiala 
2187af245d11STodd Fiala void
2188af245d11STodd Fiala NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId)
2189af245d11STodd Fiala {
2190af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2191af245d11STodd Fiala     if (log)
2192af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId);
2193af245d11STodd Fiala 
2194af245d11STodd Fiala     {
2195af245d11STodd Fiala         Mutex::Locker locker (m_mem_region_cache_mutex);
2196af245d11STodd Fiala         if (log)
2197af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ()));
2198af245d11STodd Fiala         m_mem_region_cache.clear ();
2199af245d11STodd Fiala     }
2200af245d11STodd Fiala }
2201af245d11STodd Fiala 
2202af245d11STodd Fiala Error
22033eb4b458SChaoren Lin NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr)
2204af245d11STodd Fiala {
2205af245d11STodd Fiala     // FIXME implementing this requires the equivalent of
2206af245d11STodd Fiala     // InferiorCallPOSIX::InferiorCallMmap, which depends on
2207af245d11STodd Fiala     // functional ThreadPlans working with Native*Protocol.
2208af245d11STodd Fiala #if 1
2209af245d11STodd Fiala     return Error ("not implemented yet");
2210af245d11STodd Fiala #else
2211af245d11STodd Fiala     addr = LLDB_INVALID_ADDRESS;
2212af245d11STodd Fiala 
2213af245d11STodd Fiala     unsigned prot = 0;
2214af245d11STodd Fiala     if (permissions & lldb::ePermissionsReadable)
2215af245d11STodd Fiala         prot |= eMmapProtRead;
2216af245d11STodd Fiala     if (permissions & lldb::ePermissionsWritable)
2217af245d11STodd Fiala         prot |= eMmapProtWrite;
2218af245d11STodd Fiala     if (permissions & lldb::ePermissionsExecutable)
2219af245d11STodd Fiala         prot |= eMmapProtExec;
2220af245d11STodd Fiala 
2221af245d11STodd Fiala     // TODO implement this directly in NativeProcessLinux
2222af245d11STodd Fiala     // (and lift to NativeProcessPOSIX if/when that class is
2223af245d11STodd Fiala     // refactored out).
2224af245d11STodd Fiala     if (InferiorCallMmap(this, addr, 0, size, prot,
2225af245d11STodd Fiala                          eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) {
2226af245d11STodd Fiala         m_addr_to_mmap_size[addr] = size;
2227af245d11STodd Fiala         return Error ();
2228af245d11STodd Fiala     } else {
2229af245d11STodd Fiala         addr = LLDB_INVALID_ADDRESS;
2230af245d11STodd Fiala         return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions));
2231af245d11STodd Fiala     }
2232af245d11STodd Fiala #endif
2233af245d11STodd Fiala }
2234af245d11STodd Fiala 
2235af245d11STodd Fiala Error
2236af245d11STodd Fiala NativeProcessLinux::DeallocateMemory (lldb::addr_t addr)
2237af245d11STodd Fiala {
2238af245d11STodd Fiala     // FIXME see comments in AllocateMemory - required lower-level
2239af245d11STodd Fiala     // bits not in place yet (ThreadPlans)
2240af245d11STodd Fiala     return Error ("not implemented");
2241af245d11STodd Fiala }
2242af245d11STodd Fiala 
2243af245d11STodd Fiala lldb::addr_t
2244af245d11STodd Fiala NativeProcessLinux::GetSharedLibraryInfoAddress ()
2245af245d11STodd Fiala {
2246af245d11STodd Fiala #if 1
2247af245d11STodd Fiala     // punt on this for now
2248af245d11STodd Fiala     return LLDB_INVALID_ADDRESS;
2249af245d11STodd Fiala #else
2250af245d11STodd Fiala     // Return the image info address for the exe module
2251af245d11STodd Fiala #if 1
2252af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2253af245d11STodd Fiala 
2254af245d11STodd Fiala     ModuleSP module_sp;
2255af245d11STodd Fiala     Error error = GetExeModuleSP (module_sp);
2256af245d11STodd Fiala     if (error.Fail ())
2257af245d11STodd Fiala     {
2258af245d11STodd Fiala          if (log)
2259af245d11STodd Fiala             log->Warning ("NativeProcessLinux::%s failed to retrieve exe module: %s", __FUNCTION__, error.AsCString ());
2260af245d11STodd Fiala         return LLDB_INVALID_ADDRESS;
2261af245d11STodd Fiala     }
2262af245d11STodd Fiala 
2263af245d11STodd Fiala     if (module_sp == nullptr)
2264af245d11STodd Fiala     {
2265af245d11STodd Fiala          if (log)
2266af245d11STodd Fiala             log->Warning ("NativeProcessLinux::%s exe module returned was NULL", __FUNCTION__);
2267af245d11STodd Fiala          return LLDB_INVALID_ADDRESS;
2268af245d11STodd Fiala     }
2269af245d11STodd Fiala 
2270af245d11STodd Fiala     ObjectFileSP object_file_sp = module_sp->GetObjectFile ();
2271af245d11STodd Fiala     if (object_file_sp == nullptr)
2272af245d11STodd Fiala     {
2273af245d11STodd Fiala          if (log)
2274af245d11STodd Fiala             log->Warning ("NativeProcessLinux::%s exe module returned a NULL object file", __FUNCTION__);
2275af245d11STodd Fiala          return LLDB_INVALID_ADDRESS;
2276af245d11STodd Fiala     }
2277af245d11STodd Fiala 
2278af245d11STodd Fiala     return obj_file_sp->GetImageInfoAddress();
2279af245d11STodd Fiala #else
2280af245d11STodd Fiala     Target *target = &GetTarget();
2281af245d11STodd Fiala     ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
2282af245d11STodd Fiala     Address addr = obj_file->GetImageInfoAddress(target);
2283af245d11STodd Fiala 
2284af245d11STodd Fiala     if (addr.IsValid())
2285af245d11STodd Fiala         return addr.GetLoadAddress(target);
2286af245d11STodd Fiala     return LLDB_INVALID_ADDRESS;
2287af245d11STodd Fiala #endif
2288af245d11STodd Fiala #endif // punt on this for now
2289af245d11STodd Fiala }
2290af245d11STodd Fiala 
2291af245d11STodd Fiala size_t
2292af245d11STodd Fiala NativeProcessLinux::UpdateThreads ()
2293af245d11STodd Fiala {
2294af245d11STodd Fiala     // The NativeProcessLinux monitoring threads are always up to date
2295af245d11STodd Fiala     // with respect to thread state and they keep the thread list
2296af245d11STodd Fiala     // populated properly. All this method needs to do is return the
2297af245d11STodd Fiala     // thread count.
2298af245d11STodd Fiala     Mutex::Locker locker (m_threads_mutex);
2299af245d11STodd Fiala     return m_threads.size ();
2300af245d11STodd Fiala }
2301af245d11STodd Fiala 
2302af245d11STodd Fiala bool
2303af245d11STodd Fiala NativeProcessLinux::GetArchitecture (ArchSpec &arch) const
2304af245d11STodd Fiala {
2305af245d11STodd Fiala     arch = m_arch;
2306af245d11STodd Fiala     return true;
2307af245d11STodd Fiala }
2308af245d11STodd Fiala 
2309af245d11STodd Fiala Error
231063c8be95STamas Berghammer NativeProcessLinux::GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size)
2311af245d11STodd Fiala {
2312af245d11STodd Fiala     // FIXME put this behind a breakpoint protocol class that can be
2313af245d11STodd Fiala     // set per architecture.  Need ARM, MIPS support here.
2314af245d11STodd Fiala     static const uint8_t g_i386_opcode [] = { 0xCC };
2315af245d11STodd Fiala 
2316af245d11STodd Fiala     switch (m_arch.GetMachine ())
2317af245d11STodd Fiala     {
2318af245d11STodd Fiala         case llvm::Triple::x86:
2319af245d11STodd Fiala         case llvm::Triple::x86_64:
2320af245d11STodd Fiala             actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode));
2321af245d11STodd Fiala             return Error ();
2322af245d11STodd Fiala 
2323ff7fd900STamas Berghammer         case llvm::Triple::arm:
2324ff7fd900STamas Berghammer         case llvm::Triple::aarch64:
2325e8659b5dSMohit K. Bhakkad         case llvm::Triple::mips64:
2326e8659b5dSMohit K. Bhakkad         case llvm::Triple::mips64el:
2327ce815e45SSagar Thakur         case llvm::Triple::mips:
2328ce815e45SSagar Thakur         case llvm::Triple::mipsel:
2329ff7fd900STamas Berghammer             // On these architectures the PC don't get updated for breakpoint hits
2330c60c9452SJaydeep Patil             actual_opcode_size = 0;
2331e8659b5dSMohit K. Bhakkad             return Error ();
2332e8659b5dSMohit K. Bhakkad 
2333af245d11STodd Fiala         default:
2334af245d11STodd Fiala             assert(false && "CPU type not supported!");
2335af245d11STodd Fiala             return Error ("CPU type not supported");
2336af245d11STodd Fiala     }
2337af245d11STodd Fiala }
2338af245d11STodd Fiala 
2339af245d11STodd Fiala Error
2340af245d11STodd Fiala NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware)
2341af245d11STodd Fiala {
2342af245d11STodd Fiala     if (hardware)
2343af245d11STodd Fiala         return Error ("NativeProcessLinux does not support hardware breakpoints");
2344af245d11STodd Fiala     else
2345af245d11STodd Fiala         return SetSoftwareBreakpoint (addr, size);
2346af245d11STodd Fiala }
2347af245d11STodd Fiala 
2348af245d11STodd Fiala Error
234963c8be95STamas Berghammer NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint,
235063c8be95STamas Berghammer                                                      size_t &actual_opcode_size,
235163c8be95STamas Berghammer                                                      const uint8_t *&trap_opcode_bytes)
2352af245d11STodd Fiala {
235363c8be95STamas Berghammer     // FIXME put this behind a breakpoint protocol class that can be set per
235463c8be95STamas Berghammer     // architecture.  Need MIPS support here.
23552afc5966STodd Fiala     static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 };
235663c8be95STamas Berghammer     // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the
235763c8be95STamas Berghammer     // linux kernel does otherwise.
235863c8be95STamas Berghammer     static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 };
2359af245d11STodd Fiala     static const uint8_t g_i386_opcode [] = { 0xCC };
23603df471c3SMohit K. Bhakkad     static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d };
23612c2acf96SMohit K. Bhakkad     static const uint8_t g_mips64el_opcode[] = { 0x0d, 0x00, 0x00, 0x00 };
236263c8be95STamas Berghammer     static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde };
2363af245d11STodd Fiala 
2364af245d11STodd Fiala     switch (m_arch.GetMachine ())
2365af245d11STodd Fiala     {
23662afc5966STodd Fiala     case llvm::Triple::aarch64:
23672afc5966STodd Fiala         trap_opcode_bytes = g_aarch64_opcode;
23682afc5966STodd Fiala         actual_opcode_size = sizeof(g_aarch64_opcode);
23692afc5966STodd Fiala         return Error ();
23702afc5966STodd Fiala 
237163c8be95STamas Berghammer     case llvm::Triple::arm:
237263c8be95STamas Berghammer         switch (trap_opcode_size_hint)
237363c8be95STamas Berghammer         {
237463c8be95STamas Berghammer         case 2:
237563c8be95STamas Berghammer             trap_opcode_bytes = g_thumb_breakpoint_opcode;
237663c8be95STamas Berghammer             actual_opcode_size = sizeof(g_thumb_breakpoint_opcode);
237763c8be95STamas Berghammer             return Error ();
237863c8be95STamas Berghammer         case 4:
237963c8be95STamas Berghammer             trap_opcode_bytes = g_arm_breakpoint_opcode;
238063c8be95STamas Berghammer             actual_opcode_size = sizeof(g_arm_breakpoint_opcode);
238163c8be95STamas Berghammer             return Error ();
238263c8be95STamas Berghammer         default:
238363c8be95STamas Berghammer             assert(false && "Unrecognised trap opcode size hint!");
238463c8be95STamas Berghammer             return Error ("Unrecognised trap opcode size hint!");
238563c8be95STamas Berghammer         }
238663c8be95STamas Berghammer 
2387af245d11STodd Fiala     case llvm::Triple::x86:
2388af245d11STodd Fiala     case llvm::Triple::x86_64:
2389af245d11STodd Fiala         trap_opcode_bytes = g_i386_opcode;
2390af245d11STodd Fiala         actual_opcode_size = sizeof(g_i386_opcode);
2391af245d11STodd Fiala         return Error ();
2392af245d11STodd Fiala 
2393ce815e45SSagar Thakur     case llvm::Triple::mips:
23943df471c3SMohit K. Bhakkad     case llvm::Triple::mips64:
23953df471c3SMohit K. Bhakkad         trap_opcode_bytes = g_mips64_opcode;
23963df471c3SMohit K. Bhakkad         actual_opcode_size = sizeof(g_mips64_opcode);
23973df471c3SMohit K. Bhakkad         return Error ();
23983df471c3SMohit K. Bhakkad 
2399ce815e45SSagar Thakur     case llvm::Triple::mipsel:
24002c2acf96SMohit K. Bhakkad     case llvm::Triple::mips64el:
24012c2acf96SMohit K. Bhakkad         trap_opcode_bytes = g_mips64el_opcode;
24022c2acf96SMohit K. Bhakkad         actual_opcode_size = sizeof(g_mips64el_opcode);
24032c2acf96SMohit K. Bhakkad         return Error ();
24042c2acf96SMohit K. Bhakkad 
2405af245d11STodd Fiala     default:
2406af245d11STodd Fiala         assert(false && "CPU type not supported!");
2407af245d11STodd Fiala         return Error ("CPU type not supported");
2408af245d11STodd Fiala     }
2409af245d11STodd Fiala }
2410af245d11STodd Fiala 
2411af245d11STodd Fiala #if 0
2412af245d11STodd Fiala ProcessMessage::CrashReason
2413af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info)
2414af245d11STodd Fiala {
2415af245d11STodd Fiala     ProcessMessage::CrashReason reason;
2416af245d11STodd Fiala     assert(info->si_signo == SIGSEGV);
2417af245d11STodd Fiala 
2418af245d11STodd Fiala     reason = ProcessMessage::eInvalidCrashReason;
2419af245d11STodd Fiala 
2420af245d11STodd Fiala     switch (info->si_code)
2421af245d11STodd Fiala     {
2422af245d11STodd Fiala     default:
2423af245d11STodd Fiala         assert(false && "unexpected si_code for SIGSEGV");
2424af245d11STodd Fiala         break;
2425af245d11STodd Fiala     case SI_KERNEL:
2426af245d11STodd Fiala         // Linux will occasionally send spurious SI_KERNEL codes.
2427af245d11STodd Fiala         // (this is poorly documented in sigaction)
2428af245d11STodd Fiala         // One way to get this is via unaligned SIMD loads.
2429af245d11STodd Fiala         reason = ProcessMessage::eInvalidAddress; // for lack of anything better
2430af245d11STodd Fiala         break;
2431af245d11STodd Fiala     case SEGV_MAPERR:
2432af245d11STodd Fiala         reason = ProcessMessage::eInvalidAddress;
2433af245d11STodd Fiala         break;
2434af245d11STodd Fiala     case SEGV_ACCERR:
2435af245d11STodd Fiala         reason = ProcessMessage::ePrivilegedAddress;
2436af245d11STodd Fiala         break;
2437af245d11STodd Fiala     }
2438af245d11STodd Fiala 
2439af245d11STodd Fiala     return reason;
2440af245d11STodd Fiala }
2441af245d11STodd Fiala #endif
2442af245d11STodd Fiala 
2443af245d11STodd Fiala 
2444af245d11STodd Fiala #if 0
2445af245d11STodd Fiala ProcessMessage::CrashReason
2446af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info)
2447af245d11STodd Fiala {
2448af245d11STodd Fiala     ProcessMessage::CrashReason reason;
2449af245d11STodd Fiala     assert(info->si_signo == SIGILL);
2450af245d11STodd Fiala 
2451af245d11STodd Fiala     reason = ProcessMessage::eInvalidCrashReason;
2452af245d11STodd Fiala 
2453af245d11STodd Fiala     switch (info->si_code)
2454af245d11STodd Fiala     {
2455af245d11STodd Fiala     default:
2456af245d11STodd Fiala         assert(false && "unexpected si_code for SIGILL");
2457af245d11STodd Fiala         break;
2458af245d11STodd Fiala     case ILL_ILLOPC:
2459af245d11STodd Fiala         reason = ProcessMessage::eIllegalOpcode;
2460af245d11STodd Fiala         break;
2461af245d11STodd Fiala     case ILL_ILLOPN:
2462af245d11STodd Fiala         reason = ProcessMessage::eIllegalOperand;
2463af245d11STodd Fiala         break;
2464af245d11STodd Fiala     case ILL_ILLADR:
2465af245d11STodd Fiala         reason = ProcessMessage::eIllegalAddressingMode;
2466af245d11STodd Fiala         break;
2467af245d11STodd Fiala     case ILL_ILLTRP:
2468af245d11STodd Fiala         reason = ProcessMessage::eIllegalTrap;
2469af245d11STodd Fiala         break;
2470af245d11STodd Fiala     case ILL_PRVOPC:
2471af245d11STodd Fiala         reason = ProcessMessage::ePrivilegedOpcode;
2472af245d11STodd Fiala         break;
2473af245d11STodd Fiala     case ILL_PRVREG:
2474af245d11STodd Fiala         reason = ProcessMessage::ePrivilegedRegister;
2475af245d11STodd Fiala         break;
2476af245d11STodd Fiala     case ILL_COPROC:
2477af245d11STodd Fiala         reason = ProcessMessage::eCoprocessorError;
2478af245d11STodd Fiala         break;
2479af245d11STodd Fiala     case ILL_BADSTK:
2480af245d11STodd Fiala         reason = ProcessMessage::eInternalStackError;
2481af245d11STodd Fiala         break;
2482af245d11STodd Fiala     }
2483af245d11STodd Fiala 
2484af245d11STodd Fiala     return reason;
2485af245d11STodd Fiala }
2486af245d11STodd Fiala #endif
2487af245d11STodd Fiala 
2488af245d11STodd Fiala #if 0
2489af245d11STodd Fiala ProcessMessage::CrashReason
2490af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info)
2491af245d11STodd Fiala {
2492af245d11STodd Fiala     ProcessMessage::CrashReason reason;
2493af245d11STodd Fiala     assert(info->si_signo == SIGFPE);
2494af245d11STodd Fiala 
2495af245d11STodd Fiala     reason = ProcessMessage::eInvalidCrashReason;
2496af245d11STodd Fiala 
2497af245d11STodd Fiala     switch (info->si_code)
2498af245d11STodd Fiala     {
2499af245d11STodd Fiala     default:
2500af245d11STodd Fiala         assert(false && "unexpected si_code for SIGFPE");
2501af245d11STodd Fiala         break;
2502af245d11STodd Fiala     case FPE_INTDIV:
2503af245d11STodd Fiala         reason = ProcessMessage::eIntegerDivideByZero;
2504af245d11STodd Fiala         break;
2505af245d11STodd Fiala     case FPE_INTOVF:
2506af245d11STodd Fiala         reason = ProcessMessage::eIntegerOverflow;
2507af245d11STodd Fiala         break;
2508af245d11STodd Fiala     case FPE_FLTDIV:
2509af245d11STodd Fiala         reason = ProcessMessage::eFloatDivideByZero;
2510af245d11STodd Fiala         break;
2511af245d11STodd Fiala     case FPE_FLTOVF:
2512af245d11STodd Fiala         reason = ProcessMessage::eFloatOverflow;
2513af245d11STodd Fiala         break;
2514af245d11STodd Fiala     case FPE_FLTUND:
2515af245d11STodd Fiala         reason = ProcessMessage::eFloatUnderflow;
2516af245d11STodd Fiala         break;
2517af245d11STodd Fiala     case FPE_FLTRES:
2518af245d11STodd Fiala         reason = ProcessMessage::eFloatInexactResult;
2519af245d11STodd Fiala         break;
2520af245d11STodd Fiala     case FPE_FLTINV:
2521af245d11STodd Fiala         reason = ProcessMessage::eFloatInvalidOperation;
2522af245d11STodd Fiala         break;
2523af245d11STodd Fiala     case FPE_FLTSUB:
2524af245d11STodd Fiala         reason = ProcessMessage::eFloatSubscriptRange;
2525af245d11STodd Fiala         break;
2526af245d11STodd Fiala     }
2527af245d11STodd Fiala 
2528af245d11STodd Fiala     return reason;
2529af245d11STodd Fiala }
2530af245d11STodd Fiala #endif
2531af245d11STodd Fiala 
2532af245d11STodd Fiala #if 0
2533af245d11STodd Fiala ProcessMessage::CrashReason
2534af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info)
2535af245d11STodd Fiala {
2536af245d11STodd Fiala     ProcessMessage::CrashReason reason;
2537af245d11STodd Fiala     assert(info->si_signo == SIGBUS);
2538af245d11STodd Fiala 
2539af245d11STodd Fiala     reason = ProcessMessage::eInvalidCrashReason;
2540af245d11STodd Fiala 
2541af245d11STodd Fiala     switch (info->si_code)
2542af245d11STodd Fiala     {
2543af245d11STodd Fiala     default:
2544af245d11STodd Fiala         assert(false && "unexpected si_code for SIGBUS");
2545af245d11STodd Fiala         break;
2546af245d11STodd Fiala     case BUS_ADRALN:
2547af245d11STodd Fiala         reason = ProcessMessage::eIllegalAlignment;
2548af245d11STodd Fiala         break;
2549af245d11STodd Fiala     case BUS_ADRERR:
2550af245d11STodd Fiala         reason = ProcessMessage::eIllegalAddress;
2551af245d11STodd Fiala         break;
2552af245d11STodd Fiala     case BUS_OBJERR:
2553af245d11STodd Fiala         reason = ProcessMessage::eHardwareError;
2554af245d11STodd Fiala         break;
2555af245d11STodd Fiala     }
2556af245d11STodd Fiala 
2557af245d11STodd Fiala     return reason;
2558af245d11STodd Fiala }
2559af245d11STodd Fiala #endif
2560af245d11STodd Fiala 
2561af245d11STodd Fiala Error
256226438d26SChaoren Lin NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read)
2563af245d11STodd Fiala {
2564df7c6995SPavel Labath     if (ProcessVmReadvSupported()) {
2565df7c6995SPavel Labath         // The process_vm_readv path is about 50 times faster than ptrace api. We want to use
2566df7c6995SPavel Labath         // this syscall if it is supported.
2567df7c6995SPavel Labath 
2568df7c6995SPavel Labath         const ::pid_t pid = GetID();
2569df7c6995SPavel Labath 
2570df7c6995SPavel Labath         struct iovec local_iov, remote_iov;
2571df7c6995SPavel Labath         local_iov.iov_base = buf;
2572df7c6995SPavel Labath         local_iov.iov_len = size;
2573df7c6995SPavel Labath         remote_iov.iov_base = reinterpret_cast<void *>(addr);
2574df7c6995SPavel Labath         remote_iov.iov_len = size;
2575df7c6995SPavel Labath 
2576df7c6995SPavel Labath         bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0);
2577df7c6995SPavel Labath         const bool success = bytes_read == size;
2578df7c6995SPavel Labath 
2579df7c6995SPavel Labath         Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2580df7c6995SPavel Labath         if (log)
2581df7c6995SPavel Labath             log->Printf ("NativeProcessLinux::%s using process_vm_readv to read %zd bytes from inferior address 0x%" PRIx64": %s",
2582df7c6995SPavel Labath                     __FUNCTION__, size, addr, success ? "Success" : strerror(errno));
2583df7c6995SPavel Labath 
2584df7c6995SPavel Labath         if (success)
2585df7c6995SPavel Labath             return Error();
2586df7c6995SPavel Labath         // else
2587df7c6995SPavel Labath         //     the call failed for some reason, let's retry the read using ptrace api.
2588df7c6995SPavel Labath     }
2589df7c6995SPavel Labath 
259019cbe96aSPavel Labath     unsigned char *dst = static_cast<unsigned char*>(buf);
259119cbe96aSPavel Labath     size_t remainder;
259219cbe96aSPavel Labath     long data;
259319cbe96aSPavel Labath 
259419cbe96aSPavel Labath     Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
259519cbe96aSPavel Labath     if (log)
259619cbe96aSPavel Labath         ProcessPOSIXLog::IncNestLevel();
259719cbe96aSPavel Labath     if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
259819cbe96aSPavel Labath         log->Printf ("NativeProcessLinux::%s(%p, %p, %zd, _)", __FUNCTION__, (void*)addr, buf, size);
259919cbe96aSPavel Labath 
260019cbe96aSPavel Labath     for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
260119cbe96aSPavel Labath     {
260219cbe96aSPavel Labath         Error error = NativeProcessLinux::PtraceWrapper(PTRACE_PEEKDATA, GetID(), (void*)addr, nullptr, 0, &data);
260319cbe96aSPavel Labath         if (error.Fail())
260419cbe96aSPavel Labath         {
260519cbe96aSPavel Labath             if (log)
260619cbe96aSPavel Labath                 ProcessPOSIXLog::DecNestLevel();
260719cbe96aSPavel Labath             return error;
260819cbe96aSPavel Labath         }
260919cbe96aSPavel Labath 
261019cbe96aSPavel Labath         remainder = size - bytes_read;
261119cbe96aSPavel Labath         remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder;
261219cbe96aSPavel Labath 
261319cbe96aSPavel Labath         // Copy the data into our buffer
261419cbe96aSPavel Labath         for (unsigned i = 0; i < remainder; ++i)
261519cbe96aSPavel Labath             dst[i] = ((data >> i*8) & 0xFF);
261619cbe96aSPavel Labath 
261719cbe96aSPavel Labath         if (log && ProcessPOSIXLog::AtTopNestLevel() &&
261819cbe96aSPavel Labath                 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
261919cbe96aSPavel Labath                         (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
262019cbe96aSPavel Labath                                 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
262119cbe96aSPavel Labath         {
262219cbe96aSPavel Labath             uintptr_t print_dst = 0;
262319cbe96aSPavel Labath             // Format bytes from data by moving into print_dst for log output
262419cbe96aSPavel Labath             for (unsigned i = 0; i < remainder; ++i)
262519cbe96aSPavel Labath                 print_dst |= (((data >> i*8) & 0xFF) << i*8);
262679203995SPavel Labath             log->Printf ("NativeProcessLinux::%s() [0x%" PRIx64 "]:0x%" PRIx64 " (0x%" PRIx64 ")",
262779203995SPavel Labath                     __FUNCTION__, addr, uint64_t(print_dst), uint64_t(data));
262819cbe96aSPavel Labath         }
262919cbe96aSPavel Labath         addr += k_ptrace_word_size;
263019cbe96aSPavel Labath         dst += k_ptrace_word_size;
263119cbe96aSPavel Labath     }
263219cbe96aSPavel Labath 
263319cbe96aSPavel Labath     if (log)
263419cbe96aSPavel Labath         ProcessPOSIXLog::DecNestLevel();
263519cbe96aSPavel Labath     return Error();
2636af245d11STodd Fiala }
2637af245d11STodd Fiala 
2638af245d11STodd Fiala Error
26393eb4b458SChaoren Lin NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read)
26403eb4b458SChaoren Lin {
26413eb4b458SChaoren Lin     Error error = ReadMemory(addr, buf, size, bytes_read);
26423eb4b458SChaoren Lin     if (error.Fail()) return error;
26433eb4b458SChaoren Lin     return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size);
26443eb4b458SChaoren Lin }
26453eb4b458SChaoren Lin 
26463eb4b458SChaoren Lin Error
26473eb4b458SChaoren Lin NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written)
2648af245d11STodd Fiala {
264919cbe96aSPavel Labath     const unsigned char *src = static_cast<const unsigned char*>(buf);
265019cbe96aSPavel Labath     size_t remainder;
265119cbe96aSPavel Labath     Error error;
265219cbe96aSPavel Labath 
265319cbe96aSPavel Labath     Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
265419cbe96aSPavel Labath     if (log)
265519cbe96aSPavel Labath         ProcessPOSIXLog::IncNestLevel();
265619cbe96aSPavel Labath     if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
265779203995SPavel Labath         log->Printf ("NativeProcessLinux::%s(0x%" PRIx64 ", %p, %zu)", __FUNCTION__, addr, buf, size);
265819cbe96aSPavel Labath 
265919cbe96aSPavel Labath     for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
266019cbe96aSPavel Labath     {
266119cbe96aSPavel Labath         remainder = size - bytes_written;
266219cbe96aSPavel Labath         remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder;
266319cbe96aSPavel Labath 
266419cbe96aSPavel Labath         if (remainder == k_ptrace_word_size)
266519cbe96aSPavel Labath         {
266619cbe96aSPavel Labath             unsigned long data = 0;
266719cbe96aSPavel Labath             for (unsigned i = 0; i < k_ptrace_word_size; ++i)
266819cbe96aSPavel Labath                 data |= (unsigned long)src[i] << i*8;
266919cbe96aSPavel Labath 
267019cbe96aSPavel Labath             if (log && ProcessPOSIXLog::AtTopNestLevel() &&
267119cbe96aSPavel Labath                     (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
267219cbe96aSPavel Labath                             (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
267319cbe96aSPavel Labath                                     size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
267419cbe96aSPavel Labath                 log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
267519cbe96aSPavel Labath                         (void*)addr, *(const unsigned long*)src, data);
267619cbe96aSPavel Labath 
267719cbe96aSPavel Labath             error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), (void*)addr, (void*)data);
267819cbe96aSPavel Labath             if (error.Fail())
267919cbe96aSPavel Labath             {
268019cbe96aSPavel Labath                 if (log)
268119cbe96aSPavel Labath                     ProcessPOSIXLog::DecNestLevel();
268219cbe96aSPavel Labath                 return error;
268319cbe96aSPavel Labath             }
268419cbe96aSPavel Labath         }
268519cbe96aSPavel Labath         else
268619cbe96aSPavel Labath         {
268719cbe96aSPavel Labath             unsigned char buff[8];
268819cbe96aSPavel Labath             size_t bytes_read;
268919cbe96aSPavel Labath             error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read);
269019cbe96aSPavel Labath             if (error.Fail())
269119cbe96aSPavel Labath             {
269219cbe96aSPavel Labath                 if (log)
269319cbe96aSPavel Labath                     ProcessPOSIXLog::DecNestLevel();
269419cbe96aSPavel Labath                 return error;
269519cbe96aSPavel Labath             }
269619cbe96aSPavel Labath 
269719cbe96aSPavel Labath             memcpy(buff, src, remainder);
269819cbe96aSPavel Labath 
269919cbe96aSPavel Labath             size_t bytes_written_rec;
270019cbe96aSPavel Labath             error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec);
270119cbe96aSPavel Labath             if (error.Fail())
270219cbe96aSPavel Labath             {
270319cbe96aSPavel Labath                 if (log)
270419cbe96aSPavel Labath                     ProcessPOSIXLog::DecNestLevel();
270519cbe96aSPavel Labath                 return error;
270619cbe96aSPavel Labath             }
270719cbe96aSPavel Labath 
270819cbe96aSPavel Labath             if (log && ProcessPOSIXLog::AtTopNestLevel() &&
270919cbe96aSPavel Labath                     (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
271019cbe96aSPavel Labath                             (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
271119cbe96aSPavel Labath                                     size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
271219cbe96aSPavel Labath                 log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
271319cbe96aSPavel Labath                         (void*)addr, *(const unsigned long*)src, *(unsigned long*)buff);
271419cbe96aSPavel Labath         }
271519cbe96aSPavel Labath 
271619cbe96aSPavel Labath         addr += k_ptrace_word_size;
271719cbe96aSPavel Labath         src += k_ptrace_word_size;
271819cbe96aSPavel Labath     }
271919cbe96aSPavel Labath     if (log)
272019cbe96aSPavel Labath         ProcessPOSIXLog::DecNestLevel();
272119cbe96aSPavel Labath     return error;
2722af245d11STodd Fiala }
2723af245d11STodd Fiala 
272497ccc294SChaoren Lin Error
2725af245d11STodd Fiala NativeProcessLinux::Resume (lldb::tid_t tid, uint32_t signo)
2726af245d11STodd Fiala {
2727af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
2728af245d11STodd Fiala 
2729af245d11STodd Fiala     if (log)
2730af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s() resuming thread = %"  PRIu64 " with signal %s", __FUNCTION__, tid,
273198d0a4b3SChaoren Lin                                  Host::GetSignalAsCString(signo));
2732c7512fdcSPavel Labath 
2733c7512fdcSPavel Labath 
2734c7512fdcSPavel Labath 
2735c7512fdcSPavel Labath     intptr_t data = 0;
2736c7512fdcSPavel Labath 
2737c7512fdcSPavel Labath     if (signo != LLDB_INVALID_SIGNAL_NUMBER)
2738c7512fdcSPavel Labath         data = signo;
2739c7512fdcSPavel Labath 
274019cbe96aSPavel Labath     Error error = PtraceWrapper(PTRACE_CONT, tid, nullptr, (void*)data);
2741c7512fdcSPavel Labath 
2742af245d11STodd Fiala     if (log)
2743c7512fdcSPavel Labath         log->Printf ("NativeProcessLinux::%s() resuming thread = %"  PRIu64 " result = %s", __FUNCTION__, tid, error.Success() ? "true" : "false");
2744c7512fdcSPavel Labath     return error;
2745af245d11STodd Fiala }
2746af245d11STodd Fiala 
274797ccc294SChaoren Lin Error
2748af245d11STodd Fiala NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo)
2749af245d11STodd Fiala {
2750c7512fdcSPavel Labath     intptr_t data = 0;
2751c7512fdcSPavel Labath 
2752c7512fdcSPavel Labath     if (signo != LLDB_INVALID_SIGNAL_NUMBER)
2753c7512fdcSPavel Labath         data = signo;
2754c7512fdcSPavel Labath 
275519cbe96aSPavel Labath     return PtraceWrapper(PTRACE_SINGLESTEP, tid, nullptr, (void*)data);
2756af245d11STodd Fiala }
2757af245d11STodd Fiala 
275897ccc294SChaoren Lin Error
275997ccc294SChaoren Lin NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo)
2760af245d11STodd Fiala {
276119cbe96aSPavel Labath     return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo);
2762af245d11STodd Fiala }
2763af245d11STodd Fiala 
276497ccc294SChaoren Lin Error
2765af245d11STodd Fiala NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2766af245d11STodd Fiala {
276719cbe96aSPavel Labath     return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message);
2768af245d11STodd Fiala }
2769af245d11STodd Fiala 
2770db264a6dSTamas Berghammer Error
2771af245d11STodd Fiala NativeProcessLinux::Detach(lldb::tid_t tid)
2772af245d11STodd Fiala {
277397ccc294SChaoren Lin     if (tid == LLDB_INVALID_THREAD_ID)
277497ccc294SChaoren Lin         return Error();
277597ccc294SChaoren Lin 
277619cbe96aSPavel Labath     return PtraceWrapper(PTRACE_DETACH, tid);
2777af245d11STodd Fiala }
2778af245d11STodd Fiala 
2779af245d11STodd Fiala bool
2780d3173f34SChaoren Lin NativeProcessLinux::DupDescriptor(const FileSpec &file_spec, int fd, int flags)
2781af245d11STodd Fiala {
2782d3173f34SChaoren Lin     int target_fd = open(file_spec.GetCString(), flags, 0666);
2783af245d11STodd Fiala 
2784af245d11STodd Fiala     if (target_fd == -1)
2785af245d11STodd Fiala         return false;
2786af245d11STodd Fiala 
2787493c3a12SPavel Labath     if (dup2(target_fd, fd) == -1)
2788493c3a12SPavel Labath         return false;
2789493c3a12SPavel Labath 
2790493c3a12SPavel Labath     return (close(target_fd) == -1) ? false : true;
2791af245d11STodd Fiala }
2792af245d11STodd Fiala 
2793af245d11STodd Fiala bool
2794af245d11STodd Fiala NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id)
2795af245d11STodd Fiala {
2796af245d11STodd Fiala     for (auto thread_sp : m_threads)
2797af245d11STodd Fiala     {
2798af245d11STodd Fiala         assert (thread_sp && "thread list should not contain NULL threads");
2799af245d11STodd Fiala         if (thread_sp->GetID () == thread_id)
2800af245d11STodd Fiala         {
2801af245d11STodd Fiala             // We have this thread.
2802af245d11STodd Fiala             return true;
2803af245d11STodd Fiala         }
2804af245d11STodd Fiala     }
2805af245d11STodd Fiala 
2806af245d11STodd Fiala     // We don't have this thread.
2807af245d11STodd Fiala     return false;
2808af245d11STodd Fiala }
2809af245d11STodd Fiala 
2810af245d11STodd Fiala NativeThreadProtocolSP
2811af245d11STodd Fiala NativeProcessLinux::MaybeGetThreadNoLock (lldb::tid_t thread_id)
2812af245d11STodd Fiala {
2813af245d11STodd Fiala     // CONSIDER organize threads by map - we can do better than linear.
2814af245d11STodd Fiala     for (auto thread_sp : m_threads)
2815af245d11STodd Fiala     {
2816af245d11STodd Fiala         if (thread_sp->GetID () == thread_id)
2817af245d11STodd Fiala             return thread_sp;
2818af245d11STodd Fiala     }
2819af245d11STodd Fiala 
2820af245d11STodd Fiala     // We don't have this thread.
2821af245d11STodd Fiala     return NativeThreadProtocolSP ();
2822af245d11STodd Fiala }
2823af245d11STodd Fiala 
2824af245d11STodd Fiala bool
2825af245d11STodd Fiala NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id)
2826af245d11STodd Fiala {
28271dbc6c9cSPavel Labath     Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
28281dbc6c9cSPavel Labath 
28291dbc6c9cSPavel Labath     if (log)
28301dbc6c9cSPavel Labath         log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, thread_id);
28311dbc6c9cSPavel Labath 
28321dbc6c9cSPavel Labath     bool found = false;
28331dbc6c9cSPavel Labath 
2834af245d11STodd Fiala     Mutex::Locker locker (m_threads_mutex);
2835af245d11STodd Fiala     for (auto it = m_threads.begin (); it != m_threads.end (); ++it)
2836af245d11STodd Fiala     {
2837af245d11STodd Fiala         if (*it && ((*it)->GetID () == thread_id))
2838af245d11STodd Fiala         {
2839af245d11STodd Fiala             m_threads.erase (it);
28401dbc6c9cSPavel Labath             found = true;
28411dbc6c9cSPavel Labath             break;
2842af245d11STodd Fiala         }
2843af245d11STodd Fiala     }
2844af245d11STodd Fiala 
28451dbc6c9cSPavel Labath     // If we have a pending notification, remove this from the set.
28461dbc6c9cSPavel Labath     if (m_pending_notification_up)
28471dbc6c9cSPavel Labath     {
28481dbc6c9cSPavel Labath         m_pending_notification_up->wait_for_stop_tids.erase(thread_id);
28499eb1ecb9SPavel Labath         SignalIfAllThreadsStopped();
28501dbc6c9cSPavel Labath     }
28511dbc6c9cSPavel Labath 
28521dbc6c9cSPavel Labath     return found;
2853af245d11STodd Fiala }
2854af245d11STodd Fiala 
2855af245d11STodd Fiala NativeThreadProtocolSP
2856af245d11STodd Fiala NativeProcessLinux::AddThread (lldb::tid_t thread_id)
2857af245d11STodd Fiala {
2858af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
2859af245d11STodd Fiala 
2860af245d11STodd Fiala     Mutex::Locker locker (m_threads_mutex);
2861af245d11STodd Fiala 
2862af245d11STodd Fiala     if (log)
2863af245d11STodd Fiala     {
2864af245d11STodd Fiala         log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64,
2865af245d11STodd Fiala                 __FUNCTION__,
2866af245d11STodd Fiala                 GetID (),
2867af245d11STodd Fiala                 thread_id);
2868af245d11STodd Fiala     }
2869af245d11STodd Fiala 
2870af245d11STodd Fiala     assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists");
2871af245d11STodd Fiala 
2872af245d11STodd Fiala     // If this is the first thread, save it as the current thread
2873af245d11STodd Fiala     if (m_threads.empty ())
2874af245d11STodd Fiala         SetCurrentThreadID (thread_id);
2875af245d11STodd Fiala 
2876af245d11STodd Fiala     NativeThreadProtocolSP thread_sp (new NativeThreadLinux (this, thread_id));
2877af245d11STodd Fiala     m_threads.push_back (thread_sp);
2878af245d11STodd Fiala 
2879af245d11STodd Fiala     return thread_sp;
2880af245d11STodd Fiala }
2881af245d11STodd Fiala 
2882af245d11STodd Fiala Error
2883af245d11STodd Fiala NativeProcessLinux::FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp)
2884af245d11STodd Fiala {
288575f47c3aSTodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2886af245d11STodd Fiala 
2887af245d11STodd Fiala     Error error;
2888af245d11STodd Fiala 
2889af245d11STodd Fiala     // Get a linux thread pointer.
2890af245d11STodd Fiala     if (!thread_sp)
2891af245d11STodd Fiala     {
2892af245d11STodd Fiala         error.SetErrorString ("null thread_sp");
2893af245d11STodd Fiala         if (log)
2894af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
2895af245d11STodd Fiala         return error;
2896af245d11STodd Fiala     }
2897cb84eebbSTamas Berghammer     std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp);
2898af245d11STodd Fiala 
2899af245d11STodd Fiala     // Find out the size of a breakpoint (might depend on where we are in the code).
2900cb84eebbSTamas Berghammer     NativeRegisterContextSP context_sp = linux_thread_sp->GetRegisterContext ();
2901af245d11STodd Fiala     if (!context_sp)
2902af245d11STodd Fiala     {
2903af245d11STodd Fiala         error.SetErrorString ("cannot get a NativeRegisterContext for the thread");
2904af245d11STodd Fiala         if (log)
2905af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ());
2906af245d11STodd Fiala         return error;
2907af245d11STodd Fiala     }
2908af245d11STodd Fiala 
2909af245d11STodd Fiala     uint32_t breakpoint_size = 0;
291063c8be95STamas Berghammer     error = GetSoftwareBreakpointPCOffset (context_sp, breakpoint_size);
2911af245d11STodd Fiala     if (error.Fail ())
2912af245d11STodd Fiala     {
2913af245d11STodd Fiala         if (log)
2914af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ());
2915af245d11STodd Fiala         return error;
2916af245d11STodd Fiala     }
2917af245d11STodd Fiala     else
2918af245d11STodd Fiala     {
2919af245d11STodd Fiala         if (log)
2920af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size);
2921af245d11STodd Fiala     }
2922af245d11STodd Fiala 
2923af245d11STodd Fiala     // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size.
2924c60c9452SJaydeep Patil     const lldb::addr_t initial_pc_addr = context_sp->GetPCfromBreakpointLocation ();
2925af245d11STodd Fiala     lldb::addr_t breakpoint_addr = initial_pc_addr;
29263eb4b458SChaoren Lin     if (breakpoint_size > 0)
2927af245d11STodd Fiala     {
2928af245d11STodd Fiala         // Do not allow breakpoint probe to wrap around.
29293eb4b458SChaoren Lin         if (breakpoint_addr >= breakpoint_size)
29303eb4b458SChaoren Lin             breakpoint_addr -= breakpoint_size;
2931af245d11STodd Fiala     }
2932af245d11STodd Fiala 
2933af245d11STodd Fiala     // Check if we stopped because of a breakpoint.
2934af245d11STodd Fiala     NativeBreakpointSP breakpoint_sp;
2935af245d11STodd Fiala     error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp);
2936af245d11STodd Fiala     if (!error.Success () || !breakpoint_sp)
2937af245d11STodd Fiala     {
2938af245d11STodd Fiala         // We didn't find one at a software probe location.  Nothing to do.
2939af245d11STodd Fiala         if (log)
2940af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr);
2941af245d11STodd Fiala         return Error ();
2942af245d11STodd Fiala     }
2943af245d11STodd Fiala 
2944af245d11STodd Fiala     // If the breakpoint is not a software breakpoint, nothing to do.
2945af245d11STodd Fiala     if (!breakpoint_sp->IsSoftwareBreakpoint ())
2946af245d11STodd Fiala     {
2947af245d11STodd Fiala         if (log)
2948af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr);
2949af245d11STodd Fiala         return Error ();
2950af245d11STodd Fiala     }
2951af245d11STodd Fiala 
2952af245d11STodd Fiala     //
2953af245d11STodd Fiala     // We have a software breakpoint and need to adjust the PC.
2954af245d11STodd Fiala     //
2955af245d11STodd Fiala 
2956af245d11STodd Fiala     // Sanity check.
2957af245d11STodd Fiala     if (breakpoint_size == 0)
2958af245d11STodd Fiala     {
2959af245d11STodd Fiala         // Nothing to do!  How did we get here?
2960af245d11STodd Fiala         if (log)
2961af245d11STodd Fiala             log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", it is software, but the size is zero, nothing to do (unexpected)", __FUNCTION__, GetID (), breakpoint_addr);
2962af245d11STodd Fiala         return Error ();
2963af245d11STodd Fiala     }
2964af245d11STodd Fiala 
2965af245d11STodd Fiala     // Change the program counter.
2966af245d11STodd Fiala     if (log)
2967cb84eebbSTamas Berghammer         log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": changing PC from 0x%" PRIx64 " to 0x%" PRIx64, __FUNCTION__, GetID (), linux_thread_sp->GetID (), initial_pc_addr, breakpoint_addr);
2968af245d11STodd Fiala 
2969af245d11STodd Fiala     error = context_sp->SetPC (breakpoint_addr);
2970af245d11STodd Fiala     if (error.Fail ())
2971af245d11STodd Fiala     {
2972af245d11STodd Fiala         if (log)
2973cb84eebbSTamas Berghammer             log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID (), linux_thread_sp->GetID (), error.AsCString ());
2974af245d11STodd Fiala         return error;
2975af245d11STodd Fiala     }
2976af245d11STodd Fiala 
2977af245d11STodd Fiala     return error;
2978af245d11STodd Fiala }
2979fa03ad2eSChaoren Lin 
29807cb18bf5STamas Berghammer Error
29817cb18bf5STamas Berghammer NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec)
29827cb18bf5STamas Berghammer {
29837cb18bf5STamas Berghammer     FileSpec module_file_spec(module_path, true);
29847cb18bf5STamas Berghammer 
2985162fb8e8SPavel Labath     bool found = false;
29867cb18bf5STamas Berghammer     file_spec.Clear();
2987162fb8e8SPavel Labath     ProcFileReader::ProcessLineByLine(GetID(), "maps",
2988162fb8e8SPavel Labath         [&] (const std::string &line)
2989162fb8e8SPavel Labath         {
2990162fb8e8SPavel Labath             SmallVector<StringRef, 16> columns;
2991162fb8e8SPavel Labath             StringRef(line).split(columns, " ", -1, false);
2992162fb8e8SPavel Labath             if (columns.size() < 6)
2993162fb8e8SPavel Labath                 return true; // continue searching
2994162fb8e8SPavel Labath 
2995162fb8e8SPavel Labath             FileSpec this_file_spec(columns[5].str().c_str(), false);
2996162fb8e8SPavel Labath             if (this_file_spec.GetFilename() != module_file_spec.GetFilename())
2997162fb8e8SPavel Labath                 return true; // continue searching
2998162fb8e8SPavel Labath 
2999162fb8e8SPavel Labath             file_spec = this_file_spec;
3000162fb8e8SPavel Labath             found = true;
3001162fb8e8SPavel Labath             return false; // we are done
3002162fb8e8SPavel Labath         });
3003162fb8e8SPavel Labath 
3004162fb8e8SPavel Labath     if (! found)
30057cb18bf5STamas Berghammer         return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!",
30067cb18bf5STamas Berghammer                 module_file_spec.GetFilename().AsCString(), GetID());
3007162fb8e8SPavel Labath 
3008162fb8e8SPavel Labath     return Error();
30097cb18bf5STamas Berghammer }
3010c076559aSPavel Labath 
30115eb721edSPavel Labath Error
3012783bfc8cSTamas Berghammer NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr)
3013783bfc8cSTamas Berghammer {
3014783bfc8cSTamas Berghammer     load_addr = LLDB_INVALID_ADDRESS;
3015783bfc8cSTamas Berghammer     Error error = ProcFileReader::ProcessLineByLine (GetID (), "maps",
3016783bfc8cSTamas Berghammer         [&] (const std::string &line) -> bool
3017783bfc8cSTamas Berghammer         {
3018783bfc8cSTamas Berghammer             StringRef maps_row(line);
3019783bfc8cSTamas Berghammer 
3020783bfc8cSTamas Berghammer             SmallVector<StringRef, 16> maps_columns;
3021783bfc8cSTamas Berghammer             maps_row.split(maps_columns, StringRef(" "), -1, false);
3022783bfc8cSTamas Berghammer 
3023783bfc8cSTamas Berghammer             if (maps_columns.size() < 6)
3024783bfc8cSTamas Berghammer             {
3025783bfc8cSTamas Berghammer                 // Return true to continue reading the proc file
3026783bfc8cSTamas Berghammer                 return true;
3027783bfc8cSTamas Berghammer             }
3028783bfc8cSTamas Berghammer 
3029783bfc8cSTamas Berghammer             if (maps_columns[5] == file_name)
3030783bfc8cSTamas Berghammer             {
3031783bfc8cSTamas Berghammer                 StringExtractor addr_extractor(maps_columns[0].str().c_str());
3032783bfc8cSTamas Berghammer                 load_addr = addr_extractor.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
3033783bfc8cSTamas Berghammer 
3034783bfc8cSTamas Berghammer                 // Return false to stop reading the proc file further
3035783bfc8cSTamas Berghammer                 return false;
3036783bfc8cSTamas Berghammer             }
3037783bfc8cSTamas Berghammer 
3038783bfc8cSTamas Berghammer             // Return true to continue reading the proc file
3039783bfc8cSTamas Berghammer             return true;
3040783bfc8cSTamas Berghammer         });
3041783bfc8cSTamas Berghammer     return error;
3042783bfc8cSTamas Berghammer }
3043783bfc8cSTamas Berghammer 
3044783bfc8cSTamas Berghammer Error
30451dbc6c9cSPavel Labath NativeProcessLinux::ResumeThread(
3046c076559aSPavel Labath         lldb::tid_t tid,
30478c8ff7afSPavel Labath         NativeThreadLinux::ResumeThreadFunction request_thread_resume_function,
3048c076559aSPavel Labath         bool error_when_already_running)
3049c076559aSPavel Labath {
30505eb721edSPavel Labath     Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
30515eb721edSPavel Labath 
30521dbc6c9cSPavel Labath     if (log)
30531dbc6c9cSPavel Labath         log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ", error_when_already_running: %s)",
30541dbc6c9cSPavel Labath                 __FUNCTION__, tid, error_when_already_running?"true":"false");
30551dbc6c9cSPavel Labath 
30568c8ff7afSPavel Labath     auto thread_sp = std::static_pointer_cast<NativeThreadLinux>(GetThreadByID(tid));
30578c8ff7afSPavel Labath     lldbassert(thread_sp != nullptr);
30585eb721edSPavel Labath 
30598c8ff7afSPavel Labath     auto& context = thread_sp->GetThreadContext();
3060c076559aSPavel Labath     // Tell the thread to resume if we don't already think it is running.
30618c8ff7afSPavel Labath     const bool is_stopped = StateIsStoppedState(thread_sp->GetState(), true);
30625eb721edSPavel Labath 
30635eb721edSPavel Labath     lldbassert(!(error_when_already_running && !is_stopped));
30645eb721edSPavel Labath 
3065c076559aSPavel Labath     if (!is_stopped)
3066c076559aSPavel Labath     {
3067c076559aSPavel Labath         // It's not an error, just a log, if the error_when_already_running flag is not set.
3068c076559aSPavel Labath         // This covers cases where, for instance, we're just trying to resume all threads
3069c076559aSPavel Labath         // from the user side.
30705eb721edSPavel Labath         if (log)
30715eb721edSPavel Labath             log->Printf("NativeProcessLinux::%s tid %" PRIu64 " optional resume skipped since it is already running",
3072c076559aSPavel Labath                     __FUNCTION__,
3073c076559aSPavel Labath                     tid);
30745eb721edSPavel Labath         return Error();
3075c076559aSPavel Labath     }
3076c076559aSPavel Labath 
3077c076559aSPavel Labath     // Before we do the resume below, first check if we have a pending
3078108c325dSPavel Labath     // stop notification that is currently waiting for
3079c076559aSPavel Labath     // this thread to stop.  This is potentially a buggy situation since
3080c076559aSPavel Labath     // we're ostensibly waiting for threads to stop before we send out the
3081c076559aSPavel Labath     // pending notification, and here we are resuming one before we send
3082c076559aSPavel Labath     // out the pending stop notification.
3083108c325dSPavel Labath     if (m_pending_notification_up && log && m_pending_notification_up->wait_for_stop_tids.count (tid) > 0)
3084c076559aSPavel Labath     {
30855eb721edSPavel Labath         log->Printf("NativeProcessLinux::%s about to resume tid %" PRIu64 " per explicit request but we have a pending stop notification (tid %" PRIu64 ") that is actively waiting for this thread to stop. Valid sequence of events?", __FUNCTION__, tid, m_pending_notification_up->triggering_tid);
3086c076559aSPavel Labath     }
3087c076559aSPavel Labath 
3088c076559aSPavel Labath     // Request a resume.  We expect this to be synchronous and the system
3089c076559aSPavel Labath     // to reflect it is running after this completes.
3090c076559aSPavel Labath     const auto error = request_thread_resume_function (tid, false);
3091c076559aSPavel Labath     if (error.Success())
30928c8ff7afSPavel Labath         context.request_resume_function = request_thread_resume_function;
30935eb721edSPavel Labath     else if (log)
3094c076559aSPavel Labath     {
30955eb721edSPavel Labath         log->Printf("NativeProcessLinux::%s failed to resume thread tid  %" PRIu64 ": %s",
3096c076559aSPavel Labath                          __FUNCTION__, tid, error.AsCString ());
3097c076559aSPavel Labath     }
3098c076559aSPavel Labath 
30995eb721edSPavel Labath     return error;
3100c076559aSPavel Labath }
3101c076559aSPavel Labath 
3102c076559aSPavel Labath //===----------------------------------------------------------------------===//
3103c076559aSPavel Labath 
3104c076559aSPavel Labath void
3105337f3eb9SPavel Labath NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid)
3106c076559aSPavel Labath {
31075eb721edSPavel Labath     Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
3108c076559aSPavel Labath 
31095eb721edSPavel Labath     if (log)
3110c076559aSPavel Labath     {
31115eb721edSPavel Labath         log->Printf("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ")",
3112c076559aSPavel Labath                 __FUNCTION__, triggering_tid);
3113c076559aSPavel Labath     }
3114c076559aSPavel Labath 
3115337f3eb9SPavel Labath     DoStopThreads(PendingNotificationUP(new PendingNotification(triggering_tid)));
3116c076559aSPavel Labath 
31175eb721edSPavel Labath     if (log)
3118c076559aSPavel Labath     {
31195eb721edSPavel Labath         log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__);
3120c076559aSPavel Labath     }
3121c076559aSPavel Labath }
3122c076559aSPavel Labath 
3123c076559aSPavel Labath void
31249eb1ecb9SPavel Labath NativeProcessLinux::SignalIfAllThreadsStopped()
3125c076559aSPavel Labath {
3126c076559aSPavel Labath     if (m_pending_notification_up && m_pending_notification_up->wait_for_stop_tids.empty ())
3127c076559aSPavel Labath     {
31289eb1ecb9SPavel Labath         Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS));
31299eb1ecb9SPavel Labath 
31309eb1ecb9SPavel Labath         // Clear any temporary breakpoints we used to implement software single stepping.
31319eb1ecb9SPavel Labath         for (const auto &thread_info: m_threads_stepping_with_breakpoint)
31329eb1ecb9SPavel Labath         {
31339eb1ecb9SPavel Labath             Error error = RemoveBreakpoint (thread_info.second);
31349eb1ecb9SPavel Labath             if (error.Fail())
31359eb1ecb9SPavel Labath                 if (log)
31369eb1ecb9SPavel Labath                     log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " remove stepping breakpoint: %s",
31379eb1ecb9SPavel Labath                             __FUNCTION__, thread_info.first, error.AsCString());
31389eb1ecb9SPavel Labath         }
31399eb1ecb9SPavel Labath         m_threads_stepping_with_breakpoint.clear();
31409eb1ecb9SPavel Labath 
31419eb1ecb9SPavel Labath         // Notify the delegate about the stop
3142ed89c7feSPavel Labath         SetCurrentThreadID(m_pending_notification_up->triggering_tid);
3143ed89c7feSPavel Labath         SetState(StateType::eStateStopped, true);
3144c076559aSPavel Labath         m_pending_notification_up.reset();
3145c076559aSPavel Labath     }
3146c076559aSPavel Labath }
3147c076559aSPavel Labath 
3148c076559aSPavel Labath void
3149c076559aSPavel Labath NativeProcessLinux::RequestStopOnAllRunningThreads()
3150c076559aSPavel Labath {
3151c076559aSPavel Labath     // Request a stop for all the thread stops that need to be stopped
3152c076559aSPavel Labath     // and are not already known to be stopped.  Keep a list of all the
3153c076559aSPavel Labath     // threads from which we still need to hear a stop reply.
3154c076559aSPavel Labath 
3155c076559aSPavel Labath     ThreadIDSet sent_tids;
31568c8ff7afSPavel Labath     for (const auto &thread_sp: m_threads)
3157c076559aSPavel Labath     {
31588c8ff7afSPavel Labath         // We only care about running threads
31598c8ff7afSPavel Labath         if (StateIsStoppedState(thread_sp->GetState(), true))
31608c8ff7afSPavel Labath             continue;
31618c8ff7afSPavel Labath 
31628c8ff7afSPavel Labath         static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop();
3163108c325dSPavel Labath         sent_tids.insert (thread_sp->GetID());
3164c076559aSPavel Labath     }
3165c076559aSPavel Labath 
3166c076559aSPavel Labath     // Set the wait list to the set of tids for which we requested stops.
3167c076559aSPavel Labath     m_pending_notification_up->wait_for_stop_tids.swap (sent_tids);
3168c076559aSPavel Labath }
3169c076559aSPavel Labath 
3170c076559aSPavel Labath 
31715eb721edSPavel Labath Error
31725eb721edSPavel Labath NativeProcessLinux::ThreadDidStop (lldb::tid_t tid, bool initiated_by_llgs)
3173c076559aSPavel Labath {
31741dbc6c9cSPavel Labath     Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
31751dbc6c9cSPavel Labath 
31761dbc6c9cSPavel Labath     if (log)
31771dbc6c9cSPavel Labath         log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ", %sinitiated by llgs)",
31781dbc6c9cSPavel Labath                 __FUNCTION__, tid, initiated_by_llgs?"":"not ");
31791dbc6c9cSPavel Labath 
3180c076559aSPavel Labath     // Ensure we know about the thread.
31818c8ff7afSPavel Labath     auto thread_sp = std::static_pointer_cast<NativeThreadLinux>(GetThreadByID(tid));
31828c8ff7afSPavel Labath     lldbassert(thread_sp != nullptr);
3183c076559aSPavel Labath 
3184c076559aSPavel Labath     // Update the global list of known thread states.  This one is definitely stopped.
31858c8ff7afSPavel Labath     auto& context = thread_sp->GetThreadContext();
31868c8ff7afSPavel Labath     const auto stop_was_requested = context.stop_requested;
31878c8ff7afSPavel Labath     context.stop_requested = false;
3188c076559aSPavel Labath 
3189c076559aSPavel Labath     // If we have a pending notification, remove this from the set.
3190c076559aSPavel Labath     if (m_pending_notification_up)
3191c076559aSPavel Labath     {
3192c076559aSPavel Labath         m_pending_notification_up->wait_for_stop_tids.erase(tid);
31939eb1ecb9SPavel Labath         SignalIfAllThreadsStopped();
3194c076559aSPavel Labath     }
3195c076559aSPavel Labath 
31968c8ff7afSPavel Labath     Error error;
31978c8ff7afSPavel Labath     if (initiated_by_llgs && context.request_resume_function && !stop_was_requested)
3198c076559aSPavel Labath     {
3199c076559aSPavel Labath         // We can end up here if stop was initiated by LLGS but by this time a
3200c076559aSPavel Labath         // thread stop has occurred - maybe initiated by another event.
32015eb721edSPavel Labath         if (log)
32025eb721edSPavel Labath             log->Printf("Resuming thread %"  PRIu64 " since stop wasn't requested", tid);
32038c8ff7afSPavel Labath         error = context.request_resume_function (tid, true);
32048c8ff7afSPavel Labath         if (error.Fail() && log)
32055eb721edSPavel Labath         {
32065eb721edSPavel Labath                 log->Printf("NativeProcessLinux::%s failed to resume thread tid  %" PRIu64 ": %s",
3207c076559aSPavel Labath                         __FUNCTION__, tid, error.AsCString ());
3208c076559aSPavel Labath         }
32098c8ff7afSPavel Labath     }
32105eb721edSPavel Labath     return error;
3211c076559aSPavel Labath }
3212c076559aSPavel Labath 
3213c076559aSPavel Labath void
3214ed89c7feSPavel Labath NativeProcessLinux::DoStopThreads(PendingNotificationUP &&notification_up)
3215c076559aSPavel Labath {
32165eb721edSPavel Labath     Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
32175eb721edSPavel Labath     if (m_pending_notification_up && log)
3218c076559aSPavel Labath     {
3219c076559aSPavel Labath         // Yikes - we've already got a pending signal notification in progress.
3220c076559aSPavel Labath         // Log this info.  We lose the pending notification here.
32215eb721edSPavel Labath         log->Printf("NativeProcessLinux::%s dropping existing pending signal notification for tid %" PRIu64 ", to be replaced with signal for tid %" PRIu64,
3222c076559aSPavel Labath                    __FUNCTION__,
3223c076559aSPavel Labath                    m_pending_notification_up->triggering_tid,
3224c076559aSPavel Labath                    notification_up->triggering_tid);
3225c076559aSPavel Labath     }
3226c076559aSPavel Labath     m_pending_notification_up = std::move(notification_up);
3227c076559aSPavel Labath 
3228c076559aSPavel Labath     RequestStopOnAllRunningThreads();
3229c076559aSPavel Labath 
32309eb1ecb9SPavel Labath     SignalIfAllThreadsStopped();
3231c076559aSPavel Labath }
3232c076559aSPavel Labath 
3233c076559aSPavel Labath void
32348c8ff7afSPavel Labath NativeProcessLinux::ThreadWasCreated (lldb::tid_t tid)
3235c076559aSPavel Labath {
32361dbc6c9cSPavel Labath     Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD);
32371dbc6c9cSPavel Labath 
32381dbc6c9cSPavel Labath     if (log)
32391dbc6c9cSPavel Labath         log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, tid);
32401dbc6c9cSPavel Labath 
32418c8ff7afSPavel Labath     auto thread_sp = std::static_pointer_cast<NativeThreadLinux>(GetThreadByID(tid));
32428c8ff7afSPavel Labath     lldbassert(thread_sp != nullptr);
3243c076559aSPavel Labath 
32448c8ff7afSPavel Labath     if (m_pending_notification_up && StateIsRunningState(thread_sp->GetState()))
3245c076559aSPavel Labath     {
3246c076559aSPavel Labath         // We will need to wait for this new thread to stop as well before firing the
3247c076559aSPavel Labath         // notification.
3248c076559aSPavel Labath         m_pending_notification_up->wait_for_stop_tids.insert(tid);
32498c8ff7afSPavel Labath         thread_sp->RequestStop();
3250c076559aSPavel Labath     }
3251c076559aSPavel Labath }
3252068f8a7eSTamas Berghammer 
325319cbe96aSPavel Labath void
325419cbe96aSPavel Labath NativeProcessLinux::SigchldHandler()
3255068f8a7eSTamas Berghammer {
325619cbe96aSPavel Labath     Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
325719cbe96aSPavel Labath     // Process all pending waitpid notifications.
325819cbe96aSPavel Labath     while (true)
325919cbe96aSPavel Labath     {
326019cbe96aSPavel Labath         int status = -1;
326119cbe96aSPavel Labath         ::pid_t wait_pid = waitpid(-1, &status, __WALL | __WNOTHREAD | WNOHANG);
326219cbe96aSPavel Labath 
326319cbe96aSPavel Labath         if (wait_pid == 0)
326419cbe96aSPavel Labath             break; // We are done.
326519cbe96aSPavel Labath 
326619cbe96aSPavel Labath         if (wait_pid == -1)
326719cbe96aSPavel Labath         {
326819cbe96aSPavel Labath             if (errno == EINTR)
326919cbe96aSPavel Labath                 continue;
327019cbe96aSPavel Labath 
327119cbe96aSPavel Labath             Error error(errno, eErrorTypePOSIX);
327219cbe96aSPavel Labath             if (log)
327319cbe96aSPavel Labath                 log->Printf("NativeProcessLinux::%s waitpid (-1, &status, __WALL | __WNOTHREAD | WNOHANG) failed: %s",
327419cbe96aSPavel Labath                         __FUNCTION__, error.AsCString());
327519cbe96aSPavel Labath             break;
327619cbe96aSPavel Labath         }
327719cbe96aSPavel Labath 
327819cbe96aSPavel Labath         bool exited = false;
327919cbe96aSPavel Labath         int signal = 0;
328019cbe96aSPavel Labath         int exit_status = 0;
328119cbe96aSPavel Labath         const char *status_cstr = nullptr;
328219cbe96aSPavel Labath         if (WIFSTOPPED(status))
328319cbe96aSPavel Labath         {
328419cbe96aSPavel Labath             signal = WSTOPSIG(status);
328519cbe96aSPavel Labath             status_cstr = "STOPPED";
328619cbe96aSPavel Labath         }
328719cbe96aSPavel Labath         else if (WIFEXITED(status))
328819cbe96aSPavel Labath         {
328919cbe96aSPavel Labath             exit_status = WEXITSTATUS(status);
329019cbe96aSPavel Labath             status_cstr = "EXITED";
329119cbe96aSPavel Labath             exited = true;
329219cbe96aSPavel Labath         }
329319cbe96aSPavel Labath         else if (WIFSIGNALED(status))
329419cbe96aSPavel Labath         {
329519cbe96aSPavel Labath             signal = WTERMSIG(status);
329619cbe96aSPavel Labath             status_cstr = "SIGNALED";
329719cbe96aSPavel Labath             if (wait_pid == static_cast< ::pid_t>(GetID())) {
329819cbe96aSPavel Labath                 exited = true;
329919cbe96aSPavel Labath                 exit_status = -1;
330019cbe96aSPavel Labath             }
330119cbe96aSPavel Labath         }
330219cbe96aSPavel Labath         else
330319cbe96aSPavel Labath             status_cstr = "(\?\?\?)";
330419cbe96aSPavel Labath 
330519cbe96aSPavel Labath         if (log)
330619cbe96aSPavel Labath             log->Printf("NativeProcessLinux::%s: waitpid (-1, &status, __WALL | __WNOTHREAD | WNOHANG)"
330719cbe96aSPavel Labath                 "=> pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i",
330819cbe96aSPavel Labath                 __FUNCTION__, wait_pid, status, status_cstr, signal, exit_status);
330919cbe96aSPavel Labath 
331019cbe96aSPavel Labath         MonitorCallback (wait_pid, exited, signal, exit_status);
331119cbe96aSPavel Labath     }
3312068f8a7eSTamas Berghammer }
3313068f8a7eSTamas Berghammer 
3314068f8a7eSTamas Berghammer // Wrapper for ptrace to catch errors and log calls.
3315068f8a7eSTamas Berghammer // Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*)
33164a9babb2SPavel Labath Error
33174a9babb2SPavel Labath NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, long *result)
3318068f8a7eSTamas Berghammer {
33194a9babb2SPavel Labath     Error error;
33204a9babb2SPavel Labath     long int ret;
3321068f8a7eSTamas Berghammer 
3322068f8a7eSTamas Berghammer     Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
3323068f8a7eSTamas Berghammer 
3324068f8a7eSTamas Berghammer     PtraceDisplayBytes(req, data, data_size);
3325068f8a7eSTamas Berghammer 
3326068f8a7eSTamas Berghammer     errno = 0;
3327068f8a7eSTamas Berghammer     if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
33284a9babb2SPavel Labath         ret = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data);
3329068f8a7eSTamas Berghammer     else
33304a9babb2SPavel Labath         ret = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data);
3331068f8a7eSTamas Berghammer 
33324a9babb2SPavel Labath     if (ret == -1)
3333068f8a7eSTamas Berghammer         error.SetErrorToErrno();
3334068f8a7eSTamas Berghammer 
33354a9babb2SPavel Labath     if (result)
33364a9babb2SPavel Labath         *result = ret;
33374a9babb2SPavel Labath 
3338068f8a7eSTamas Berghammer     if (log)
33394a9babb2SPavel Labath         log->Printf("ptrace(%d, %" PRIu64 ", %p, %p, %zu)=%lX", req, pid, addr, data, data_size, ret);
3340068f8a7eSTamas Berghammer 
3341068f8a7eSTamas Berghammer     PtraceDisplayBytes(req, data, data_size);
3342068f8a7eSTamas Berghammer 
3343068f8a7eSTamas Berghammer     if (log && error.GetError() != 0)
3344068f8a7eSTamas Berghammer     {
3345068f8a7eSTamas Berghammer         const char* str;
3346068f8a7eSTamas Berghammer         switch (error.GetError())
3347068f8a7eSTamas Berghammer         {
3348068f8a7eSTamas Berghammer         case ESRCH:  str = "ESRCH"; break;
3349068f8a7eSTamas Berghammer         case EINVAL: str = "EINVAL"; break;
3350068f8a7eSTamas Berghammer         case EBUSY:  str = "EBUSY"; break;
3351068f8a7eSTamas Berghammer         case EPERM:  str = "EPERM"; break;
3352068f8a7eSTamas Berghammer         default:     str = error.AsCString();
3353068f8a7eSTamas Berghammer         }
3354068f8a7eSTamas Berghammer         log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str);
3355068f8a7eSTamas Berghammer     }
3356068f8a7eSTamas Berghammer 
33574a9babb2SPavel Labath     return error;
3358068f8a7eSTamas Berghammer }
3359