blob: 4b29a09ccfcff619fc385020380bc9b58c320b3b [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
Tamas Berghammerd8584872015-02-06 10:57:40 +000044#include <sys/personality.h>
45#include <sys/ptrace.h>
46#include <sys/socket.h>
47#include <sys/syscall.h>
48#include <sys/types.h>
49#include <sys/uio.h>
50#include <sys/user.h>
51#include <sys/wait.h>
52
Todd Fialacacde7d2014-09-27 16:54:22 +000053#ifdef __ANDROID__
54#define __ptrace_request int
55#define PT_DETACH PTRACE_DETACH
56#endif
57
Greg Clayton386ff182011-11-05 01:09:16 +000058#define DEBUG_PTRACE_MAXBYTES 20
59
Matt Kopec58c0b962013-03-20 20:34:35 +000060// Support ptrace extensions even when compiled without required kernel support
61#ifndef PTRACE_GETREGSET
62 #define PTRACE_GETREGSET 0x4204
63#endif
64#ifndef PTRACE_SETREGSET
65 #define PTRACE_SETREGSET 0x4205
66#endif
Richard Mitton0a558352013-10-17 21:14:00 +000067#ifndef PTRACE_GET_THREAD_AREA
68 #define PTRACE_GET_THREAD_AREA 25
69#endif
70#ifndef PTRACE_ARCH_PRCTL
71 #define PTRACE_ARCH_PRCTL 30
72#endif
73#ifndef ARCH_GET_FS
74 #define ARCH_SET_GS 0x1001
75 #define ARCH_SET_FS 0x1002
76 #define ARCH_GET_FS 0x1003
77 #define ARCH_GET_GS 0x1004
78#endif
79
Todd Fiala0bce1b62014-08-17 00:10:50 +000080#define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff
Matt Kopec58c0b962013-03-20 20:34:35 +000081
Todd Fialadbec1ff2014-09-04 16:08:20 +000082#define LLDB_PTRACE_NT_ARM_TLS 0x401 // ARM TLS register
83
Matt Kopece9ea0da2013-05-07 19:29:28 +000084// Support hardware breakpoints in case it has not been defined
85#ifndef TRAP_HWBKPT
86 #define TRAP_HWBKPT 4
87#endif
88
Andrew Kaylor93132f52013-05-28 23:04:25 +000089// Try to define a macro to encapsulate the tgkill syscall
90// fall back on kill() if tgkill isn't available
Chaoren Linc9346592015-02-28 00:20:16 +000091#define tgkill(pid, tid, sig) \
92 syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig)
Andrew Kaylor93132f52013-05-28 23:04:25 +000093
Stephen Wilsone6f9f662010-07-24 02:19:04 +000094using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000095using namespace lldb_private::process_linux;
Stephen Wilsone6f9f662010-07-24 02:19:04 +000096
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +000097static Operation* EXIT_OPERATION = nullptr;
98
Johnny Chen0d5f2d42011-10-18 18:09:30 +000099// FIXME: this code is host-dependent with respect to types and
100// endianness and needs to be fixed. For example, lldb::addr_t is
101// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
102// 32-bit pointer arguments. This code uses casts to work around the
103// problem.
104
105// We disable the tracing of ptrace calls for integration builds to
106// avoid the additional indirection and checks.
107#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
108
Greg Clayton386ff182011-11-05 01:09:16 +0000109static void
110DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
111{
112 uint8_t *ptr = (uint8_t *)bytes;
113 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
114 for(uint32_t i=0; i<loop_count; i++)
115 {
116 s.Printf ("[%x]", *ptr);
117 ptr++;
118 }
119}
120
Matt Kopec58c0b962013-03-20 20:34:35 +0000121static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +0000122{
123 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000124 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000125 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000126
127 if (verbose_log)
128 {
129 switch(req)
130 {
131 case PTRACE_POKETEXT:
132 {
133 DisplayBytes(buf, &data, 8);
134 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
135 break;
136 }
Greg Clayton542e4072012-09-07 17:49:29 +0000137 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000138 {
139 DisplayBytes(buf, &data, 8);
140 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
141 break;
142 }
Greg Clayton542e4072012-09-07 17:49:29 +0000143 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000144 {
145 DisplayBytes(buf, &data, 8);
146 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
147 break;
148 }
Todd Fiala6ac1be42014-08-21 16:34:03 +0000149#if !defined (__arm64__) && !defined (__aarch64__)
Greg Clayton542e4072012-09-07 17:49:29 +0000150 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000151 {
Matt Kopec7de48462013-03-06 17:20:48 +0000152 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000153 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
154 break;
155 }
156 case PTRACE_SETFPREGS:
157 {
Matt Kopec7de48462013-03-06 17:20:48 +0000158 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000159 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
160 break;
161 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000162#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000163 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000164 {
165 DisplayBytes(buf, data, sizeof(siginfo_t));
166 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
167 break;
168 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000169 case PTRACE_SETREGSET:
170 {
171 // Extract iov_base from data, which is a pointer to the struct IOVEC
172 DisplayBytes(buf, *(void **)data, data_size);
173 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
174 break;
175 }
Greg Clayton386ff182011-11-05 01:09:16 +0000176 default:
177 {
178 }
179 }
180 }
181}
182
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000183// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000184// 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 +0000185extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000186PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000187 const char* reqName, const char* file, int line)
188{
Greg Clayton386ff182011-11-05 01:09:16 +0000189 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000190
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000191 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000192
Matt Kopec7de48462013-03-06 17:20:48 +0000193 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000194
195 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000196 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000197 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000198 else
Todd Fiala4507f062014-02-27 20:46:12 +0000199 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000200
Ed Mastec099c952014-02-24 14:07:45 +0000201 if (log)
202 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
203 reqName, pid, addr, data, data_size, result, file, line);
204
Matt Kopec7de48462013-03-06 17:20:48 +0000205 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000206
Matt Kopec7de48462013-03-06 17:20:48 +0000207 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000208 {
209 const char* str;
210 switch (errno)
211 {
212 case ESRCH: str = "ESRCH"; break;
213 case EINVAL: str = "EINVAL"; break;
214 case EBUSY: str = "EBUSY"; break;
215 case EPERM: str = "EPERM"; break;
216 default: str = "<unknown>";
217 }
218 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
219 }
220
221 return result;
222}
223
Matt Kopec7de48462013-03-06 17:20:48 +0000224// Wrapper for ptrace when logging is not required.
225// Sets errno to 0 prior to calling ptrace.
226extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000227PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000228{
Matt Kopec58c0b962013-03-20 20:34:35 +0000229 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000230 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000231 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
232 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
233 else
234 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000235 return result;
236}
237
238#define PTRACE(req, pid, addr, data, data_size) \
239 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000240#else
Matt Kopec7de48462013-03-06 17:20:48 +0000241 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000242#endif
243
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000244//------------------------------------------------------------------------------
245// Static implementations of ProcessMonitor::ReadMemory and
246// ProcessMonitor::WriteMemory. This enables mutual recursion between these
247// functions without needed to go thru the thread funnel.
248
249static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000250DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000251 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
252{
Greg Clayton542e4072012-09-07 17:49:29 +0000253 // ptrace word size is determined by the host, not the child
254 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000255 unsigned char *dst = static_cast<unsigned char*>(buf);
256 size_t bytes_read;
257 size_t remainder;
258 long data;
259
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000260 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000261 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000262 ProcessPOSIXLog::IncNestLevel();
263 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000264 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000265 pid, word_size, (void*)vm_addr, buf, size);
266
267 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000268 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
269 {
270 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000271 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
272 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000273 {
274 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000275 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000276 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000277 return bytes_read;
278 }
279
280 remainder = size - bytes_read;
281 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000282
283 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000284 for (unsigned i = 0; i < remainder; ++i)
285 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000286
Johnny Chen30213ff2012-01-05 19:17:38 +0000287 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
288 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
289 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
290 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000291 {
292 uintptr_t print_dst = 0;
293 // Format bytes from data by moving into print_dst for log output
294 for (unsigned i = 0; i < remainder; ++i)
295 print_dst |= (((data >> i*8) & 0xFF) << i*8);
296 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
297 (void*)vm_addr, print_dst, (unsigned long)data);
298 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000299
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000300 vm_addr += word_size;
301 dst += word_size;
302 }
303
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000304 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000305 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000306 return bytes_read;
307}
308
309static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000310DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000311 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
312{
Greg Clayton542e4072012-09-07 17:49:29 +0000313 // ptrace word size is determined by the host, not the child
314 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000315 const unsigned char *src = static_cast<const unsigned char*>(buf);
316 size_t bytes_written = 0;
317 size_t remainder;
318
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000319 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000320 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000321 ProcessPOSIXLog::IncNestLevel();
322 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000323 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000324 pid, word_size, (void*)vm_addr, buf, size);
325
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000326 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
327 {
328 remainder = size - bytes_written;
329 remainder = remainder > word_size ? word_size : remainder;
330
331 if (remainder == word_size)
332 {
333 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000334 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000335 for (unsigned i = 0; i < word_size; ++i)
336 data |= (unsigned long)src[i] << i*8;
337
Johnny Chen30213ff2012-01-05 19:17:38 +0000338 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
339 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
340 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
341 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000342 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
343 (void*)vm_addr, *(unsigned long*)src, data);
344
Matt Kopec7de48462013-03-06 17:20:48 +0000345 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000346 {
347 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000348 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000349 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000350 return bytes_written;
351 }
352 }
353 else
354 {
355 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000356 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000357 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000358 {
359 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000360 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000361 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000362 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000363
364 memcpy(buff, src, remainder);
365
Greg Clayton542e4072012-09-07 17:49:29 +0000366 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000367 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000368 {
369 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000370 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000371 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000372 }
373
Johnny Chen30213ff2012-01-05 19:17:38 +0000374 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
375 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
376 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
377 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000378 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
379 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000380 }
381
382 vm_addr += word_size;
383 src += word_size;
384 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000385 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000386 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000387 return bytes_written;
388}
389
Stephen Wilson26977162011-03-23 02:14:42 +0000390// Simple helper function to ensure flags are enabled on the given file
391// descriptor.
392static bool
393EnsureFDFlags(int fd, int flags, Error &error)
394{
395 int status;
396
397 if ((status = fcntl(fd, F_GETFL)) == -1)
398 {
399 error.SetErrorToErrno();
400 return false;
401 }
402
403 if (fcntl(fd, F_SETFL, status | flags) == -1)
404 {
405 error.SetErrorToErrno();
406 return false;
407 }
408
409 return true;
410}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000411
412//------------------------------------------------------------------------------
413/// @class Operation
414/// @brief Represents a ProcessMonitor operation.
415///
416/// Under Linux, it is not possible to ptrace() from any other thread but the
417/// one that spawned or attached to the process from the start. Therefore, when
418/// a ProcessMonitor is asked to deliver or change the state of an inferior
419/// process the operation must be "funneled" to a specific thread to perform the
420/// task. The Operation class provides an abstract base for all services the
421/// ProcessMonitor must perform via the single virtual function Execute, thus
422/// encapsulating the code that needs to run in the privileged context.
423class Operation
424{
425public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000426 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000427 virtual void Execute(ProcessMonitor *monitor) = 0;
428};
429
430//------------------------------------------------------------------------------
431/// @class ReadOperation
432/// @brief Implements ProcessMonitor::ReadMemory.
433class ReadOperation : public Operation
434{
435public:
436 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
437 Error &error, size_t &result)
438 : m_addr(addr), m_buff(buff), m_size(size),
439 m_error(error), m_result(result)
440 { }
441
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000442 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000443
444private:
445 lldb::addr_t m_addr;
446 void *m_buff;
447 size_t m_size;
448 Error &m_error;
449 size_t &m_result;
450};
451
452void
453ReadOperation::Execute(ProcessMonitor *monitor)
454{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000455 lldb::pid_t pid = monitor->GetPID();
456
Greg Clayton542e4072012-09-07 17:49:29 +0000457 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000458}
459
460//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000461/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000462/// @brief Implements ProcessMonitor::WriteMemory.
463class WriteOperation : public Operation
464{
465public:
466 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
467 Error &error, size_t &result)
468 : m_addr(addr), m_buff(buff), m_size(size),
469 m_error(error), m_result(result)
470 { }
471
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000472 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000473
474private:
475 lldb::addr_t m_addr;
476 const void *m_buff;
477 size_t m_size;
478 Error &m_error;
479 size_t &m_result;
480};
481
482void
483WriteOperation::Execute(ProcessMonitor *monitor)
484{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000485 lldb::pid_t pid = monitor->GetPID();
486
Greg Clayton542e4072012-09-07 17:49:29 +0000487 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000488}
489
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000490
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000491//------------------------------------------------------------------------------
492/// @class ReadRegOperation
493/// @brief Implements ProcessMonitor::ReadRegisterValue.
494class ReadRegOperation : public Operation
495{
496public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000497 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000498 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000499 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000500 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000501 { }
502
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000503 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000504
505private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000506 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000507 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000508 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000509 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000510 bool &m_result;
511};
512
513void
514ReadRegOperation::Execute(ProcessMonitor *monitor)
515{
Todd Fiala49131cf2014-09-12 16:57:28 +0000516#if defined (__arm64__) || defined (__aarch64__)
517 if (m_offset > sizeof(struct user_pt_regs))
518 {
519 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
520 if (offset > sizeof(struct user_fpsimd_state))
521 {
522 m_result = false;
523 }
524 else
525 {
526 elf_fpregset_t regs;
527 int regset = NT_FPREGSET;
528 struct iovec ioVec;
529
530 ioVec.iov_base = &regs;
531 ioVec.iov_len = sizeof regs;
532 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
533 m_result = false;
534 else
535 {
536 m_result = true;
537 m_value.SetBytes((void *)(((unsigned char *)(&regs)) + offset), 16, monitor->GetProcess().GetByteOrder());
538 }
539 }
540 }
541 else
542 {
543 elf_gregset_t regs;
544 int regset = NT_PRSTATUS;
545 struct iovec ioVec;
546
547 ioVec.iov_base = &regs;
548 ioVec.iov_len = sizeof regs;
549 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
550 m_result = false;
551 else
552 {
553 m_result = true;
554 m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, monitor->GetProcess().GetByteOrder());
555 }
556 }
557#else
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000558 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000559
560 // Set errno to zero so that we can detect a failed peek.
561 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000562 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
563 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000564 m_result = false;
565 else
566 {
567 m_value = data;
568 m_result = true;
569 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000570 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000571 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000572 m_reg_name, data);
Todd Fiala49131cf2014-09-12 16:57:28 +0000573#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000574}
575
576//------------------------------------------------------------------------------
577/// @class WriteRegOperation
578/// @brief Implements ProcessMonitor::WriteRegisterValue.
579class WriteRegOperation : public Operation
580{
581public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000582 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000583 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000584 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000585 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000586 { }
587
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000588 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000589
590private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000591 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000592 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000593 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000594 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000595 bool &m_result;
596};
597
598void
599WriteRegOperation::Execute(ProcessMonitor *monitor)
600{
Todd Fiala49131cf2014-09-12 16:57:28 +0000601#if defined (__arm64__) || defined (__aarch64__)
602 if (m_offset > sizeof(struct user_pt_regs))
603 {
604 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
605 if (offset > sizeof(struct user_fpsimd_state))
606 {
607 m_result = false;
608 }
609 else
610 {
611 elf_fpregset_t regs;
612 int regset = NT_FPREGSET;
613 struct iovec ioVec;
614
615 ioVec.iov_base = &regs;
616 ioVec.iov_len = sizeof regs;
617 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
618 m_result = false;
619 else
620 {
621 ::memcpy((void *)(((unsigned char *)(&regs)) + offset), m_value.GetBytes(), 16);
622 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
623 m_result = false;
624 else
625 m_result = true;
626 }
627 }
628 }
629 else
630 {
631 elf_gregset_t regs;
632 int regset = NT_PRSTATUS;
633 struct iovec ioVec;
634
635 ioVec.iov_base = &regs;
636 ioVec.iov_len = sizeof regs;
637 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
638 m_result = false;
639 else
640 {
641 ::memcpy((void *)(((unsigned char *)(&regs)) + m_offset), m_value.GetBytes(), 8);
642 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
643 m_result = false;
644 else
645 m_result = true;
646 }
647 }
648#else
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000649 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000650 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000651
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000652 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000653
654 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000655 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000656 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000657 m_result = false;
658 else
659 m_result = true;
Todd Fiala49131cf2014-09-12 16:57:28 +0000660#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000661}
662
663//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000664/// @class ReadGPROperation
665/// @brief Implements ProcessMonitor::ReadGPR.
666class ReadGPROperation : public Operation
667{
668public:
Matt Kopec7de48462013-03-06 17:20:48 +0000669 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
670 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000671 { }
672
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000673 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000674
675private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000676 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000677 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000678 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000679 bool &m_result;
680};
681
682void
683ReadGPROperation::Execute(ProcessMonitor *monitor)
684{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000685#if defined (__arm64__) || defined (__aarch64__)
686 int regset = NT_PRSTATUS;
687 struct iovec ioVec;
688
689 ioVec.iov_base = m_buf;
690 ioVec.iov_len = m_buf_size;
691 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000692 m_result = false;
693 else
694 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000695#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000696 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
697 m_result = false;
698 else
699 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000700#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000701}
702
703//------------------------------------------------------------------------------
704/// @class ReadFPROperation
705/// @brief Implements ProcessMonitor::ReadFPR.
706class ReadFPROperation : public Operation
707{
708public:
Matt Kopec7de48462013-03-06 17:20:48 +0000709 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
710 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000711 { }
712
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000713 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000714
715private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000716 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000717 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000718 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000719 bool &m_result;
720};
721
722void
723ReadFPROperation::Execute(ProcessMonitor *monitor)
724{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000725#if defined (__arm64__) || defined (__aarch64__)
726 int regset = NT_FPREGSET;
727 struct iovec ioVec;
728
729 ioVec.iov_base = m_buf;
730 ioVec.iov_len = m_buf_size;
731 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000732 m_result = false;
733 else
734 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000735#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000736 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
737 m_result = false;
738 else
739 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000740#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000741}
742
743//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000744/// @class ReadRegisterSetOperation
745/// @brief Implements ProcessMonitor::ReadRegisterSet.
746class ReadRegisterSetOperation : public Operation
747{
748public:
749 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
750 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
751 { }
752
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000753 void Execute(ProcessMonitor *monitor) override;
Matt Kopec58c0b962013-03-20 20:34:35 +0000754
755private:
756 lldb::tid_t m_tid;
757 void *m_buf;
758 size_t m_buf_size;
759 const unsigned int m_regset;
760 bool &m_result;
761};
762
763void
764ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
765{
766 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
767 m_result = false;
768 else
769 m_result = true;
770}
771
772//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000773/// @class WriteGPROperation
774/// @brief Implements ProcessMonitor::WriteGPR.
775class WriteGPROperation : public Operation
776{
777public:
Matt Kopec7de48462013-03-06 17:20:48 +0000778 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
779 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000780 { }
781
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000782 void Execute(ProcessMonitor *monitor) override;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000783
784private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000785 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000786 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000787 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000788 bool &m_result;
789};
790
791void
792WriteGPROperation::Execute(ProcessMonitor *monitor)
793{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000794#if defined (__arm64__) || defined (__aarch64__)
795 int regset = NT_PRSTATUS;
796 struct iovec ioVec;
797
798 ioVec.iov_base = m_buf;
799 ioVec.iov_len = m_buf_size;
800 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000801 m_result = false;
802 else
803 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000804#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000805 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
806 m_result = false;
807 else
808 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000809#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000810}
811
812//------------------------------------------------------------------------------
813/// @class WriteFPROperation
814/// @brief Implements ProcessMonitor::WriteFPR.
815class WriteFPROperation : public Operation
816{
817public:
Matt Kopec7de48462013-03-06 17:20:48 +0000818 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
819 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000820 { }
821
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000822 void Execute(ProcessMonitor *monitor) override;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000823
824private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000825 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000826 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000827 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000828 bool &m_result;
829};
830
831void
832WriteFPROperation::Execute(ProcessMonitor *monitor)
833{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000834#if defined (__arm64__) || defined (__aarch64__)
835 int regset = NT_FPREGSET;
836 struct iovec ioVec;
837
838 ioVec.iov_base = m_buf;
839 ioVec.iov_len = m_buf_size;
840 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000841 m_result = false;
842 else
843 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000844#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000845 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
846 m_result = false;
847 else
848 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000849#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000850}
851
852//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000853/// @class WriteRegisterSetOperation
854/// @brief Implements ProcessMonitor::WriteRegisterSet.
855class WriteRegisterSetOperation : public Operation
856{
857public:
858 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
859 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
860 { }
861
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000862 void Execute(ProcessMonitor *monitor) override;
Matt Kopec58c0b962013-03-20 20:34:35 +0000863
864private:
865 lldb::tid_t m_tid;
866 void *m_buf;
867 size_t m_buf_size;
868 const unsigned int m_regset;
869 bool &m_result;
870};
871
872void
873WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
874{
875 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
876 m_result = false;
877 else
878 m_result = true;
879}
880
881//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000882/// @class ReadThreadPointerOperation
883/// @brief Implements ProcessMonitor::ReadThreadPointer.
884class ReadThreadPointerOperation : public Operation
885{
886public:
887 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
888 : m_tid(tid), m_addr(addr), m_result(result)
889 { }
890
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000891 void Execute(ProcessMonitor *monitor) override;
Richard Mitton0a558352013-10-17 21:14:00 +0000892
893private:
894 lldb::tid_t m_tid;
895 lldb::addr_t *m_addr;
896 bool &m_result;
897};
898
899void
900ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
901{
902 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
903 if (log)
904 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
905
906 // The process for getting the thread area on Linux is
907 // somewhat... obscure. There's several different ways depending on
908 // what arch you're on, and what kernel version you have.
909
910 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
911 switch(arch.GetMachine())
912 {
Todd Fiala42079682014-08-27 16:05:26 +0000913 case llvm::Triple::aarch64:
914 {
Todd Fialadbec1ff2014-09-04 16:08:20 +0000915 int regset = LLDB_PTRACE_NT_ARM_TLS;
Todd Fiala42079682014-08-27 16:05:26 +0000916 struct iovec ioVec;
917
918 ioVec.iov_base = m_addr;
919 ioVec.iov_len = sizeof(lldb::addr_t);
920 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, ioVec.iov_len) < 0)
921 m_result = false;
922 else
923 m_result = true;
924 break;
925 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000926#if defined(__i386__) || defined(__x86_64__)
927 // Note that struct user below has a field named i387 which is x86-specific.
928 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000929 case llvm::Triple::x86:
930 {
931 // Find the GS register location for our host architecture.
932 size_t gs_user_offset = offsetof(struct user, regs);
933#ifdef __x86_64__
934 gs_user_offset += offsetof(struct user_regs_struct, gs);
935#endif
936#ifdef __i386__
937 gs_user_offset += offsetof(struct user_regs_struct, xgs);
938#endif
939
940 // Read the GS register value to get the selector.
941 errno = 0;
942 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
943 if (errno)
944 {
945 m_result = false;
946 break;
947 }
948
949 // Read the LDT base for that selector.
950 uint32_t tmp[4];
951 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
952 *m_addr = tmp[1];
953 break;
954 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000955#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000956 case llvm::Triple::x86_64:
957 // Read the FS register base.
958 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
959 break;
960 default:
961 m_result = false;
962 break;
963 }
964}
965
966//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000967/// @class ResumeOperation
968/// @brief Implements ProcessMonitor::Resume.
969class ResumeOperation : public Operation
970{
971public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000972 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
973 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000974
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000975 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000976
977private:
978 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000979 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000980 bool &m_result;
981};
982
983void
984ResumeOperation::Execute(ProcessMonitor *monitor)
985{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000986 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000987
988 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
989 data = m_signo;
990
Matt Kopec7de48462013-03-06 17:20:48 +0000991 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000992 {
993 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
994
995 if (log)
996 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000997 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000998 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000999 else
1000 m_result = true;
1001}
1002
1003//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +00001004/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001005/// @brief Implements ProcessMonitor::SingleStep.
1006class SingleStepOperation : public Operation
1007{
1008public:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001009 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
1010 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001011
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001012 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001013
1014private:
1015 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001016 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001017 bool &m_result;
1018};
1019
1020void
1021SingleStepOperation::Execute(ProcessMonitor *monitor)
1022{
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001023 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001024
1025 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
1026 data = m_signo;
1027
Matt Kopec7de48462013-03-06 17:20:48 +00001028 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001029 m_result = false;
1030 else
1031 m_result = true;
1032}
1033
1034//------------------------------------------------------------------------------
1035/// @class SiginfoOperation
1036/// @brief Implements ProcessMonitor::GetSignalInfo.
1037class SiginfoOperation : public Operation
1038{
1039public:
Daniel Maleaa35970a2012-11-23 18:09:58 +00001040 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
1041 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001042
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001043 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001044
1045private:
1046 lldb::tid_t m_tid;
1047 void *m_info;
1048 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001049 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001050};
1051
1052void
1053SiginfoOperation::Execute(ProcessMonitor *monitor)
1054{
Matt Kopec7de48462013-03-06 17:20:48 +00001055 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001056 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001057 m_err = errno;
1058 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001059 else
1060 m_result = true;
1061}
1062
1063//------------------------------------------------------------------------------
1064/// @class EventMessageOperation
1065/// @brief Implements ProcessMonitor::GetEventMessage.
1066class EventMessageOperation : public Operation
1067{
1068public:
1069 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
1070 : m_tid(tid), m_message(message), m_result(result) { }
1071
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001072 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001073
1074private:
1075 lldb::tid_t m_tid;
1076 unsigned long *m_message;
1077 bool &m_result;
1078};
1079
1080void
1081EventMessageOperation::Execute(ProcessMonitor *monitor)
1082{
Matt Kopec7de48462013-03-06 17:20:48 +00001083 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001084 m_result = false;
1085 else
1086 m_result = true;
1087}
1088
1089//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +00001090/// @class DetachOperation
1091/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +00001092class DetachOperation : public Operation
1093{
1094public:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001095 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +00001096
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001097 void Execute(ProcessMonitor *monitor) override;
Greg Clayton28041352011-11-29 20:50:10 +00001098
1099private:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001100 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +00001101 Error &m_error;
1102};
1103
1104void
1105DetachOperation::Execute(ProcessMonitor *monitor)
1106{
Matt Kopec085d6ce2013-05-31 22:00:07 +00001107 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +00001108 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +00001109}
1110
Johnny Chen25e68e32011-06-14 19:19:50 +00001111ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
1112 : m_monitor(monitor)
1113{
1114 sem_init(&m_semaphore, 0, 0);
1115}
1116
1117ProcessMonitor::OperationArgs::~OperationArgs()
1118{
1119 sem_destroy(&m_semaphore);
1120}
1121
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001122ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
1123 lldb_private::Module *module,
1124 char const **argv,
1125 char const **envp,
1126 const char *stdin_path,
1127 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001128 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001129 const char *working_dir,
1130 const lldb_private::ProcessLaunchInfo &launch_info)
Johnny Chen25e68e32011-06-14 19:19:50 +00001131 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001132 m_module(module),
1133 m_argv(argv),
1134 m_envp(envp),
1135 m_stdin_path(stdin_path),
1136 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +00001137 m_stderr_path(stderr_path),
Todd Fiala0bce1b62014-08-17 00:10:50 +00001138 m_working_dir(working_dir),
1139 m_launch_info(launch_info)
1140{
1141}
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001142
1143ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +00001144{ }
1145
1146ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
1147 lldb::pid_t pid)
1148 : OperationArgs(monitor), m_pid(pid) { }
1149
1150ProcessMonitor::AttachArgs::~AttachArgs()
1151{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001152
1153//------------------------------------------------------------------------------
1154/// The basic design of the ProcessMonitor is built around two threads.
1155///
1156/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
1157/// for changes in the debugee state. When a change is detected a
1158/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1159/// "drives" state changes in the debugger.
1160///
1161/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001162/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001163/// operations such as register reads/writes, stepping, etc. See the comments
1164/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001165ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001166 Module *module,
1167 const char *argv[],
1168 const char *envp[],
1169 const char *stdin_path,
1170 const char *stdout_path,
1171 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001172 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001173 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001174 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001175 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001176 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001177 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001178 m_pid(LLDB_INVALID_PROCESS_ID),
1179 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001180 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001181{
Daniel Malea1efb4182013-09-16 23:12:18 +00001182 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1183 stdin_path, stdout_path, stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001184 working_dir, launch_info));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001185
Daniel Malea1efb4182013-09-16 23:12:18 +00001186 sem_init(&m_operation_pending, 0, 0);
1187 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001188
Johnny Chen25e68e32011-06-14 19:19:50 +00001189 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001190 if (!error.Success())
1191 return;
1192
1193WAIT_AGAIN:
1194 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001195 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001196 {
1197 if (errno == EINTR)
1198 goto WAIT_AGAIN;
1199 else
1200 {
1201 error.SetErrorToErrno();
1202 return;
1203 }
1204 }
1205
1206 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001207 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001208 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001209 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001210 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001211 return;
1212 }
1213
1214 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001215 m_monitor_thread = Host::StartMonitoringChildProcess(
1216 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001217 if (!m_monitor_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001218 {
1219 error.SetErrorToGenericError();
1220 error.SetErrorString("Process launch failed.");
1221 return;
1222 }
1223}
1224
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001225ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001226 lldb::pid_t pid,
1227 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001228 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001229 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001230 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001231 m_pid(LLDB_INVALID_PROCESS_ID),
1232 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001233 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001234{
Daniel Malea1efb4182013-09-16 23:12:18 +00001235 sem_init(&m_operation_pending, 0, 0);
1236 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001237
Daniel Malea1efb4182013-09-16 23:12:18 +00001238 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001239
1240 StartAttachOpThread(args.get(), error);
1241 if (!error.Success())
1242 return;
1243
1244WAIT_AGAIN:
1245 // Wait for the operation thread to initialize.
1246 if (sem_wait(&args->m_semaphore))
1247 {
1248 if (errno == EINTR)
1249 goto WAIT_AGAIN;
1250 else
1251 {
1252 error.SetErrorToErrno();
1253 return;
1254 }
1255 }
1256
Greg Clayton743ecf42012-10-16 20:20:18 +00001257 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001258 if (!args->m_error.Success())
1259 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001260 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001261 error = args->m_error;
1262 return;
1263 }
1264
1265 // Finally, start monitoring the child process for change in state.
1266 m_monitor_thread = Host::StartMonitoringChildProcess(
1267 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001268 if (!m_monitor_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001269 {
1270 error.SetErrorToGenericError();
1271 error.SetErrorString("Process attach failed.");
1272 return;
1273 }
1274}
1275
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001276ProcessMonitor::~ProcessMonitor()
1277{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001278 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001279}
1280
1281//------------------------------------------------------------------------------
1282// Thread setup and tear down.
1283void
Johnny Chen25e68e32011-06-14 19:19:50 +00001284ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001285{
1286 static const char *g_thread_name = "lldb.process.linux.operation";
1287
Zachary Turneracee96a2014-09-23 18:32:09 +00001288 if (m_operation_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001289 return;
1290
Zachary Turner39de3112014-09-09 20:54:56 +00001291 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001292}
1293
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001294void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001295ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001296{
1297 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1298
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001299 if (!Launch(args)) {
1300 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001301 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001302 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001303
Stephen Wilson570243b2011-01-19 01:37:06 +00001304 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001305 return NULL;
1306}
1307
1308bool
1309ProcessMonitor::Launch(LaunchArgs *args)
1310{
Todd Fiala0bce1b62014-08-17 00:10:50 +00001311 assert (args && "null args");
1312 if (!args)
1313 return false;
1314
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001315 ProcessMonitor *monitor = args->m_monitor;
1316 ProcessLinux &process = monitor->GetProcess();
1317 const char **argv = args->m_argv;
1318 const char **envp = args->m_envp;
1319 const char *stdin_path = args->m_stdin_path;
1320 const char *stdout_path = args->m_stdout_path;
1321 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001322 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001323
1324 lldb_utility::PseudoTerminal terminal;
1325 const size_t err_len = 1024;
1326 char err_str[err_len];
1327 lldb::pid_t pid;
1328
1329 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001330 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001331
Stephen Wilson57740ec2011-01-15 00:12:41 +00001332 // Propagate the environment if one is not supplied.
1333 if (envp == NULL || envp[0] == NULL)
1334 envp = const_cast<const char **>(environ);
1335
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001336 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001337 {
1338 args->m_error.SetErrorToGenericError();
1339 args->m_error.SetErrorString("Process fork failed.");
1340 goto FINISH;
1341 }
1342
Peter Collingbourne6a520222011-06-14 03:55:58 +00001343 // Recognized child exit status codes.
1344 enum {
1345 ePtraceFailed = 1,
1346 eDupStdinFailed,
1347 eDupStdoutFailed,
1348 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001349 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001350 eExecFailed,
1351 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001352 };
1353
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001354 // Child process.
1355 if (pid == 0)
1356 {
1357 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001358 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001359 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001360
Pavel Labath493c3a12015-02-04 10:36:57 +00001361 // terminal has already dupped the tty descriptors to stdin/out/err.
1362 // This closes original fd from which they were copied (and avoids
1363 // leaking descriptors to the debugged process.
1364 terminal.CloseSlaveFileDescriptor();
1365
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001366 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001367 if (setgid(getgid()) != 0)
1368 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001369
1370 // Let us have our own process group.
1371 setpgid(0, 0);
1372
Greg Clayton710dd5a2011-01-08 20:28:42 +00001373 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001374 //
1375 // FIXME: If two or more of the paths are the same we needlessly open
1376 // the same file multiple times.
1377 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001378 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001379 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001380
1381 if (stdout_path != NULL && stdout_path[0])
1382 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001383 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001384
1385 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001386 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001387 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001388
Daniel Malea6217d2a2013-01-08 14:49:22 +00001389 // Change working directory
1390 if (working_dir != NULL && working_dir[0])
1391 if (0 != ::chdir(working_dir))
1392 exit(eChdirFailed);
1393
Todd Fiala0bce1b62014-08-17 00:10:50 +00001394 // Disable ASLR if requested.
1395 if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
1396 {
1397 const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
1398 if (old_personality == -1)
1399 {
1400 if (log)
1401 log->Printf ("ProcessMonitor::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno));
1402 }
1403 else
1404 {
1405 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
1406 if (new_personality == -1)
1407 {
1408 if (log)
1409 log->Printf ("ProcessMonitor::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno));
1410
1411 }
1412 else
1413 {
1414 if (log)
Todd Fiala850f9a22014-09-19 18:27:45 +00001415 log->Printf ("ProcessMonitor::%s disabling ASLR: SUCCESS", __FUNCTION__);
Todd Fiala0bce1b62014-08-17 00:10:50 +00001416
1417 }
1418 }
1419 }
1420
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001421 // Execute. We should never return.
1422 execve(argv[0],
1423 const_cast<char *const *>(argv),
1424 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001425 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001426 }
1427
1428 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001429 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001430 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001431 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001432
1433 raw_pid = waitpid(pid, &status, 0);
1434 wpid = static_cast <lldb::pid_t> (raw_pid);
1435 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001436 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001437 args->m_error.SetErrorToErrno();
1438 goto FINISH;
1439 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001440 else if (WIFEXITED(status))
1441 {
1442 // open, dup or execve likely failed for some reason.
1443 args->m_error.SetErrorToGenericError();
1444 switch (WEXITSTATUS(status))
1445 {
Greg Clayton542e4072012-09-07 17:49:29 +00001446 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001447 args->m_error.SetErrorString("Child ptrace failed.");
1448 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001449 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001450 args->m_error.SetErrorString("Child open stdin failed.");
1451 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001452 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001453 args->m_error.SetErrorString("Child open stdout failed.");
1454 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001455 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001456 args->m_error.SetErrorString("Child open stderr failed.");
1457 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001458 case eChdirFailed:
1459 args->m_error.SetErrorString("Child failed to set working directory.");
1460 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001461 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001462 args->m_error.SetErrorString("Child exec failed.");
1463 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001464 case eSetGidFailed:
1465 args->m_error.SetErrorString("Child setgid failed.");
1466 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001467 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001468 args->m_error.SetErrorString("Child returned unknown exit status.");
1469 break;
1470 }
1471 goto FINISH;
1472 }
1473 assert(WIFSTOPPED(status) && wpid == pid &&
1474 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001475
Matt Kopec085d6ce2013-05-31 22:00:07 +00001476 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001477 {
1478 args->m_error.SetErrorToErrno();
1479 goto FINISH;
1480 }
1481
1482 // Release the master terminal descriptor and pass it off to the
1483 // ProcessMonitor instance. Similarly stash the inferior pid.
1484 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1485 monitor->m_pid = pid;
1486
Stephen Wilson26977162011-03-23 02:14:42 +00001487 // Set the terminal fd to be in non blocking mode (it simplifies the
1488 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1489 // descriptor to read from).
1490 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1491 goto FINISH;
1492
Johnny Chen30213ff2012-01-05 19:17:38 +00001493 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001494 // FIXME: should we be letting UpdateThreadList handle this?
1495 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001496 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001497
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001498 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001499 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001500 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001501
Matt Kopecb2910442013-07-09 15:09:45 +00001502 process.AddThreadForInitialStopIfNeeded(pid);
1503
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001504 // Let our process instance know the thread has stopped.
1505 process.SendMessage(ProcessMessage::Trace(pid));
1506
1507FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001508 return args->m_error.Success();
1509}
1510
Johnny Chen25e68e32011-06-14 19:19:50 +00001511void
1512ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1513{
1514 static const char *g_thread_name = "lldb.process.linux.operation";
1515
Zachary Turneracee96a2014-09-23 18:32:09 +00001516 if (m_operation_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001517 return;
1518
Zachary Turner39de3112014-09-09 20:54:56 +00001519 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen25e68e32011-06-14 19:19:50 +00001520}
1521
Johnny Chen25e68e32011-06-14 19:19:50 +00001522void *
1523ProcessMonitor::AttachOpThread(void *arg)
1524{
1525 AttachArgs *args = static_cast<AttachArgs*>(arg);
1526
Greg Clayton743ecf42012-10-16 20:20:18 +00001527 if (!Attach(args)) {
1528 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001529 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001530 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001531
1532 ServeOperation(args);
1533 return NULL;
1534}
1535
1536bool
1537ProcessMonitor::Attach(AttachArgs *args)
1538{
1539 lldb::pid_t pid = args->m_pid;
1540
1541 ProcessMonitor *monitor = args->m_monitor;
1542 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001543 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001544 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001545
Matt Kopec085d6ce2013-05-31 22:00:07 +00001546 // Use a map to keep track of the threads which we have attached/need to attach.
1547 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001548 if (pid <= 1)
1549 {
1550 args->m_error.SetErrorToGenericError();
1551 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1552 goto FINISH;
1553 }
1554
Matt Kopec085d6ce2013-05-31 22:00:07 +00001555 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001556 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001557 for (Host::TidMap::iterator it = tids_to_attach.begin();
1558 it != tids_to_attach.end(); ++it)
1559 {
1560 if (it->second == false)
1561 {
1562 lldb::tid_t tid = it->first;
1563
1564 // Attach to the requested process.
1565 // An attach will cause the thread to stop with a SIGSTOP.
1566 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1567 {
1568 // No such thread. The thread may have exited.
1569 // More error handling may be needed.
1570 if (errno == ESRCH)
1571 {
1572 tids_to_attach.erase(it);
1573 continue;
1574 }
1575 else
1576 {
1577 args->m_error.SetErrorToErrno();
1578 goto FINISH;
1579 }
1580 }
1581
Todd Fiala9be50492014-07-01 16:30:53 +00001582 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001583 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1584 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001585 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001586 {
1587 // No such thread. The thread may have exited.
1588 // More error handling may be needed.
1589 if (errno == ESRCH)
1590 {
1591 tids_to_attach.erase(it);
1592 continue;
1593 }
1594 else
1595 {
1596 args->m_error.SetErrorToErrno();
1597 goto FINISH;
1598 }
1599 }
1600
1601 if (!SetDefaultPtraceOpts(tid))
1602 {
1603 args->m_error.SetErrorToErrno();
1604 goto FINISH;
1605 }
1606
1607 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001608 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001609
Matt Kopec085d6ce2013-05-31 22:00:07 +00001610 if (log)
1611 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1612 process.GetThreadList().AddThread(inferior);
1613 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001614 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001615 }
1616 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001617 }
1618
Matt Kopec085d6ce2013-05-31 22:00:07 +00001619 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001620 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001621 monitor->m_pid = pid;
1622 // Let our process instance know the thread has stopped.
1623 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001624 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001625 else
1626 {
1627 args->m_error.SetErrorToGenericError();
1628 args->m_error.SetErrorString("No such process.");
1629 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001630
1631 FINISH:
1632 return args->m_error.Success();
1633}
1634
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001635bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001636ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1637{
1638 long ptrace_opts = 0;
1639
1640 // Have the child raise an event on exit. This is used to keep the child in
1641 // limbo until it is destroyed.
1642 ptrace_opts |= PTRACE_O_TRACEEXIT;
1643
1644 // Have the tracer trace threads which spawn in the inferior process.
1645 // TODO: if we want to support tracing the inferiors' child, add the
1646 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1647 ptrace_opts |= PTRACE_O_TRACECLONE;
1648
1649 // Have the tracer notify us before execve returns
1650 // (needed to disable legacy SIGTRAP generation)
1651 ptrace_opts |= PTRACE_O_TRACEEXEC;
1652
1653 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1654}
1655
1656bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001657ProcessMonitor::MonitorCallback(void *callback_baton,
1658 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001659 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001660 int signal,
1661 int status)
1662{
1663 ProcessMessage message;
1664 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001665 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001666 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001667 bool stop_monitoring;
1668 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001669 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001670
Andrew Kaylor93132f52013-05-28 23:04:25 +00001671 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1672
1673 if (exited)
1674 {
1675 if (log)
1676 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1677 message = ProcessMessage::Exit(pid, status);
1678 process->SendMessage(message);
1679 return pid == process->GetID();
1680 }
1681
Daniel Maleaa35970a2012-11-23 18:09:58 +00001682 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1683 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001684 if (log)
1685 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001686 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1687 if (!monitor->Resume(pid, SIGSTOP)) {
1688 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1689 }
1690 stop_monitoring = false;
1691 } else {
1692 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1693 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001694 // stop the monitor thread if this is the main pid.
1695 if (log)
1696 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1697 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1698 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001699 // If we are going to stop monitoring, we need to notify our process object
1700 if (stop_monitoring)
1701 {
1702 message = ProcessMessage::Exit(pid, status);
1703 process->SendMessage(message);
1704 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001705 }
1706 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001707 else {
1708 switch (info.si_signo)
1709 {
1710 case SIGTRAP:
1711 message = MonitorSIGTRAP(monitor, &info, pid);
1712 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001713
Stephen Wilson84ffe702011-03-30 15:55:52 +00001714 default:
1715 message = MonitorSignal(monitor, &info, pid);
1716 break;
1717 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001718
Stephen Wilson84ffe702011-03-30 15:55:52 +00001719 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001720 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001721 }
1722
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001723 return stop_monitoring;
1724}
1725
1726ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001727ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001728 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001729{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001730 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001731
Andrew Kaylor93132f52013-05-28 23:04:25 +00001732 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1733
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001734 assert(monitor);
1735 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001736
Stephen Wilson84ffe702011-03-30 15:55:52 +00001737 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001738 {
1739 default:
1740 assert(false && "Unexpected SIGTRAP code!");
1741 break;
1742
Matt Kopeca360d7e2013-05-17 19:27:47 +00001743 // TODO: these two cases are required if we want to support tracing
1744 // of the inferiors' children
1745 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1746 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1747
Matt Kopec650648f2013-01-08 16:30:18 +00001748 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1749 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001750 if (log)
1751 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1752
Matt Kopec650648f2013-01-08 16:30:18 +00001753 unsigned long tid = 0;
1754 if (!monitor->GetEventMessage(pid, &tid))
1755 tid = -1;
1756 message = ProcessMessage::NewThread(pid, tid);
1757 break;
1758 }
1759
Matt Kopeca360d7e2013-05-17 19:27:47 +00001760 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001761 if (log)
1762 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1763
1764 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001765 break;
1766
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001767 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1768 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001769 // The inferior process or one of its threads is about to exit.
1770 // Maintain the process or thread in a state of "limbo" until we are
1771 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001772 unsigned long data = 0;
1773 if (!monitor->GetEventMessage(pid, &data))
1774 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001775 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001776 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001777 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001778 break;
1779 }
1780
1781 case 0:
1782 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001783 if (log)
1784 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001785 message = ProcessMessage::Trace(pid);
1786 break;
1787
1788 case SI_KERNEL:
1789 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001790 if (log)
1791 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001792 message = ProcessMessage::Break(pid);
1793 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001794
1795 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001796 if (log)
1797 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001798 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1799 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001800
1801 case SIGTRAP:
1802 case (SIGTRAP | 0x80):
1803 if (log)
1804 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1805 // Ignore these signals until we know more about them
1806 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001807 }
1808
1809 return message;
1810}
1811
Stephen Wilson84ffe702011-03-30 15:55:52 +00001812ProcessMessage
1813ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001814 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001815{
1816 ProcessMessage message;
1817 int signo = info->si_signo;
1818
Andrew Kaylor93132f52013-05-28 23:04:25 +00001819 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1820
Stephen Wilson84ffe702011-03-30 15:55:52 +00001821 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1822 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1823 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1824 //
1825 // IOW, user generated signals never generate what we consider to be a
1826 // "crash".
1827 //
1828 // Similarly, ACK signals generated by this monitor.
1829 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1830 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001831 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001832 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001833 __FUNCTION__,
1834 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1835 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1836 info->si_pid);
1837
Stephen Wilson84ffe702011-03-30 15:55:52 +00001838 if (info->si_pid == getpid())
1839 return ProcessMessage::SignalDelivered(pid, signo);
1840 else
1841 return ProcessMessage::Signal(pid, signo);
1842 }
1843
Andrew Kaylor93132f52013-05-28 23:04:25 +00001844 if (log)
1845 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1846
Chaoren Lin28e57422015-02-03 01:51:25 +00001847 switch (signo)
1848 {
1849 case SIGSEGV:
1850 case SIGILL:
1851 case SIGFPE:
1852 case SIGBUS:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001853 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
Chaoren Lin28e57422015-02-03 01:51:25 +00001854 const auto reason = GetCrashReason(*info);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001855 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1856 }
1857
1858 // Everything else is "normal" and does not require any special action on
1859 // our part.
1860 return ProcessMessage::Signal(pid, signo);
1861}
1862
Andrew Kaylord4d54992013-09-17 00:30:24 +00001863// On Linux, when a new thread is created, we receive to notifications,
1864// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1865// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1866// the new child thread indicating that it has is stopped because we attached.
1867// We have no guarantee of the order in which these arrive, but we need both
1868// before we are ready to proceed. We currently keep a list of threads which
1869// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1870// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1871// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1872
1873bool
1874ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1875{
1876 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1877 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001878 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001879
1880 // Wait for the thread to stop
1881 while (true)
1882 {
1883 int status = -1;
1884 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001885 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001886 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001887 if (status == -1)
1888 {
1889 // If we got interrupted by a signal (in our process, not the
1890 // inferior) try again.
1891 if (errno == EINTR)
1892 continue;
1893 else
1894 {
1895 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001896 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001897 return false; // This is bad, but there's nothing we can do.
1898 }
1899 }
1900
1901 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001902 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001903
Todd Fiala9be50492014-07-01 16:30:53 +00001904 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001905
1906 siginfo_t info;
1907 int ptrace_err;
1908 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1909 {
1910 if (log)
1911 {
1912 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1913 }
1914 return false;
1915 }
1916
1917 // If this is a thread exit, we won't get any more information.
1918 if (WIFEXITED(status))
1919 {
1920 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001921 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001922 return true;
1923 continue;
1924 }
1925
1926 assert(info.si_code == SI_USER);
1927 assert(WSTOPSIG(status) == SIGSTOP);
1928
1929 if (log)
1930 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1931 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1932 return true;
1933 }
1934 return false;
1935}
1936
Andrew Kaylor93132f52013-05-28 23:04:25 +00001937bool
1938ProcessMonitor::StopThread(lldb::tid_t tid)
1939{
1940 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1941
1942 // FIXME: Try to use tgkill or tkill
1943 int ret = tgkill(m_pid, tid, SIGSTOP);
1944 if (log)
1945 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1946
1947 // This can happen if a thread exited while we were trying to stop it. That's OK.
1948 // We'll get the signal for that later.
1949 if (ret < 0)
1950 return false;
1951
1952 // Wait for the thread to stop
1953 while (true)
1954 {
1955 int status = -1;
1956 if (log)
1957 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001958 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001959 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001960 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1961 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001962
Todd Fiala9be50492014-07-01 16:30:53 +00001963 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001964 {
1965 // If we got interrupted by a signal (in our process, not the
1966 // inferior) try again.
1967 if (errno == EINTR)
1968 continue;
1969 else
1970 return false; // This is bad, but there's nothing we can do.
1971 }
1972
1973 // If this is a thread exit, we won't get any more information.
1974 if (WIFEXITED(status))
1975 {
1976 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001977 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001978 return true;
1979 continue;
1980 }
1981
1982 siginfo_t info;
1983 int ptrace_err;
1984 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1985 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001986 // another signal causing a StopAllThreads may have been received
1987 // before wait_pid's group-stop was processed, handle it now
1988 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001989 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001990 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001991
Todd Fiala1b0539c2014-01-27 17:03:57 +00001992 if (log)
1993 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1994 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1995 if (!Resume(wait_pid, SIGSTOP)) {
1996 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1997 }
1998 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001999 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00002000
2001 if (log)
2002 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002003 return false;
2004 }
2005
2006 // Handle events from other threads
2007 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00002008 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
2009 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00002010
2011 ProcessMessage message;
2012 if (info.si_signo == SIGTRAP)
2013 message = MonitorSIGTRAP(this, &info, wait_pid);
2014 else
2015 message = MonitorSignal(this, &info, wait_pid);
2016
2017 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
2018
2019 // When a new thread is created, we may get a SIGSTOP for the new thread
2020 // just before we get the SIGTRAP that we use to add the thread to our
2021 // process thread list. We don't need to worry about that signal here.
2022 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
2023
2024 if (!thread)
2025 {
2026 m_process->SendMessage(message);
2027 continue;
2028 }
2029
2030 switch (message.GetKind())
2031 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00002032 case ProcessMessage::eExecMessage:
2033 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00002034 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00002035 case ProcessMessage::eInvalidMessage:
2036 break;
2037
2038 // These need special handling because we don't want to send a
2039 // resume even if we already sent a SIGSTOP to this thread. In
2040 // this case the resume will cause the thread to disappear. It is
2041 // unlikely that we'll ever get eExitMessage here, but the same
2042 // reasoning applies.
2043 case ProcessMessage::eLimboMessage:
2044 case ProcessMessage::eExitMessage:
2045 if (log)
2046 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2047 // SendMessage will set the thread state as needed.
2048 m_process->SendMessage(message);
2049 // If this is the thread we're waiting for, stop waiting. Even
2050 // though this wasn't the signal we expected, it's the last
2051 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00002052 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002053 return true;
2054 break;
2055
Matt Kopecb2910442013-07-09 15:09:45 +00002056 case ProcessMessage::eSignalMessage:
2057 if (log)
2058 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2059 if (WSTOPSIG(status) == SIGSTOP)
2060 {
2061 m_process->AddThreadForInitialStopIfNeeded(tid);
2062 thread->SetState(lldb::eStateStopped);
2063 }
2064 else
2065 {
2066 m_process->SendMessage(message);
2067 // This isn't the stop we were expecting, but the thread is
2068 // stopped. SendMessage will handle processing of this event,
2069 // but we need to resume here to get the stop we are waiting
2070 // for (otherwise the thread will stop again immediately when
2071 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002072 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00002073 Resume(wait_pid, eResumeSignalNone);
2074 }
2075 break;
2076
Andrew Kaylor93132f52013-05-28 23:04:25 +00002077 case ProcessMessage::eSignalDeliveredMessage:
2078 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00002079 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
2080 WIFSTOPPED(status) &&
2081 WSTOPSIG(status) == SIGSTOP &&
2082 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002083 {
2084 if (log)
2085 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
2086 thread->SetState(lldb::eStateStopped);
2087 return true;
2088 }
2089 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00002090 case ProcessMessage::eBreakpointMessage:
2091 case ProcessMessage::eTraceMessage:
2092 case ProcessMessage::eWatchpointMessage:
2093 case ProcessMessage::eCrashMessage:
2094 case ProcessMessage::eNewThreadMessage:
2095 if (log)
2096 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2097 // SendMessage will set the thread state as needed.
2098 m_process->SendMessage(message);
2099 // This isn't the stop we were expecting, but the thread is
2100 // stopped. SendMessage will handle processing of this event,
2101 // but we need to resume here to get the stop we are waiting
2102 // for (otherwise the thread will stop again immediately when
2103 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002104 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002105 Resume(wait_pid, eResumeSignalNone);
2106 break;
2107 }
2108 }
2109 return false;
2110}
2111
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002112void
Johnny Chen25e68e32011-06-14 19:19:50 +00002113ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002114{
Stephen Wilson570243b2011-01-19 01:37:06 +00002115 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002116
Stephen Wilson570243b2011-01-19 01:37:06 +00002117 // We are finised with the arguments and are ready to go. Sync with the
2118 // parent thread and start serving operations on the inferior.
2119 sem_post(&args->m_semaphore);
2120
Michael Sartain704bf892013-10-09 01:28:57 +00002121 for(;;)
2122 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002123 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002124 if (sem_wait(&monitor->m_operation_pending))
2125 {
2126 if (errno == EINTR)
2127 continue;
2128 assert(false && "Unexpected errno from sem_wait");
2129 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002130
Daniel Malea1efb4182013-09-16 23:12:18 +00002131 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002132
Daniel Malea1efb4182013-09-16 23:12:18 +00002133 // notify calling thread that operation is complete
2134 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002135 }
2136}
2137
2138void
2139ProcessMonitor::DoOperation(Operation *op)
2140{
Daniel Malea1efb4182013-09-16 23:12:18 +00002141 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002142
Daniel Malea1efb4182013-09-16 23:12:18 +00002143 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002144
Daniel Malea1efb4182013-09-16 23:12:18 +00002145 // notify operation thread that an operation is ready to be processed
2146 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002147
Daniel Malea1efb4182013-09-16 23:12:18 +00002148 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002149 while (sem_wait(&m_operation_done))
2150 {
2151 if (errno == EINTR)
2152 continue;
2153 assert(false && "Unexpected errno from sem_wait");
2154 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002155}
2156
2157size_t
2158ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2159 Error &error)
2160{
2161 size_t result;
2162 ReadOperation op(vm_addr, buf, size, error, result);
2163 DoOperation(&op);
2164 return result;
2165}
2166
2167size_t
2168ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2169 lldb_private::Error &error)
2170{
2171 size_t result;
2172 WriteOperation op(vm_addr, buf, size, error, result);
2173 DoOperation(&op);
2174 return result;
2175}
2176
2177bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002178ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002179 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002180{
2181 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002182 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002183 DoOperation(&op);
2184 return result;
2185}
2186
2187bool
Matt Kopec7de48462013-03-06 17:20:48 +00002188ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002189 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002190{
2191 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002192 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002193 DoOperation(&op);
2194 return result;
2195}
2196
2197bool
Matt Kopec7de48462013-03-06 17:20:48 +00002198ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002199{
2200 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002201 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002202 DoOperation(&op);
2203 return result;
2204}
2205
2206bool
Matt Kopec7de48462013-03-06 17:20:48 +00002207ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002208{
2209 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002210 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002211 DoOperation(&op);
2212 return result;
2213}
2214
2215bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002216ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2217{
2218 bool result;
2219 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2220 DoOperation(&op);
2221 return result;
2222}
2223
2224bool
Matt Kopec7de48462013-03-06 17:20:48 +00002225ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002226{
2227 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002228 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002229 DoOperation(&op);
2230 return result;
2231}
2232
2233bool
Matt Kopec7de48462013-03-06 17:20:48 +00002234ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002235{
2236 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002237 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002238 DoOperation(&op);
2239 return result;
2240}
2241
2242bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002243ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2244{
2245 bool result;
2246 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2247 DoOperation(&op);
2248 return result;
2249}
2250
2251bool
Richard Mitton0a558352013-10-17 21:14:00 +00002252ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2253{
2254 bool result;
2255 ReadThreadPointerOperation op(tid, &value, result);
2256 DoOperation(&op);
2257 return result;
2258}
2259
2260bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002261ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002262{
2263 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002264 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2265
2266 if (log)
2267 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2268 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002269 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002270 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002271 if (log)
2272 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002273 return result;
2274}
2275
2276bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002277ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002278{
2279 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002280 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002281 DoOperation(&op);
2282 return result;
2283}
2284
2285bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002286ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002287{
Ed Maste4e0999b2014-04-01 18:14:06 +00002288 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002289}
2290
2291bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002292ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002293{
2294 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002295 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002296 DoOperation(&op);
2297 return result;
2298}
2299
2300bool
2301ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2302{
2303 bool result;
2304 EventMessageOperation op(tid, message, result);
2305 DoOperation(&op);
2306 return result;
2307}
2308
Greg Clayton743ecf42012-10-16 20:20:18 +00002309lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002310ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002311{
Greg Clayton28041352011-11-29 20:50:10 +00002312 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002313 if (tid != LLDB_INVALID_THREAD_ID)
2314 {
2315 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002316 DoOperation(&op);
2317 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002318 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002319}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002320
2321bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002322ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2323{
Peter Collingbourne62343202011-06-14 03:55:54 +00002324 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002325
2326 if (target_fd == -1)
2327 return false;
2328
Pavel Labath493c3a12015-02-04 10:36:57 +00002329 if (dup2(target_fd, fd) == -1)
2330 return false;
2331
2332 return (close(target_fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002333}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002334
2335void
2336ProcessMonitor::StopMonitoringChildProcess()
2337{
Zachary Turneracee96a2014-09-23 18:32:09 +00002338 if (m_monitor_thread.IsJoinable())
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002339 {
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +00002340 ::pthread_kill(m_monitor_thread.GetNativeThread().GetSystemHandle(), SIGUSR1);
Zachary Turner39de3112014-09-09 20:54:56 +00002341 m_monitor_thread.Join(nullptr);
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002342 }
2343}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002344
2345void
2346ProcessMonitor::StopMonitor()
2347{
2348 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002349 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002350 sem_destroy(&m_operation_pending);
2351 sem_destroy(&m_operation_done);
Pavel Labath3a2da9e2015-02-06 11:32:52 +00002352 if (m_terminal_fd >= 0) {
2353 close(m_terminal_fd);
2354 m_terminal_fd = -1;
2355 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00002356}
2357
2358void
Greg Clayton743ecf42012-10-16 20:20:18 +00002359ProcessMonitor::StopOpThread()
2360{
Zachary Turneracee96a2014-09-23 18:32:09 +00002361 if (!m_operation_thread.IsJoinable())
Greg Clayton743ecf42012-10-16 20:20:18 +00002362 return;
2363
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +00002364 DoOperation(EXIT_OPERATION);
Zachary Turner39de3112014-09-09 20:54:56 +00002365 m_operation_thread.Join(nullptr);
Greg Clayton743ecf42012-10-16 20:20:18 +00002366}