blob: dff4e8d9ce92d40d3a4172a12875bb026614eb2f [file] [log] [blame]
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001//===-- ProcessMonitor.cpp ------------------------------------ -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Stephen Wilsone6f9f662010-07-24 02:19:04 +000012// C Includes
13#include <errno.h>
14#include <poll.h>
15#include <string.h>
Daniel Maleaa85e6b62012-12-07 22:21:08 +000016#include <stdint.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000017#include <unistd.h>
Todd Fiala42079682014-08-27 16:05:26 +000018#include <elf.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000019
20// C++ Includes
21// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000022#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000023#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000024#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000025#include "lldb/Core/Scalar.h"
26#include "lldb/Host/Host.h"
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +000027#include "lldb/Host/HostNativeThread.h"
Zachary Turner39de3112014-09-09 20:54:56 +000028#include "lldb/Host/HostThread.h"
29#include "lldb/Host/ThreadLauncher.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000030#include "lldb/Target/Thread.h"
31#include "lldb/Target/RegisterContext.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000032#include "lldb/Target/UnixSignals.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000033#include "lldb/Utility/PseudoTerminal.h"
34
Chaoren Lin28e57422015-02-03 01:51:25 +000035#include "Plugins/Process/POSIX/CrashReason.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000036#include "Plugins/Process/POSIX/POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037#include "ProcessLinux.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000038#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000039#include "ProcessMonitor.h"
Tamas Berghammer1e209fc2015-03-13 11:36:47 +000040#include "Procfs.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000041
Tamas Berghammerd8584872015-02-06 10:57:40 +000042// System includes - They have to be included after framework includes because they define some
43// macros which collide with variable names in other modules
Tamas Berghammerd8584872015-02-06 10:57:40 +000044#include <sys/personality.h>
45#include <sys/ptrace.h>
46#include <sys/socket.h>
47#include <sys/syscall.h>
48#include <sys/types.h>
49#include <sys/uio.h>
50#include <sys/user.h>
51#include <sys/wait.h>
52
Todd Fialacacde7d2014-09-27 16:54:22 +000053#ifdef __ANDROID__
54#define __ptrace_request int
55#define PT_DETACH PTRACE_DETACH
56#endif
57
Greg Clayton386ff182011-11-05 01:09:16 +000058#define DEBUG_PTRACE_MAXBYTES 20
59
Matt Kopec58c0b962013-03-20 20:34:35 +000060// Support ptrace extensions even when compiled without required kernel support
61#ifndef PTRACE_GETREGSET
62 #define PTRACE_GETREGSET 0x4204
63#endif
64#ifndef PTRACE_SETREGSET
65 #define PTRACE_SETREGSET 0x4205
66#endif
Richard Mitton0a558352013-10-17 21:14:00 +000067#ifndef PTRACE_GET_THREAD_AREA
68 #define PTRACE_GET_THREAD_AREA 25
69#endif
70#ifndef PTRACE_ARCH_PRCTL
71 #define PTRACE_ARCH_PRCTL 30
72#endif
73#ifndef ARCH_GET_FS
74 #define ARCH_SET_GS 0x1001
75 #define ARCH_SET_FS 0x1002
76 #define ARCH_GET_FS 0x1003
77 #define ARCH_GET_GS 0x1004
78#endif
79
Todd Fiala0bce1b62014-08-17 00:10:50 +000080#define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff
Matt Kopec58c0b962013-03-20 20:34:35 +000081
Todd Fialadbec1ff2014-09-04 16:08:20 +000082#define LLDB_PTRACE_NT_ARM_TLS 0x401 // ARM TLS register
83
Matt Kopece9ea0da2013-05-07 19:29:28 +000084// Support hardware breakpoints in case it has not been defined
85#ifndef TRAP_HWBKPT
86 #define TRAP_HWBKPT 4
87#endif
88
Andrew Kaylor93132f52013-05-28 23:04:25 +000089// Try to define a macro to encapsulate the tgkill syscall
90// fall back on kill() if tgkill isn't available
Chaoren Linc9346592015-02-28 00:20:16 +000091#define tgkill(pid, tid, sig) \
92 syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig)
Andrew Kaylor93132f52013-05-28 23:04:25 +000093
Stephen Wilsone6f9f662010-07-24 02:19:04 +000094using namespace lldb_private;
95
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +000096static Operation* EXIT_OPERATION = nullptr;
97
Johnny Chen0d5f2d42011-10-18 18:09:30 +000098// FIXME: this code is host-dependent with respect to types and
99// endianness and needs to be fixed. For example, lldb::addr_t is
100// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
101// 32-bit pointer arguments. This code uses casts to work around the
102// problem.
103
104// We disable the tracing of ptrace calls for integration builds to
105// avoid the additional indirection and checks.
106#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
107
Greg Clayton386ff182011-11-05 01:09:16 +0000108static void
109DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
110{
111 uint8_t *ptr = (uint8_t *)bytes;
112 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
113 for(uint32_t i=0; i<loop_count; i++)
114 {
115 s.Printf ("[%x]", *ptr);
116 ptr++;
117 }
118}
119
Matt Kopec58c0b962013-03-20 20:34:35 +0000120static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +0000121{
122 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000123 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000124 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000125
126 if (verbose_log)
127 {
128 switch(req)
129 {
130 case PTRACE_POKETEXT:
131 {
132 DisplayBytes(buf, &data, 8);
133 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
134 break;
135 }
Greg Clayton542e4072012-09-07 17:49:29 +0000136 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000137 {
138 DisplayBytes(buf, &data, 8);
139 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
140 break;
141 }
Greg Clayton542e4072012-09-07 17:49:29 +0000142 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000143 {
144 DisplayBytes(buf, &data, 8);
145 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
146 break;
147 }
Todd Fiala6ac1be42014-08-21 16:34:03 +0000148#if !defined (__arm64__) && !defined (__aarch64__)
Greg Clayton542e4072012-09-07 17:49:29 +0000149 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000150 {
Matt Kopec7de48462013-03-06 17:20:48 +0000151 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000152 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
153 break;
154 }
155 case PTRACE_SETFPREGS:
156 {
Matt Kopec7de48462013-03-06 17:20:48 +0000157 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000158 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
159 break;
160 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000161#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000162 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000163 {
164 DisplayBytes(buf, data, sizeof(siginfo_t));
165 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
166 break;
167 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000168 case PTRACE_SETREGSET:
169 {
170 // Extract iov_base from data, which is a pointer to the struct IOVEC
171 DisplayBytes(buf, *(void **)data, data_size);
172 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
173 break;
174 }
Greg Clayton386ff182011-11-05 01:09:16 +0000175 default:
176 {
177 }
178 }
179 }
180}
181
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000182// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000183// 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 +0000184extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000185PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000186 const char* reqName, const char* file, int line)
187{
Greg Clayton386ff182011-11-05 01:09:16 +0000188 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000189
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000190 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000191
Matt Kopec7de48462013-03-06 17:20:48 +0000192 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000193
194 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000195 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000196 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000197 else
Todd Fiala4507f062014-02-27 20:46:12 +0000198 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000199
Ed Mastec099c952014-02-24 14:07:45 +0000200 if (log)
201 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
202 reqName, pid, addr, data, data_size, result, file, line);
203
Matt Kopec7de48462013-03-06 17:20:48 +0000204 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000205
Matt Kopec7de48462013-03-06 17:20:48 +0000206 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000207 {
208 const char* str;
209 switch (errno)
210 {
211 case ESRCH: str = "ESRCH"; break;
212 case EINVAL: str = "EINVAL"; break;
213 case EBUSY: str = "EBUSY"; break;
214 case EPERM: str = "EPERM"; break;
215 default: str = "<unknown>";
216 }
217 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
218 }
219
220 return result;
221}
222
Matt Kopec7de48462013-03-06 17:20:48 +0000223// Wrapper for ptrace when logging is not required.
224// Sets errno to 0 prior to calling ptrace.
225extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000226PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000227{
Matt Kopec58c0b962013-03-20 20:34:35 +0000228 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000229 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000230 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
231 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
232 else
233 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000234 return result;
235}
236
237#define PTRACE(req, pid, addr, data, data_size) \
238 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000239#else
Matt Kopec7de48462013-03-06 17:20:48 +0000240 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000241#endif
242
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000243//------------------------------------------------------------------------------
244// Static implementations of ProcessMonitor::ReadMemory and
245// ProcessMonitor::WriteMemory. This enables mutual recursion between these
246// functions without needed to go thru the thread funnel.
247
248static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000249DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000250 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
251{
Greg Clayton542e4072012-09-07 17:49:29 +0000252 // ptrace word size is determined by the host, not the child
253 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000254 unsigned char *dst = static_cast<unsigned char*>(buf);
255 size_t bytes_read;
256 size_t remainder;
257 long data;
258
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000259 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000260 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000261 ProcessPOSIXLog::IncNestLevel();
262 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000263 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000264 pid, word_size, (void*)vm_addr, buf, size);
265
266 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000267 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
268 {
269 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000270 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
271 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000272 {
273 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000274 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000275 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000276 return bytes_read;
277 }
278
279 remainder = size - bytes_read;
280 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000281
282 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000283 for (unsigned i = 0; i < remainder; ++i)
284 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000285
Johnny Chen30213ff2012-01-05 19:17:38 +0000286 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
287 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
288 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
289 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000290 {
291 uintptr_t print_dst = 0;
292 // Format bytes from data by moving into print_dst for log output
293 for (unsigned i = 0; i < remainder; ++i)
294 print_dst |= (((data >> i*8) & 0xFF) << i*8);
295 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
296 (void*)vm_addr, print_dst, (unsigned long)data);
297 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000298
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000299 vm_addr += word_size;
300 dst += word_size;
301 }
302
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000303 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000304 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000305 return bytes_read;
306}
307
308static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000309DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000310 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
311{
Greg Clayton542e4072012-09-07 17:49:29 +0000312 // ptrace word size is determined by the host, not the child
313 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000314 const unsigned char *src = static_cast<const unsigned char*>(buf);
315 size_t bytes_written = 0;
316 size_t remainder;
317
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000318 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000319 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000320 ProcessPOSIXLog::IncNestLevel();
321 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000322 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000323 pid, word_size, (void*)vm_addr, buf, size);
324
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000325 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
326 {
327 remainder = size - bytes_written;
328 remainder = remainder > word_size ? word_size : remainder;
329
330 if (remainder == word_size)
331 {
332 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000333 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000334 for (unsigned i = 0; i < word_size; ++i)
335 data |= (unsigned long)src[i] << i*8;
336
Johnny Chen30213ff2012-01-05 19:17:38 +0000337 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
338 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
339 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
340 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000341 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
342 (void*)vm_addr, *(unsigned long*)src, data);
343
Matt Kopec7de48462013-03-06 17:20:48 +0000344 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000345 {
346 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000347 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000348 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000349 return bytes_written;
350 }
351 }
352 else
353 {
354 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000355 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000356 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000357 {
358 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000359 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000360 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000361 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000362
363 memcpy(buff, src, remainder);
364
Greg Clayton542e4072012-09-07 17:49:29 +0000365 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000366 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000367 {
368 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000369 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000370 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000371 }
372
Johnny Chen30213ff2012-01-05 19:17:38 +0000373 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
374 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
375 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
376 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000377 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
378 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000379 }
380
381 vm_addr += word_size;
382 src += word_size;
383 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000384 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000385 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000386 return bytes_written;
387}
388
Stephen Wilson26977162011-03-23 02:14:42 +0000389// Simple helper function to ensure flags are enabled on the given file
390// descriptor.
391static bool
392EnsureFDFlags(int fd, int flags, Error &error)
393{
394 int status;
395
396 if ((status = fcntl(fd, F_GETFL)) == -1)
397 {
398 error.SetErrorToErrno();
399 return false;
400 }
401
402 if (fcntl(fd, F_SETFL, status | flags) == -1)
403 {
404 error.SetErrorToErrno();
405 return false;
406 }
407
408 return true;
409}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000410
411//------------------------------------------------------------------------------
412/// @class Operation
413/// @brief Represents a ProcessMonitor operation.
414///
415/// Under Linux, it is not possible to ptrace() from any other thread but the
416/// one that spawned or attached to the process from the start. Therefore, when
417/// a ProcessMonitor is asked to deliver or change the state of an inferior
418/// process the operation must be "funneled" to a specific thread to perform the
419/// task. The Operation class provides an abstract base for all services the
420/// ProcessMonitor must perform via the single virtual function Execute, thus
421/// encapsulating the code that needs to run in the privileged context.
422class Operation
423{
424public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000425 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000426 virtual void Execute(ProcessMonitor *monitor) = 0;
427};
428
429//------------------------------------------------------------------------------
430/// @class ReadOperation
431/// @brief Implements ProcessMonitor::ReadMemory.
432class ReadOperation : public Operation
433{
434public:
435 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
436 Error &error, size_t &result)
437 : m_addr(addr), m_buff(buff), m_size(size),
438 m_error(error), m_result(result)
439 { }
440
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000441 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000442
443private:
444 lldb::addr_t m_addr;
445 void *m_buff;
446 size_t m_size;
447 Error &m_error;
448 size_t &m_result;
449};
450
451void
452ReadOperation::Execute(ProcessMonitor *monitor)
453{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000454 lldb::pid_t pid = monitor->GetPID();
455
Greg Clayton542e4072012-09-07 17:49:29 +0000456 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000457}
458
459//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000460/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000461/// @brief Implements ProcessMonitor::WriteMemory.
462class WriteOperation : public Operation
463{
464public:
465 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
466 Error &error, size_t &result)
467 : m_addr(addr), m_buff(buff), m_size(size),
468 m_error(error), m_result(result)
469 { }
470
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000471 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000472
473private:
474 lldb::addr_t m_addr;
475 const void *m_buff;
476 size_t m_size;
477 Error &m_error;
478 size_t &m_result;
479};
480
481void
482WriteOperation::Execute(ProcessMonitor *monitor)
483{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000484 lldb::pid_t pid = monitor->GetPID();
485
Greg Clayton542e4072012-09-07 17:49:29 +0000486 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000487}
488
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000489
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000490//------------------------------------------------------------------------------
491/// @class ReadRegOperation
492/// @brief Implements ProcessMonitor::ReadRegisterValue.
493class ReadRegOperation : public Operation
494{
495public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000496 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000497 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000498 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000499 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000500 { }
501
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000502 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000503
504private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000505 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000506 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000507 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000508 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000509 bool &m_result;
510};
511
512void
513ReadRegOperation::Execute(ProcessMonitor *monitor)
514{
Todd Fiala49131cf2014-09-12 16:57:28 +0000515#if defined (__arm64__) || defined (__aarch64__)
516 if (m_offset > sizeof(struct user_pt_regs))
517 {
518 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
519 if (offset > sizeof(struct user_fpsimd_state))
520 {
521 m_result = false;
522 }
523 else
524 {
525 elf_fpregset_t regs;
526 int regset = NT_FPREGSET;
527 struct iovec ioVec;
528
529 ioVec.iov_base = &regs;
530 ioVec.iov_len = sizeof regs;
531 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
532 m_result = false;
533 else
534 {
535 m_result = true;
536 m_value.SetBytes((void *)(((unsigned char *)(&regs)) + offset), 16, monitor->GetProcess().GetByteOrder());
537 }
538 }
539 }
540 else
541 {
542 elf_gregset_t regs;
543 int regset = NT_PRSTATUS;
544 struct iovec ioVec;
545
546 ioVec.iov_base = &regs;
547 ioVec.iov_len = sizeof regs;
548 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
549 m_result = false;
550 else
551 {
552 m_result = true;
553 m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, monitor->GetProcess().GetByteOrder());
554 }
555 }
556#else
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000557 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000558
559 // Set errno to zero so that we can detect a failed peek.
560 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000561 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
562 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000563 m_result = false;
564 else
565 {
566 m_value = data;
567 m_result = true;
568 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000569 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000570 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000571 m_reg_name, data);
Todd Fiala49131cf2014-09-12 16:57:28 +0000572#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000573}
574
575//------------------------------------------------------------------------------
576/// @class WriteRegOperation
577/// @brief Implements ProcessMonitor::WriteRegisterValue.
578class WriteRegOperation : public Operation
579{
580public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000581 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000582 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000583 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000584 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000585 { }
586
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000587 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000588
589private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000590 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000591 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000592 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000593 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000594 bool &m_result;
595};
596
597void
598WriteRegOperation::Execute(ProcessMonitor *monitor)
599{
Todd Fiala49131cf2014-09-12 16:57:28 +0000600#if defined (__arm64__) || defined (__aarch64__)
601 if (m_offset > sizeof(struct user_pt_regs))
602 {
603 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
604 if (offset > sizeof(struct user_fpsimd_state))
605 {
606 m_result = false;
607 }
608 else
609 {
610 elf_fpregset_t regs;
611 int regset = NT_FPREGSET;
612 struct iovec ioVec;
613
614 ioVec.iov_base = &regs;
615 ioVec.iov_len = sizeof regs;
616 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
617 m_result = false;
618 else
619 {
620 ::memcpy((void *)(((unsigned char *)(&regs)) + offset), m_value.GetBytes(), 16);
621 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
622 m_result = false;
623 else
624 m_result = true;
625 }
626 }
627 }
628 else
629 {
630 elf_gregset_t regs;
631 int regset = NT_PRSTATUS;
632 struct iovec ioVec;
633
634 ioVec.iov_base = &regs;
635 ioVec.iov_len = sizeof regs;
636 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
637 m_result = false;
638 else
639 {
640 ::memcpy((void *)(((unsigned char *)(&regs)) + m_offset), m_value.GetBytes(), 8);
641 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
642 m_result = false;
643 else
644 m_result = true;
645 }
646 }
647#else
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000648 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000649 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000650
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000651 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000652
653 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000654 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000655 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000656 m_result = false;
657 else
658 m_result = true;
Todd Fiala49131cf2014-09-12 16:57:28 +0000659#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000660}
661
662//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000663/// @class ReadGPROperation
664/// @brief Implements ProcessMonitor::ReadGPR.
665class ReadGPROperation : public Operation
666{
667public:
Matt Kopec7de48462013-03-06 17:20:48 +0000668 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
669 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000670 { }
671
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000672 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000673
674private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000675 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000676 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000677 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000678 bool &m_result;
679};
680
681void
682ReadGPROperation::Execute(ProcessMonitor *monitor)
683{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000684#if defined (__arm64__) || defined (__aarch64__)
685 int regset = NT_PRSTATUS;
686 struct iovec ioVec;
687
688 ioVec.iov_base = m_buf;
689 ioVec.iov_len = m_buf_size;
690 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000691 m_result = false;
692 else
693 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000694#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000695 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
696 m_result = false;
697 else
698 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000699#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000700}
701
702//------------------------------------------------------------------------------
703/// @class ReadFPROperation
704/// @brief Implements ProcessMonitor::ReadFPR.
705class ReadFPROperation : public Operation
706{
707public:
Matt Kopec7de48462013-03-06 17:20:48 +0000708 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
709 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000710 { }
711
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000712 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000713
714private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000715 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000716 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000717 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000718 bool &m_result;
719};
720
721void
722ReadFPROperation::Execute(ProcessMonitor *monitor)
723{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000724#if defined (__arm64__) || defined (__aarch64__)
725 int regset = NT_FPREGSET;
726 struct iovec ioVec;
727
728 ioVec.iov_base = m_buf;
729 ioVec.iov_len = m_buf_size;
730 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000731 m_result = false;
732 else
733 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000734#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000735 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
736 m_result = false;
737 else
738 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000739#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000740}
741
742//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000743/// @class ReadRegisterSetOperation
744/// @brief Implements ProcessMonitor::ReadRegisterSet.
745class ReadRegisterSetOperation : public Operation
746{
747public:
748 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
749 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
750 { }
751
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000752 void Execute(ProcessMonitor *monitor) override;
Matt Kopec58c0b962013-03-20 20:34:35 +0000753
754private:
755 lldb::tid_t m_tid;
756 void *m_buf;
757 size_t m_buf_size;
758 const unsigned int m_regset;
759 bool &m_result;
760};
761
762void
763ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
764{
765 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
766 m_result = false;
767 else
768 m_result = true;
769}
770
771//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000772/// @class WriteGPROperation
773/// @brief Implements ProcessMonitor::WriteGPR.
774class WriteGPROperation : public Operation
775{
776public:
Matt Kopec7de48462013-03-06 17:20:48 +0000777 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
778 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000779 { }
780
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000781 void Execute(ProcessMonitor *monitor) override;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000782
783private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000784 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000785 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000786 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000787 bool &m_result;
788};
789
790void
791WriteGPROperation::Execute(ProcessMonitor *monitor)
792{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000793#if defined (__arm64__) || defined (__aarch64__)
794 int regset = NT_PRSTATUS;
795 struct iovec ioVec;
796
797 ioVec.iov_base = m_buf;
798 ioVec.iov_len = m_buf_size;
799 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000800 m_result = false;
801 else
802 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000803#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000804 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
805 m_result = false;
806 else
807 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000808#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000809}
810
811//------------------------------------------------------------------------------
812/// @class WriteFPROperation
813/// @brief Implements ProcessMonitor::WriteFPR.
814class WriteFPROperation : public Operation
815{
816public:
Matt Kopec7de48462013-03-06 17:20:48 +0000817 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
818 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000819 { }
820
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000821 void Execute(ProcessMonitor *monitor) override;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000822
823private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000824 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000825 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000826 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000827 bool &m_result;
828};
829
830void
831WriteFPROperation::Execute(ProcessMonitor *monitor)
832{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000833#if defined (__arm64__) || defined (__aarch64__)
834 int regset = NT_FPREGSET;
835 struct iovec ioVec;
836
837 ioVec.iov_base = m_buf;
838 ioVec.iov_len = m_buf_size;
839 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000840 m_result = false;
841 else
842 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000843#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000844 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
845 m_result = false;
846 else
847 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000848#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000849}
850
851//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000852/// @class WriteRegisterSetOperation
853/// @brief Implements ProcessMonitor::WriteRegisterSet.
854class WriteRegisterSetOperation : public Operation
855{
856public:
857 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
858 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
859 { }
860
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000861 void Execute(ProcessMonitor *monitor) override;
Matt Kopec58c0b962013-03-20 20:34:35 +0000862
863private:
864 lldb::tid_t m_tid;
865 void *m_buf;
866 size_t m_buf_size;
867 const unsigned int m_regset;
868 bool &m_result;
869};
870
871void
872WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
873{
874 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
875 m_result = false;
876 else
877 m_result = true;
878}
879
880//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000881/// @class ReadThreadPointerOperation
882/// @brief Implements ProcessMonitor::ReadThreadPointer.
883class ReadThreadPointerOperation : public Operation
884{
885public:
886 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
887 : m_tid(tid), m_addr(addr), m_result(result)
888 { }
889
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000890 void Execute(ProcessMonitor *monitor) override;
Richard Mitton0a558352013-10-17 21:14:00 +0000891
892private:
893 lldb::tid_t m_tid;
894 lldb::addr_t *m_addr;
895 bool &m_result;
896};
897
898void
899ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
900{
901 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
902 if (log)
903 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
904
905 // The process for getting the thread area on Linux is
906 // somewhat... obscure. There's several different ways depending on
907 // what arch you're on, and what kernel version you have.
908
909 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
910 switch(arch.GetMachine())
911 {
Todd Fiala42079682014-08-27 16:05:26 +0000912 case llvm::Triple::aarch64:
913 {
Todd Fialadbec1ff2014-09-04 16:08:20 +0000914 int regset = LLDB_PTRACE_NT_ARM_TLS;
Todd Fiala42079682014-08-27 16:05:26 +0000915 struct iovec ioVec;
916
917 ioVec.iov_base = m_addr;
918 ioVec.iov_len = sizeof(lldb::addr_t);
919 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, ioVec.iov_len) < 0)
920 m_result = false;
921 else
922 m_result = true;
923 break;
924 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000925#if defined(__i386__) || defined(__x86_64__)
926 // Note that struct user below has a field named i387 which is x86-specific.
927 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000928 case llvm::Triple::x86:
929 {
930 // Find the GS register location for our host architecture.
931 size_t gs_user_offset = offsetof(struct user, regs);
932#ifdef __x86_64__
933 gs_user_offset += offsetof(struct user_regs_struct, gs);
934#endif
935#ifdef __i386__
936 gs_user_offset += offsetof(struct user_regs_struct, xgs);
937#endif
938
939 // Read the GS register value to get the selector.
940 errno = 0;
941 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
942 if (errno)
943 {
944 m_result = false;
945 break;
946 }
947
948 // Read the LDT base for that selector.
949 uint32_t tmp[4];
950 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
951 *m_addr = tmp[1];
952 break;
953 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000954#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000955 case llvm::Triple::x86_64:
956 // Read the FS register base.
957 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
958 break;
959 default:
960 m_result = false;
961 break;
962 }
963}
964
965//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000966/// @class ResumeOperation
967/// @brief Implements ProcessMonitor::Resume.
968class ResumeOperation : public Operation
969{
970public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000971 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
972 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000973
Tamas Berghammerd542efd2015-03-25 15:37:56 +0000974 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000975
976private:
977 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000978 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000979 bool &m_result;
980};
981
982void
983ResumeOperation::Execute(ProcessMonitor *monitor)
984{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000985 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000986
987 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
988 data = m_signo;
989
Matt Kopec7de48462013-03-06 17:20:48 +0000990 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000991 {
992 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
993
994 if (log)
995 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000996 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000997 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000998 else
999 m_result = true;
1000}
1001
1002//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +00001003/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001004/// @brief Implements ProcessMonitor::SingleStep.
1005class SingleStepOperation : public Operation
1006{
1007public:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001008 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
1009 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001010
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001011 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001012
1013private:
1014 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001015 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001016 bool &m_result;
1017};
1018
1019void
1020SingleStepOperation::Execute(ProcessMonitor *monitor)
1021{
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001022 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001023
1024 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
1025 data = m_signo;
1026
Matt Kopec7de48462013-03-06 17:20:48 +00001027 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001028 m_result = false;
1029 else
1030 m_result = true;
1031}
1032
1033//------------------------------------------------------------------------------
1034/// @class SiginfoOperation
1035/// @brief Implements ProcessMonitor::GetSignalInfo.
1036class SiginfoOperation : public Operation
1037{
1038public:
Daniel Maleaa35970a2012-11-23 18:09:58 +00001039 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
1040 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001041
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001042 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001043
1044private:
1045 lldb::tid_t m_tid;
1046 void *m_info;
1047 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001048 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001049};
1050
1051void
1052SiginfoOperation::Execute(ProcessMonitor *monitor)
1053{
Matt Kopec7de48462013-03-06 17:20:48 +00001054 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001055 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001056 m_err = errno;
1057 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001058 else
1059 m_result = true;
1060}
1061
1062//------------------------------------------------------------------------------
1063/// @class EventMessageOperation
1064/// @brief Implements ProcessMonitor::GetEventMessage.
1065class EventMessageOperation : public Operation
1066{
1067public:
1068 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
1069 : m_tid(tid), m_message(message), m_result(result) { }
1070
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001071 void Execute(ProcessMonitor *monitor) override;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001072
1073private:
1074 lldb::tid_t m_tid;
1075 unsigned long *m_message;
1076 bool &m_result;
1077};
1078
1079void
1080EventMessageOperation::Execute(ProcessMonitor *monitor)
1081{
Matt Kopec7de48462013-03-06 17:20:48 +00001082 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001083 m_result = false;
1084 else
1085 m_result = true;
1086}
1087
1088//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +00001089/// @class DetachOperation
1090/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +00001091class DetachOperation : public Operation
1092{
1093public:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001094 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +00001095
Tamas Berghammerd542efd2015-03-25 15:37:56 +00001096 void Execute(ProcessMonitor *monitor) override;
Greg Clayton28041352011-11-29 20:50:10 +00001097
1098private:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001099 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +00001100 Error &m_error;
1101};
1102
1103void
1104DetachOperation::Execute(ProcessMonitor *monitor)
1105{
Matt Kopec085d6ce2013-05-31 22:00:07 +00001106 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +00001107 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +00001108}
1109
Johnny Chen25e68e32011-06-14 19:19:50 +00001110ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
1111 : m_monitor(monitor)
1112{
1113 sem_init(&m_semaphore, 0, 0);
1114}
1115
1116ProcessMonitor::OperationArgs::~OperationArgs()
1117{
1118 sem_destroy(&m_semaphore);
1119}
1120
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001121ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
1122 lldb_private::Module *module,
1123 char const **argv,
1124 char const **envp,
1125 const char *stdin_path,
1126 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001127 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001128 const char *working_dir,
1129 const lldb_private::ProcessLaunchInfo &launch_info)
Johnny Chen25e68e32011-06-14 19:19:50 +00001130 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001131 m_module(module),
1132 m_argv(argv),
1133 m_envp(envp),
1134 m_stdin_path(stdin_path),
1135 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +00001136 m_stderr_path(stderr_path),
Todd Fiala0bce1b62014-08-17 00:10:50 +00001137 m_working_dir(working_dir),
1138 m_launch_info(launch_info)
1139{
1140}
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001141
1142ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +00001143{ }
1144
1145ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
1146 lldb::pid_t pid)
1147 : OperationArgs(monitor), m_pid(pid) { }
1148
1149ProcessMonitor::AttachArgs::~AttachArgs()
1150{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001151
1152//------------------------------------------------------------------------------
1153/// The basic design of the ProcessMonitor is built around two threads.
1154///
1155/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
1156/// for changes in the debugee state. When a change is detected a
1157/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1158/// "drives" state changes in the debugger.
1159///
1160/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001161/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001162/// operations such as register reads/writes, stepping, etc. See the comments
1163/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001164ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001165 Module *module,
1166 const char *argv[],
1167 const char *envp[],
1168 const char *stdin_path,
1169 const char *stdout_path,
1170 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001171 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001172 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001173 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001174 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001175 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001176 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001177 m_pid(LLDB_INVALID_PROCESS_ID),
1178 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001179 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001180{
Daniel Malea1efb4182013-09-16 23:12:18 +00001181 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1182 stdin_path, stdout_path, stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001183 working_dir, launch_info));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001184
Daniel Malea1efb4182013-09-16 23:12:18 +00001185 sem_init(&m_operation_pending, 0, 0);
1186 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001187
Johnny Chen25e68e32011-06-14 19:19:50 +00001188 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001189 if (!error.Success())
1190 return;
1191
1192WAIT_AGAIN:
1193 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001194 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001195 {
1196 if (errno == EINTR)
1197 goto WAIT_AGAIN;
1198 else
1199 {
1200 error.SetErrorToErrno();
1201 return;
1202 }
1203 }
1204
1205 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001206 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001207 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001208 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001209 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001210 return;
1211 }
1212
1213 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001214 m_monitor_thread = Host::StartMonitoringChildProcess(
1215 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001216 if (!m_monitor_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001217 {
1218 error.SetErrorToGenericError();
1219 error.SetErrorString("Process launch failed.");
1220 return;
1221 }
1222}
1223
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001224ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001225 lldb::pid_t pid,
1226 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001227 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001228 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001229 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001230 m_pid(LLDB_INVALID_PROCESS_ID),
1231 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001232 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001233{
Daniel Malea1efb4182013-09-16 23:12:18 +00001234 sem_init(&m_operation_pending, 0, 0);
1235 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001236
Daniel Malea1efb4182013-09-16 23:12:18 +00001237 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001238
1239 StartAttachOpThread(args.get(), error);
1240 if (!error.Success())
1241 return;
1242
1243WAIT_AGAIN:
1244 // Wait for the operation thread to initialize.
1245 if (sem_wait(&args->m_semaphore))
1246 {
1247 if (errno == EINTR)
1248 goto WAIT_AGAIN;
1249 else
1250 {
1251 error.SetErrorToErrno();
1252 return;
1253 }
1254 }
1255
Greg Clayton743ecf42012-10-16 20:20:18 +00001256 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001257 if (!args->m_error.Success())
1258 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001259 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001260 error = args->m_error;
1261 return;
1262 }
1263
1264 // Finally, start monitoring the child process for change in state.
1265 m_monitor_thread = Host::StartMonitoringChildProcess(
1266 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001267 if (!m_monitor_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001268 {
1269 error.SetErrorToGenericError();
1270 error.SetErrorString("Process attach failed.");
1271 return;
1272 }
1273}
1274
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001275ProcessMonitor::~ProcessMonitor()
1276{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001277 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001278}
1279
1280//------------------------------------------------------------------------------
1281// Thread setup and tear down.
1282void
Johnny Chen25e68e32011-06-14 19:19:50 +00001283ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001284{
1285 static const char *g_thread_name = "lldb.process.linux.operation";
1286
Zachary Turneracee96a2014-09-23 18:32:09 +00001287 if (m_operation_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001288 return;
1289
Zachary Turner39de3112014-09-09 20:54:56 +00001290 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001291}
1292
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001293void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001294ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001295{
1296 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1297
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001298 if (!Launch(args)) {
1299 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001300 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001301 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001302
Stephen Wilson570243b2011-01-19 01:37:06 +00001303 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001304 return NULL;
1305}
1306
1307bool
1308ProcessMonitor::Launch(LaunchArgs *args)
1309{
Todd Fiala0bce1b62014-08-17 00:10:50 +00001310 assert (args && "null args");
1311 if (!args)
1312 return false;
1313
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001314 ProcessMonitor *monitor = args->m_monitor;
1315 ProcessLinux &process = monitor->GetProcess();
1316 const char **argv = args->m_argv;
1317 const char **envp = args->m_envp;
1318 const char *stdin_path = args->m_stdin_path;
1319 const char *stdout_path = args->m_stdout_path;
1320 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001321 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001322
1323 lldb_utility::PseudoTerminal terminal;
1324 const size_t err_len = 1024;
1325 char err_str[err_len];
1326 lldb::pid_t pid;
1327
1328 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001329 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001330
Stephen Wilson57740ec2011-01-15 00:12:41 +00001331 // Propagate the environment if one is not supplied.
1332 if (envp == NULL || envp[0] == NULL)
1333 envp = const_cast<const char **>(environ);
1334
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001335 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001336 {
1337 args->m_error.SetErrorToGenericError();
1338 args->m_error.SetErrorString("Process fork failed.");
1339 goto FINISH;
1340 }
1341
Peter Collingbourne6a520222011-06-14 03:55:58 +00001342 // Recognized child exit status codes.
1343 enum {
1344 ePtraceFailed = 1,
1345 eDupStdinFailed,
1346 eDupStdoutFailed,
1347 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001348 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001349 eExecFailed,
1350 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001351 };
1352
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001353 // Child process.
1354 if (pid == 0)
1355 {
1356 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001357 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001358 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001359
Pavel Labath493c3a12015-02-04 10:36:57 +00001360 // terminal has already dupped the tty descriptors to stdin/out/err.
1361 // This closes original fd from which they were copied (and avoids
1362 // leaking descriptors to the debugged process.
1363 terminal.CloseSlaveFileDescriptor();
1364
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001365 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001366 if (setgid(getgid()) != 0)
1367 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001368
1369 // Let us have our own process group.
1370 setpgid(0, 0);
1371
Greg Clayton710dd5a2011-01-08 20:28:42 +00001372 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001373 //
1374 // FIXME: If two or more of the paths are the same we needlessly open
1375 // the same file multiple times.
1376 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001377 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001378 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001379
1380 if (stdout_path != NULL && stdout_path[0])
1381 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001382 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001383
1384 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001385 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001386 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001387
Daniel Malea6217d2a2013-01-08 14:49:22 +00001388 // Change working directory
1389 if (working_dir != NULL && working_dir[0])
1390 if (0 != ::chdir(working_dir))
1391 exit(eChdirFailed);
1392
Todd Fiala0bce1b62014-08-17 00:10:50 +00001393 // Disable ASLR if requested.
1394 if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
1395 {
1396 const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
1397 if (old_personality == -1)
1398 {
1399 if (log)
1400 log->Printf ("ProcessMonitor::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno));
1401 }
1402 else
1403 {
1404 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
1405 if (new_personality == -1)
1406 {
1407 if (log)
1408 log->Printf ("ProcessMonitor::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno));
1409
1410 }
1411 else
1412 {
1413 if (log)
Todd Fiala850f9a22014-09-19 18:27:45 +00001414 log->Printf ("ProcessMonitor::%s disabling ASLR: SUCCESS", __FUNCTION__);
Todd Fiala0bce1b62014-08-17 00:10:50 +00001415
1416 }
1417 }
1418 }
1419
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001420 // Execute. We should never return.
1421 execve(argv[0],
1422 const_cast<char *const *>(argv),
1423 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001424 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001425 }
1426
1427 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001428 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001429 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001430 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001431
1432 raw_pid = waitpid(pid, &status, 0);
1433 wpid = static_cast <lldb::pid_t> (raw_pid);
1434 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001435 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001436 args->m_error.SetErrorToErrno();
1437 goto FINISH;
1438 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001439 else if (WIFEXITED(status))
1440 {
1441 // open, dup or execve likely failed for some reason.
1442 args->m_error.SetErrorToGenericError();
1443 switch (WEXITSTATUS(status))
1444 {
Greg Clayton542e4072012-09-07 17:49:29 +00001445 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001446 args->m_error.SetErrorString("Child ptrace failed.");
1447 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001448 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001449 args->m_error.SetErrorString("Child open stdin failed.");
1450 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001451 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001452 args->m_error.SetErrorString("Child open stdout failed.");
1453 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001454 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001455 args->m_error.SetErrorString("Child open stderr failed.");
1456 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001457 case eChdirFailed:
1458 args->m_error.SetErrorString("Child failed to set working directory.");
1459 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001460 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001461 args->m_error.SetErrorString("Child exec failed.");
1462 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001463 case eSetGidFailed:
1464 args->m_error.SetErrorString("Child setgid failed.");
1465 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001466 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001467 args->m_error.SetErrorString("Child returned unknown exit status.");
1468 break;
1469 }
1470 goto FINISH;
1471 }
1472 assert(WIFSTOPPED(status) && wpid == pid &&
1473 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001474
Matt Kopec085d6ce2013-05-31 22:00:07 +00001475 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001476 {
1477 args->m_error.SetErrorToErrno();
1478 goto FINISH;
1479 }
1480
1481 // Release the master terminal descriptor and pass it off to the
1482 // ProcessMonitor instance. Similarly stash the inferior pid.
1483 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1484 monitor->m_pid = pid;
1485
Stephen Wilson26977162011-03-23 02:14:42 +00001486 // Set the terminal fd to be in non blocking mode (it simplifies the
1487 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1488 // descriptor to read from).
1489 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1490 goto FINISH;
1491
Johnny Chen30213ff2012-01-05 19:17:38 +00001492 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001493 // FIXME: should we be letting UpdateThreadList handle this?
1494 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001495 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001496
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001497 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001498 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001499 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001500
Matt Kopecb2910442013-07-09 15:09:45 +00001501 process.AddThreadForInitialStopIfNeeded(pid);
1502
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001503 // Let our process instance know the thread has stopped.
1504 process.SendMessage(ProcessMessage::Trace(pid));
1505
1506FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001507 return args->m_error.Success();
1508}
1509
Johnny Chen25e68e32011-06-14 19:19:50 +00001510void
1511ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1512{
1513 static const char *g_thread_name = "lldb.process.linux.operation";
1514
Zachary Turneracee96a2014-09-23 18:32:09 +00001515 if (m_operation_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001516 return;
1517
Zachary Turner39de3112014-09-09 20:54:56 +00001518 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen25e68e32011-06-14 19:19:50 +00001519}
1520
Johnny Chen25e68e32011-06-14 19:19:50 +00001521void *
1522ProcessMonitor::AttachOpThread(void *arg)
1523{
1524 AttachArgs *args = static_cast<AttachArgs*>(arg);
1525
Greg Clayton743ecf42012-10-16 20:20:18 +00001526 if (!Attach(args)) {
1527 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001528 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001529 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001530
1531 ServeOperation(args);
1532 return NULL;
1533}
1534
1535bool
1536ProcessMonitor::Attach(AttachArgs *args)
1537{
1538 lldb::pid_t pid = args->m_pid;
1539
1540 ProcessMonitor *monitor = args->m_monitor;
1541 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001542 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001543 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001544
Matt Kopec085d6ce2013-05-31 22:00:07 +00001545 // Use a map to keep track of the threads which we have attached/need to attach.
1546 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001547 if (pid <= 1)
1548 {
1549 args->m_error.SetErrorToGenericError();
1550 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1551 goto FINISH;
1552 }
1553
Matt Kopec085d6ce2013-05-31 22:00:07 +00001554 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001555 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001556 for (Host::TidMap::iterator it = tids_to_attach.begin();
1557 it != tids_to_attach.end(); ++it)
1558 {
1559 if (it->second == false)
1560 {
1561 lldb::tid_t tid = it->first;
1562
1563 // Attach to the requested process.
1564 // An attach will cause the thread to stop with a SIGSTOP.
1565 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1566 {
1567 // No such thread. The thread may have exited.
1568 // More error handling may be needed.
1569 if (errno == ESRCH)
1570 {
1571 tids_to_attach.erase(it);
1572 continue;
1573 }
1574 else
1575 {
1576 args->m_error.SetErrorToErrno();
1577 goto FINISH;
1578 }
1579 }
1580
Todd Fiala9be50492014-07-01 16:30:53 +00001581 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001582 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1583 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001584 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001585 {
1586 // No such thread. The thread may have exited.
1587 // More error handling may be needed.
1588 if (errno == ESRCH)
1589 {
1590 tids_to_attach.erase(it);
1591 continue;
1592 }
1593 else
1594 {
1595 args->m_error.SetErrorToErrno();
1596 goto FINISH;
1597 }
1598 }
1599
1600 if (!SetDefaultPtraceOpts(tid))
1601 {
1602 args->m_error.SetErrorToErrno();
1603 goto FINISH;
1604 }
1605
1606 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001607 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001608
Matt Kopec085d6ce2013-05-31 22:00:07 +00001609 if (log)
1610 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1611 process.GetThreadList().AddThread(inferior);
1612 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001613 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001614 }
1615 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001616 }
1617
Matt Kopec085d6ce2013-05-31 22:00:07 +00001618 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001619 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001620 monitor->m_pid = pid;
1621 // Let our process instance know the thread has stopped.
1622 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001623 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001624 else
1625 {
1626 args->m_error.SetErrorToGenericError();
1627 args->m_error.SetErrorString("No such process.");
1628 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001629
1630 FINISH:
1631 return args->m_error.Success();
1632}
1633
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001634bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001635ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1636{
1637 long ptrace_opts = 0;
1638
1639 // Have the child raise an event on exit. This is used to keep the child in
1640 // limbo until it is destroyed.
1641 ptrace_opts |= PTRACE_O_TRACEEXIT;
1642
1643 // Have the tracer trace threads which spawn in the inferior process.
1644 // TODO: if we want to support tracing the inferiors' child, add the
1645 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1646 ptrace_opts |= PTRACE_O_TRACECLONE;
1647
1648 // Have the tracer notify us before execve returns
1649 // (needed to disable legacy SIGTRAP generation)
1650 ptrace_opts |= PTRACE_O_TRACEEXEC;
1651
1652 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1653}
1654
1655bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001656ProcessMonitor::MonitorCallback(void *callback_baton,
1657 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001658 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001659 int signal,
1660 int status)
1661{
1662 ProcessMessage message;
1663 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001664 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001665 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001666 bool stop_monitoring;
1667 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001668 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001669
Andrew Kaylor93132f52013-05-28 23:04:25 +00001670 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1671
1672 if (exited)
1673 {
1674 if (log)
1675 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1676 message = ProcessMessage::Exit(pid, status);
1677 process->SendMessage(message);
1678 return pid == process->GetID();
1679 }
1680
Daniel Maleaa35970a2012-11-23 18:09:58 +00001681 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1682 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001683 if (log)
1684 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001685 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1686 if (!monitor->Resume(pid, SIGSTOP)) {
1687 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1688 }
1689 stop_monitoring = false;
1690 } else {
1691 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1692 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001693 // stop the monitor thread if this is the main pid.
1694 if (log)
1695 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1696 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1697 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001698 // If we are going to stop monitoring, we need to notify our process object
1699 if (stop_monitoring)
1700 {
1701 message = ProcessMessage::Exit(pid, status);
1702 process->SendMessage(message);
1703 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001704 }
1705 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001706 else {
1707 switch (info.si_signo)
1708 {
1709 case SIGTRAP:
1710 message = MonitorSIGTRAP(monitor, &info, pid);
1711 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001712
Stephen Wilson84ffe702011-03-30 15:55:52 +00001713 default:
1714 message = MonitorSignal(monitor, &info, pid);
1715 break;
1716 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001717
Stephen Wilson84ffe702011-03-30 15:55:52 +00001718 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001719 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001720 }
1721
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001722 return stop_monitoring;
1723}
1724
1725ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001726ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001727 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001728{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001729 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001730
Andrew Kaylor93132f52013-05-28 23:04:25 +00001731 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1732
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001733 assert(monitor);
1734 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001735
Stephen Wilson84ffe702011-03-30 15:55:52 +00001736 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001737 {
1738 default:
1739 assert(false && "Unexpected SIGTRAP code!");
1740 break;
1741
Matt Kopeca360d7e2013-05-17 19:27:47 +00001742 // TODO: these two cases are required if we want to support tracing
1743 // of the inferiors' children
1744 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1745 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1746
Matt Kopec650648f2013-01-08 16:30:18 +00001747 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1748 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001749 if (log)
1750 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1751
Matt Kopec650648f2013-01-08 16:30:18 +00001752 unsigned long tid = 0;
1753 if (!monitor->GetEventMessage(pid, &tid))
1754 tid = -1;
1755 message = ProcessMessage::NewThread(pid, tid);
1756 break;
1757 }
1758
Matt Kopeca360d7e2013-05-17 19:27:47 +00001759 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001760 if (log)
1761 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1762
1763 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001764 break;
1765
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001766 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1767 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001768 // The inferior process or one of its threads is about to exit.
1769 // Maintain the process or thread in a state of "limbo" until we are
1770 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001771 unsigned long data = 0;
1772 if (!monitor->GetEventMessage(pid, &data))
1773 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001774 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001775 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001776 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001777 break;
1778 }
1779
1780 case 0:
1781 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001782 if (log)
1783 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001784 message = ProcessMessage::Trace(pid);
1785 break;
1786
1787 case SI_KERNEL:
1788 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001789 if (log)
1790 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001791 message = ProcessMessage::Break(pid);
1792 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001793
1794 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001795 if (log)
1796 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001797 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1798 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001799
1800 case SIGTRAP:
1801 case (SIGTRAP | 0x80):
1802 if (log)
1803 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1804 // Ignore these signals until we know more about them
1805 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001806 }
1807
1808 return message;
1809}
1810
Stephen Wilson84ffe702011-03-30 15:55:52 +00001811ProcessMessage
1812ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001813 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001814{
1815 ProcessMessage message;
1816 int signo = info->si_signo;
1817
Andrew Kaylor93132f52013-05-28 23:04:25 +00001818 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1819
Stephen Wilson84ffe702011-03-30 15:55:52 +00001820 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1821 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1822 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1823 //
1824 // IOW, user generated signals never generate what we consider to be a
1825 // "crash".
1826 //
1827 // Similarly, ACK signals generated by this monitor.
1828 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1829 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001830 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001831 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001832 __FUNCTION__,
1833 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1834 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1835 info->si_pid);
1836
Stephen Wilson84ffe702011-03-30 15:55:52 +00001837 if (info->si_pid == getpid())
1838 return ProcessMessage::SignalDelivered(pid, signo);
1839 else
1840 return ProcessMessage::Signal(pid, signo);
1841 }
1842
Andrew Kaylor93132f52013-05-28 23:04:25 +00001843 if (log)
1844 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1845
Chaoren Lin28e57422015-02-03 01:51:25 +00001846 switch (signo)
1847 {
1848 case SIGSEGV:
1849 case SIGILL:
1850 case SIGFPE:
1851 case SIGBUS:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001852 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
Chaoren Lin28e57422015-02-03 01:51:25 +00001853 const auto reason = GetCrashReason(*info);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001854 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1855 }
1856
1857 // Everything else is "normal" and does not require any special action on
1858 // our part.
1859 return ProcessMessage::Signal(pid, signo);
1860}
1861
Andrew Kaylord4d54992013-09-17 00:30:24 +00001862// On Linux, when a new thread is created, we receive to notifications,
1863// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1864// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1865// the new child thread indicating that it has is stopped because we attached.
1866// We have no guarantee of the order in which these arrive, but we need both
1867// before we are ready to proceed. We currently keep a list of threads which
1868// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1869// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1870// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1871
1872bool
1873ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1874{
1875 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1876 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001877 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001878
1879 // Wait for the thread to stop
1880 while (true)
1881 {
1882 int status = -1;
1883 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001884 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001885 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001886 if (status == -1)
1887 {
1888 // If we got interrupted by a signal (in our process, not the
1889 // inferior) try again.
1890 if (errno == EINTR)
1891 continue;
1892 else
1893 {
1894 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001895 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001896 return false; // This is bad, but there's nothing we can do.
1897 }
1898 }
1899
1900 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001901 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001902
Todd Fiala9be50492014-07-01 16:30:53 +00001903 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001904
1905 siginfo_t info;
1906 int ptrace_err;
1907 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1908 {
1909 if (log)
1910 {
1911 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1912 }
1913 return false;
1914 }
1915
1916 // If this is a thread exit, we won't get any more information.
1917 if (WIFEXITED(status))
1918 {
1919 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001920 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001921 return true;
1922 continue;
1923 }
1924
1925 assert(info.si_code == SI_USER);
1926 assert(WSTOPSIG(status) == SIGSTOP);
1927
1928 if (log)
1929 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1930 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1931 return true;
1932 }
1933 return false;
1934}
1935
Andrew Kaylor93132f52013-05-28 23:04:25 +00001936bool
1937ProcessMonitor::StopThread(lldb::tid_t tid)
1938{
1939 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1940
1941 // FIXME: Try to use tgkill or tkill
1942 int ret = tgkill(m_pid, tid, SIGSTOP);
1943 if (log)
1944 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1945
1946 // This can happen if a thread exited while we were trying to stop it. That's OK.
1947 // We'll get the signal for that later.
1948 if (ret < 0)
1949 return false;
1950
1951 // Wait for the thread to stop
1952 while (true)
1953 {
1954 int status = -1;
1955 if (log)
1956 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001957 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001958 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001959 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1960 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001961
Todd Fiala9be50492014-07-01 16:30:53 +00001962 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001963 {
1964 // If we got interrupted by a signal (in our process, not the
1965 // inferior) try again.
1966 if (errno == EINTR)
1967 continue;
1968 else
1969 return false; // This is bad, but there's nothing we can do.
1970 }
1971
1972 // If this is a thread exit, we won't get any more information.
1973 if (WIFEXITED(status))
1974 {
1975 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001976 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001977 return true;
1978 continue;
1979 }
1980
1981 siginfo_t info;
1982 int ptrace_err;
1983 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1984 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001985 // another signal causing a StopAllThreads may have been received
1986 // before wait_pid's group-stop was processed, handle it now
1987 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001988 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001989 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001990
Todd Fiala1b0539c2014-01-27 17:03:57 +00001991 if (log)
1992 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1993 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1994 if (!Resume(wait_pid, SIGSTOP)) {
1995 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1996 }
1997 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001998 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001999
2000 if (log)
2001 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002002 return false;
2003 }
2004
2005 // Handle events from other threads
2006 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00002007 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
2008 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00002009
2010 ProcessMessage message;
2011 if (info.si_signo == SIGTRAP)
2012 message = MonitorSIGTRAP(this, &info, wait_pid);
2013 else
2014 message = MonitorSignal(this, &info, wait_pid);
2015
2016 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
2017
2018 // When a new thread is created, we may get a SIGSTOP for the new thread
2019 // just before we get the SIGTRAP that we use to add the thread to our
2020 // process thread list. We don't need to worry about that signal here.
2021 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
2022
2023 if (!thread)
2024 {
2025 m_process->SendMessage(message);
2026 continue;
2027 }
2028
2029 switch (message.GetKind())
2030 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00002031 case ProcessMessage::eExecMessage:
2032 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00002033 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00002034 case ProcessMessage::eInvalidMessage:
2035 break;
2036
2037 // These need special handling because we don't want to send a
2038 // resume even if we already sent a SIGSTOP to this thread. In
2039 // this case the resume will cause the thread to disappear. It is
2040 // unlikely that we'll ever get eExitMessage here, but the same
2041 // reasoning applies.
2042 case ProcessMessage::eLimboMessage:
2043 case ProcessMessage::eExitMessage:
2044 if (log)
2045 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2046 // SendMessage will set the thread state as needed.
2047 m_process->SendMessage(message);
2048 // If this is the thread we're waiting for, stop waiting. Even
2049 // though this wasn't the signal we expected, it's the last
2050 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00002051 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002052 return true;
2053 break;
2054
Matt Kopecb2910442013-07-09 15:09:45 +00002055 case ProcessMessage::eSignalMessage:
2056 if (log)
2057 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2058 if (WSTOPSIG(status) == SIGSTOP)
2059 {
2060 m_process->AddThreadForInitialStopIfNeeded(tid);
2061 thread->SetState(lldb::eStateStopped);
2062 }
2063 else
2064 {
2065 m_process->SendMessage(message);
2066 // This isn't the stop we were expecting, but the thread is
2067 // stopped. SendMessage will handle processing of this event,
2068 // but we need to resume here to get the stop we are waiting
2069 // for (otherwise the thread will stop again immediately when
2070 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002071 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00002072 Resume(wait_pid, eResumeSignalNone);
2073 }
2074 break;
2075
Andrew Kaylor93132f52013-05-28 23:04:25 +00002076 case ProcessMessage::eSignalDeliveredMessage:
2077 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00002078 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
2079 WIFSTOPPED(status) &&
2080 WSTOPSIG(status) == SIGSTOP &&
2081 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002082 {
2083 if (log)
2084 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
2085 thread->SetState(lldb::eStateStopped);
2086 return true;
2087 }
2088 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00002089 case ProcessMessage::eBreakpointMessage:
2090 case ProcessMessage::eTraceMessage:
2091 case ProcessMessage::eWatchpointMessage:
2092 case ProcessMessage::eCrashMessage:
2093 case ProcessMessage::eNewThreadMessage:
2094 if (log)
2095 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2096 // SendMessage will set the thread state as needed.
2097 m_process->SendMessage(message);
2098 // This isn't the stop we were expecting, but the thread is
2099 // stopped. SendMessage will handle processing of this event,
2100 // but we need to resume here to get the stop we are waiting
2101 // for (otherwise the thread will stop again immediately when
2102 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002103 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002104 Resume(wait_pid, eResumeSignalNone);
2105 break;
2106 }
2107 }
2108 return false;
2109}
2110
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002111void
Johnny Chen25e68e32011-06-14 19:19:50 +00002112ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002113{
Stephen Wilson570243b2011-01-19 01:37:06 +00002114 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002115
Stephen Wilson570243b2011-01-19 01:37:06 +00002116 // We are finised with the arguments and are ready to go. Sync with the
2117 // parent thread and start serving operations on the inferior.
2118 sem_post(&args->m_semaphore);
2119
Michael Sartain704bf892013-10-09 01:28:57 +00002120 for(;;)
2121 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002122 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002123 if (sem_wait(&monitor->m_operation_pending))
2124 {
2125 if (errno == EINTR)
2126 continue;
2127 assert(false && "Unexpected errno from sem_wait");
2128 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002129
Daniel Malea1efb4182013-09-16 23:12:18 +00002130 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002131
Daniel Malea1efb4182013-09-16 23:12:18 +00002132 // notify calling thread that operation is complete
2133 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002134 }
2135}
2136
2137void
2138ProcessMonitor::DoOperation(Operation *op)
2139{
Daniel Malea1efb4182013-09-16 23:12:18 +00002140 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002141
Daniel Malea1efb4182013-09-16 23:12:18 +00002142 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002143
Daniel Malea1efb4182013-09-16 23:12:18 +00002144 // notify operation thread that an operation is ready to be processed
2145 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002146
Daniel Malea1efb4182013-09-16 23:12:18 +00002147 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002148 while (sem_wait(&m_operation_done))
2149 {
2150 if (errno == EINTR)
2151 continue;
2152 assert(false && "Unexpected errno from sem_wait");
2153 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002154}
2155
2156size_t
2157ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2158 Error &error)
2159{
2160 size_t result;
2161 ReadOperation op(vm_addr, buf, size, error, result);
2162 DoOperation(&op);
2163 return result;
2164}
2165
2166size_t
2167ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2168 lldb_private::Error &error)
2169{
2170 size_t result;
2171 WriteOperation op(vm_addr, buf, size, error, result);
2172 DoOperation(&op);
2173 return result;
2174}
2175
2176bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002177ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002178 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002179{
2180 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002181 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002182 DoOperation(&op);
2183 return result;
2184}
2185
2186bool
Matt Kopec7de48462013-03-06 17:20:48 +00002187ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002188 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002189{
2190 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002191 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002192 DoOperation(&op);
2193 return result;
2194}
2195
2196bool
Matt Kopec7de48462013-03-06 17:20:48 +00002197ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002198{
2199 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002200 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002201 DoOperation(&op);
2202 return result;
2203}
2204
2205bool
Matt Kopec7de48462013-03-06 17:20:48 +00002206ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002207{
2208 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002209 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002210 DoOperation(&op);
2211 return result;
2212}
2213
2214bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002215ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2216{
2217 bool result;
2218 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2219 DoOperation(&op);
2220 return result;
2221}
2222
2223bool
Matt Kopec7de48462013-03-06 17:20:48 +00002224ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002225{
2226 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002227 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002228 DoOperation(&op);
2229 return result;
2230}
2231
2232bool
Matt Kopec7de48462013-03-06 17:20:48 +00002233ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002234{
2235 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002236 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002237 DoOperation(&op);
2238 return result;
2239}
2240
2241bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002242ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2243{
2244 bool result;
2245 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2246 DoOperation(&op);
2247 return result;
2248}
2249
2250bool
Richard Mitton0a558352013-10-17 21:14:00 +00002251ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2252{
2253 bool result;
2254 ReadThreadPointerOperation op(tid, &value, result);
2255 DoOperation(&op);
2256 return result;
2257}
2258
2259bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002260ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002261{
2262 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002263 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2264
2265 if (log)
2266 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2267 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002268 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002269 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002270 if (log)
2271 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002272 return result;
2273}
2274
2275bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002276ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002277{
2278 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002279 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002280 DoOperation(&op);
2281 return result;
2282}
2283
2284bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002285ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002286{
Ed Maste4e0999b2014-04-01 18:14:06 +00002287 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002288}
2289
2290bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002291ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002292{
2293 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002294 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002295 DoOperation(&op);
2296 return result;
2297}
2298
2299bool
2300ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2301{
2302 bool result;
2303 EventMessageOperation op(tid, message, result);
2304 DoOperation(&op);
2305 return result;
2306}
2307
Greg Clayton743ecf42012-10-16 20:20:18 +00002308lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002309ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002310{
Greg Clayton28041352011-11-29 20:50:10 +00002311 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002312 if (tid != LLDB_INVALID_THREAD_ID)
2313 {
2314 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002315 DoOperation(&op);
2316 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002317 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002318}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002319
2320bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002321ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2322{
Peter Collingbourne62343202011-06-14 03:55:54 +00002323 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002324
2325 if (target_fd == -1)
2326 return false;
2327
Pavel Labath493c3a12015-02-04 10:36:57 +00002328 if (dup2(target_fd, fd) == -1)
2329 return false;
2330
2331 return (close(target_fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002332}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002333
2334void
2335ProcessMonitor::StopMonitoringChildProcess()
2336{
Zachary Turneracee96a2014-09-23 18:32:09 +00002337 if (m_monitor_thread.IsJoinable())
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002338 {
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +00002339 ::pthread_kill(m_monitor_thread.GetNativeThread().GetSystemHandle(), SIGUSR1);
Zachary Turner39de3112014-09-09 20:54:56 +00002340 m_monitor_thread.Join(nullptr);
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002341 }
2342}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002343
2344void
2345ProcessMonitor::StopMonitor()
2346{
2347 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002348 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002349 sem_destroy(&m_operation_pending);
2350 sem_destroy(&m_operation_done);
Pavel Labath3a2da9e2015-02-06 11:32:52 +00002351 if (m_terminal_fd >= 0) {
2352 close(m_terminal_fd);
2353 m_terminal_fd = -1;
2354 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00002355}
2356
2357void
Greg Clayton743ecf42012-10-16 20:20:18 +00002358ProcessMonitor::StopOpThread()
2359{
Zachary Turneracee96a2014-09-23 18:32:09 +00002360 if (!m_operation_thread.IsJoinable())
Greg Clayton743ecf42012-10-16 20:20:18 +00002361 return;
2362
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +00002363 DoOperation(EXIT_OPERATION);
Zachary Turner39de3112014-09-09 20:54:56 +00002364 m_operation_thread.Join(nullptr);
Greg Clayton743ecf42012-10-16 20:20:18 +00002365}