blob: b2754bcc1a372fa861809bcd5452222cffd1e60b [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"
Zachary Turner39de3112014-09-09 20:54:56 +000027#include "lldb/Host/HostThread.h"
28#include "lldb/Host/ThreadLauncher.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/RegisterContext.h"
31#include "lldb/Utility/PseudoTerminal.h"
32
Chaoren Lin28e57422015-02-03 01:51:25 +000033#include "Plugins/Process/POSIX/CrashReason.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000034#include "Plugins/Process/POSIX/POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000035#include "ProcessLinux.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000036#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037#include "ProcessMonitor.h"
38
Tamas Berghammerd8584872015-02-06 10:57:40 +000039// System includes - They have to be included after framework includes because they define some
40// macros which collide with variable names in other modules
41#ifndef __ANDROID__
42#include <sys/procfs.h>
43#endif
44#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
91#define tgkill(pid, tid, sig) syscall(SYS_tgkill, pid, tid, sig)
92
Stephen Wilsone6f9f662010-07-24 02:19:04 +000093using namespace lldb_private;
94
Johnny Chen0d5f2d42011-10-18 18:09:30 +000095// FIXME: this code is host-dependent with respect to types and
96// endianness and needs to be fixed. For example, lldb::addr_t is
97// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
98// 32-bit pointer arguments. This code uses casts to work around the
99// problem.
100
101// We disable the tracing of ptrace calls for integration builds to
102// avoid the additional indirection and checks.
103#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
104
Greg Clayton386ff182011-11-05 01:09:16 +0000105static void
106DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
107{
108 uint8_t *ptr = (uint8_t *)bytes;
109 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
110 for(uint32_t i=0; i<loop_count; i++)
111 {
112 s.Printf ("[%x]", *ptr);
113 ptr++;
114 }
115}
116
Matt Kopec58c0b962013-03-20 20:34:35 +0000117static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +0000118{
119 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000120 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000121 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000122
123 if (verbose_log)
124 {
125 switch(req)
126 {
127 case PTRACE_POKETEXT:
128 {
129 DisplayBytes(buf, &data, 8);
130 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
131 break;
132 }
Greg Clayton542e4072012-09-07 17:49:29 +0000133 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000134 {
135 DisplayBytes(buf, &data, 8);
136 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
137 break;
138 }
Greg Clayton542e4072012-09-07 17:49:29 +0000139 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000140 {
141 DisplayBytes(buf, &data, 8);
142 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
143 break;
144 }
Todd Fiala6ac1be42014-08-21 16:34:03 +0000145#if !defined (__arm64__) && !defined (__aarch64__)
Greg Clayton542e4072012-09-07 17:49:29 +0000146 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000147 {
Matt Kopec7de48462013-03-06 17:20:48 +0000148 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000149 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
150 break;
151 }
152 case PTRACE_SETFPREGS:
153 {
Matt Kopec7de48462013-03-06 17:20:48 +0000154 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000155 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
156 break;
157 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000158#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000159 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000160 {
161 DisplayBytes(buf, data, sizeof(siginfo_t));
162 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
163 break;
164 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000165 case PTRACE_SETREGSET:
166 {
167 // Extract iov_base from data, which is a pointer to the struct IOVEC
168 DisplayBytes(buf, *(void **)data, data_size);
169 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
170 break;
171 }
Greg Clayton386ff182011-11-05 01:09:16 +0000172 default:
173 {
174 }
175 }
176 }
177}
178
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000179// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000180// 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 +0000181extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000182PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000183 const char* reqName, const char* file, int line)
184{
Greg Clayton386ff182011-11-05 01:09:16 +0000185 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000186
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000187 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000188
Matt Kopec7de48462013-03-06 17:20:48 +0000189 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000190
191 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000192 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000193 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000194 else
Todd Fiala4507f062014-02-27 20:46:12 +0000195 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000196
Ed Mastec099c952014-02-24 14:07:45 +0000197 if (log)
198 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
199 reqName, pid, addr, data, data_size, result, file, line);
200
Matt Kopec7de48462013-03-06 17:20:48 +0000201 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000202
Matt Kopec7de48462013-03-06 17:20:48 +0000203 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000204 {
205 const char* str;
206 switch (errno)
207 {
208 case ESRCH: str = "ESRCH"; break;
209 case EINVAL: str = "EINVAL"; break;
210 case EBUSY: str = "EBUSY"; break;
211 case EPERM: str = "EPERM"; break;
212 default: str = "<unknown>";
213 }
214 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
215 }
216
217 return result;
218}
219
Matt Kopec7de48462013-03-06 17:20:48 +0000220// Wrapper for ptrace when logging is not required.
221// Sets errno to 0 prior to calling ptrace.
222extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000223PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000224{
Matt Kopec58c0b962013-03-20 20:34:35 +0000225 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000226 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000227 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
228 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
229 else
230 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000231 return result;
232}
233
234#define PTRACE(req, pid, addr, data, data_size) \
235 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000236#else
Matt Kopec7de48462013-03-06 17:20:48 +0000237 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000238#endif
239
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000240//------------------------------------------------------------------------------
241// Static implementations of ProcessMonitor::ReadMemory and
242// ProcessMonitor::WriteMemory. This enables mutual recursion between these
243// functions without needed to go thru the thread funnel.
244
245static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000246DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000247 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
248{
Greg Clayton542e4072012-09-07 17:49:29 +0000249 // ptrace word size is determined by the host, not the child
250 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000251 unsigned char *dst = static_cast<unsigned char*>(buf);
252 size_t bytes_read;
253 size_t remainder;
254 long data;
255
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000256 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000257 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000258 ProcessPOSIXLog::IncNestLevel();
259 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000260 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000261 pid, word_size, (void*)vm_addr, buf, size);
262
263 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000264 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
265 {
266 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000267 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
268 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000269 {
270 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000271 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000272 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000273 return bytes_read;
274 }
275
276 remainder = size - bytes_read;
277 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000278
279 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000280 for (unsigned i = 0; i < remainder; ++i)
281 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000282
Johnny Chen30213ff2012-01-05 19:17:38 +0000283 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
284 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
285 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
286 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000287 {
288 uintptr_t print_dst = 0;
289 // Format bytes from data by moving into print_dst for log output
290 for (unsigned i = 0; i < remainder; ++i)
291 print_dst |= (((data >> i*8) & 0xFF) << i*8);
292 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
293 (void*)vm_addr, print_dst, (unsigned long)data);
294 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000295
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000296 vm_addr += word_size;
297 dst += word_size;
298 }
299
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000300 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000301 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000302 return bytes_read;
303}
304
305static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000306DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000307 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
308{
Greg Clayton542e4072012-09-07 17:49:29 +0000309 // ptrace word size is determined by the host, not the child
310 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000311 const unsigned char *src = static_cast<const unsigned char*>(buf);
312 size_t bytes_written = 0;
313 size_t remainder;
314
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000315 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000316 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000317 ProcessPOSIXLog::IncNestLevel();
318 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000319 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000320 pid, word_size, (void*)vm_addr, buf, size);
321
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000322 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
323 {
324 remainder = size - bytes_written;
325 remainder = remainder > word_size ? word_size : remainder;
326
327 if (remainder == word_size)
328 {
329 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000330 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000331 for (unsigned i = 0; i < word_size; ++i)
332 data |= (unsigned long)src[i] << i*8;
333
Johnny Chen30213ff2012-01-05 19:17:38 +0000334 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
335 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
336 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
337 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000338 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
339 (void*)vm_addr, *(unsigned long*)src, data);
340
Matt Kopec7de48462013-03-06 17:20:48 +0000341 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000342 {
343 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000344 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000345 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000346 return bytes_written;
347 }
348 }
349 else
350 {
351 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000352 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000353 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000354 {
355 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000356 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000357 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000358 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000359
360 memcpy(buff, src, remainder);
361
Greg Clayton542e4072012-09-07 17:49:29 +0000362 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000363 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000364 {
365 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000366 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000367 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000368 }
369
Johnny Chen30213ff2012-01-05 19:17:38 +0000370 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
371 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
372 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
373 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000374 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
375 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000376 }
377
378 vm_addr += word_size;
379 src += word_size;
380 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000381 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000382 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000383 return bytes_written;
384}
385
Stephen Wilson26977162011-03-23 02:14:42 +0000386// Simple helper function to ensure flags are enabled on the given file
387// descriptor.
388static bool
389EnsureFDFlags(int fd, int flags, Error &error)
390{
391 int status;
392
393 if ((status = fcntl(fd, F_GETFL)) == -1)
394 {
395 error.SetErrorToErrno();
396 return false;
397 }
398
399 if (fcntl(fd, F_SETFL, status | flags) == -1)
400 {
401 error.SetErrorToErrno();
402 return false;
403 }
404
405 return true;
406}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000407
408//------------------------------------------------------------------------------
409/// @class Operation
410/// @brief Represents a ProcessMonitor operation.
411///
412/// Under Linux, it is not possible to ptrace() from any other thread but the
413/// one that spawned or attached to the process from the start. Therefore, when
414/// a ProcessMonitor is asked to deliver or change the state of an inferior
415/// process the operation must be "funneled" to a specific thread to perform the
416/// task. The Operation class provides an abstract base for all services the
417/// ProcessMonitor must perform via the single virtual function Execute, thus
418/// encapsulating the code that needs to run in the privileged context.
419class Operation
420{
421public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000422 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000423 virtual void Execute(ProcessMonitor *monitor) = 0;
424};
425
426//------------------------------------------------------------------------------
427/// @class ReadOperation
428/// @brief Implements ProcessMonitor::ReadMemory.
429class ReadOperation : public Operation
430{
431public:
432 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
433 Error &error, size_t &result)
434 : m_addr(addr), m_buff(buff), m_size(size),
435 m_error(error), m_result(result)
436 { }
437
438 void Execute(ProcessMonitor *monitor);
439
440private:
441 lldb::addr_t m_addr;
442 void *m_buff;
443 size_t m_size;
444 Error &m_error;
445 size_t &m_result;
446};
447
448void
449ReadOperation::Execute(ProcessMonitor *monitor)
450{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000451 lldb::pid_t pid = monitor->GetPID();
452
Greg Clayton542e4072012-09-07 17:49:29 +0000453 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000454}
455
456//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000457/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000458/// @brief Implements ProcessMonitor::WriteMemory.
459class WriteOperation : public Operation
460{
461public:
462 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
463 Error &error, size_t &result)
464 : m_addr(addr), m_buff(buff), m_size(size),
465 m_error(error), m_result(result)
466 { }
467
468 void Execute(ProcessMonitor *monitor);
469
470private:
471 lldb::addr_t m_addr;
472 const void *m_buff;
473 size_t m_size;
474 Error &m_error;
475 size_t &m_result;
476};
477
478void
479WriteOperation::Execute(ProcessMonitor *monitor)
480{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000481 lldb::pid_t pid = monitor->GetPID();
482
Greg Clayton542e4072012-09-07 17:49:29 +0000483 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000484}
485
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000486
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000487//------------------------------------------------------------------------------
488/// @class ReadRegOperation
489/// @brief Implements ProcessMonitor::ReadRegisterValue.
490class ReadRegOperation : public Operation
491{
492public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000493 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000494 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000495 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000496 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000497 { }
498
499 void Execute(ProcessMonitor *monitor);
500
501private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000502 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000503 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000504 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000505 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000506 bool &m_result;
507};
508
509void
510ReadRegOperation::Execute(ProcessMonitor *monitor)
511{
Todd Fiala49131cf2014-09-12 16:57:28 +0000512#if defined (__arm64__) || defined (__aarch64__)
513 if (m_offset > sizeof(struct user_pt_regs))
514 {
515 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
516 if (offset > sizeof(struct user_fpsimd_state))
517 {
518 m_result = false;
519 }
520 else
521 {
522 elf_fpregset_t regs;
523 int regset = NT_FPREGSET;
524 struct iovec ioVec;
525
526 ioVec.iov_base = &regs;
527 ioVec.iov_len = sizeof regs;
528 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
529 m_result = false;
530 else
531 {
532 m_result = true;
533 m_value.SetBytes((void *)(((unsigned char *)(&regs)) + offset), 16, monitor->GetProcess().GetByteOrder());
534 }
535 }
536 }
537 else
538 {
539 elf_gregset_t regs;
540 int regset = NT_PRSTATUS;
541 struct iovec ioVec;
542
543 ioVec.iov_base = &regs;
544 ioVec.iov_len = sizeof regs;
545 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
546 m_result = false;
547 else
548 {
549 m_result = true;
550 m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, monitor->GetProcess().GetByteOrder());
551 }
552 }
553#else
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000554 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000555
556 // Set errno to zero so that we can detect a failed peek.
557 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000558 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
559 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000560 m_result = false;
561 else
562 {
563 m_value = data;
564 m_result = true;
565 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000566 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000567 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000568 m_reg_name, data);
Todd Fiala49131cf2014-09-12 16:57:28 +0000569#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000570}
571
572//------------------------------------------------------------------------------
573/// @class WriteRegOperation
574/// @brief Implements ProcessMonitor::WriteRegisterValue.
575class WriteRegOperation : public Operation
576{
577public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000578 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000579 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000580 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000581 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000582 { }
583
584 void Execute(ProcessMonitor *monitor);
585
586private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000587 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000588 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000589 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000590 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000591 bool &m_result;
592};
593
594void
595WriteRegOperation::Execute(ProcessMonitor *monitor)
596{
Todd Fiala49131cf2014-09-12 16:57:28 +0000597#if defined (__arm64__) || defined (__aarch64__)
598 if (m_offset > sizeof(struct user_pt_regs))
599 {
600 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
601 if (offset > sizeof(struct user_fpsimd_state))
602 {
603 m_result = false;
604 }
605 else
606 {
607 elf_fpregset_t regs;
608 int regset = NT_FPREGSET;
609 struct iovec ioVec;
610
611 ioVec.iov_base = &regs;
612 ioVec.iov_len = sizeof regs;
613 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
614 m_result = false;
615 else
616 {
617 ::memcpy((void *)(((unsigned char *)(&regs)) + offset), m_value.GetBytes(), 16);
618 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
619 m_result = false;
620 else
621 m_result = true;
622 }
623 }
624 }
625 else
626 {
627 elf_gregset_t regs;
628 int regset = NT_PRSTATUS;
629 struct iovec ioVec;
630
631 ioVec.iov_base = &regs;
632 ioVec.iov_len = sizeof regs;
633 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
634 m_result = false;
635 else
636 {
637 ::memcpy((void *)(((unsigned char *)(&regs)) + m_offset), m_value.GetBytes(), 8);
638 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
639 m_result = false;
640 else
641 m_result = true;
642 }
643 }
644#else
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000645 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000646 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000647
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000648 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000649
650 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000651 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000652 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000653 m_result = false;
654 else
655 m_result = true;
Todd Fiala49131cf2014-09-12 16:57:28 +0000656#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000657}
658
659//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000660/// @class ReadGPROperation
661/// @brief Implements ProcessMonitor::ReadGPR.
662class ReadGPROperation : public Operation
663{
664public:
Matt Kopec7de48462013-03-06 17:20:48 +0000665 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
666 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000667 { }
668
669 void Execute(ProcessMonitor *monitor);
670
671private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000672 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000673 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000674 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000675 bool &m_result;
676};
677
678void
679ReadGPROperation::Execute(ProcessMonitor *monitor)
680{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000681#if defined (__arm64__) || defined (__aarch64__)
682 int regset = NT_PRSTATUS;
683 struct iovec ioVec;
684
685 ioVec.iov_base = m_buf;
686 ioVec.iov_len = m_buf_size;
687 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000688 m_result = false;
689 else
690 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000691#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000692 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
693 m_result = false;
694 else
695 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000696#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000697}
698
699//------------------------------------------------------------------------------
700/// @class ReadFPROperation
701/// @brief Implements ProcessMonitor::ReadFPR.
702class ReadFPROperation : public Operation
703{
704public:
Matt Kopec7de48462013-03-06 17:20:48 +0000705 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
706 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000707 { }
708
709 void Execute(ProcessMonitor *monitor);
710
711private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000712 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000713 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000714 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000715 bool &m_result;
716};
717
718void
719ReadFPROperation::Execute(ProcessMonitor *monitor)
720{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000721#if defined (__arm64__) || defined (__aarch64__)
722 int regset = NT_FPREGSET;
723 struct iovec ioVec;
724
725 ioVec.iov_base = m_buf;
726 ioVec.iov_len = m_buf_size;
727 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000728 m_result = false;
729 else
730 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000731#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000732 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
733 m_result = false;
734 else
735 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000736#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000737}
738
739//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000740/// @class ReadRegisterSetOperation
741/// @brief Implements ProcessMonitor::ReadRegisterSet.
742class ReadRegisterSetOperation : public Operation
743{
744public:
745 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
746 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
747 { }
748
749 void Execute(ProcessMonitor *monitor);
750
751private:
752 lldb::tid_t m_tid;
753 void *m_buf;
754 size_t m_buf_size;
755 const unsigned int m_regset;
756 bool &m_result;
757};
758
759void
760ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
761{
762 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
763 m_result = false;
764 else
765 m_result = true;
766}
767
768//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000769/// @class WriteGPROperation
770/// @brief Implements ProcessMonitor::WriteGPR.
771class WriteGPROperation : public Operation
772{
773public:
Matt Kopec7de48462013-03-06 17:20:48 +0000774 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
775 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000776 { }
777
778 void Execute(ProcessMonitor *monitor);
779
780private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000781 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000782 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000783 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000784 bool &m_result;
785};
786
787void
788WriteGPROperation::Execute(ProcessMonitor *monitor)
789{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000790#if defined (__arm64__) || defined (__aarch64__)
791 int regset = NT_PRSTATUS;
792 struct iovec ioVec;
793
794 ioVec.iov_base = m_buf;
795 ioVec.iov_len = m_buf_size;
796 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000797 m_result = false;
798 else
799 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000800#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000801 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
802 m_result = false;
803 else
804 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000805#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000806}
807
808//------------------------------------------------------------------------------
809/// @class WriteFPROperation
810/// @brief Implements ProcessMonitor::WriteFPR.
811class WriteFPROperation : public Operation
812{
813public:
Matt Kopec7de48462013-03-06 17:20:48 +0000814 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
815 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000816 { }
817
818 void Execute(ProcessMonitor *monitor);
819
820private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000821 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000822 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000823 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000824 bool &m_result;
825};
826
827void
828WriteFPROperation::Execute(ProcessMonitor *monitor)
829{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000830#if defined (__arm64__) || defined (__aarch64__)
831 int regset = NT_FPREGSET;
832 struct iovec ioVec;
833
834 ioVec.iov_base = m_buf;
835 ioVec.iov_len = m_buf_size;
836 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000837 m_result = false;
838 else
839 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000840#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000841 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
842 m_result = false;
843 else
844 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000845#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000846}
847
848//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000849/// @class WriteRegisterSetOperation
850/// @brief Implements ProcessMonitor::WriteRegisterSet.
851class WriteRegisterSetOperation : public Operation
852{
853public:
854 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
855 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
856 { }
857
858 void Execute(ProcessMonitor *monitor);
859
860private:
861 lldb::tid_t m_tid;
862 void *m_buf;
863 size_t m_buf_size;
864 const unsigned int m_regset;
865 bool &m_result;
866};
867
868void
869WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
870{
871 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
872 m_result = false;
873 else
874 m_result = true;
875}
876
877//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000878/// @class ReadThreadPointerOperation
879/// @brief Implements ProcessMonitor::ReadThreadPointer.
880class ReadThreadPointerOperation : public Operation
881{
882public:
883 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
884 : m_tid(tid), m_addr(addr), m_result(result)
885 { }
886
887 void Execute(ProcessMonitor *monitor);
888
889private:
890 lldb::tid_t m_tid;
891 lldb::addr_t *m_addr;
892 bool &m_result;
893};
894
895void
896ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
897{
898 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
899 if (log)
900 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
901
902 // The process for getting the thread area on Linux is
903 // somewhat... obscure. There's several different ways depending on
904 // what arch you're on, and what kernel version you have.
905
906 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
907 switch(arch.GetMachine())
908 {
Todd Fiala42079682014-08-27 16:05:26 +0000909 case llvm::Triple::aarch64:
910 {
Todd Fialadbec1ff2014-09-04 16:08:20 +0000911 int regset = LLDB_PTRACE_NT_ARM_TLS;
Todd Fiala42079682014-08-27 16:05:26 +0000912 struct iovec ioVec;
913
914 ioVec.iov_base = m_addr;
915 ioVec.iov_len = sizeof(lldb::addr_t);
916 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, ioVec.iov_len) < 0)
917 m_result = false;
918 else
919 m_result = true;
920 break;
921 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000922#if defined(__i386__) || defined(__x86_64__)
923 // Note that struct user below has a field named i387 which is x86-specific.
924 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000925 case llvm::Triple::x86:
926 {
927 // Find the GS register location for our host architecture.
928 size_t gs_user_offset = offsetof(struct user, regs);
929#ifdef __x86_64__
930 gs_user_offset += offsetof(struct user_regs_struct, gs);
931#endif
932#ifdef __i386__
933 gs_user_offset += offsetof(struct user_regs_struct, xgs);
934#endif
935
936 // Read the GS register value to get the selector.
937 errno = 0;
938 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
939 if (errno)
940 {
941 m_result = false;
942 break;
943 }
944
945 // Read the LDT base for that selector.
946 uint32_t tmp[4];
947 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
948 *m_addr = tmp[1];
949 break;
950 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000951#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000952 case llvm::Triple::x86_64:
953 // Read the FS register base.
954 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
955 break;
956 default:
957 m_result = false;
958 break;
959 }
960}
961
962//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000963/// @class ResumeOperation
964/// @brief Implements ProcessMonitor::Resume.
965class ResumeOperation : public Operation
966{
967public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000968 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
969 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000970
971 void Execute(ProcessMonitor *monitor);
972
973private:
974 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000975 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000976 bool &m_result;
977};
978
979void
980ResumeOperation::Execute(ProcessMonitor *monitor)
981{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000982 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000983
984 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
985 data = m_signo;
986
Matt Kopec7de48462013-03-06 17:20:48 +0000987 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000988 {
989 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
990
991 if (log)
992 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000993 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000994 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000995 else
996 m_result = true;
997}
998
999//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +00001000/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001001/// @brief Implements ProcessMonitor::SingleStep.
1002class SingleStepOperation : public Operation
1003{
1004public:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001005 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
1006 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001007
1008 void Execute(ProcessMonitor *monitor);
1009
1010private:
1011 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001012 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001013 bool &m_result;
1014};
1015
1016void
1017SingleStepOperation::Execute(ProcessMonitor *monitor)
1018{
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001019 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001020
1021 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
1022 data = m_signo;
1023
Matt Kopec7de48462013-03-06 17:20:48 +00001024 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001025 m_result = false;
1026 else
1027 m_result = true;
1028}
1029
1030//------------------------------------------------------------------------------
1031/// @class SiginfoOperation
1032/// @brief Implements ProcessMonitor::GetSignalInfo.
1033class SiginfoOperation : public Operation
1034{
1035public:
Daniel Maleaa35970a2012-11-23 18:09:58 +00001036 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
1037 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001038
1039 void Execute(ProcessMonitor *monitor);
1040
1041private:
1042 lldb::tid_t m_tid;
1043 void *m_info;
1044 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001045 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001046};
1047
1048void
1049SiginfoOperation::Execute(ProcessMonitor *monitor)
1050{
Matt Kopec7de48462013-03-06 17:20:48 +00001051 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001052 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001053 m_err = errno;
1054 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001055 else
1056 m_result = true;
1057}
1058
1059//------------------------------------------------------------------------------
1060/// @class EventMessageOperation
1061/// @brief Implements ProcessMonitor::GetEventMessage.
1062class EventMessageOperation : public Operation
1063{
1064public:
1065 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
1066 : m_tid(tid), m_message(message), m_result(result) { }
1067
1068 void Execute(ProcessMonitor *monitor);
1069
1070private:
1071 lldb::tid_t m_tid;
1072 unsigned long *m_message;
1073 bool &m_result;
1074};
1075
1076void
1077EventMessageOperation::Execute(ProcessMonitor *monitor)
1078{
Matt Kopec7de48462013-03-06 17:20:48 +00001079 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001080 m_result = false;
1081 else
1082 m_result = true;
1083}
1084
1085//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +00001086/// @class DetachOperation
1087/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +00001088class DetachOperation : public Operation
1089{
1090public:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001091 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +00001092
1093 void Execute(ProcessMonitor *monitor);
1094
1095private:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001096 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +00001097 Error &m_error;
1098};
1099
1100void
1101DetachOperation::Execute(ProcessMonitor *monitor)
1102{
Matt Kopec085d6ce2013-05-31 22:00:07 +00001103 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +00001104 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +00001105}
1106
Johnny Chen25e68e32011-06-14 19:19:50 +00001107ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
1108 : m_monitor(monitor)
1109{
1110 sem_init(&m_semaphore, 0, 0);
1111}
1112
1113ProcessMonitor::OperationArgs::~OperationArgs()
1114{
1115 sem_destroy(&m_semaphore);
1116}
1117
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001118ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
1119 lldb_private::Module *module,
1120 char const **argv,
1121 char const **envp,
1122 const char *stdin_path,
1123 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001124 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001125 const char *working_dir,
1126 const lldb_private::ProcessLaunchInfo &launch_info)
Johnny Chen25e68e32011-06-14 19:19:50 +00001127 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001128 m_module(module),
1129 m_argv(argv),
1130 m_envp(envp),
1131 m_stdin_path(stdin_path),
1132 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +00001133 m_stderr_path(stderr_path),
Todd Fiala0bce1b62014-08-17 00:10:50 +00001134 m_working_dir(working_dir),
1135 m_launch_info(launch_info)
1136{
1137}
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001138
1139ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +00001140{ }
1141
1142ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
1143 lldb::pid_t pid)
1144 : OperationArgs(monitor), m_pid(pid) { }
1145
1146ProcessMonitor::AttachArgs::~AttachArgs()
1147{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001148
1149//------------------------------------------------------------------------------
1150/// The basic design of the ProcessMonitor is built around two threads.
1151///
1152/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
1153/// for changes in the debugee state. When a change is detected a
1154/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1155/// "drives" state changes in the debugger.
1156///
1157/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001158/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001159/// operations such as register reads/writes, stepping, etc. See the comments
1160/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001161ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001162 Module *module,
1163 const char *argv[],
1164 const char *envp[],
1165 const char *stdin_path,
1166 const char *stdout_path,
1167 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001168 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001169 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001170 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001171 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001172 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001173 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001174 m_pid(LLDB_INVALID_PROCESS_ID),
1175 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001176 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001177{
Daniel Malea1efb4182013-09-16 23:12:18 +00001178 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1179 stdin_path, stdout_path, stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001180 working_dir, launch_info));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001181
Daniel Malea1efb4182013-09-16 23:12:18 +00001182 sem_init(&m_operation_pending, 0, 0);
1183 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001184
Johnny Chen25e68e32011-06-14 19:19:50 +00001185 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001186 if (!error.Success())
1187 return;
1188
1189WAIT_AGAIN:
1190 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001191 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001192 {
1193 if (errno == EINTR)
1194 goto WAIT_AGAIN;
1195 else
1196 {
1197 error.SetErrorToErrno();
1198 return;
1199 }
1200 }
1201
1202 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001203 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001204 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001205 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001206 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001207 return;
1208 }
1209
1210 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001211 m_monitor_thread = Host::StartMonitoringChildProcess(
1212 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001213 if (!m_monitor_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001214 {
1215 error.SetErrorToGenericError();
1216 error.SetErrorString("Process launch failed.");
1217 return;
1218 }
1219}
1220
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001221ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001222 lldb::pid_t pid,
1223 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001224 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001225 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001226 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001227 m_pid(LLDB_INVALID_PROCESS_ID),
1228 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001229 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001230{
Daniel Malea1efb4182013-09-16 23:12:18 +00001231 sem_init(&m_operation_pending, 0, 0);
1232 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001233
Daniel Malea1efb4182013-09-16 23:12:18 +00001234 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001235
1236 StartAttachOpThread(args.get(), error);
1237 if (!error.Success())
1238 return;
1239
1240WAIT_AGAIN:
1241 // Wait for the operation thread to initialize.
1242 if (sem_wait(&args->m_semaphore))
1243 {
1244 if (errno == EINTR)
1245 goto WAIT_AGAIN;
1246 else
1247 {
1248 error.SetErrorToErrno();
1249 return;
1250 }
1251 }
1252
Greg Clayton743ecf42012-10-16 20:20:18 +00001253 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001254 if (!args->m_error.Success())
1255 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001256 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001257 error = args->m_error;
1258 return;
1259 }
1260
1261 // Finally, start monitoring the child process for change in state.
1262 m_monitor_thread = Host::StartMonitoringChildProcess(
1263 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001264 if (!m_monitor_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001265 {
1266 error.SetErrorToGenericError();
1267 error.SetErrorString("Process attach failed.");
1268 return;
1269 }
1270}
1271
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001272ProcessMonitor::~ProcessMonitor()
1273{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001274 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001275}
1276
1277//------------------------------------------------------------------------------
1278// Thread setup and tear down.
1279void
Johnny Chen25e68e32011-06-14 19:19:50 +00001280ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001281{
1282 static const char *g_thread_name = "lldb.process.linux.operation";
1283
Zachary Turneracee96a2014-09-23 18:32:09 +00001284 if (m_operation_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001285 return;
1286
Zachary Turner39de3112014-09-09 20:54:56 +00001287 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001288}
1289
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001290void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001291ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001292{
1293 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1294
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001295 if (!Launch(args)) {
1296 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001297 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001298 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001299
Stephen Wilson570243b2011-01-19 01:37:06 +00001300 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001301 return NULL;
1302}
1303
1304bool
1305ProcessMonitor::Launch(LaunchArgs *args)
1306{
Todd Fiala0bce1b62014-08-17 00:10:50 +00001307 assert (args && "null args");
1308 if (!args)
1309 return false;
1310
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001311 ProcessMonitor *monitor = args->m_monitor;
1312 ProcessLinux &process = monitor->GetProcess();
1313 const char **argv = args->m_argv;
1314 const char **envp = args->m_envp;
1315 const char *stdin_path = args->m_stdin_path;
1316 const char *stdout_path = args->m_stdout_path;
1317 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001318 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001319
1320 lldb_utility::PseudoTerminal terminal;
1321 const size_t err_len = 1024;
1322 char err_str[err_len];
1323 lldb::pid_t pid;
1324
1325 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001326 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001327
Stephen Wilson57740ec2011-01-15 00:12:41 +00001328 // Propagate the environment if one is not supplied.
1329 if (envp == NULL || envp[0] == NULL)
1330 envp = const_cast<const char **>(environ);
1331
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001332 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001333 {
1334 args->m_error.SetErrorToGenericError();
1335 args->m_error.SetErrorString("Process fork failed.");
1336 goto FINISH;
1337 }
1338
Peter Collingbourne6a520222011-06-14 03:55:58 +00001339 // Recognized child exit status codes.
1340 enum {
1341 ePtraceFailed = 1,
1342 eDupStdinFailed,
1343 eDupStdoutFailed,
1344 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001345 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001346 eExecFailed,
1347 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001348 };
1349
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001350 // Child process.
1351 if (pid == 0)
1352 {
1353 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001354 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001355 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001356
Pavel Labath493c3a12015-02-04 10:36:57 +00001357 // terminal has already dupped the tty descriptors to stdin/out/err.
1358 // This closes original fd from which they were copied (and avoids
1359 // leaking descriptors to the debugged process.
1360 terminal.CloseSlaveFileDescriptor();
1361
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001362 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001363 if (setgid(getgid()) != 0)
1364 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001365
1366 // Let us have our own process group.
1367 setpgid(0, 0);
1368
Greg Clayton710dd5a2011-01-08 20:28:42 +00001369 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001370 //
1371 // FIXME: If two or more of the paths are the same we needlessly open
1372 // the same file multiple times.
1373 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001374 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001375 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001376
1377 if (stdout_path != NULL && stdout_path[0])
1378 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001379 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001380
1381 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001382 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001383 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001384
Daniel Malea6217d2a2013-01-08 14:49:22 +00001385 // Change working directory
1386 if (working_dir != NULL && working_dir[0])
1387 if (0 != ::chdir(working_dir))
1388 exit(eChdirFailed);
1389
Todd Fiala0bce1b62014-08-17 00:10:50 +00001390 // Disable ASLR if requested.
1391 if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
1392 {
1393 const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
1394 if (old_personality == -1)
1395 {
1396 if (log)
1397 log->Printf ("ProcessMonitor::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno));
1398 }
1399 else
1400 {
1401 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
1402 if (new_personality == -1)
1403 {
1404 if (log)
1405 log->Printf ("ProcessMonitor::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno));
1406
1407 }
1408 else
1409 {
1410 if (log)
Todd Fiala850f9a22014-09-19 18:27:45 +00001411 log->Printf ("ProcessMonitor::%s disabling ASLR: SUCCESS", __FUNCTION__);
Todd Fiala0bce1b62014-08-17 00:10:50 +00001412
1413 }
1414 }
1415 }
1416
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001417 // Execute. We should never return.
1418 execve(argv[0],
1419 const_cast<char *const *>(argv),
1420 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001421 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001422 }
1423
1424 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001425 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001426 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001427 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001428
1429 raw_pid = waitpid(pid, &status, 0);
1430 wpid = static_cast <lldb::pid_t> (raw_pid);
1431 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001432 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001433 args->m_error.SetErrorToErrno();
1434 goto FINISH;
1435 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001436 else if (WIFEXITED(status))
1437 {
1438 // open, dup or execve likely failed for some reason.
1439 args->m_error.SetErrorToGenericError();
1440 switch (WEXITSTATUS(status))
1441 {
Greg Clayton542e4072012-09-07 17:49:29 +00001442 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001443 args->m_error.SetErrorString("Child ptrace failed.");
1444 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001445 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001446 args->m_error.SetErrorString("Child open stdin failed.");
1447 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001448 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001449 args->m_error.SetErrorString("Child open stdout failed.");
1450 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001451 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001452 args->m_error.SetErrorString("Child open stderr failed.");
1453 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001454 case eChdirFailed:
1455 args->m_error.SetErrorString("Child failed to set working directory.");
1456 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001457 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001458 args->m_error.SetErrorString("Child exec failed.");
1459 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001460 case eSetGidFailed:
1461 args->m_error.SetErrorString("Child setgid failed.");
1462 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001463 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001464 args->m_error.SetErrorString("Child returned unknown exit status.");
1465 break;
1466 }
1467 goto FINISH;
1468 }
1469 assert(WIFSTOPPED(status) && wpid == pid &&
1470 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001471
Matt Kopec085d6ce2013-05-31 22:00:07 +00001472 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001473 {
1474 args->m_error.SetErrorToErrno();
1475 goto FINISH;
1476 }
1477
1478 // Release the master terminal descriptor and pass it off to the
1479 // ProcessMonitor instance. Similarly stash the inferior pid.
1480 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1481 monitor->m_pid = pid;
1482
Stephen Wilson26977162011-03-23 02:14:42 +00001483 // Set the terminal fd to be in non blocking mode (it simplifies the
1484 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1485 // descriptor to read from).
1486 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1487 goto FINISH;
1488
Johnny Chen30213ff2012-01-05 19:17:38 +00001489 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001490 // FIXME: should we be letting UpdateThreadList handle this?
1491 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001492 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001493
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001494 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001495 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001496 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001497
Matt Kopecb2910442013-07-09 15:09:45 +00001498 process.AddThreadForInitialStopIfNeeded(pid);
1499
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001500 // Let our process instance know the thread has stopped.
1501 process.SendMessage(ProcessMessage::Trace(pid));
1502
1503FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001504 return args->m_error.Success();
1505}
1506
Johnny Chen25e68e32011-06-14 19:19:50 +00001507void
1508ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1509{
1510 static const char *g_thread_name = "lldb.process.linux.operation";
1511
Zachary Turneracee96a2014-09-23 18:32:09 +00001512 if (m_operation_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001513 return;
1514
Zachary Turner39de3112014-09-09 20:54:56 +00001515 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen25e68e32011-06-14 19:19:50 +00001516}
1517
Johnny Chen25e68e32011-06-14 19:19:50 +00001518void *
1519ProcessMonitor::AttachOpThread(void *arg)
1520{
1521 AttachArgs *args = static_cast<AttachArgs*>(arg);
1522
Greg Clayton743ecf42012-10-16 20:20:18 +00001523 if (!Attach(args)) {
1524 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001525 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001526 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001527
1528 ServeOperation(args);
1529 return NULL;
1530}
1531
1532bool
1533ProcessMonitor::Attach(AttachArgs *args)
1534{
1535 lldb::pid_t pid = args->m_pid;
1536
1537 ProcessMonitor *monitor = args->m_monitor;
1538 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001539 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001540 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001541
Matt Kopec085d6ce2013-05-31 22:00:07 +00001542 // Use a map to keep track of the threads which we have attached/need to attach.
1543 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001544 if (pid <= 1)
1545 {
1546 args->m_error.SetErrorToGenericError();
1547 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1548 goto FINISH;
1549 }
1550
Matt Kopec085d6ce2013-05-31 22:00:07 +00001551 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001552 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001553 for (Host::TidMap::iterator it = tids_to_attach.begin();
1554 it != tids_to_attach.end(); ++it)
1555 {
1556 if (it->second == false)
1557 {
1558 lldb::tid_t tid = it->first;
1559
1560 // Attach to the requested process.
1561 // An attach will cause the thread to stop with a SIGSTOP.
1562 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1563 {
1564 // No such thread. The thread may have exited.
1565 // More error handling may be needed.
1566 if (errno == ESRCH)
1567 {
1568 tids_to_attach.erase(it);
1569 continue;
1570 }
1571 else
1572 {
1573 args->m_error.SetErrorToErrno();
1574 goto FINISH;
1575 }
1576 }
1577
Todd Fiala9be50492014-07-01 16:30:53 +00001578 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001579 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1580 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001581 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001582 {
1583 // No such thread. The thread may have exited.
1584 // More error handling may be needed.
1585 if (errno == ESRCH)
1586 {
1587 tids_to_attach.erase(it);
1588 continue;
1589 }
1590 else
1591 {
1592 args->m_error.SetErrorToErrno();
1593 goto FINISH;
1594 }
1595 }
1596
1597 if (!SetDefaultPtraceOpts(tid))
1598 {
1599 args->m_error.SetErrorToErrno();
1600 goto FINISH;
1601 }
1602
1603 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001604 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001605
Matt Kopec085d6ce2013-05-31 22:00:07 +00001606 if (log)
1607 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1608 process.GetThreadList().AddThread(inferior);
1609 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001610 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001611 }
1612 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001613 }
1614
Matt Kopec085d6ce2013-05-31 22:00:07 +00001615 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001616 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001617 monitor->m_pid = pid;
1618 // Let our process instance know the thread has stopped.
1619 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001620 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001621 else
1622 {
1623 args->m_error.SetErrorToGenericError();
1624 args->m_error.SetErrorString("No such process.");
1625 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001626
1627 FINISH:
1628 return args->m_error.Success();
1629}
1630
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001631bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001632ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1633{
1634 long ptrace_opts = 0;
1635
1636 // Have the child raise an event on exit. This is used to keep the child in
1637 // limbo until it is destroyed.
1638 ptrace_opts |= PTRACE_O_TRACEEXIT;
1639
1640 // Have the tracer trace threads which spawn in the inferior process.
1641 // TODO: if we want to support tracing the inferiors' child, add the
1642 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1643 ptrace_opts |= PTRACE_O_TRACECLONE;
1644
1645 // Have the tracer notify us before execve returns
1646 // (needed to disable legacy SIGTRAP generation)
1647 ptrace_opts |= PTRACE_O_TRACEEXEC;
1648
1649 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1650}
1651
1652bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001653ProcessMonitor::MonitorCallback(void *callback_baton,
1654 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001655 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001656 int signal,
1657 int status)
1658{
1659 ProcessMessage message;
1660 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001661 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001662 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001663 bool stop_monitoring;
1664 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001665 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001666
Andrew Kaylor93132f52013-05-28 23:04:25 +00001667 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1668
1669 if (exited)
1670 {
1671 if (log)
1672 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1673 message = ProcessMessage::Exit(pid, status);
1674 process->SendMessage(message);
1675 return pid == process->GetID();
1676 }
1677
Daniel Maleaa35970a2012-11-23 18:09:58 +00001678 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1679 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001680 if (log)
1681 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001682 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1683 if (!monitor->Resume(pid, SIGSTOP)) {
1684 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1685 }
1686 stop_monitoring = false;
1687 } else {
1688 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1689 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001690 // stop the monitor thread if this is the main pid.
1691 if (log)
1692 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1693 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1694 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001695 // If we are going to stop monitoring, we need to notify our process object
1696 if (stop_monitoring)
1697 {
1698 message = ProcessMessage::Exit(pid, status);
1699 process->SendMessage(message);
1700 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001701 }
1702 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001703 else {
1704 switch (info.si_signo)
1705 {
1706 case SIGTRAP:
1707 message = MonitorSIGTRAP(monitor, &info, pid);
1708 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001709
Stephen Wilson84ffe702011-03-30 15:55:52 +00001710 default:
1711 message = MonitorSignal(monitor, &info, pid);
1712 break;
1713 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001714
Stephen Wilson84ffe702011-03-30 15:55:52 +00001715 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001716 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001717 }
1718
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001719 return stop_monitoring;
1720}
1721
1722ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001723ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001724 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001725{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001726 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001727
Andrew Kaylor93132f52013-05-28 23:04:25 +00001728 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1729
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001730 assert(monitor);
1731 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001732
Stephen Wilson84ffe702011-03-30 15:55:52 +00001733 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001734 {
1735 default:
1736 assert(false && "Unexpected SIGTRAP code!");
1737 break;
1738
Matt Kopeca360d7e2013-05-17 19:27:47 +00001739 // TODO: these two cases are required if we want to support tracing
1740 // of the inferiors' children
1741 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1742 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1743
Matt Kopec650648f2013-01-08 16:30:18 +00001744 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1745 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001746 if (log)
1747 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1748
Matt Kopec650648f2013-01-08 16:30:18 +00001749 unsigned long tid = 0;
1750 if (!monitor->GetEventMessage(pid, &tid))
1751 tid = -1;
1752 message = ProcessMessage::NewThread(pid, tid);
1753 break;
1754 }
1755
Matt Kopeca360d7e2013-05-17 19:27:47 +00001756 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001757 if (log)
1758 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1759
1760 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001761 break;
1762
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001763 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1764 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001765 // The inferior process or one of its threads is about to exit.
1766 // Maintain the process or thread in a state of "limbo" until we are
1767 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001768 unsigned long data = 0;
1769 if (!monitor->GetEventMessage(pid, &data))
1770 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001771 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001772 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001773 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001774 break;
1775 }
1776
1777 case 0:
1778 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001779 if (log)
1780 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001781 message = ProcessMessage::Trace(pid);
1782 break;
1783
1784 case SI_KERNEL:
1785 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001786 if (log)
1787 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001788 message = ProcessMessage::Break(pid);
1789 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001790
1791 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001792 if (log)
1793 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001794 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1795 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001796
1797 case SIGTRAP:
1798 case (SIGTRAP | 0x80):
1799 if (log)
1800 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1801 // Ignore these signals until we know more about them
1802 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001803 }
1804
1805 return message;
1806}
1807
Stephen Wilson84ffe702011-03-30 15:55:52 +00001808ProcessMessage
1809ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001810 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001811{
1812 ProcessMessage message;
1813 int signo = info->si_signo;
1814
Andrew Kaylor93132f52013-05-28 23:04:25 +00001815 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1816
Stephen Wilson84ffe702011-03-30 15:55:52 +00001817 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1818 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1819 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1820 //
1821 // IOW, user generated signals never generate what we consider to be a
1822 // "crash".
1823 //
1824 // Similarly, ACK signals generated by this monitor.
1825 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1826 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001827 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001828 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001829 __FUNCTION__,
1830 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1831 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1832 info->si_pid);
1833
Stephen Wilson84ffe702011-03-30 15:55:52 +00001834 if (info->si_pid == getpid())
1835 return ProcessMessage::SignalDelivered(pid, signo);
1836 else
1837 return ProcessMessage::Signal(pid, signo);
1838 }
1839
Andrew Kaylor93132f52013-05-28 23:04:25 +00001840 if (log)
1841 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1842
Chaoren Lin28e57422015-02-03 01:51:25 +00001843 switch (signo)
1844 {
1845 case SIGSEGV:
1846 case SIGILL:
1847 case SIGFPE:
1848 case SIGBUS:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001849 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
Chaoren Lin28e57422015-02-03 01:51:25 +00001850 const auto reason = GetCrashReason(*info);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001851 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1852 }
1853
1854 // Everything else is "normal" and does not require any special action on
1855 // our part.
1856 return ProcessMessage::Signal(pid, signo);
1857}
1858
Andrew Kaylord4d54992013-09-17 00:30:24 +00001859// On Linux, when a new thread is created, we receive to notifications,
1860// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1861// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1862// the new child thread indicating that it has is stopped because we attached.
1863// We have no guarantee of the order in which these arrive, but we need both
1864// before we are ready to proceed. We currently keep a list of threads which
1865// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1866// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1867// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1868
1869bool
1870ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1871{
1872 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1873 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001874 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001875
1876 // Wait for the thread to stop
1877 while (true)
1878 {
1879 int status = -1;
1880 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001881 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001882 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001883 if (status == -1)
1884 {
1885 // If we got interrupted by a signal (in our process, not the
1886 // inferior) try again.
1887 if (errno == EINTR)
1888 continue;
1889 else
1890 {
1891 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001892 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001893 return false; // This is bad, but there's nothing we can do.
1894 }
1895 }
1896
1897 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001898 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001899
Todd Fiala9be50492014-07-01 16:30:53 +00001900 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001901
1902 siginfo_t info;
1903 int ptrace_err;
1904 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1905 {
1906 if (log)
1907 {
1908 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1909 }
1910 return false;
1911 }
1912
1913 // If this is a thread exit, we won't get any more information.
1914 if (WIFEXITED(status))
1915 {
1916 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001917 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001918 return true;
1919 continue;
1920 }
1921
1922 assert(info.si_code == SI_USER);
1923 assert(WSTOPSIG(status) == SIGSTOP);
1924
1925 if (log)
1926 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1927 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1928 return true;
1929 }
1930 return false;
1931}
1932
Andrew Kaylor93132f52013-05-28 23:04:25 +00001933bool
1934ProcessMonitor::StopThread(lldb::tid_t tid)
1935{
1936 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1937
1938 // FIXME: Try to use tgkill or tkill
1939 int ret = tgkill(m_pid, tid, SIGSTOP);
1940 if (log)
1941 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1942
1943 // This can happen if a thread exited while we were trying to stop it. That's OK.
1944 // We'll get the signal for that later.
1945 if (ret < 0)
1946 return false;
1947
1948 // Wait for the thread to stop
1949 while (true)
1950 {
1951 int status = -1;
1952 if (log)
1953 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001954 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001955 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001956 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1957 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001958
Todd Fiala9be50492014-07-01 16:30:53 +00001959 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001960 {
1961 // If we got interrupted by a signal (in our process, not the
1962 // inferior) try again.
1963 if (errno == EINTR)
1964 continue;
1965 else
1966 return false; // This is bad, but there's nothing we can do.
1967 }
1968
1969 // If this is a thread exit, we won't get any more information.
1970 if (WIFEXITED(status))
1971 {
1972 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001973 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001974 return true;
1975 continue;
1976 }
1977
1978 siginfo_t info;
1979 int ptrace_err;
1980 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1981 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001982 // another signal causing a StopAllThreads may have been received
1983 // before wait_pid's group-stop was processed, handle it now
1984 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001985 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001986 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001987
Todd Fiala1b0539c2014-01-27 17:03:57 +00001988 if (log)
1989 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1990 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1991 if (!Resume(wait_pid, SIGSTOP)) {
1992 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1993 }
1994 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001995 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001996
1997 if (log)
1998 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001999 return false;
2000 }
2001
2002 // Handle events from other threads
2003 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00002004 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
2005 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00002006
2007 ProcessMessage message;
2008 if (info.si_signo == SIGTRAP)
2009 message = MonitorSIGTRAP(this, &info, wait_pid);
2010 else
2011 message = MonitorSignal(this, &info, wait_pid);
2012
2013 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
2014
2015 // When a new thread is created, we may get a SIGSTOP for the new thread
2016 // just before we get the SIGTRAP that we use to add the thread to our
2017 // process thread list. We don't need to worry about that signal here.
2018 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
2019
2020 if (!thread)
2021 {
2022 m_process->SendMessage(message);
2023 continue;
2024 }
2025
2026 switch (message.GetKind())
2027 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00002028 case ProcessMessage::eExecMessage:
2029 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00002030 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00002031 case ProcessMessage::eInvalidMessage:
2032 break;
2033
2034 // These need special handling because we don't want to send a
2035 // resume even if we already sent a SIGSTOP to this thread. In
2036 // this case the resume will cause the thread to disappear. It is
2037 // unlikely that we'll ever get eExitMessage here, but the same
2038 // reasoning applies.
2039 case ProcessMessage::eLimboMessage:
2040 case ProcessMessage::eExitMessage:
2041 if (log)
2042 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2043 // SendMessage will set the thread state as needed.
2044 m_process->SendMessage(message);
2045 // If this is the thread we're waiting for, stop waiting. Even
2046 // though this wasn't the signal we expected, it's the last
2047 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00002048 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002049 return true;
2050 break;
2051
Matt Kopecb2910442013-07-09 15:09:45 +00002052 case ProcessMessage::eSignalMessage:
2053 if (log)
2054 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2055 if (WSTOPSIG(status) == SIGSTOP)
2056 {
2057 m_process->AddThreadForInitialStopIfNeeded(tid);
2058 thread->SetState(lldb::eStateStopped);
2059 }
2060 else
2061 {
2062 m_process->SendMessage(message);
2063 // This isn't the stop we were expecting, but the thread is
2064 // stopped. SendMessage will handle processing of this event,
2065 // but we need to resume here to get the stop we are waiting
2066 // for (otherwise the thread will stop again immediately when
2067 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002068 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00002069 Resume(wait_pid, eResumeSignalNone);
2070 }
2071 break;
2072
Andrew Kaylor93132f52013-05-28 23:04:25 +00002073 case ProcessMessage::eSignalDeliveredMessage:
2074 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00002075 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
2076 WIFSTOPPED(status) &&
2077 WSTOPSIG(status) == SIGSTOP &&
2078 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002079 {
2080 if (log)
2081 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
2082 thread->SetState(lldb::eStateStopped);
2083 return true;
2084 }
2085 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00002086 case ProcessMessage::eBreakpointMessage:
2087 case ProcessMessage::eTraceMessage:
2088 case ProcessMessage::eWatchpointMessage:
2089 case ProcessMessage::eCrashMessage:
2090 case ProcessMessage::eNewThreadMessage:
2091 if (log)
2092 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2093 // SendMessage will set the thread state as needed.
2094 m_process->SendMessage(message);
2095 // This isn't the stop we were expecting, but the thread is
2096 // stopped. SendMessage will handle processing of this event,
2097 // but we need to resume here to get the stop we are waiting
2098 // for (otherwise the thread will stop again immediately when
2099 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002100 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002101 Resume(wait_pid, eResumeSignalNone);
2102 break;
2103 }
2104 }
2105 return false;
2106}
2107
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002108void
Johnny Chen25e68e32011-06-14 19:19:50 +00002109ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002110{
Stephen Wilson570243b2011-01-19 01:37:06 +00002111 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002112
Stephen Wilson570243b2011-01-19 01:37:06 +00002113 // We are finised with the arguments and are ready to go. Sync with the
2114 // parent thread and start serving operations on the inferior.
2115 sem_post(&args->m_semaphore);
2116
Michael Sartain704bf892013-10-09 01:28:57 +00002117 for(;;)
2118 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002119 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002120 if (sem_wait(&monitor->m_operation_pending))
2121 {
2122 if (errno == EINTR)
2123 continue;
2124 assert(false && "Unexpected errno from sem_wait");
2125 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002126
Daniel Malea1efb4182013-09-16 23:12:18 +00002127 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002128
Daniel Malea1efb4182013-09-16 23:12:18 +00002129 // notify calling thread that operation is complete
2130 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002131 }
2132}
2133
2134void
2135ProcessMonitor::DoOperation(Operation *op)
2136{
Daniel Malea1efb4182013-09-16 23:12:18 +00002137 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002138
Daniel Malea1efb4182013-09-16 23:12:18 +00002139 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002140
Daniel Malea1efb4182013-09-16 23:12:18 +00002141 // notify operation thread that an operation is ready to be processed
2142 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002143
Daniel Malea1efb4182013-09-16 23:12:18 +00002144 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002145 while (sem_wait(&m_operation_done))
2146 {
2147 if (errno == EINTR)
2148 continue;
2149 assert(false && "Unexpected errno from sem_wait");
2150 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002151}
2152
2153size_t
2154ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2155 Error &error)
2156{
2157 size_t result;
2158 ReadOperation op(vm_addr, buf, size, error, result);
2159 DoOperation(&op);
2160 return result;
2161}
2162
2163size_t
2164ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2165 lldb_private::Error &error)
2166{
2167 size_t result;
2168 WriteOperation op(vm_addr, buf, size, error, result);
2169 DoOperation(&op);
2170 return result;
2171}
2172
2173bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002174ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002175 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002176{
2177 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002178 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002179 DoOperation(&op);
2180 return result;
2181}
2182
2183bool
Matt Kopec7de48462013-03-06 17:20:48 +00002184ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002185 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002186{
2187 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002188 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002189 DoOperation(&op);
2190 return result;
2191}
2192
2193bool
Matt Kopec7de48462013-03-06 17:20:48 +00002194ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002195{
2196 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002197 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002198 DoOperation(&op);
2199 return result;
2200}
2201
2202bool
Matt Kopec7de48462013-03-06 17:20:48 +00002203ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002204{
2205 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002206 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002207 DoOperation(&op);
2208 return result;
2209}
2210
2211bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002212ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2213{
2214 bool result;
2215 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2216 DoOperation(&op);
2217 return result;
2218}
2219
2220bool
Matt Kopec7de48462013-03-06 17:20:48 +00002221ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002222{
2223 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002224 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002225 DoOperation(&op);
2226 return result;
2227}
2228
2229bool
Matt Kopec7de48462013-03-06 17:20:48 +00002230ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002231{
2232 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002233 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002234 DoOperation(&op);
2235 return result;
2236}
2237
2238bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002239ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2240{
2241 bool result;
2242 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2243 DoOperation(&op);
2244 return result;
2245}
2246
2247bool
Richard Mitton0a558352013-10-17 21:14:00 +00002248ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2249{
2250 bool result;
2251 ReadThreadPointerOperation op(tid, &value, result);
2252 DoOperation(&op);
2253 return result;
2254}
2255
2256bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002257ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002258{
2259 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002260 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2261
2262 if (log)
2263 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2264 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002265 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002266 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002267 if (log)
2268 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002269 return result;
2270}
2271
2272bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002273ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002274{
2275 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002276 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002277 DoOperation(&op);
2278 return result;
2279}
2280
2281bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002282ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002283{
Ed Maste4e0999b2014-04-01 18:14:06 +00002284 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002285}
2286
2287bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002288ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002289{
2290 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002291 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002292 DoOperation(&op);
2293 return result;
2294}
2295
2296bool
2297ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2298{
2299 bool result;
2300 EventMessageOperation op(tid, message, result);
2301 DoOperation(&op);
2302 return result;
2303}
2304
Greg Clayton743ecf42012-10-16 20:20:18 +00002305lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002306ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002307{
Greg Clayton28041352011-11-29 20:50:10 +00002308 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002309 if (tid != LLDB_INVALID_THREAD_ID)
2310 {
2311 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002312 DoOperation(&op);
2313 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002314 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002315}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002316
2317bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002318ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2319{
Peter Collingbourne62343202011-06-14 03:55:54 +00002320 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002321
2322 if (target_fd == -1)
2323 return false;
2324
Pavel Labath493c3a12015-02-04 10:36:57 +00002325 if (dup2(target_fd, fd) == -1)
2326 return false;
2327
2328 return (close(target_fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002329}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002330
2331void
2332ProcessMonitor::StopMonitoringChildProcess()
2333{
Zachary Turneracee96a2014-09-23 18:32:09 +00002334 if (m_monitor_thread.IsJoinable())
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002335 {
Zachary Turner39de3112014-09-09 20:54:56 +00002336 m_monitor_thread.Cancel();
2337 m_monitor_thread.Join(nullptr);
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002338 }
2339}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002340
2341void
2342ProcessMonitor::StopMonitor()
2343{
2344 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002345 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002346 sem_destroy(&m_operation_pending);
2347 sem_destroy(&m_operation_done);
Pavel Labath3a2da9e2015-02-06 11:32:52 +00002348 if (m_terminal_fd >= 0) {
2349 close(m_terminal_fd);
2350 m_terminal_fd = -1;
2351 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00002352}
2353
2354void
Greg Clayton743ecf42012-10-16 20:20:18 +00002355ProcessMonitor::StopOpThread()
2356{
Zachary Turneracee96a2014-09-23 18:32:09 +00002357 if (!m_operation_thread.IsJoinable())
Greg Clayton743ecf42012-10-16 20:20:18 +00002358 return;
2359
Zachary Turner39de3112014-09-09 20:54:56 +00002360 m_operation_thread.Cancel();
2361 m_operation_thread.Join(nullptr);
Greg Clayton743ecf42012-10-16 20:20:18 +00002362}