blob: c08c46e6c7edc1282ebb4a23f4cb4a1d4e0697e3 [file] [log] [blame]
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001//===-- ProcessMonitor.cpp ------------------------------------ -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Stephen Wilsone6f9f662010-07-24 02:19:04 +000012// C Includes
13#include <errno.h>
14#include <poll.h>
15#include <string.h>
Daniel Maleaa85e6b62012-12-07 22:21:08 +000016#include <stdint.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000017#include <unistd.h>
Todd Fiala42079682014-08-27 16:05:26 +000018#include <elf.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000019
20// C++ Includes
21// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000022#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000023#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000024#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000025#include "lldb/Core/Scalar.h"
26#include "lldb/Host/Host.h"
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +000027#include "lldb/Host/HostNativeThread.h"
Zachary Turner39de3112014-09-09 20:54:56 +000028#include "lldb/Host/HostThread.h"
29#include "lldb/Host/ThreadLauncher.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000030#include "lldb/Target/Thread.h"
31#include "lldb/Target/RegisterContext.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000032#include "lldb/Target/UnixSignals.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000033#include "lldb/Utility/PseudoTerminal.h"
34
Chaoren Lin28e57422015-02-03 01:51:25 +000035#include "Plugins/Process/POSIX/CrashReason.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000036#include "Plugins/Process/POSIX/POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037#include "ProcessLinux.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000038#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000039#include "ProcessMonitor.h"
Tamas Berghammer1e209fc2015-03-13 11:36:47 +000040#include "Procfs.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000041
Tamas Berghammerd8584872015-02-06 10:57:40 +000042// System includes - They have to be included after framework includes because they define some
43// macros which collide with variable names in other modules
Vince Harron8b335672015-05-12 01:10:56 +000044
45#include "lldb/Host/linux/Personality.h"
46#include "lldb/Host/linux/Ptrace.h"
47#include "lldb/Host/linux/Signalfd.h"
48#include "lldb/Host/android/Android.h"
49
Tamas Berghammerd8584872015-02-06 10:57:40 +000050#include <sys/socket.h>
51#include <sys/syscall.h>
52#include <sys/types.h>
53#include <sys/uio.h>
54#include <sys/user.h>
55#include <sys/wait.h>
56
Todd Fiala0bce1b62014-08-17 00:10:50 +000057#define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff
Matt Kopec58c0b962013-03-20 20:34:35 +000058
Matt Kopece9ea0da2013-05-07 19:29:28 +000059// Support hardware breakpoints in case it has not been defined
60#ifndef TRAP_HWBKPT
61 #define TRAP_HWBKPT 4
62#endif
63
Andrew Kaylor93132f52013-05-28 23:04:25 +000064// Try to define a macro to encapsulate the tgkill syscall
65// fall back on kill() if tgkill isn't available
Chaoren Linc9346592015-02-28 00:20:16 +000066#define tgkill(pid, tid, sig) \
67 syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig)
Andrew Kaylor93132f52013-05-28 23:04:25 +000068
Stephen Wilsone6f9f662010-07-24 02:19:04 +000069using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000070using namespace lldb_private::process_linux;
Stephen Wilsone6f9f662010-07-24 02:19:04 +000071
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +000072static Operation* EXIT_OPERATION = nullptr;
73
Johnny Chen0d5f2d42011-10-18 18:09:30 +000074// FIXME: this code is host-dependent with respect to types and
75// endianness and needs to be fixed. For example, lldb::addr_t is
76// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
77// 32-bit pointer arguments. This code uses casts to work around the
78// problem.
79
80// We disable the tracing of ptrace calls for integration builds to
81// avoid the additional indirection and checks.
82#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
83
Greg Clayton386ff182011-11-05 01:09:16 +000084static void
85DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
86{
87 uint8_t *ptr = (uint8_t *)bytes;
88 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
89 for(uint32_t i=0; i<loop_count; i++)
90 {
91 s.Printf ("[%x]", *ptr);
92 ptr++;
93 }
94}
95
Matt Kopec58c0b962013-03-20 20:34:35 +000096static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +000097{
98 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +000099 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000100 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000101
102 if (verbose_log)
103 {
104 switch(req)
105 {
106 case PTRACE_POKETEXT:
107 {
108 DisplayBytes(buf, &data, 8);
109 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
110 break;
111 }
Greg Clayton542e4072012-09-07 17:49:29 +0000112 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000113 {
114 DisplayBytes(buf, &data, 8);
115 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
116 break;
117 }
Greg Clayton542e4072012-09-07 17:49:29 +0000118 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000119 {
120 DisplayBytes(buf, &data, 8);
121 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
122 break;
123 }
Todd Fiala6ac1be42014-08-21 16:34:03 +0000124#if !defined (__arm64__) && !defined (__aarch64__)
Greg Clayton542e4072012-09-07 17:49:29 +0000125 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000126 {
Matt Kopec7de48462013-03-06 17:20:48 +0000127 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000128 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
129 break;
130 }
131 case PTRACE_SETFPREGS:
132 {
Matt Kopec7de48462013-03-06 17:20:48 +0000133 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000134 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
135 break;
136 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000137#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000138 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000139 {
140 DisplayBytes(buf, data, sizeof(siginfo_t));
141 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
142 break;
143 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000144 case PTRACE_SETREGSET:
145 {
146 // Extract iov_base from data, which is a pointer to the struct IOVEC
147 DisplayBytes(buf, *(void **)data, data_size);
148 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
149 break;
150 }
Greg Clayton386ff182011-11-05 01:09:16 +0000151 default:
152 {
153 }
154 }
155 }
156}
157
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000158// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000159// Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000160extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000161PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000162 const char* reqName, const char* file, int line)
163{
Greg Clayton386ff182011-11-05 01:09:16 +0000164 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000165
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000166 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000167
Matt Kopec7de48462013-03-06 17:20:48 +0000168 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000169
170 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000171 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000172 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000173 else
Todd Fiala4507f062014-02-27 20:46:12 +0000174 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000175
Ed Mastec099c952014-02-24 14:07:45 +0000176 if (log)
177 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
178 reqName, pid, addr, data, data_size, result, file, line);
179
Matt Kopec7de48462013-03-06 17:20:48 +0000180 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000181
Matt Kopec7de48462013-03-06 17:20:48 +0000182 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000183 {
184 const char* str;
185 switch (errno)
186 {
187 case ESRCH: str = "ESRCH"; break;
188 case EINVAL: str = "EINVAL"; break;
189 case EBUSY: str = "EBUSY"; break;
190 case EPERM: str = "EPERM"; break;
191 default: str = "<unknown>";
192 }
193 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
194 }
195
196 return result;
197}
198
Matt Kopec7de48462013-03-06 17:20:48 +0000199// Wrapper for ptrace when logging is not required.
200// Sets errno to 0 prior to calling ptrace.
201extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000202PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000203{
Matt Kopec58c0b962013-03-20 20:34:35 +0000204 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000205 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000206 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
207 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
208 else
209 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000210 return result;
211}
212
213#define PTRACE(req, pid, addr, data, data_size) \
214 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000215#else
Matt Kopec7de48462013-03-06 17:20:48 +0000216 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000217#endif
218
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000219//------------------------------------------------------------------------------
220// Static implementations of ProcessMonitor::ReadMemory and
221// ProcessMonitor::WriteMemory. This enables mutual recursion between these
222// functions without needed to go thru the thread funnel.
223
224static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000225DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000226 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
227{
Greg Clayton542e4072012-09-07 17:49:29 +0000228 // ptrace word size is determined by the host, not the child
229 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000230 unsigned char *dst = static_cast<unsigned char*>(buf);
231 size_t bytes_read;
232 size_t remainder;
233 long data;
234
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000235 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000236 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000237 ProcessPOSIXLog::IncNestLevel();
238 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000239 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000240 pid, word_size, (void*)vm_addr, buf, size);
241
242 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000243 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
244 {
245 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000246 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
247 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000248 {
249 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000250 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000251 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000252 return bytes_read;
253 }
254
255 remainder = size - bytes_read;
256 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000257
258 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000259 for (unsigned i = 0; i < remainder; ++i)
260 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000261
Johnny Chen30213ff2012-01-05 19:17:38 +0000262 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
263 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
264 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
265 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000266 {
267 uintptr_t print_dst = 0;
268 // Format bytes from data by moving into print_dst for log output
269 for (unsigned i = 0; i < remainder; ++i)
270 print_dst |= (((data >> i*8) & 0xFF) << i*8);
271 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
272 (void*)vm_addr, print_dst, (unsigned long)data);
273 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000274
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000275 vm_addr += word_size;
276 dst += word_size;
277 }
278
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000279 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000280 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000281 return bytes_read;
282}
283
284static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000285DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000286 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
287{
Greg Clayton542e4072012-09-07 17:49:29 +0000288 // ptrace word size is determined by the host, not the child
289 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000290 const unsigned char *src = static_cast<const unsigned char*>(buf);
291 size_t bytes_written = 0;
292 size_t remainder;
293
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000294 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000295 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000296 ProcessPOSIXLog::IncNestLevel();
297 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000298 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000299 pid, word_size, (void*)vm_addr, buf, size);
300
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000301 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
302 {
303 remainder = size - bytes_written;
304 remainder = remainder > word_size ? word_size : remainder;
305
306 if (remainder == word_size)
307 {
308 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000309 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000310 for (unsigned i = 0; i < word_size; ++i)
311 data |= (unsigned long)src[i] << i*8;
312
Johnny Chen30213ff2012-01-05 19:17:38 +0000313 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
314 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
315 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
316 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000317 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
318 (void*)vm_addr, *(unsigned long*)src, data);
319
Matt Kopec7de48462013-03-06 17:20:48 +0000320 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000321 {
322 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000323 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000324 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000325 return bytes_written;
326 }
327 }
328 else
329 {
330 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000331 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000332 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000333 {
334 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000335 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000336 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000337 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000338
339 memcpy(buff, src, remainder);
340
Greg Clayton542e4072012-09-07 17:49:29 +0000341 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000342 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000343 {
344 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000345 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000346 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000347 }
348
Johnny Chen30213ff2012-01-05 19:17:38 +0000349 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
350 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
351 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
352 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000353 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
Vince Harrond7e6a4f2015-05-13 00:25:54 +0000354 (void*)vm_addr, *(const unsigned long*)src, *(const unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000355 }
356
357 vm_addr += word_size;
358 src += word_size;
359 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000360 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000361 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000362 return bytes_written;
363}
364
Stephen Wilson26977162011-03-23 02:14:42 +0000365// Simple helper function to ensure flags are enabled on the given file
366// descriptor.
367static bool
368EnsureFDFlags(int fd, int flags, Error &error)
369{
370 int status;
371
372 if ((status = fcntl(fd, F_GETFL)) == -1)
373 {
374 error.SetErrorToErrno();
375 return false;
376 }
377
378 if (fcntl(fd, F_SETFL, status | flags) == -1)
379 {
380 error.SetErrorToErrno();
381 return false;
382 }
383
384 return true;
385}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000386
387//------------------------------------------------------------------------------
388/// @class Operation
389/// @brief Represents a ProcessMonitor operation.
390///
391/// Under Linux, it is not possible to ptrace() from any other thread but the
392/// one that spawned or attached to the process from the start. Therefore, when
393/// a ProcessMonitor is asked to deliver or change the state of an inferior
394/// process the operation must be "funneled" to a specific thread to perform the
395/// task. The Operation class provides an abstract base for all services the
396/// ProcessMonitor must perform via the single virtual function Execute, thus
397/// encapsulating the code that needs to run in the privileged context.
398class Operation
399{
400public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000401 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000402 virtual void Execute(ProcessMonitor *monitor) = 0;
403};
404
405//------------------------------------------------------------------------------
406/// @class ReadOperation
407/// @brief Implements ProcessMonitor::ReadMemory.
408class ReadOperation : public Operation
409{
410public:
411 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
412 Error &error, size_t &result)
413 : m_addr(addr), m_buff(buff), m_size(size),
414 m_error(error), m_result(result)
415 { }
416
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000417 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000418
419private:
420 lldb::addr_t m_addr;
421 void *m_buff;
422 size_t m_size;
423 Error &m_error;
424 size_t &m_result;
425};
426
427void
428ReadOperation::Execute(ProcessMonitor *monitor)
429{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000430 lldb::pid_t pid = monitor->GetPID();
431
Greg Clayton542e4072012-09-07 17:49:29 +0000432 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000433}
434
435//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000436/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000437/// @brief Implements ProcessMonitor::WriteMemory.
438class WriteOperation : public Operation
439{
440public:
441 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
442 Error &error, size_t &result)
443 : m_addr(addr), m_buff(buff), m_size(size),
444 m_error(error), m_result(result)
445 { }
446
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000447 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000448
449private:
450 lldb::addr_t m_addr;
451 const void *m_buff;
452 size_t m_size;
453 Error &m_error;
454 size_t &m_result;
455};
456
457void
458WriteOperation::Execute(ProcessMonitor *monitor)
459{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000460 lldb::pid_t pid = monitor->GetPID();
461
Greg Clayton542e4072012-09-07 17:49:29 +0000462 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000463}
464
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000465
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000466//------------------------------------------------------------------------------
467/// @class ReadRegOperation
468/// @brief Implements ProcessMonitor::ReadRegisterValue.
469class ReadRegOperation : public Operation
470{
471public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000472 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000473 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000474 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000475 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000476 { }
477
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000478 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000479
480private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000481 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000482 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000483 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000484 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000485 bool &m_result;
486};
487
488void
489ReadRegOperation::Execute(ProcessMonitor *monitor)
490{
Todd Fiala49131cf2014-09-12 16:57:28 +0000491#if defined (__arm64__) || defined (__aarch64__)
492 if (m_offset > sizeof(struct user_pt_regs))
493 {
494 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
495 if (offset > sizeof(struct user_fpsimd_state))
496 {
497 m_result = false;
498 }
499 else
500 {
501 elf_fpregset_t regs;
502 int regset = NT_FPREGSET;
503 struct iovec ioVec;
504
505 ioVec.iov_base = &regs;
506 ioVec.iov_len = sizeof regs;
507 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
508 m_result = false;
509 else
510 {
511 m_result = true;
512 m_value.SetBytes((void *)(((unsigned char *)(&regs)) + offset), 16, monitor->GetProcess().GetByteOrder());
513 }
514 }
515 }
516 else
517 {
518 elf_gregset_t regs;
519 int regset = NT_PRSTATUS;
520 struct iovec ioVec;
521
522 ioVec.iov_base = &regs;
523 ioVec.iov_len = sizeof regs;
524 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
525 m_result = false;
526 else
527 {
528 m_result = true;
529 m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, monitor->GetProcess().GetByteOrder());
530 }
531 }
532#else
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000533 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000534
535 // Set errno to zero so that we can detect a failed peek.
536 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000537 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
538 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000539 m_result = false;
540 else
541 {
542 m_value = data;
543 m_result = true;
544 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000545 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000546 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000547 m_reg_name, data);
Todd Fiala49131cf2014-09-12 16:57:28 +0000548#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000549}
550
551//------------------------------------------------------------------------------
552/// @class WriteRegOperation
553/// @brief Implements ProcessMonitor::WriteRegisterValue.
554class WriteRegOperation : public Operation
555{
556public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000557 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000558 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000559 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000560 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000561 { }
562
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000563 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000564
565private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000566 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000567 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000568 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000569 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000570 bool &m_result;
571};
572
573void
574WriteRegOperation::Execute(ProcessMonitor *monitor)
575{
Todd Fiala49131cf2014-09-12 16:57:28 +0000576#if defined (__arm64__) || defined (__aarch64__)
577 if (m_offset > sizeof(struct user_pt_regs))
578 {
579 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
580 if (offset > sizeof(struct user_fpsimd_state))
581 {
582 m_result = false;
583 }
584 else
585 {
586 elf_fpregset_t regs;
587 int regset = NT_FPREGSET;
588 struct iovec ioVec;
589
590 ioVec.iov_base = &regs;
591 ioVec.iov_len = sizeof regs;
592 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
593 m_result = false;
594 else
595 {
596 ::memcpy((void *)(((unsigned char *)(&regs)) + offset), m_value.GetBytes(), 16);
597 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
598 m_result = false;
599 else
600 m_result = true;
601 }
602 }
603 }
604 else
605 {
606 elf_gregset_t regs;
607 int regset = NT_PRSTATUS;
608 struct iovec ioVec;
609
610 ioVec.iov_base = &regs;
611 ioVec.iov_len = sizeof regs;
612 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
613 m_result = false;
614 else
615 {
616 ::memcpy((void *)(((unsigned char *)(&regs)) + m_offset), m_value.GetBytes(), 8);
617 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
618 m_result = false;
619 else
620 m_result = true;
621 }
622 }
623#else
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000624 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000625 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000626
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000627 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000628
629 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000630 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000631 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000632 m_result = false;
633 else
634 m_result = true;
Todd Fiala49131cf2014-09-12 16:57:28 +0000635#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000636}
637
638//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000639/// @class ReadGPROperation
640/// @brief Implements ProcessMonitor::ReadGPR.
641class ReadGPROperation : public Operation
642{
643public:
Matt Kopec7de48462013-03-06 17:20:48 +0000644 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
645 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000646 { }
647
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000648 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000649
650private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000651 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000652 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000653 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000654 bool &m_result;
655};
656
657void
658ReadGPROperation::Execute(ProcessMonitor *monitor)
659{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000660#if defined (__arm64__) || defined (__aarch64__)
661 int regset = NT_PRSTATUS;
662 struct iovec ioVec;
663
664 ioVec.iov_base = m_buf;
665 ioVec.iov_len = m_buf_size;
666 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000667 m_result = false;
668 else
669 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000670#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000671 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
672 m_result = false;
673 else
674 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000675#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000676}
677
678//------------------------------------------------------------------------------
679/// @class ReadFPROperation
680/// @brief Implements ProcessMonitor::ReadFPR.
681class ReadFPROperation : public Operation
682{
683public:
Matt Kopec7de48462013-03-06 17:20:48 +0000684 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
685 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000686 { }
687
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000688 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000689
690private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000691 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000692 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000693 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000694 bool &m_result;
695};
696
697void
698ReadFPROperation::Execute(ProcessMonitor *monitor)
699{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000700#if defined (__arm64__) || defined (__aarch64__)
701 int regset = NT_FPREGSET;
702 struct iovec ioVec;
703
704 ioVec.iov_base = m_buf;
705 ioVec.iov_len = m_buf_size;
706 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000707 m_result = false;
708 else
709 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000710#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000711 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
712 m_result = false;
713 else
714 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000715#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000716}
717
718//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000719/// @class ReadRegisterSetOperation
720/// @brief Implements ProcessMonitor::ReadRegisterSet.
721class ReadRegisterSetOperation : public Operation
722{
723public:
724 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
725 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
726 { }
727
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000728 void Execute(ProcessMonitor *monitor) override;
Matt Kopec58c0b962013-03-20 20:34:35 +0000729
730private:
731 lldb::tid_t m_tid;
732 void *m_buf;
733 size_t m_buf_size;
734 const unsigned int m_regset;
735 bool &m_result;
736};
737
738void
739ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
740{
741 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
742 m_result = false;
743 else
744 m_result = true;
745}
746
747//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000748/// @class WriteGPROperation
749/// @brief Implements ProcessMonitor::WriteGPR.
750class WriteGPROperation : public Operation
751{
752public:
Matt Kopec7de48462013-03-06 17:20:48 +0000753 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
754 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000755 { }
756
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000757 void Execute(ProcessMonitor *monitor) override;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000758
759private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000760 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000761 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000762 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000763 bool &m_result;
764};
765
766void
767WriteGPROperation::Execute(ProcessMonitor *monitor)
768{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000769#if defined (__arm64__) || defined (__aarch64__)
770 int regset = NT_PRSTATUS;
771 struct iovec ioVec;
772
773 ioVec.iov_base = m_buf;
774 ioVec.iov_len = m_buf_size;
775 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000776 m_result = false;
777 else
778 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000779#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000780 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
781 m_result = false;
782 else
783 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000784#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000785}
786
787//------------------------------------------------------------------------------
788/// @class WriteFPROperation
789/// @brief Implements ProcessMonitor::WriteFPR.
790class WriteFPROperation : public Operation
791{
792public:
Matt Kopec7de48462013-03-06 17:20:48 +0000793 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
794 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000795 { }
796
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000797 void Execute(ProcessMonitor *monitor) override;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000798
799private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000800 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000801 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000802 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000803 bool &m_result;
804};
805
806void
807WriteFPROperation::Execute(ProcessMonitor *monitor)
808{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000809#if defined (__arm64__) || defined (__aarch64__)
810 int regset = NT_FPREGSET;
811 struct iovec ioVec;
812
813 ioVec.iov_base = m_buf;
814 ioVec.iov_len = m_buf_size;
815 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000816 m_result = false;
817 else
818 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000819#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000820 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
821 m_result = false;
822 else
823 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000824#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000825}
826
827//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000828/// @class WriteRegisterSetOperation
829/// @brief Implements ProcessMonitor::WriteRegisterSet.
830class WriteRegisterSetOperation : public Operation
831{
832public:
833 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
834 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
835 { }
836
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000837 void Execute(ProcessMonitor *monitor) override;
Matt Kopec58c0b962013-03-20 20:34:35 +0000838
839private:
840 lldb::tid_t m_tid;
841 void *m_buf;
842 size_t m_buf_size;
843 const unsigned int m_regset;
844 bool &m_result;
845};
846
847void
848WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
849{
850 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
851 m_result = false;
852 else
853 m_result = true;
854}
855
856//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000857/// @class ReadThreadPointerOperation
858/// @brief Implements ProcessMonitor::ReadThreadPointer.
859class ReadThreadPointerOperation : public Operation
860{
861public:
862 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
863 : m_tid(tid), m_addr(addr), m_result(result)
864 { }
865
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000866 void Execute(ProcessMonitor *monitor) override;
Richard Mitton0a558352013-10-17 21:14:00 +0000867
868private:
869 lldb::tid_t m_tid;
870 lldb::addr_t *m_addr;
871 bool &m_result;
872};
873
874void
875ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
876{
877 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
878 if (log)
879 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
880
881 // The process for getting the thread area on Linux is
882 // somewhat... obscure. There's several different ways depending on
883 // what arch you're on, and what kernel version you have.
884
885 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
886 switch(arch.GetMachine())
887 {
Todd Fiala42079682014-08-27 16:05:26 +0000888 case llvm::Triple::aarch64:
889 {
Todd Fialadbec1ff2014-09-04 16:08:20 +0000890 int regset = LLDB_PTRACE_NT_ARM_TLS;
Todd Fiala42079682014-08-27 16:05:26 +0000891 struct iovec ioVec;
892
893 ioVec.iov_base = m_addr;
894 ioVec.iov_len = sizeof(lldb::addr_t);
895 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, ioVec.iov_len) < 0)
896 m_result = false;
897 else
898 m_result = true;
899 break;
900 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000901#if defined(__i386__) || defined(__x86_64__)
902 // Note that struct user below has a field named i387 which is x86-specific.
903 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000904 case llvm::Triple::x86:
905 {
906 // Find the GS register location for our host architecture.
907 size_t gs_user_offset = offsetof(struct user, regs);
908#ifdef __x86_64__
909 gs_user_offset += offsetof(struct user_regs_struct, gs);
910#endif
911#ifdef __i386__
912 gs_user_offset += offsetof(struct user_regs_struct, xgs);
913#endif
914
915 // Read the GS register value to get the selector.
916 errno = 0;
917 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
918 if (errno)
919 {
920 m_result = false;
921 break;
922 }
923
924 // Read the LDT base for that selector.
925 uint32_t tmp[4];
926 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
927 *m_addr = tmp[1];
928 break;
929 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000930#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000931 case llvm::Triple::x86_64:
932 // Read the FS register base.
933 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
934 break;
935 default:
936 m_result = false;
937 break;
938 }
939}
940
941//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000942/// @class ResumeOperation
943/// @brief Implements ProcessMonitor::Resume.
944class ResumeOperation : public Operation
945{
946public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000947 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
948 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000949
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000950 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000951
952private:
953 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000954 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000955 bool &m_result;
956};
957
958void
959ResumeOperation::Execute(ProcessMonitor *monitor)
960{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000961 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000962
963 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
964 data = m_signo;
965
Matt Kopec7de48462013-03-06 17:20:48 +0000966 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000967 {
968 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
969
970 if (log)
971 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000972 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000973 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000974 else
975 m_result = true;
976}
977
978//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +0000979/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000980/// @brief Implements ProcessMonitor::SingleStep.
981class SingleStepOperation : public Operation
982{
983public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000984 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
985 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000986
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000987 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000988
989private:
990 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000991 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000992 bool &m_result;
993};
994
995void
996SingleStepOperation::Execute(ProcessMonitor *monitor)
997{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000998 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000999
1000 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
1001 data = m_signo;
1002
Matt Kopec7de48462013-03-06 17:20:48 +00001003 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001004 m_result = false;
1005 else
1006 m_result = true;
1007}
1008
1009//------------------------------------------------------------------------------
1010/// @class SiginfoOperation
1011/// @brief Implements ProcessMonitor::GetSignalInfo.
1012class SiginfoOperation : public Operation
1013{
1014public:
Daniel Maleaa35970a2012-11-23 18:09:58 +00001015 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
1016 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001017
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001018 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001019
1020private:
1021 lldb::tid_t m_tid;
1022 void *m_info;
1023 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001024 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001025};
1026
1027void
1028SiginfoOperation::Execute(ProcessMonitor *monitor)
1029{
Matt Kopec7de48462013-03-06 17:20:48 +00001030 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001031 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001032 m_err = errno;
1033 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001034 else
1035 m_result = true;
1036}
1037
1038//------------------------------------------------------------------------------
1039/// @class EventMessageOperation
1040/// @brief Implements ProcessMonitor::GetEventMessage.
1041class EventMessageOperation : public Operation
1042{
1043public:
1044 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
1045 : m_tid(tid), m_message(message), m_result(result) { }
1046
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001047 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001048
1049private:
1050 lldb::tid_t m_tid;
1051 unsigned long *m_message;
1052 bool &m_result;
1053};
1054
1055void
1056EventMessageOperation::Execute(ProcessMonitor *monitor)
1057{
Matt Kopec7de48462013-03-06 17:20:48 +00001058 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001059 m_result = false;
1060 else
1061 m_result = true;
1062}
1063
1064//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +00001065/// @class DetachOperation
1066/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +00001067class DetachOperation : public Operation
1068{
1069public:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001070 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +00001071
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001072 void Execute(ProcessMonitor *monitor) override;
Greg Clayton28041352011-11-29 20:50:10 +00001073
1074private:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001075 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +00001076 Error &m_error;
1077};
1078
1079void
1080DetachOperation::Execute(ProcessMonitor *monitor)
1081{
Matt Kopec085d6ce2013-05-31 22:00:07 +00001082 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +00001083 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +00001084}
1085
Johnny Chen25e68e32011-06-14 19:19:50 +00001086ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
1087 : m_monitor(monitor)
1088{
1089 sem_init(&m_semaphore, 0, 0);
1090}
1091
1092ProcessMonitor::OperationArgs::~OperationArgs()
1093{
1094 sem_destroy(&m_semaphore);
1095}
1096
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001097ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
1098 lldb_private::Module *module,
1099 char const **argv,
1100 char const **envp,
1101 const char *stdin_path,
1102 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001103 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001104 const char *working_dir,
1105 const lldb_private::ProcessLaunchInfo &launch_info)
Johnny Chen25e68e32011-06-14 19:19:50 +00001106 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001107 m_module(module),
1108 m_argv(argv),
1109 m_envp(envp),
1110 m_stdin_path(stdin_path),
1111 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +00001112 m_stderr_path(stderr_path),
Todd Fiala0bce1b62014-08-17 00:10:50 +00001113 m_working_dir(working_dir),
1114 m_launch_info(launch_info)
1115{
1116}
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001117
1118ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +00001119{ }
1120
1121ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
1122 lldb::pid_t pid)
1123 : OperationArgs(monitor), m_pid(pid) { }
1124
1125ProcessMonitor::AttachArgs::~AttachArgs()
1126{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001127
1128//------------------------------------------------------------------------------
1129/// The basic design of the ProcessMonitor is built around two threads.
1130///
1131/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
1132/// for changes in the debugee state. When a change is detected a
1133/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1134/// "drives" state changes in the debugger.
1135///
1136/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001137/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001138/// operations such as register reads/writes, stepping, etc. See the comments
1139/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001140ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001141 Module *module,
1142 const char *argv[],
1143 const char *envp[],
1144 const char *stdin_path,
1145 const char *stdout_path,
1146 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001147 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001148 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001149 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001150 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001151 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001152 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001153 m_pid(LLDB_INVALID_PROCESS_ID),
1154 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001155 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001156{
Daniel Malea1efb4182013-09-16 23:12:18 +00001157 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1158 stdin_path, stdout_path, stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001159 working_dir, launch_info));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001160
Daniel Malea1efb4182013-09-16 23:12:18 +00001161 sem_init(&m_operation_pending, 0, 0);
1162 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001163
Johnny Chen25e68e32011-06-14 19:19:50 +00001164 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001165 if (!error.Success())
1166 return;
1167
1168WAIT_AGAIN:
1169 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001170 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001171 {
1172 if (errno == EINTR)
1173 goto WAIT_AGAIN;
1174 else
1175 {
1176 error.SetErrorToErrno();
1177 return;
1178 }
1179 }
1180
1181 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001182 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001183 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001184 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001185 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001186 return;
1187 }
1188
1189 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001190 m_monitor_thread = Host::StartMonitoringChildProcess(
1191 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001192 if (!m_monitor_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001193 {
1194 error.SetErrorToGenericError();
1195 error.SetErrorString("Process launch failed.");
1196 return;
1197 }
1198}
1199
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001200ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001201 lldb::pid_t pid,
1202 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001203 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001204 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001205 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001206 m_pid(LLDB_INVALID_PROCESS_ID),
1207 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001208 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001209{
Daniel Malea1efb4182013-09-16 23:12:18 +00001210 sem_init(&m_operation_pending, 0, 0);
1211 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001212
Daniel Malea1efb4182013-09-16 23:12:18 +00001213 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001214
1215 StartAttachOpThread(args.get(), error);
1216 if (!error.Success())
1217 return;
1218
1219WAIT_AGAIN:
1220 // Wait for the operation thread to initialize.
1221 if (sem_wait(&args->m_semaphore))
1222 {
1223 if (errno == EINTR)
1224 goto WAIT_AGAIN;
1225 else
1226 {
1227 error.SetErrorToErrno();
1228 return;
1229 }
1230 }
1231
Greg Clayton743ecf42012-10-16 20:20:18 +00001232 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001233 if (!args->m_error.Success())
1234 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001235 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001236 error = args->m_error;
1237 return;
1238 }
1239
1240 // Finally, start monitoring the child process for change in state.
1241 m_monitor_thread = Host::StartMonitoringChildProcess(
1242 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001243 if (!m_monitor_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001244 {
1245 error.SetErrorToGenericError();
1246 error.SetErrorString("Process attach failed.");
1247 return;
1248 }
1249}
1250
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001251ProcessMonitor::~ProcessMonitor()
1252{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001253 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001254}
1255
1256//------------------------------------------------------------------------------
1257// Thread setup and tear down.
1258void
Johnny Chen25e68e32011-06-14 19:19:50 +00001259ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001260{
1261 static const char *g_thread_name = "lldb.process.linux.operation";
1262
Zachary Turneracee96a2014-09-23 18:32:09 +00001263 if (m_operation_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001264 return;
1265
Zachary Turner39de3112014-09-09 20:54:56 +00001266 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001267}
1268
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001269void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001270ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001271{
1272 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1273
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001274 if (!Launch(args)) {
1275 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001276 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001277 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001278
Stephen Wilson570243b2011-01-19 01:37:06 +00001279 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001280 return NULL;
1281}
1282
1283bool
1284ProcessMonitor::Launch(LaunchArgs *args)
1285{
Todd Fiala0bce1b62014-08-17 00:10:50 +00001286 assert (args && "null args");
1287 if (!args)
1288 return false;
1289
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001290 ProcessMonitor *monitor = args->m_monitor;
1291 ProcessLinux &process = monitor->GetProcess();
1292 const char **argv = args->m_argv;
1293 const char **envp = args->m_envp;
1294 const char *stdin_path = args->m_stdin_path;
1295 const char *stdout_path = args->m_stdout_path;
1296 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001297 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001298
1299 lldb_utility::PseudoTerminal terminal;
1300 const size_t err_len = 1024;
1301 char err_str[err_len];
1302 lldb::pid_t pid;
1303
1304 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001305 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001306
Stephen Wilson57740ec2011-01-15 00:12:41 +00001307 // Propagate the environment if one is not supplied.
1308 if (envp == NULL || envp[0] == NULL)
1309 envp = const_cast<const char **>(environ);
1310
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001311 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001312 {
1313 args->m_error.SetErrorToGenericError();
1314 args->m_error.SetErrorString("Process fork failed.");
1315 goto FINISH;
1316 }
1317
Peter Collingbourne6a520222011-06-14 03:55:58 +00001318 // Recognized child exit status codes.
1319 enum {
1320 ePtraceFailed = 1,
1321 eDupStdinFailed,
1322 eDupStdoutFailed,
1323 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001324 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001325 eExecFailed,
1326 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001327 };
1328
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001329 // Child process.
1330 if (pid == 0)
1331 {
1332 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001333 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001334 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001335
Pavel Labath493c3a12015-02-04 10:36:57 +00001336 // terminal has already dupped the tty descriptors to stdin/out/err.
1337 // This closes original fd from which they were copied (and avoids
1338 // leaking descriptors to the debugged process.
1339 terminal.CloseSlaveFileDescriptor();
1340
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001341 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001342 if (setgid(getgid()) != 0)
1343 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001344
1345 // Let us have our own process group.
1346 setpgid(0, 0);
1347
Greg Clayton710dd5a2011-01-08 20:28:42 +00001348 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001349 //
1350 // FIXME: If two or more of the paths are the same we needlessly open
1351 // the same file multiple times.
1352 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001353 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001354 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001355
1356 if (stdout_path != NULL && stdout_path[0])
1357 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001358 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001359
1360 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001361 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001362 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001363
Daniel Malea6217d2a2013-01-08 14:49:22 +00001364 // Change working directory
1365 if (working_dir != NULL && working_dir[0])
1366 if (0 != ::chdir(working_dir))
1367 exit(eChdirFailed);
1368
Todd Fiala0bce1b62014-08-17 00:10:50 +00001369 // Disable ASLR if requested.
1370 if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
1371 {
1372 const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
1373 if (old_personality == -1)
1374 {
1375 if (log)
1376 log->Printf ("ProcessMonitor::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno));
1377 }
1378 else
1379 {
1380 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
1381 if (new_personality == -1)
1382 {
1383 if (log)
1384 log->Printf ("ProcessMonitor::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno));
1385
1386 }
1387 else
1388 {
1389 if (log)
Todd Fiala850f9a22014-09-19 18:27:45 +00001390 log->Printf ("ProcessMonitor::%s disabling ASLR: SUCCESS", __FUNCTION__);
Todd Fiala0bce1b62014-08-17 00:10:50 +00001391
1392 }
1393 }
1394 }
1395
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001396 // Execute. We should never return.
1397 execve(argv[0],
1398 const_cast<char *const *>(argv),
1399 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001400 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001401 }
1402
1403 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001404 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001405 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001406 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001407
1408 raw_pid = waitpid(pid, &status, 0);
1409 wpid = static_cast <lldb::pid_t> (raw_pid);
1410 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001411 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001412 args->m_error.SetErrorToErrno();
1413 goto FINISH;
1414 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001415 else if (WIFEXITED(status))
1416 {
1417 // open, dup or execve likely failed for some reason.
1418 args->m_error.SetErrorToGenericError();
1419 switch (WEXITSTATUS(status))
1420 {
Greg Clayton542e4072012-09-07 17:49:29 +00001421 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001422 args->m_error.SetErrorString("Child ptrace failed.");
1423 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001424 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001425 args->m_error.SetErrorString("Child open stdin failed.");
1426 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001427 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001428 args->m_error.SetErrorString("Child open stdout failed.");
1429 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001430 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001431 args->m_error.SetErrorString("Child open stderr failed.");
1432 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001433 case eChdirFailed:
1434 args->m_error.SetErrorString("Child failed to set working directory.");
1435 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001436 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001437 args->m_error.SetErrorString("Child exec failed.");
1438 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001439 case eSetGidFailed:
1440 args->m_error.SetErrorString("Child setgid failed.");
1441 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001442 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001443 args->m_error.SetErrorString("Child returned unknown exit status.");
1444 break;
1445 }
1446 goto FINISH;
1447 }
1448 assert(WIFSTOPPED(status) && wpid == pid &&
1449 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001450
Matt Kopec085d6ce2013-05-31 22:00:07 +00001451 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001452 {
1453 args->m_error.SetErrorToErrno();
1454 goto FINISH;
1455 }
1456
1457 // Release the master terminal descriptor and pass it off to the
1458 // ProcessMonitor instance. Similarly stash the inferior pid.
1459 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1460 monitor->m_pid = pid;
1461
Stephen Wilson26977162011-03-23 02:14:42 +00001462 // Set the terminal fd to be in non blocking mode (it simplifies the
1463 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1464 // descriptor to read from).
1465 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1466 goto FINISH;
1467
Johnny Chen30213ff2012-01-05 19:17:38 +00001468 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001469 // FIXME: should we be letting UpdateThreadList handle this?
1470 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001471 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001472
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001473 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001474 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001475 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001476
Matt Kopecb2910442013-07-09 15:09:45 +00001477 process.AddThreadForInitialStopIfNeeded(pid);
1478
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001479 // Let our process instance know the thread has stopped.
1480 process.SendMessage(ProcessMessage::Trace(pid));
1481
1482FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001483 return args->m_error.Success();
1484}
1485
Johnny Chen25e68e32011-06-14 19:19:50 +00001486void
1487ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1488{
1489 static const char *g_thread_name = "lldb.process.linux.operation";
1490
Zachary Turneracee96a2014-09-23 18:32:09 +00001491 if (m_operation_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001492 return;
1493
Zachary Turner39de3112014-09-09 20:54:56 +00001494 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen25e68e32011-06-14 19:19:50 +00001495}
1496
Johnny Chen25e68e32011-06-14 19:19:50 +00001497void *
1498ProcessMonitor::AttachOpThread(void *arg)
1499{
1500 AttachArgs *args = static_cast<AttachArgs*>(arg);
1501
Greg Clayton743ecf42012-10-16 20:20:18 +00001502 if (!Attach(args)) {
1503 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001504 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001505 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001506
1507 ServeOperation(args);
1508 return NULL;
1509}
1510
1511bool
1512ProcessMonitor::Attach(AttachArgs *args)
1513{
1514 lldb::pid_t pid = args->m_pid;
1515
1516 ProcessMonitor *monitor = args->m_monitor;
1517 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001518 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001519 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001520
Matt Kopec085d6ce2013-05-31 22:00:07 +00001521 // Use a map to keep track of the threads which we have attached/need to attach.
1522 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001523 if (pid <= 1)
1524 {
1525 args->m_error.SetErrorToGenericError();
1526 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1527 goto FINISH;
1528 }
1529
Matt Kopec085d6ce2013-05-31 22:00:07 +00001530 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001531 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001532 for (Host::TidMap::iterator it = tids_to_attach.begin();
1533 it != tids_to_attach.end(); ++it)
1534 {
1535 if (it->second == false)
1536 {
1537 lldb::tid_t tid = it->first;
1538
1539 // Attach to the requested process.
1540 // An attach will cause the thread to stop with a SIGSTOP.
1541 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1542 {
1543 // No such thread. The thread may have exited.
1544 // More error handling may be needed.
1545 if (errno == ESRCH)
1546 {
1547 tids_to_attach.erase(it);
1548 continue;
1549 }
1550 else
1551 {
1552 args->m_error.SetErrorToErrno();
1553 goto FINISH;
1554 }
1555 }
1556
Todd Fiala9be50492014-07-01 16:30:53 +00001557 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001558 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1559 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001560 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001561 {
1562 // No such thread. The thread may have exited.
1563 // More error handling may be needed.
1564 if (errno == ESRCH)
1565 {
1566 tids_to_attach.erase(it);
1567 continue;
1568 }
1569 else
1570 {
1571 args->m_error.SetErrorToErrno();
1572 goto FINISH;
1573 }
1574 }
1575
1576 if (!SetDefaultPtraceOpts(tid))
1577 {
1578 args->m_error.SetErrorToErrno();
1579 goto FINISH;
1580 }
1581
1582 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001583 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001584
Matt Kopec085d6ce2013-05-31 22:00:07 +00001585 if (log)
1586 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1587 process.GetThreadList().AddThread(inferior);
1588 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001589 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001590 }
1591 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001592 }
1593
Matt Kopec085d6ce2013-05-31 22:00:07 +00001594 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001595 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001596 monitor->m_pid = pid;
1597 // Let our process instance know the thread has stopped.
1598 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001599 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001600 else
1601 {
1602 args->m_error.SetErrorToGenericError();
1603 args->m_error.SetErrorString("No such process.");
1604 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001605
1606 FINISH:
1607 return args->m_error.Success();
1608}
1609
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001610bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001611ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1612{
1613 long ptrace_opts = 0;
1614
1615 // Have the child raise an event on exit. This is used to keep the child in
1616 // limbo until it is destroyed.
1617 ptrace_opts |= PTRACE_O_TRACEEXIT;
1618
1619 // Have the tracer trace threads which spawn in the inferior process.
1620 // TODO: if we want to support tracing the inferiors' child, add the
1621 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1622 ptrace_opts |= PTRACE_O_TRACECLONE;
1623
1624 // Have the tracer notify us before execve returns
1625 // (needed to disable legacy SIGTRAP generation)
1626 ptrace_opts |= PTRACE_O_TRACEEXEC;
1627
1628 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1629}
1630
1631bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001632ProcessMonitor::MonitorCallback(void *callback_baton,
1633 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001634 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001635 int signal,
1636 int status)
1637{
1638 ProcessMessage message;
1639 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001640 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001641 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001642 bool stop_monitoring;
1643 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001644 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001645
Andrew Kaylor93132f52013-05-28 23:04:25 +00001646 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1647
1648 if (exited)
1649 {
1650 if (log)
1651 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1652 message = ProcessMessage::Exit(pid, status);
1653 process->SendMessage(message);
1654 return pid == process->GetID();
1655 }
1656
Daniel Maleaa35970a2012-11-23 18:09:58 +00001657 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1658 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001659 if (log)
1660 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001661 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1662 if (!monitor->Resume(pid, SIGSTOP)) {
1663 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1664 }
1665 stop_monitoring = false;
1666 } else {
1667 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1668 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001669 // stop the monitor thread if this is the main pid.
1670 if (log)
1671 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1672 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1673 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001674 // If we are going to stop monitoring, we need to notify our process object
1675 if (stop_monitoring)
1676 {
1677 message = ProcessMessage::Exit(pid, status);
1678 process->SendMessage(message);
1679 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001680 }
1681 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001682 else {
1683 switch (info.si_signo)
1684 {
1685 case SIGTRAP:
1686 message = MonitorSIGTRAP(monitor, &info, pid);
1687 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001688
Stephen Wilson84ffe702011-03-30 15:55:52 +00001689 default:
1690 message = MonitorSignal(monitor, &info, pid);
1691 break;
1692 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001693
Stephen Wilson84ffe702011-03-30 15:55:52 +00001694 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001695 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001696 }
1697
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001698 return stop_monitoring;
1699}
1700
1701ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001702ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001703 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001704{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001705 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001706
Andrew Kaylor93132f52013-05-28 23:04:25 +00001707 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1708
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001709 assert(monitor);
1710 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001711
Stephen Wilson84ffe702011-03-30 15:55:52 +00001712 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001713 {
1714 default:
1715 assert(false && "Unexpected SIGTRAP code!");
1716 break;
1717
Matt Kopeca360d7e2013-05-17 19:27:47 +00001718 // TODO: these two cases are required if we want to support tracing
1719 // of the inferiors' children
1720 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1721 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1722
Matt Kopec650648f2013-01-08 16:30:18 +00001723 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1724 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001725 if (log)
1726 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1727
Matt Kopec650648f2013-01-08 16:30:18 +00001728 unsigned long tid = 0;
1729 if (!monitor->GetEventMessage(pid, &tid))
1730 tid = -1;
1731 message = ProcessMessage::NewThread(pid, tid);
1732 break;
1733 }
1734
Matt Kopeca360d7e2013-05-17 19:27:47 +00001735 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001736 if (log)
1737 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1738
1739 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001740 break;
1741
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001742 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1743 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001744 // The inferior process or one of its threads is about to exit.
1745 // Maintain the process or thread in a state of "limbo" until we are
1746 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001747 unsigned long data = 0;
1748 if (!monitor->GetEventMessage(pid, &data))
1749 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001750 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001751 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001752 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001753 break;
1754 }
1755
1756 case 0:
1757 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001758 if (log)
1759 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001760 message = ProcessMessage::Trace(pid);
1761 break;
1762
1763 case SI_KERNEL:
1764 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001765 if (log)
1766 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001767 message = ProcessMessage::Break(pid);
1768 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001769
1770 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001771 if (log)
1772 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001773 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1774 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001775
1776 case SIGTRAP:
1777 case (SIGTRAP | 0x80):
1778 if (log)
1779 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1780 // Ignore these signals until we know more about them
1781 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001782 }
1783
1784 return message;
1785}
1786
Stephen Wilson84ffe702011-03-30 15:55:52 +00001787ProcessMessage
1788ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001789 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001790{
1791 ProcessMessage message;
1792 int signo = info->si_signo;
1793
Andrew Kaylor93132f52013-05-28 23:04:25 +00001794 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1795
Stephen Wilson84ffe702011-03-30 15:55:52 +00001796 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1797 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1798 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1799 //
1800 // IOW, user generated signals never generate what we consider to be a
1801 // "crash".
1802 //
1803 // Similarly, ACK signals generated by this monitor.
1804 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1805 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001806 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001807 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001808 __FUNCTION__,
1809 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1810 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1811 info->si_pid);
1812
Stephen Wilson84ffe702011-03-30 15:55:52 +00001813 if (info->si_pid == getpid())
1814 return ProcessMessage::SignalDelivered(pid, signo);
1815 else
1816 return ProcessMessage::Signal(pid, signo);
1817 }
1818
Andrew Kaylor93132f52013-05-28 23:04:25 +00001819 if (log)
1820 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1821
Chaoren Lin28e57422015-02-03 01:51:25 +00001822 switch (signo)
1823 {
1824 case SIGSEGV:
1825 case SIGILL:
1826 case SIGFPE:
1827 case SIGBUS:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001828 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
Chaoren Lin28e57422015-02-03 01:51:25 +00001829 const auto reason = GetCrashReason(*info);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001830 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1831 }
1832
1833 // Everything else is "normal" and does not require any special action on
1834 // our part.
1835 return ProcessMessage::Signal(pid, signo);
1836}
1837
Andrew Kaylord4d54992013-09-17 00:30:24 +00001838// On Linux, when a new thread is created, we receive to notifications,
1839// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1840// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1841// the new child thread indicating that it has is stopped because we attached.
1842// We have no guarantee of the order in which these arrive, but we need both
1843// before we are ready to proceed. We currently keep a list of threads which
1844// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1845// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1846// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1847
1848bool
1849ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1850{
1851 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1852 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001853 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001854
1855 // Wait for the thread to stop
1856 while (true)
1857 {
1858 int status = -1;
1859 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001860 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001861 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001862 if (status == -1)
1863 {
1864 // If we got interrupted by a signal (in our process, not the
1865 // inferior) try again.
1866 if (errno == EINTR)
1867 continue;
1868 else
1869 {
1870 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001871 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001872 return false; // This is bad, but there's nothing we can do.
1873 }
1874 }
1875
1876 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001877 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001878
Todd Fiala9be50492014-07-01 16:30:53 +00001879 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001880
1881 siginfo_t info;
1882 int ptrace_err;
1883 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1884 {
1885 if (log)
1886 {
1887 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1888 }
1889 return false;
1890 }
1891
1892 // If this is a thread exit, we won't get any more information.
1893 if (WIFEXITED(status))
1894 {
1895 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001896 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001897 return true;
1898 continue;
1899 }
1900
1901 assert(info.si_code == SI_USER);
1902 assert(WSTOPSIG(status) == SIGSTOP);
1903
1904 if (log)
1905 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1906 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1907 return true;
1908 }
1909 return false;
1910}
1911
Andrew Kaylor93132f52013-05-28 23:04:25 +00001912bool
1913ProcessMonitor::StopThread(lldb::tid_t tid)
1914{
1915 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1916
1917 // FIXME: Try to use tgkill or tkill
1918 int ret = tgkill(m_pid, tid, SIGSTOP);
1919 if (log)
1920 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1921
1922 // This can happen if a thread exited while we were trying to stop it. That's OK.
1923 // We'll get the signal for that later.
1924 if (ret < 0)
1925 return false;
1926
1927 // Wait for the thread to stop
1928 while (true)
1929 {
1930 int status = -1;
1931 if (log)
1932 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001933 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001934 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001935 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1936 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001937
Todd Fiala9be50492014-07-01 16:30:53 +00001938 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001939 {
1940 // If we got interrupted by a signal (in our process, not the
1941 // inferior) try again.
1942 if (errno == EINTR)
1943 continue;
1944 else
1945 return false; // This is bad, but there's nothing we can do.
1946 }
1947
1948 // If this is a thread exit, we won't get any more information.
1949 if (WIFEXITED(status))
1950 {
1951 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001952 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001953 return true;
1954 continue;
1955 }
1956
1957 siginfo_t info;
1958 int ptrace_err;
1959 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1960 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001961 // another signal causing a StopAllThreads may have been received
1962 // before wait_pid's group-stop was processed, handle it now
1963 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001964 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001965 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001966
Todd Fiala1b0539c2014-01-27 17:03:57 +00001967 if (log)
1968 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1969 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1970 if (!Resume(wait_pid, SIGSTOP)) {
1971 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1972 }
1973 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001974 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001975
1976 if (log)
1977 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001978 return false;
1979 }
1980
1981 // Handle events from other threads
1982 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001983 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
1984 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00001985
1986 ProcessMessage message;
1987 if (info.si_signo == SIGTRAP)
1988 message = MonitorSIGTRAP(this, &info, wait_pid);
1989 else
1990 message = MonitorSignal(this, &info, wait_pid);
1991
1992 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
1993
1994 // When a new thread is created, we may get a SIGSTOP for the new thread
1995 // just before we get the SIGTRAP that we use to add the thread to our
1996 // process thread list. We don't need to worry about that signal here.
1997 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
1998
1999 if (!thread)
2000 {
2001 m_process->SendMessage(message);
2002 continue;
2003 }
2004
2005 switch (message.GetKind())
2006 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00002007 case ProcessMessage::eExecMessage:
2008 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00002009 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00002010 case ProcessMessage::eInvalidMessage:
2011 break;
2012
2013 // These need special handling because we don't want to send a
2014 // resume even if we already sent a SIGSTOP to this thread. In
2015 // this case the resume will cause the thread to disappear. It is
2016 // unlikely that we'll ever get eExitMessage here, but the same
2017 // reasoning applies.
2018 case ProcessMessage::eLimboMessage:
2019 case ProcessMessage::eExitMessage:
2020 if (log)
2021 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2022 // SendMessage will set the thread state as needed.
2023 m_process->SendMessage(message);
2024 // If this is the thread we're waiting for, stop waiting. Even
2025 // though this wasn't the signal we expected, it's the last
2026 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00002027 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002028 return true;
2029 break;
2030
Matt Kopecb2910442013-07-09 15:09:45 +00002031 case ProcessMessage::eSignalMessage:
2032 if (log)
2033 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2034 if (WSTOPSIG(status) == SIGSTOP)
2035 {
2036 m_process->AddThreadForInitialStopIfNeeded(tid);
2037 thread->SetState(lldb::eStateStopped);
2038 }
2039 else
2040 {
2041 m_process->SendMessage(message);
2042 // This isn't the stop we were expecting, but the thread is
2043 // stopped. SendMessage will handle processing of this event,
2044 // but we need to resume here to get the stop we are waiting
2045 // for (otherwise the thread will stop again immediately when
2046 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002047 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00002048 Resume(wait_pid, eResumeSignalNone);
2049 }
2050 break;
2051
Andrew Kaylor93132f52013-05-28 23:04:25 +00002052 case ProcessMessage::eSignalDeliveredMessage:
2053 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00002054 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
2055 WIFSTOPPED(status) &&
2056 WSTOPSIG(status) == SIGSTOP &&
2057 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002058 {
2059 if (log)
2060 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
2061 thread->SetState(lldb::eStateStopped);
2062 return true;
2063 }
2064 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00002065 case ProcessMessage::eBreakpointMessage:
2066 case ProcessMessage::eTraceMessage:
2067 case ProcessMessage::eWatchpointMessage:
2068 case ProcessMessage::eCrashMessage:
2069 case ProcessMessage::eNewThreadMessage:
2070 if (log)
2071 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2072 // SendMessage will set the thread state as needed.
2073 m_process->SendMessage(message);
2074 // This isn't the stop we were expecting, but the thread is
2075 // stopped. SendMessage will handle processing of this event,
2076 // but we need to resume here to get the stop we are waiting
2077 // for (otherwise the thread will stop again immediately when
2078 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002079 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002080 Resume(wait_pid, eResumeSignalNone);
2081 break;
2082 }
2083 }
2084 return false;
2085}
2086
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002087void
Johnny Chen25e68e32011-06-14 19:19:50 +00002088ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002089{
Stephen Wilson570243b2011-01-19 01:37:06 +00002090 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002091
Stephen Wilson570243b2011-01-19 01:37:06 +00002092 // We are finised with the arguments and are ready to go. Sync with the
2093 // parent thread and start serving operations on the inferior.
2094 sem_post(&args->m_semaphore);
2095
Michael Sartain704bf892013-10-09 01:28:57 +00002096 for(;;)
2097 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002098 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002099 if (sem_wait(&monitor->m_operation_pending))
2100 {
2101 if (errno == EINTR)
2102 continue;
2103 assert(false && "Unexpected errno from sem_wait");
2104 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002105
Daniel Malea1efb4182013-09-16 23:12:18 +00002106 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002107
Daniel Malea1efb4182013-09-16 23:12:18 +00002108 // notify calling thread that operation is complete
2109 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002110 }
2111}
2112
2113void
2114ProcessMonitor::DoOperation(Operation *op)
2115{
Daniel Malea1efb4182013-09-16 23:12:18 +00002116 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002117
Daniel Malea1efb4182013-09-16 23:12:18 +00002118 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002119
Daniel Malea1efb4182013-09-16 23:12:18 +00002120 // notify operation thread that an operation is ready to be processed
2121 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002122
Daniel Malea1efb4182013-09-16 23:12:18 +00002123 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002124 while (sem_wait(&m_operation_done))
2125 {
2126 if (errno == EINTR)
2127 continue;
2128 assert(false && "Unexpected errno from sem_wait");
2129 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002130}
2131
2132size_t
2133ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2134 Error &error)
2135{
2136 size_t result;
2137 ReadOperation op(vm_addr, buf, size, error, result);
2138 DoOperation(&op);
2139 return result;
2140}
2141
2142size_t
2143ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2144 lldb_private::Error &error)
2145{
2146 size_t result;
2147 WriteOperation op(vm_addr, buf, size, error, result);
2148 DoOperation(&op);
2149 return result;
2150}
2151
2152bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002153ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002154 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002155{
2156 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002157 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002158 DoOperation(&op);
2159 return result;
2160}
2161
2162bool
Matt Kopec7de48462013-03-06 17:20:48 +00002163ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002164 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002165{
2166 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002167 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002168 DoOperation(&op);
2169 return result;
2170}
2171
2172bool
Matt Kopec7de48462013-03-06 17:20:48 +00002173ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002174{
2175 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002176 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002177 DoOperation(&op);
2178 return result;
2179}
2180
2181bool
Matt Kopec7de48462013-03-06 17:20:48 +00002182ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002183{
2184 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002185 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002186 DoOperation(&op);
2187 return result;
2188}
2189
2190bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002191ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2192{
2193 bool result;
2194 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2195 DoOperation(&op);
2196 return result;
2197}
2198
2199bool
Matt Kopec7de48462013-03-06 17:20:48 +00002200ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002201{
2202 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002203 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002204 DoOperation(&op);
2205 return result;
2206}
2207
2208bool
Matt Kopec7de48462013-03-06 17:20:48 +00002209ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002210{
2211 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002212 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002213 DoOperation(&op);
2214 return result;
2215}
2216
2217bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002218ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2219{
2220 bool result;
2221 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2222 DoOperation(&op);
2223 return result;
2224}
2225
2226bool
Richard Mitton0a558352013-10-17 21:14:00 +00002227ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2228{
2229 bool result;
2230 ReadThreadPointerOperation op(tid, &value, result);
2231 DoOperation(&op);
2232 return result;
2233}
2234
2235bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002236ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002237{
2238 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002239 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2240
2241 if (log)
2242 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2243 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002244 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002245 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002246 if (log)
2247 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002248 return result;
2249}
2250
2251bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002252ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002253{
2254 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002255 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002256 DoOperation(&op);
2257 return result;
2258}
2259
2260bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002261ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002262{
Ed Maste4e0999b2014-04-01 18:14:06 +00002263 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002264}
2265
2266bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002267ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002268{
2269 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002270 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002271 DoOperation(&op);
2272 return result;
2273}
2274
2275bool
2276ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2277{
2278 bool result;
2279 EventMessageOperation op(tid, message, result);
2280 DoOperation(&op);
2281 return result;
2282}
2283
Greg Clayton743ecf42012-10-16 20:20:18 +00002284lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002285ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002286{
Greg Clayton28041352011-11-29 20:50:10 +00002287 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002288 if (tid != LLDB_INVALID_THREAD_ID)
2289 {
2290 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002291 DoOperation(&op);
2292 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002293 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002294}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002295
2296bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002297ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2298{
Peter Collingbourne62343202011-06-14 03:55:54 +00002299 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002300
2301 if (target_fd == -1)
2302 return false;
2303
Pavel Labath493c3a12015-02-04 10:36:57 +00002304 if (dup2(target_fd, fd) == -1)
2305 return false;
2306
2307 return (close(target_fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002308}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002309
2310void
2311ProcessMonitor::StopMonitoringChildProcess()
2312{
Zachary Turneracee96a2014-09-23 18:32:09 +00002313 if (m_monitor_thread.IsJoinable())
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002314 {
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +00002315 ::pthread_kill(m_monitor_thread.GetNativeThread().GetSystemHandle(), SIGUSR1);
Zachary Turner39de3112014-09-09 20:54:56 +00002316 m_monitor_thread.Join(nullptr);
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002317 }
2318}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002319
2320void
2321ProcessMonitor::StopMonitor()
2322{
2323 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002324 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002325 sem_destroy(&m_operation_pending);
2326 sem_destroy(&m_operation_done);
Pavel Labath3a2da9e2015-02-06 11:32:52 +00002327 if (m_terminal_fd >= 0) {
2328 close(m_terminal_fd);
2329 m_terminal_fd = -1;
2330 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00002331}
2332
2333void
Greg Clayton743ecf42012-10-16 20:20:18 +00002334ProcessMonitor::StopOpThread()
2335{
Zachary Turneracee96a2014-09-23 18:32:09 +00002336 if (!m_operation_thread.IsJoinable())
Greg Clayton743ecf42012-10-16 20:20:18 +00002337 return;
2338
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +00002339 DoOperation(EXIT_OPERATION);
Zachary Turner39de3112014-09-09 20:54:56 +00002340 m_operation_thread.Join(nullptr);
Greg Clayton743ecf42012-10-16 20:20:18 +00002341}