blob: fcfda216231488a2d70ea2757e712de7d824487b [file] [log] [blame]
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001//===-- ProcessMonitor.cpp ------------------------------------ -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Stephen Wilsone6f9f662010-07-24 02:19:04 +000012// C Includes
13#include <errno.h>
14#include <poll.h>
15#include <string.h>
Daniel Maleaa85e6b62012-12-07 22:21:08 +000016#include <stdint.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000017#include <unistd.h>
Todd Fiala42079682014-08-27 16:05:26 +000018#include <elf.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000019
20// C++ Includes
21// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000022#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000023#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000024#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000025#include "lldb/Core/Scalar.h"
26#include "lldb/Host/Host.h"
Zachary Turner39de3112014-09-09 20:54:56 +000027#include "lldb/Host/HostThread.h"
28#include "lldb/Host/ThreadLauncher.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/RegisterContext.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000031#include "lldb/Target/UnixSignals.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000032#include "lldb/Utility/PseudoTerminal.h"
33
Chaoren Lin28e57422015-02-03 01:51:25 +000034#include "Plugins/Process/POSIX/CrashReason.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000035#include "Plugins/Process/POSIX/POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000036#include "ProcessLinux.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000037#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000038#include "ProcessMonitor.h"
39
Tamas Berghammerd8584872015-02-06 10:57:40 +000040// System includes - They have to be included after framework includes because they define some
41// macros which collide with variable names in other modules
42#ifndef __ANDROID__
43#include <sys/procfs.h>
44#endif
45#include <sys/personality.h>
46#include <sys/ptrace.h>
47#include <sys/socket.h>
48#include <sys/syscall.h>
49#include <sys/types.h>
50#include <sys/uio.h>
51#include <sys/user.h>
52#include <sys/wait.h>
53
Todd Fialacacde7d2014-09-27 16:54:22 +000054#ifdef __ANDROID__
55#define __ptrace_request int
56#define PT_DETACH PTRACE_DETACH
57#endif
58
Greg Clayton386ff182011-11-05 01:09:16 +000059#define DEBUG_PTRACE_MAXBYTES 20
60
Matt Kopec58c0b962013-03-20 20:34:35 +000061// Support ptrace extensions even when compiled without required kernel support
62#ifndef PTRACE_GETREGSET
63 #define PTRACE_GETREGSET 0x4204
64#endif
65#ifndef PTRACE_SETREGSET
66 #define PTRACE_SETREGSET 0x4205
67#endif
Richard Mitton0a558352013-10-17 21:14:00 +000068#ifndef PTRACE_GET_THREAD_AREA
69 #define PTRACE_GET_THREAD_AREA 25
70#endif
71#ifndef PTRACE_ARCH_PRCTL
72 #define PTRACE_ARCH_PRCTL 30
73#endif
74#ifndef ARCH_GET_FS
75 #define ARCH_SET_GS 0x1001
76 #define ARCH_SET_FS 0x1002
77 #define ARCH_GET_FS 0x1003
78 #define ARCH_GET_GS 0x1004
79#endif
80
Todd Fiala0bce1b62014-08-17 00:10:50 +000081#define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff
Matt Kopec58c0b962013-03-20 20:34:35 +000082
Todd Fialadbec1ff2014-09-04 16:08:20 +000083#define LLDB_PTRACE_NT_ARM_TLS 0x401 // ARM TLS register
84
Matt Kopece9ea0da2013-05-07 19:29:28 +000085// Support hardware breakpoints in case it has not been defined
86#ifndef TRAP_HWBKPT
87 #define TRAP_HWBKPT 4
88#endif
89
Andrew Kaylor93132f52013-05-28 23:04:25 +000090// Try to define a macro to encapsulate the tgkill syscall
91// fall back on kill() if tgkill isn't available
Chaoren Linc9346592015-02-28 00:20:16 +000092#define tgkill(pid, tid, sig) \
93 syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig)
Andrew Kaylor93132f52013-05-28 23:04:25 +000094
Stephen Wilsone6f9f662010-07-24 02:19:04 +000095using namespace lldb_private;
96
Johnny Chen0d5f2d42011-10-18 18:09:30 +000097// FIXME: this code is host-dependent with respect to types and
98// endianness and needs to be fixed. For example, lldb::addr_t is
99// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
100// 32-bit pointer arguments. This code uses casts to work around the
101// problem.
102
103// We disable the tracing of ptrace calls for integration builds to
104// avoid the additional indirection and checks.
105#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
106
Greg Clayton386ff182011-11-05 01:09:16 +0000107static void
108DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
109{
110 uint8_t *ptr = (uint8_t *)bytes;
111 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
112 for(uint32_t i=0; i<loop_count; i++)
113 {
114 s.Printf ("[%x]", *ptr);
115 ptr++;
116 }
117}
118
Matt Kopec58c0b962013-03-20 20:34:35 +0000119static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +0000120{
121 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000122 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000123 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000124
125 if (verbose_log)
126 {
127 switch(req)
128 {
129 case PTRACE_POKETEXT:
130 {
131 DisplayBytes(buf, &data, 8);
132 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
133 break;
134 }
Greg Clayton542e4072012-09-07 17:49:29 +0000135 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000136 {
137 DisplayBytes(buf, &data, 8);
138 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
139 break;
140 }
Greg Clayton542e4072012-09-07 17:49:29 +0000141 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000142 {
143 DisplayBytes(buf, &data, 8);
144 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
145 break;
146 }
Todd Fiala6ac1be42014-08-21 16:34:03 +0000147#if !defined (__arm64__) && !defined (__aarch64__)
Greg Clayton542e4072012-09-07 17:49:29 +0000148 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000149 {
Matt Kopec7de48462013-03-06 17:20:48 +0000150 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000151 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
152 break;
153 }
154 case PTRACE_SETFPREGS:
155 {
Matt Kopec7de48462013-03-06 17:20:48 +0000156 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000157 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
158 break;
159 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000160#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000161 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000162 {
163 DisplayBytes(buf, data, sizeof(siginfo_t));
164 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
165 break;
166 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000167 case PTRACE_SETREGSET:
168 {
169 // Extract iov_base from data, which is a pointer to the struct IOVEC
170 DisplayBytes(buf, *(void **)data, data_size);
171 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
172 break;
173 }
Greg Clayton386ff182011-11-05 01:09:16 +0000174 default:
175 {
176 }
177 }
178 }
179}
180
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000181// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000182// 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 +0000183extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000184PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000185 const char* reqName, const char* file, int line)
186{
Greg Clayton386ff182011-11-05 01:09:16 +0000187 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000188
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000189 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000190
Matt Kopec7de48462013-03-06 17:20:48 +0000191 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000192
193 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000194 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000195 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000196 else
Todd Fiala4507f062014-02-27 20:46:12 +0000197 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000198
Ed Mastec099c952014-02-24 14:07:45 +0000199 if (log)
200 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
201 reqName, pid, addr, data, data_size, result, file, line);
202
Matt Kopec7de48462013-03-06 17:20:48 +0000203 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000204
Matt Kopec7de48462013-03-06 17:20:48 +0000205 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000206 {
207 const char* str;
208 switch (errno)
209 {
210 case ESRCH: str = "ESRCH"; break;
211 case EINVAL: str = "EINVAL"; break;
212 case EBUSY: str = "EBUSY"; break;
213 case EPERM: str = "EPERM"; break;
214 default: str = "<unknown>";
215 }
216 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
217 }
218
219 return result;
220}
221
Matt Kopec7de48462013-03-06 17:20:48 +0000222// Wrapper for ptrace when logging is not required.
223// Sets errno to 0 prior to calling ptrace.
224extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000225PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000226{
Matt Kopec58c0b962013-03-20 20:34:35 +0000227 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000228 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000229 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
230 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
231 else
232 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000233 return result;
234}
235
236#define PTRACE(req, pid, addr, data, data_size) \
237 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000238#else
Matt Kopec7de48462013-03-06 17:20:48 +0000239 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000240#endif
241
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000242//------------------------------------------------------------------------------
243// Static implementations of ProcessMonitor::ReadMemory and
244// ProcessMonitor::WriteMemory. This enables mutual recursion between these
245// functions without needed to go thru the thread funnel.
246
247static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000248DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000249 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
250{
Greg Clayton542e4072012-09-07 17:49:29 +0000251 // ptrace word size is determined by the host, not the child
252 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000253 unsigned char *dst = static_cast<unsigned char*>(buf);
254 size_t bytes_read;
255 size_t remainder;
256 long data;
257
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000258 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000259 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000260 ProcessPOSIXLog::IncNestLevel();
261 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000262 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000263 pid, word_size, (void*)vm_addr, buf, size);
264
265 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000266 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
267 {
268 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000269 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
270 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000271 {
272 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000273 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000274 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000275 return bytes_read;
276 }
277
278 remainder = size - bytes_read;
279 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000280
281 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000282 for (unsigned i = 0; i < remainder; ++i)
283 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000284
Johnny Chen30213ff2012-01-05 19:17:38 +0000285 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
286 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
287 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
288 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000289 {
290 uintptr_t print_dst = 0;
291 // Format bytes from data by moving into print_dst for log output
292 for (unsigned i = 0; i < remainder; ++i)
293 print_dst |= (((data >> i*8) & 0xFF) << i*8);
294 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
295 (void*)vm_addr, print_dst, (unsigned long)data);
296 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000297
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000298 vm_addr += word_size;
299 dst += word_size;
300 }
301
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000302 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000303 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000304 return bytes_read;
305}
306
307static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000308DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000309 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
310{
Greg Clayton542e4072012-09-07 17:49:29 +0000311 // ptrace word size is determined by the host, not the child
312 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000313 const unsigned char *src = static_cast<const unsigned char*>(buf);
314 size_t bytes_written = 0;
315 size_t remainder;
316
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000317 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000318 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000319 ProcessPOSIXLog::IncNestLevel();
320 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000321 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000322 pid, word_size, (void*)vm_addr, buf, size);
323
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000324 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
325 {
326 remainder = size - bytes_written;
327 remainder = remainder > word_size ? word_size : remainder;
328
329 if (remainder == word_size)
330 {
331 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000332 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000333 for (unsigned i = 0; i < word_size; ++i)
334 data |= (unsigned long)src[i] << i*8;
335
Johnny Chen30213ff2012-01-05 19:17:38 +0000336 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
337 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
338 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
339 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000340 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
341 (void*)vm_addr, *(unsigned long*)src, data);
342
Matt Kopec7de48462013-03-06 17:20:48 +0000343 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000344 {
345 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000346 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000347 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000348 return bytes_written;
349 }
350 }
351 else
352 {
353 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000354 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000355 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000356 {
357 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000358 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000359 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000360 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000361
362 memcpy(buff, src, remainder);
363
Greg Clayton542e4072012-09-07 17:49:29 +0000364 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000365 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000366 {
367 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000368 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000369 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000370 }
371
Johnny Chen30213ff2012-01-05 19:17:38 +0000372 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
373 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
374 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
375 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000376 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
377 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000378 }
379
380 vm_addr += word_size;
381 src += word_size;
382 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000383 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000384 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000385 return bytes_written;
386}
387
Stephen Wilson26977162011-03-23 02:14:42 +0000388// Simple helper function to ensure flags are enabled on the given file
389// descriptor.
390static bool
391EnsureFDFlags(int fd, int flags, Error &error)
392{
393 int status;
394
395 if ((status = fcntl(fd, F_GETFL)) == -1)
396 {
397 error.SetErrorToErrno();
398 return false;
399 }
400
401 if (fcntl(fd, F_SETFL, status | flags) == -1)
402 {
403 error.SetErrorToErrno();
404 return false;
405 }
406
407 return true;
408}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000409
410//------------------------------------------------------------------------------
411/// @class Operation
412/// @brief Represents a ProcessMonitor operation.
413///
414/// Under Linux, it is not possible to ptrace() from any other thread but the
415/// one that spawned or attached to the process from the start. Therefore, when
416/// a ProcessMonitor is asked to deliver or change the state of an inferior
417/// process the operation must be "funneled" to a specific thread to perform the
418/// task. The Operation class provides an abstract base for all services the
419/// ProcessMonitor must perform via the single virtual function Execute, thus
420/// encapsulating the code that needs to run in the privileged context.
421class Operation
422{
423public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000424 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000425 virtual void Execute(ProcessMonitor *monitor) = 0;
426};
427
428//------------------------------------------------------------------------------
429/// @class ReadOperation
430/// @brief Implements ProcessMonitor::ReadMemory.
431class ReadOperation : public Operation
432{
433public:
434 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
435 Error &error, size_t &result)
436 : m_addr(addr), m_buff(buff), m_size(size),
437 m_error(error), m_result(result)
438 { }
439
440 void Execute(ProcessMonitor *monitor);
441
442private:
443 lldb::addr_t m_addr;
444 void *m_buff;
445 size_t m_size;
446 Error &m_error;
447 size_t &m_result;
448};
449
450void
451ReadOperation::Execute(ProcessMonitor *monitor)
452{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000453 lldb::pid_t pid = monitor->GetPID();
454
Greg Clayton542e4072012-09-07 17:49:29 +0000455 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000456}
457
458//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000459/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000460/// @brief Implements ProcessMonitor::WriteMemory.
461class WriteOperation : public Operation
462{
463public:
464 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
465 Error &error, size_t &result)
466 : m_addr(addr), m_buff(buff), m_size(size),
467 m_error(error), m_result(result)
468 { }
469
470 void Execute(ProcessMonitor *monitor);
471
472private:
473 lldb::addr_t m_addr;
474 const void *m_buff;
475 size_t m_size;
476 Error &m_error;
477 size_t &m_result;
478};
479
480void
481WriteOperation::Execute(ProcessMonitor *monitor)
482{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000483 lldb::pid_t pid = monitor->GetPID();
484
Greg Clayton542e4072012-09-07 17:49:29 +0000485 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000486}
487
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000488
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000489//------------------------------------------------------------------------------
490/// @class ReadRegOperation
491/// @brief Implements ProcessMonitor::ReadRegisterValue.
492class ReadRegOperation : public Operation
493{
494public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000495 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000496 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000497 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000498 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000499 { }
500
501 void Execute(ProcessMonitor *monitor);
502
503private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000504 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000505 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000506 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000507 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000508 bool &m_result;
509};
510
511void
512ReadRegOperation::Execute(ProcessMonitor *monitor)
513{
Todd Fiala49131cf2014-09-12 16:57:28 +0000514#if defined (__arm64__) || defined (__aarch64__)
515 if (m_offset > sizeof(struct user_pt_regs))
516 {
517 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
518 if (offset > sizeof(struct user_fpsimd_state))
519 {
520 m_result = false;
521 }
522 else
523 {
524 elf_fpregset_t regs;
525 int regset = NT_FPREGSET;
526 struct iovec ioVec;
527
528 ioVec.iov_base = &regs;
529 ioVec.iov_len = sizeof regs;
530 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
531 m_result = false;
532 else
533 {
534 m_result = true;
535 m_value.SetBytes((void *)(((unsigned char *)(&regs)) + offset), 16, monitor->GetProcess().GetByteOrder());
536 }
537 }
538 }
539 else
540 {
541 elf_gregset_t regs;
542 int regset = NT_PRSTATUS;
543 struct iovec ioVec;
544
545 ioVec.iov_base = &regs;
546 ioVec.iov_len = sizeof regs;
547 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
548 m_result = false;
549 else
550 {
551 m_result = true;
552 m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, monitor->GetProcess().GetByteOrder());
553 }
554 }
555#else
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000556 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000557
558 // Set errno to zero so that we can detect a failed peek.
559 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000560 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
561 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000562 m_result = false;
563 else
564 {
565 m_value = data;
566 m_result = true;
567 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000568 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000569 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000570 m_reg_name, data);
Todd Fiala49131cf2014-09-12 16:57:28 +0000571#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000572}
573
574//------------------------------------------------------------------------------
575/// @class WriteRegOperation
576/// @brief Implements ProcessMonitor::WriteRegisterValue.
577class WriteRegOperation : public Operation
578{
579public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000580 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000581 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000582 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000583 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000584 { }
585
586 void Execute(ProcessMonitor *monitor);
587
588private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000589 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000590 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000591 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000592 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000593 bool &m_result;
594};
595
596void
597WriteRegOperation::Execute(ProcessMonitor *monitor)
598{
Todd Fiala49131cf2014-09-12 16:57:28 +0000599#if defined (__arm64__) || defined (__aarch64__)
600 if (m_offset > sizeof(struct user_pt_regs))
601 {
602 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
603 if (offset > sizeof(struct user_fpsimd_state))
604 {
605 m_result = false;
606 }
607 else
608 {
609 elf_fpregset_t regs;
610 int regset = NT_FPREGSET;
611 struct iovec ioVec;
612
613 ioVec.iov_base = &regs;
614 ioVec.iov_len = sizeof regs;
615 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
616 m_result = false;
617 else
618 {
619 ::memcpy((void *)(((unsigned char *)(&regs)) + offset), m_value.GetBytes(), 16);
620 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
621 m_result = false;
622 else
623 m_result = true;
624 }
625 }
626 }
627 else
628 {
629 elf_gregset_t regs;
630 int regset = NT_PRSTATUS;
631 struct iovec ioVec;
632
633 ioVec.iov_base = &regs;
634 ioVec.iov_len = sizeof regs;
635 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
636 m_result = false;
637 else
638 {
639 ::memcpy((void *)(((unsigned char *)(&regs)) + m_offset), m_value.GetBytes(), 8);
640 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
641 m_result = false;
642 else
643 m_result = true;
644 }
645 }
646#else
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000647 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000648 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000649
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000650 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000651
652 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000653 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000654 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000655 m_result = false;
656 else
657 m_result = true;
Todd Fiala49131cf2014-09-12 16:57:28 +0000658#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000659}
660
661//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000662/// @class ReadGPROperation
663/// @brief Implements ProcessMonitor::ReadGPR.
664class ReadGPROperation : public Operation
665{
666public:
Matt Kopec7de48462013-03-06 17:20:48 +0000667 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
668 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000669 { }
670
671 void Execute(ProcessMonitor *monitor);
672
673private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000674 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000675 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000676 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000677 bool &m_result;
678};
679
680void
681ReadGPROperation::Execute(ProcessMonitor *monitor)
682{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000683#if defined (__arm64__) || defined (__aarch64__)
684 int regset = NT_PRSTATUS;
685 struct iovec ioVec;
686
687 ioVec.iov_base = m_buf;
688 ioVec.iov_len = m_buf_size;
689 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000690 m_result = false;
691 else
692 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000693#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000694 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
695 m_result = false;
696 else
697 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000698#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000699}
700
701//------------------------------------------------------------------------------
702/// @class ReadFPROperation
703/// @brief Implements ProcessMonitor::ReadFPR.
704class ReadFPROperation : public Operation
705{
706public:
Matt Kopec7de48462013-03-06 17:20:48 +0000707 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
708 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000709 { }
710
711 void Execute(ProcessMonitor *monitor);
712
713private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000714 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000715 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000716 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000717 bool &m_result;
718};
719
720void
721ReadFPROperation::Execute(ProcessMonitor *monitor)
722{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000723#if defined (__arm64__) || defined (__aarch64__)
724 int regset = NT_FPREGSET;
725 struct iovec ioVec;
726
727 ioVec.iov_base = m_buf;
728 ioVec.iov_len = m_buf_size;
729 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000730 m_result = false;
731 else
732 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000733#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000734 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
735 m_result = false;
736 else
737 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000738#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000739}
740
741//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000742/// @class ReadRegisterSetOperation
743/// @brief Implements ProcessMonitor::ReadRegisterSet.
744class ReadRegisterSetOperation : public Operation
745{
746public:
747 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
748 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
749 { }
750
751 void Execute(ProcessMonitor *monitor);
752
753private:
754 lldb::tid_t m_tid;
755 void *m_buf;
756 size_t m_buf_size;
757 const unsigned int m_regset;
758 bool &m_result;
759};
760
761void
762ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
763{
764 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
765 m_result = false;
766 else
767 m_result = true;
768}
769
770//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000771/// @class WriteGPROperation
772/// @brief Implements ProcessMonitor::WriteGPR.
773class WriteGPROperation : public Operation
774{
775public:
Matt Kopec7de48462013-03-06 17:20:48 +0000776 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
777 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000778 { }
779
780 void Execute(ProcessMonitor *monitor);
781
782private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000783 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000784 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000785 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000786 bool &m_result;
787};
788
789void
790WriteGPROperation::Execute(ProcessMonitor *monitor)
791{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000792#if defined (__arm64__) || defined (__aarch64__)
793 int regset = NT_PRSTATUS;
794 struct iovec ioVec;
795
796 ioVec.iov_base = m_buf;
797 ioVec.iov_len = m_buf_size;
798 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000799 m_result = false;
800 else
801 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000802#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000803 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
804 m_result = false;
805 else
806 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000807#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000808}
809
810//------------------------------------------------------------------------------
811/// @class WriteFPROperation
812/// @brief Implements ProcessMonitor::WriteFPR.
813class WriteFPROperation : public Operation
814{
815public:
Matt Kopec7de48462013-03-06 17:20:48 +0000816 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
817 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000818 { }
819
820 void Execute(ProcessMonitor *monitor);
821
822private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000823 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000824 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000825 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000826 bool &m_result;
827};
828
829void
830WriteFPROperation::Execute(ProcessMonitor *monitor)
831{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000832#if defined (__arm64__) || defined (__aarch64__)
833 int regset = NT_FPREGSET;
834 struct iovec ioVec;
835
836 ioVec.iov_base = m_buf;
837 ioVec.iov_len = m_buf_size;
838 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000839 m_result = false;
840 else
841 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000842#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000843 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
844 m_result = false;
845 else
846 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000847#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000848}
849
850//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000851/// @class WriteRegisterSetOperation
852/// @brief Implements ProcessMonitor::WriteRegisterSet.
853class WriteRegisterSetOperation : public Operation
854{
855public:
856 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
857 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
858 { }
859
860 void Execute(ProcessMonitor *monitor);
861
862private:
863 lldb::tid_t m_tid;
864 void *m_buf;
865 size_t m_buf_size;
866 const unsigned int m_regset;
867 bool &m_result;
868};
869
870void
871WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
872{
873 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
874 m_result = false;
875 else
876 m_result = true;
877}
878
879//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000880/// @class ReadThreadPointerOperation
881/// @brief Implements ProcessMonitor::ReadThreadPointer.
882class ReadThreadPointerOperation : public Operation
883{
884public:
885 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
886 : m_tid(tid), m_addr(addr), m_result(result)
887 { }
888
889 void Execute(ProcessMonitor *monitor);
890
891private:
892 lldb::tid_t m_tid;
893 lldb::addr_t *m_addr;
894 bool &m_result;
895};
896
897void
898ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
899{
900 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
901 if (log)
902 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
903
904 // The process for getting the thread area on Linux is
905 // somewhat... obscure. There's several different ways depending on
906 // what arch you're on, and what kernel version you have.
907
908 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
909 switch(arch.GetMachine())
910 {
Todd Fiala42079682014-08-27 16:05:26 +0000911 case llvm::Triple::aarch64:
912 {
Todd Fialadbec1ff2014-09-04 16:08:20 +0000913 int regset = LLDB_PTRACE_NT_ARM_TLS;
Todd Fiala42079682014-08-27 16:05:26 +0000914 struct iovec ioVec;
915
916 ioVec.iov_base = m_addr;
917 ioVec.iov_len = sizeof(lldb::addr_t);
918 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, ioVec.iov_len) < 0)
919 m_result = false;
920 else
921 m_result = true;
922 break;
923 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000924#if defined(__i386__) || defined(__x86_64__)
925 // Note that struct user below has a field named i387 which is x86-specific.
926 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000927 case llvm::Triple::x86:
928 {
929 // Find the GS register location for our host architecture.
930 size_t gs_user_offset = offsetof(struct user, regs);
931#ifdef __x86_64__
932 gs_user_offset += offsetof(struct user_regs_struct, gs);
933#endif
934#ifdef __i386__
935 gs_user_offset += offsetof(struct user_regs_struct, xgs);
936#endif
937
938 // Read the GS register value to get the selector.
939 errno = 0;
940 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
941 if (errno)
942 {
943 m_result = false;
944 break;
945 }
946
947 // Read the LDT base for that selector.
948 uint32_t tmp[4];
949 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
950 *m_addr = tmp[1];
951 break;
952 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000953#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000954 case llvm::Triple::x86_64:
955 // Read the FS register base.
956 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
957 break;
958 default:
959 m_result = false;
960 break;
961 }
962}
963
964//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000965/// @class ResumeOperation
966/// @brief Implements ProcessMonitor::Resume.
967class ResumeOperation : public Operation
968{
969public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000970 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
971 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000972
973 void Execute(ProcessMonitor *monitor);
974
975private:
976 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000977 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000978 bool &m_result;
979};
980
981void
982ResumeOperation::Execute(ProcessMonitor *monitor)
983{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000984 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000985
986 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
987 data = m_signo;
988
Matt Kopec7de48462013-03-06 17:20:48 +0000989 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000990 {
991 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
992
993 if (log)
994 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000995 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000996 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000997 else
998 m_result = true;
999}
1000
1001//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +00001002/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001003/// @brief Implements ProcessMonitor::SingleStep.
1004class SingleStepOperation : public Operation
1005{
1006public:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001007 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
1008 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001009
1010 void Execute(ProcessMonitor *monitor);
1011
1012private:
1013 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001014 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001015 bool &m_result;
1016};
1017
1018void
1019SingleStepOperation::Execute(ProcessMonitor *monitor)
1020{
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001021 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001022
1023 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
1024 data = m_signo;
1025
Matt Kopec7de48462013-03-06 17:20:48 +00001026 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001027 m_result = false;
1028 else
1029 m_result = true;
1030}
1031
1032//------------------------------------------------------------------------------
1033/// @class SiginfoOperation
1034/// @brief Implements ProcessMonitor::GetSignalInfo.
1035class SiginfoOperation : public Operation
1036{
1037public:
Daniel Maleaa35970a2012-11-23 18:09:58 +00001038 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
1039 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001040
1041 void Execute(ProcessMonitor *monitor);
1042
1043private:
1044 lldb::tid_t m_tid;
1045 void *m_info;
1046 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001047 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001048};
1049
1050void
1051SiginfoOperation::Execute(ProcessMonitor *monitor)
1052{
Matt Kopec7de48462013-03-06 17:20:48 +00001053 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001054 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001055 m_err = errno;
1056 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001057 else
1058 m_result = true;
1059}
1060
1061//------------------------------------------------------------------------------
1062/// @class EventMessageOperation
1063/// @brief Implements ProcessMonitor::GetEventMessage.
1064class EventMessageOperation : public Operation
1065{
1066public:
1067 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
1068 : m_tid(tid), m_message(message), m_result(result) { }
1069
1070 void Execute(ProcessMonitor *monitor);
1071
1072private:
1073 lldb::tid_t m_tid;
1074 unsigned long *m_message;
1075 bool &m_result;
1076};
1077
1078void
1079EventMessageOperation::Execute(ProcessMonitor *monitor)
1080{
Matt Kopec7de48462013-03-06 17:20:48 +00001081 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001082 m_result = false;
1083 else
1084 m_result = true;
1085}
1086
1087//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +00001088/// @class DetachOperation
1089/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +00001090class DetachOperation : public Operation
1091{
1092public:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001093 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +00001094
1095 void Execute(ProcessMonitor *monitor);
1096
1097private:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001098 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +00001099 Error &m_error;
1100};
1101
1102void
1103DetachOperation::Execute(ProcessMonitor *monitor)
1104{
Matt Kopec085d6ce2013-05-31 22:00:07 +00001105 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +00001106 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +00001107}
1108
Johnny Chen25e68e32011-06-14 19:19:50 +00001109ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
1110 : m_monitor(monitor)
1111{
1112 sem_init(&m_semaphore, 0, 0);
1113}
1114
1115ProcessMonitor::OperationArgs::~OperationArgs()
1116{
1117 sem_destroy(&m_semaphore);
1118}
1119
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001120ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
1121 lldb_private::Module *module,
1122 char const **argv,
1123 char const **envp,
1124 const char *stdin_path,
1125 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001126 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001127 const char *working_dir,
1128 const lldb_private::ProcessLaunchInfo &launch_info)
Johnny Chen25e68e32011-06-14 19:19:50 +00001129 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001130 m_module(module),
1131 m_argv(argv),
1132 m_envp(envp),
1133 m_stdin_path(stdin_path),
1134 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +00001135 m_stderr_path(stderr_path),
Todd Fiala0bce1b62014-08-17 00:10:50 +00001136 m_working_dir(working_dir),
1137 m_launch_info(launch_info)
1138{
1139}
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001140
1141ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +00001142{ }
1143
1144ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
1145 lldb::pid_t pid)
1146 : OperationArgs(monitor), m_pid(pid) { }
1147
1148ProcessMonitor::AttachArgs::~AttachArgs()
1149{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001150
1151//------------------------------------------------------------------------------
1152/// The basic design of the ProcessMonitor is built around two threads.
1153///
1154/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
1155/// for changes in the debugee state. When a change is detected a
1156/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1157/// "drives" state changes in the debugger.
1158///
1159/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001160/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001161/// operations such as register reads/writes, stepping, etc. See the comments
1162/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001163ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001164 Module *module,
1165 const char *argv[],
1166 const char *envp[],
1167 const char *stdin_path,
1168 const char *stdout_path,
1169 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001170 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001171 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001172 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001173 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001174 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001175 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001176 m_pid(LLDB_INVALID_PROCESS_ID),
1177 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001178 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001179{
Daniel Malea1efb4182013-09-16 23:12:18 +00001180 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1181 stdin_path, stdout_path, stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001182 working_dir, launch_info));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001183
Daniel Malea1efb4182013-09-16 23:12:18 +00001184 sem_init(&m_operation_pending, 0, 0);
1185 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001186
Johnny Chen25e68e32011-06-14 19:19:50 +00001187 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001188 if (!error.Success())
1189 return;
1190
1191WAIT_AGAIN:
1192 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001193 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001194 {
1195 if (errno == EINTR)
1196 goto WAIT_AGAIN;
1197 else
1198 {
1199 error.SetErrorToErrno();
1200 return;
1201 }
1202 }
1203
1204 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001205 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001206 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001207 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001208 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001209 return;
1210 }
1211
1212 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001213 m_monitor_thread = Host::StartMonitoringChildProcess(
1214 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001215 if (!m_monitor_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001216 {
1217 error.SetErrorToGenericError();
1218 error.SetErrorString("Process launch failed.");
1219 return;
1220 }
1221}
1222
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001223ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001224 lldb::pid_t pid,
1225 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001226 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001227 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001228 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001229 m_pid(LLDB_INVALID_PROCESS_ID),
1230 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001231 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001232{
Daniel Malea1efb4182013-09-16 23:12:18 +00001233 sem_init(&m_operation_pending, 0, 0);
1234 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001235
Daniel Malea1efb4182013-09-16 23:12:18 +00001236 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001237
1238 StartAttachOpThread(args.get(), error);
1239 if (!error.Success())
1240 return;
1241
1242WAIT_AGAIN:
1243 // Wait for the operation thread to initialize.
1244 if (sem_wait(&args->m_semaphore))
1245 {
1246 if (errno == EINTR)
1247 goto WAIT_AGAIN;
1248 else
1249 {
1250 error.SetErrorToErrno();
1251 return;
1252 }
1253 }
1254
Greg Clayton743ecf42012-10-16 20:20:18 +00001255 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001256 if (!args->m_error.Success())
1257 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001258 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001259 error = args->m_error;
1260 return;
1261 }
1262
1263 // Finally, start monitoring the child process for change in state.
1264 m_monitor_thread = Host::StartMonitoringChildProcess(
1265 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001266 if (!m_monitor_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001267 {
1268 error.SetErrorToGenericError();
1269 error.SetErrorString("Process attach failed.");
1270 return;
1271 }
1272}
1273
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001274ProcessMonitor::~ProcessMonitor()
1275{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001276 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001277}
1278
1279//------------------------------------------------------------------------------
1280// Thread setup and tear down.
1281void
Johnny Chen25e68e32011-06-14 19:19:50 +00001282ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001283{
1284 static const char *g_thread_name = "lldb.process.linux.operation";
1285
Zachary Turneracee96a2014-09-23 18:32:09 +00001286 if (m_operation_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001287 return;
1288
Zachary Turner39de3112014-09-09 20:54:56 +00001289 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001290}
1291
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001292void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001293ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001294{
1295 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1296
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001297 if (!Launch(args)) {
1298 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001299 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001300 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001301
Stephen Wilson570243b2011-01-19 01:37:06 +00001302 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001303 return NULL;
1304}
1305
1306bool
1307ProcessMonitor::Launch(LaunchArgs *args)
1308{
Todd Fiala0bce1b62014-08-17 00:10:50 +00001309 assert (args && "null args");
1310 if (!args)
1311 return false;
1312
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001313 ProcessMonitor *monitor = args->m_monitor;
1314 ProcessLinux &process = monitor->GetProcess();
1315 const char **argv = args->m_argv;
1316 const char **envp = args->m_envp;
1317 const char *stdin_path = args->m_stdin_path;
1318 const char *stdout_path = args->m_stdout_path;
1319 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001320 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001321
1322 lldb_utility::PseudoTerminal terminal;
1323 const size_t err_len = 1024;
1324 char err_str[err_len];
1325 lldb::pid_t pid;
1326
1327 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001328 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001329
Stephen Wilson57740ec2011-01-15 00:12:41 +00001330 // Propagate the environment if one is not supplied.
1331 if (envp == NULL || envp[0] == NULL)
1332 envp = const_cast<const char **>(environ);
1333
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001334 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001335 {
1336 args->m_error.SetErrorToGenericError();
1337 args->m_error.SetErrorString("Process fork failed.");
1338 goto FINISH;
1339 }
1340
Peter Collingbourne6a520222011-06-14 03:55:58 +00001341 // Recognized child exit status codes.
1342 enum {
1343 ePtraceFailed = 1,
1344 eDupStdinFailed,
1345 eDupStdoutFailed,
1346 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001347 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001348 eExecFailed,
1349 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001350 };
1351
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001352 // Child process.
1353 if (pid == 0)
1354 {
1355 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001356 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001357 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001358
Pavel Labath493c3a12015-02-04 10:36:57 +00001359 // terminal has already dupped the tty descriptors to stdin/out/err.
1360 // This closes original fd from which they were copied (and avoids
1361 // leaking descriptors to the debugged process.
1362 terminal.CloseSlaveFileDescriptor();
1363
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001364 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001365 if (setgid(getgid()) != 0)
1366 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001367
1368 // Let us have our own process group.
1369 setpgid(0, 0);
1370
Greg Clayton710dd5a2011-01-08 20:28:42 +00001371 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001372 //
1373 // FIXME: If two or more of the paths are the same we needlessly open
1374 // the same file multiple times.
1375 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001376 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001377 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001378
1379 if (stdout_path != NULL && stdout_path[0])
1380 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001381 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001382
1383 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001384 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001385 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001386
Daniel Malea6217d2a2013-01-08 14:49:22 +00001387 // Change working directory
1388 if (working_dir != NULL && working_dir[0])
1389 if (0 != ::chdir(working_dir))
1390 exit(eChdirFailed);
1391
Todd Fiala0bce1b62014-08-17 00:10:50 +00001392 // Disable ASLR if requested.
1393 if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
1394 {
1395 const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
1396 if (old_personality == -1)
1397 {
1398 if (log)
1399 log->Printf ("ProcessMonitor::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno));
1400 }
1401 else
1402 {
1403 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
1404 if (new_personality == -1)
1405 {
1406 if (log)
1407 log->Printf ("ProcessMonitor::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno));
1408
1409 }
1410 else
1411 {
1412 if (log)
Todd Fiala850f9a22014-09-19 18:27:45 +00001413 log->Printf ("ProcessMonitor::%s disabling ASLR: SUCCESS", __FUNCTION__);
Todd Fiala0bce1b62014-08-17 00:10:50 +00001414
1415 }
1416 }
1417 }
1418
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001419 // Execute. We should never return.
1420 execve(argv[0],
1421 const_cast<char *const *>(argv),
1422 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001423 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001424 }
1425
1426 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001427 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001428 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001429 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001430
1431 raw_pid = waitpid(pid, &status, 0);
1432 wpid = static_cast <lldb::pid_t> (raw_pid);
1433 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001434 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001435 args->m_error.SetErrorToErrno();
1436 goto FINISH;
1437 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001438 else if (WIFEXITED(status))
1439 {
1440 // open, dup or execve likely failed for some reason.
1441 args->m_error.SetErrorToGenericError();
1442 switch (WEXITSTATUS(status))
1443 {
Greg Clayton542e4072012-09-07 17:49:29 +00001444 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001445 args->m_error.SetErrorString("Child ptrace failed.");
1446 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001447 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001448 args->m_error.SetErrorString("Child open stdin failed.");
1449 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001450 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001451 args->m_error.SetErrorString("Child open stdout failed.");
1452 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001453 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001454 args->m_error.SetErrorString("Child open stderr failed.");
1455 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001456 case eChdirFailed:
1457 args->m_error.SetErrorString("Child failed to set working directory.");
1458 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001459 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001460 args->m_error.SetErrorString("Child exec failed.");
1461 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001462 case eSetGidFailed:
1463 args->m_error.SetErrorString("Child setgid failed.");
1464 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001465 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001466 args->m_error.SetErrorString("Child returned unknown exit status.");
1467 break;
1468 }
1469 goto FINISH;
1470 }
1471 assert(WIFSTOPPED(status) && wpid == pid &&
1472 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001473
Matt Kopec085d6ce2013-05-31 22:00:07 +00001474 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001475 {
1476 args->m_error.SetErrorToErrno();
1477 goto FINISH;
1478 }
1479
1480 // Release the master terminal descriptor and pass it off to the
1481 // ProcessMonitor instance. Similarly stash the inferior pid.
1482 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1483 monitor->m_pid = pid;
1484
Stephen Wilson26977162011-03-23 02:14:42 +00001485 // Set the terminal fd to be in non blocking mode (it simplifies the
1486 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1487 // descriptor to read from).
1488 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1489 goto FINISH;
1490
Johnny Chen30213ff2012-01-05 19:17:38 +00001491 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001492 // FIXME: should we be letting UpdateThreadList handle this?
1493 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001494 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001495
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001496 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001497 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001498 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001499
Matt Kopecb2910442013-07-09 15:09:45 +00001500 process.AddThreadForInitialStopIfNeeded(pid);
1501
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001502 // Let our process instance know the thread has stopped.
1503 process.SendMessage(ProcessMessage::Trace(pid));
1504
1505FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001506 return args->m_error.Success();
1507}
1508
Johnny Chen25e68e32011-06-14 19:19:50 +00001509void
1510ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1511{
1512 static const char *g_thread_name = "lldb.process.linux.operation";
1513
Zachary Turneracee96a2014-09-23 18:32:09 +00001514 if (m_operation_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001515 return;
1516
Zachary Turner39de3112014-09-09 20:54:56 +00001517 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen25e68e32011-06-14 19:19:50 +00001518}
1519
Johnny Chen25e68e32011-06-14 19:19:50 +00001520void *
1521ProcessMonitor::AttachOpThread(void *arg)
1522{
1523 AttachArgs *args = static_cast<AttachArgs*>(arg);
1524
Greg Clayton743ecf42012-10-16 20:20:18 +00001525 if (!Attach(args)) {
1526 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001527 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001528 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001529
1530 ServeOperation(args);
1531 return NULL;
1532}
1533
1534bool
1535ProcessMonitor::Attach(AttachArgs *args)
1536{
1537 lldb::pid_t pid = args->m_pid;
1538
1539 ProcessMonitor *monitor = args->m_monitor;
1540 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001541 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001542 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001543
Matt Kopec085d6ce2013-05-31 22:00:07 +00001544 // Use a map to keep track of the threads which we have attached/need to attach.
1545 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001546 if (pid <= 1)
1547 {
1548 args->m_error.SetErrorToGenericError();
1549 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1550 goto FINISH;
1551 }
1552
Matt Kopec085d6ce2013-05-31 22:00:07 +00001553 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001554 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001555 for (Host::TidMap::iterator it = tids_to_attach.begin();
1556 it != tids_to_attach.end(); ++it)
1557 {
1558 if (it->second == false)
1559 {
1560 lldb::tid_t tid = it->first;
1561
1562 // Attach to the requested process.
1563 // An attach will cause the thread to stop with a SIGSTOP.
1564 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1565 {
1566 // No such thread. The thread may have exited.
1567 // More error handling may be needed.
1568 if (errno == ESRCH)
1569 {
1570 tids_to_attach.erase(it);
1571 continue;
1572 }
1573 else
1574 {
1575 args->m_error.SetErrorToErrno();
1576 goto FINISH;
1577 }
1578 }
1579
Todd Fiala9be50492014-07-01 16:30:53 +00001580 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001581 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1582 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001583 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001584 {
1585 // No such thread. The thread may have exited.
1586 // More error handling may be needed.
1587 if (errno == ESRCH)
1588 {
1589 tids_to_attach.erase(it);
1590 continue;
1591 }
1592 else
1593 {
1594 args->m_error.SetErrorToErrno();
1595 goto FINISH;
1596 }
1597 }
1598
1599 if (!SetDefaultPtraceOpts(tid))
1600 {
1601 args->m_error.SetErrorToErrno();
1602 goto FINISH;
1603 }
1604
1605 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001606 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001607
Matt Kopec085d6ce2013-05-31 22:00:07 +00001608 if (log)
1609 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1610 process.GetThreadList().AddThread(inferior);
1611 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001612 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001613 }
1614 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001615 }
1616
Matt Kopec085d6ce2013-05-31 22:00:07 +00001617 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001618 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001619 monitor->m_pid = pid;
1620 // Let our process instance know the thread has stopped.
1621 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001622 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001623 else
1624 {
1625 args->m_error.SetErrorToGenericError();
1626 args->m_error.SetErrorString("No such process.");
1627 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001628
1629 FINISH:
1630 return args->m_error.Success();
1631}
1632
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001633bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001634ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1635{
1636 long ptrace_opts = 0;
1637
1638 // Have the child raise an event on exit. This is used to keep the child in
1639 // limbo until it is destroyed.
1640 ptrace_opts |= PTRACE_O_TRACEEXIT;
1641
1642 // Have the tracer trace threads which spawn in the inferior process.
1643 // TODO: if we want to support tracing the inferiors' child, add the
1644 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1645 ptrace_opts |= PTRACE_O_TRACECLONE;
1646
1647 // Have the tracer notify us before execve returns
1648 // (needed to disable legacy SIGTRAP generation)
1649 ptrace_opts |= PTRACE_O_TRACEEXEC;
1650
1651 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1652}
1653
1654bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001655ProcessMonitor::MonitorCallback(void *callback_baton,
1656 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001657 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001658 int signal,
1659 int status)
1660{
1661 ProcessMessage message;
1662 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001663 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001664 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001665 bool stop_monitoring;
1666 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001667 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001668
Andrew Kaylor93132f52013-05-28 23:04:25 +00001669 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1670
1671 if (exited)
1672 {
1673 if (log)
1674 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1675 message = ProcessMessage::Exit(pid, status);
1676 process->SendMessage(message);
1677 return pid == process->GetID();
1678 }
1679
Daniel Maleaa35970a2012-11-23 18:09:58 +00001680 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1681 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001682 if (log)
1683 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001684 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1685 if (!monitor->Resume(pid, SIGSTOP)) {
1686 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1687 }
1688 stop_monitoring = false;
1689 } else {
1690 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1691 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001692 // stop the monitor thread if this is the main pid.
1693 if (log)
1694 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1695 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1696 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001697 // If we are going to stop monitoring, we need to notify our process object
1698 if (stop_monitoring)
1699 {
1700 message = ProcessMessage::Exit(pid, status);
1701 process->SendMessage(message);
1702 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001703 }
1704 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001705 else {
1706 switch (info.si_signo)
1707 {
1708 case SIGTRAP:
1709 message = MonitorSIGTRAP(monitor, &info, pid);
1710 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001711
Stephen Wilson84ffe702011-03-30 15:55:52 +00001712 default:
1713 message = MonitorSignal(monitor, &info, pid);
1714 break;
1715 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001716
Stephen Wilson84ffe702011-03-30 15:55:52 +00001717 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001718 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001719 }
1720
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001721 return stop_monitoring;
1722}
1723
1724ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001725ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001726 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001727{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001728 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001729
Andrew Kaylor93132f52013-05-28 23:04:25 +00001730 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1731
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001732 assert(monitor);
1733 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001734
Stephen Wilson84ffe702011-03-30 15:55:52 +00001735 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001736 {
1737 default:
1738 assert(false && "Unexpected SIGTRAP code!");
1739 break;
1740
Matt Kopeca360d7e2013-05-17 19:27:47 +00001741 // TODO: these two cases are required if we want to support tracing
1742 // of the inferiors' children
1743 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1744 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1745
Matt Kopec650648f2013-01-08 16:30:18 +00001746 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1747 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001748 if (log)
1749 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1750
Matt Kopec650648f2013-01-08 16:30:18 +00001751 unsigned long tid = 0;
1752 if (!monitor->GetEventMessage(pid, &tid))
1753 tid = -1;
1754 message = ProcessMessage::NewThread(pid, tid);
1755 break;
1756 }
1757
Matt Kopeca360d7e2013-05-17 19:27:47 +00001758 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001759 if (log)
1760 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1761
1762 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001763 break;
1764
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001765 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1766 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001767 // The inferior process or one of its threads is about to exit.
1768 // Maintain the process or thread in a state of "limbo" until we are
1769 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001770 unsigned long data = 0;
1771 if (!monitor->GetEventMessage(pid, &data))
1772 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001773 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001774 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001775 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001776 break;
1777 }
1778
1779 case 0:
1780 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001781 if (log)
1782 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001783 message = ProcessMessage::Trace(pid);
1784 break;
1785
1786 case SI_KERNEL:
1787 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001788 if (log)
1789 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001790 message = ProcessMessage::Break(pid);
1791 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001792
1793 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001794 if (log)
1795 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001796 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1797 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001798
1799 case SIGTRAP:
1800 case (SIGTRAP | 0x80):
1801 if (log)
1802 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1803 // Ignore these signals until we know more about them
1804 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001805 }
1806
1807 return message;
1808}
1809
Stephen Wilson84ffe702011-03-30 15:55:52 +00001810ProcessMessage
1811ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001812 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001813{
1814 ProcessMessage message;
1815 int signo = info->si_signo;
1816
Andrew Kaylor93132f52013-05-28 23:04:25 +00001817 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1818
Stephen Wilson84ffe702011-03-30 15:55:52 +00001819 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1820 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1821 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1822 //
1823 // IOW, user generated signals never generate what we consider to be a
1824 // "crash".
1825 //
1826 // Similarly, ACK signals generated by this monitor.
1827 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1828 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001829 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001830 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001831 __FUNCTION__,
1832 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1833 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1834 info->si_pid);
1835
Stephen Wilson84ffe702011-03-30 15:55:52 +00001836 if (info->si_pid == getpid())
1837 return ProcessMessage::SignalDelivered(pid, signo);
1838 else
1839 return ProcessMessage::Signal(pid, signo);
1840 }
1841
Andrew Kaylor93132f52013-05-28 23:04:25 +00001842 if (log)
1843 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1844
Chaoren Lin28e57422015-02-03 01:51:25 +00001845 switch (signo)
1846 {
1847 case SIGSEGV:
1848 case SIGILL:
1849 case SIGFPE:
1850 case SIGBUS:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001851 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
Chaoren Lin28e57422015-02-03 01:51:25 +00001852 const auto reason = GetCrashReason(*info);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001853 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1854 }
1855
1856 // Everything else is "normal" and does not require any special action on
1857 // our part.
1858 return ProcessMessage::Signal(pid, signo);
1859}
1860
Andrew Kaylord4d54992013-09-17 00:30:24 +00001861// On Linux, when a new thread is created, we receive to notifications,
1862// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1863// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1864// the new child thread indicating that it has is stopped because we attached.
1865// We have no guarantee of the order in which these arrive, but we need both
1866// before we are ready to proceed. We currently keep a list of threads which
1867// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1868// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1869// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1870
1871bool
1872ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1873{
1874 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1875 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001876 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001877
1878 // Wait for the thread to stop
1879 while (true)
1880 {
1881 int status = -1;
1882 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001883 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001884 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001885 if (status == -1)
1886 {
1887 // If we got interrupted by a signal (in our process, not the
1888 // inferior) try again.
1889 if (errno == EINTR)
1890 continue;
1891 else
1892 {
1893 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001894 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001895 return false; // This is bad, but there's nothing we can do.
1896 }
1897 }
1898
1899 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001900 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001901
Todd Fiala9be50492014-07-01 16:30:53 +00001902 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001903
1904 siginfo_t info;
1905 int ptrace_err;
1906 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1907 {
1908 if (log)
1909 {
1910 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1911 }
1912 return false;
1913 }
1914
1915 // If this is a thread exit, we won't get any more information.
1916 if (WIFEXITED(status))
1917 {
1918 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001919 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001920 return true;
1921 continue;
1922 }
1923
1924 assert(info.si_code == SI_USER);
1925 assert(WSTOPSIG(status) == SIGSTOP);
1926
1927 if (log)
1928 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1929 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1930 return true;
1931 }
1932 return false;
1933}
1934
Andrew Kaylor93132f52013-05-28 23:04:25 +00001935bool
1936ProcessMonitor::StopThread(lldb::tid_t tid)
1937{
1938 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1939
1940 // FIXME: Try to use tgkill or tkill
1941 int ret = tgkill(m_pid, tid, SIGSTOP);
1942 if (log)
1943 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1944
1945 // This can happen if a thread exited while we were trying to stop it. That's OK.
1946 // We'll get the signal for that later.
1947 if (ret < 0)
1948 return false;
1949
1950 // Wait for the thread to stop
1951 while (true)
1952 {
1953 int status = -1;
1954 if (log)
1955 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001956 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001957 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001958 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1959 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001960
Todd Fiala9be50492014-07-01 16:30:53 +00001961 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001962 {
1963 // If we got interrupted by a signal (in our process, not the
1964 // inferior) try again.
1965 if (errno == EINTR)
1966 continue;
1967 else
1968 return false; // This is bad, but there's nothing we can do.
1969 }
1970
1971 // If this is a thread exit, we won't get any more information.
1972 if (WIFEXITED(status))
1973 {
1974 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001975 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001976 return true;
1977 continue;
1978 }
1979
1980 siginfo_t info;
1981 int ptrace_err;
1982 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1983 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001984 // another signal causing a StopAllThreads may have been received
1985 // before wait_pid's group-stop was processed, handle it now
1986 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001987 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001988 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001989
Todd Fiala1b0539c2014-01-27 17:03:57 +00001990 if (log)
1991 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1992 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1993 if (!Resume(wait_pid, SIGSTOP)) {
1994 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1995 }
1996 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001997 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00001998
1999 if (log)
2000 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002001 return false;
2002 }
2003
2004 // Handle events from other threads
2005 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00002006 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
2007 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00002008
2009 ProcessMessage message;
2010 if (info.si_signo == SIGTRAP)
2011 message = MonitorSIGTRAP(this, &info, wait_pid);
2012 else
2013 message = MonitorSignal(this, &info, wait_pid);
2014
2015 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
2016
2017 // When a new thread is created, we may get a SIGSTOP for the new thread
2018 // just before we get the SIGTRAP that we use to add the thread to our
2019 // process thread list. We don't need to worry about that signal here.
2020 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
2021
2022 if (!thread)
2023 {
2024 m_process->SendMessage(message);
2025 continue;
2026 }
2027
2028 switch (message.GetKind())
2029 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00002030 case ProcessMessage::eExecMessage:
2031 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00002032 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00002033 case ProcessMessage::eInvalidMessage:
2034 break;
2035
2036 // These need special handling because we don't want to send a
2037 // resume even if we already sent a SIGSTOP to this thread. In
2038 // this case the resume will cause the thread to disappear. It is
2039 // unlikely that we'll ever get eExitMessage here, but the same
2040 // reasoning applies.
2041 case ProcessMessage::eLimboMessage:
2042 case ProcessMessage::eExitMessage:
2043 if (log)
2044 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2045 // SendMessage will set the thread state as needed.
2046 m_process->SendMessage(message);
2047 // If this is the thread we're waiting for, stop waiting. Even
2048 // though this wasn't the signal we expected, it's the last
2049 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00002050 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002051 return true;
2052 break;
2053
Matt Kopecb2910442013-07-09 15:09:45 +00002054 case ProcessMessage::eSignalMessage:
2055 if (log)
2056 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2057 if (WSTOPSIG(status) == SIGSTOP)
2058 {
2059 m_process->AddThreadForInitialStopIfNeeded(tid);
2060 thread->SetState(lldb::eStateStopped);
2061 }
2062 else
2063 {
2064 m_process->SendMessage(message);
2065 // This isn't the stop we were expecting, but the thread is
2066 // stopped. SendMessage will handle processing of this event,
2067 // but we need to resume here to get the stop we are waiting
2068 // for (otherwise the thread will stop again immediately when
2069 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002070 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00002071 Resume(wait_pid, eResumeSignalNone);
2072 }
2073 break;
2074
Andrew Kaylor93132f52013-05-28 23:04:25 +00002075 case ProcessMessage::eSignalDeliveredMessage:
2076 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00002077 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
2078 WIFSTOPPED(status) &&
2079 WSTOPSIG(status) == SIGSTOP &&
2080 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002081 {
2082 if (log)
2083 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
2084 thread->SetState(lldb::eStateStopped);
2085 return true;
2086 }
2087 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00002088 case ProcessMessage::eBreakpointMessage:
2089 case ProcessMessage::eTraceMessage:
2090 case ProcessMessage::eWatchpointMessage:
2091 case ProcessMessage::eCrashMessage:
2092 case ProcessMessage::eNewThreadMessage:
2093 if (log)
2094 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2095 // SendMessage will set the thread state as needed.
2096 m_process->SendMessage(message);
2097 // This isn't the stop we were expecting, but the thread is
2098 // stopped. SendMessage will handle processing of this event,
2099 // but we need to resume here to get the stop we are waiting
2100 // for (otherwise the thread will stop again immediately when
2101 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002102 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002103 Resume(wait_pid, eResumeSignalNone);
2104 break;
2105 }
2106 }
2107 return false;
2108}
2109
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002110void
Johnny Chen25e68e32011-06-14 19:19:50 +00002111ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002112{
Stephen Wilson570243b2011-01-19 01:37:06 +00002113 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002114
Stephen Wilson570243b2011-01-19 01:37:06 +00002115 // We are finised with the arguments and are ready to go. Sync with the
2116 // parent thread and start serving operations on the inferior.
2117 sem_post(&args->m_semaphore);
2118
Michael Sartain704bf892013-10-09 01:28:57 +00002119 for(;;)
2120 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002121 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002122 if (sem_wait(&monitor->m_operation_pending))
2123 {
2124 if (errno == EINTR)
2125 continue;
2126 assert(false && "Unexpected errno from sem_wait");
2127 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002128
Daniel Malea1efb4182013-09-16 23:12:18 +00002129 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002130
Daniel Malea1efb4182013-09-16 23:12:18 +00002131 // notify calling thread that operation is complete
2132 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002133 }
2134}
2135
2136void
2137ProcessMonitor::DoOperation(Operation *op)
2138{
Daniel Malea1efb4182013-09-16 23:12:18 +00002139 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002140
Daniel Malea1efb4182013-09-16 23:12:18 +00002141 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002142
Daniel Malea1efb4182013-09-16 23:12:18 +00002143 // notify operation thread that an operation is ready to be processed
2144 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002145
Daniel Malea1efb4182013-09-16 23:12:18 +00002146 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002147 while (sem_wait(&m_operation_done))
2148 {
2149 if (errno == EINTR)
2150 continue;
2151 assert(false && "Unexpected errno from sem_wait");
2152 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002153}
2154
2155size_t
2156ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2157 Error &error)
2158{
2159 size_t result;
2160 ReadOperation op(vm_addr, buf, size, error, result);
2161 DoOperation(&op);
2162 return result;
2163}
2164
2165size_t
2166ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2167 lldb_private::Error &error)
2168{
2169 size_t result;
2170 WriteOperation op(vm_addr, buf, size, error, result);
2171 DoOperation(&op);
2172 return result;
2173}
2174
2175bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002176ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002177 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002178{
2179 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002180 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002181 DoOperation(&op);
2182 return result;
2183}
2184
2185bool
Matt Kopec7de48462013-03-06 17:20:48 +00002186ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002187 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002188{
2189 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002190 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002191 DoOperation(&op);
2192 return result;
2193}
2194
2195bool
Matt Kopec7de48462013-03-06 17:20:48 +00002196ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002197{
2198 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002199 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002200 DoOperation(&op);
2201 return result;
2202}
2203
2204bool
Matt Kopec7de48462013-03-06 17:20:48 +00002205ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002206{
2207 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002208 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002209 DoOperation(&op);
2210 return result;
2211}
2212
2213bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002214ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2215{
2216 bool result;
2217 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2218 DoOperation(&op);
2219 return result;
2220}
2221
2222bool
Matt Kopec7de48462013-03-06 17:20:48 +00002223ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002224{
2225 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002226 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002227 DoOperation(&op);
2228 return result;
2229}
2230
2231bool
Matt Kopec7de48462013-03-06 17:20:48 +00002232ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002233{
2234 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002235 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002236 DoOperation(&op);
2237 return result;
2238}
2239
2240bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002241ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2242{
2243 bool result;
2244 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2245 DoOperation(&op);
2246 return result;
2247}
2248
2249bool
Richard Mitton0a558352013-10-17 21:14:00 +00002250ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2251{
2252 bool result;
2253 ReadThreadPointerOperation op(tid, &value, result);
2254 DoOperation(&op);
2255 return result;
2256}
2257
2258bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002259ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002260{
2261 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002262 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2263
2264 if (log)
2265 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2266 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002267 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002268 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002269 if (log)
2270 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002271 return result;
2272}
2273
2274bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002275ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002276{
2277 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002278 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002279 DoOperation(&op);
2280 return result;
2281}
2282
2283bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002284ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002285{
Ed Maste4e0999b2014-04-01 18:14:06 +00002286 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002287}
2288
2289bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002290ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002291{
2292 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002293 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002294 DoOperation(&op);
2295 return result;
2296}
2297
2298bool
2299ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2300{
2301 bool result;
2302 EventMessageOperation op(tid, message, result);
2303 DoOperation(&op);
2304 return result;
2305}
2306
Greg Clayton743ecf42012-10-16 20:20:18 +00002307lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002308ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002309{
Greg Clayton28041352011-11-29 20:50:10 +00002310 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002311 if (tid != LLDB_INVALID_THREAD_ID)
2312 {
2313 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002314 DoOperation(&op);
2315 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002316 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002317}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002318
2319bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002320ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2321{
Peter Collingbourne62343202011-06-14 03:55:54 +00002322 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002323
2324 if (target_fd == -1)
2325 return false;
2326
Pavel Labath493c3a12015-02-04 10:36:57 +00002327 if (dup2(target_fd, fd) == -1)
2328 return false;
2329
2330 return (close(target_fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002331}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002332
2333void
2334ProcessMonitor::StopMonitoringChildProcess()
2335{
Zachary Turneracee96a2014-09-23 18:32:09 +00002336 if (m_monitor_thread.IsJoinable())
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002337 {
Zachary Turner39de3112014-09-09 20:54:56 +00002338 m_monitor_thread.Cancel();
2339 m_monitor_thread.Join(nullptr);
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002340 }
2341}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002342
2343void
2344ProcessMonitor::StopMonitor()
2345{
2346 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002347 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002348 sem_destroy(&m_operation_pending);
2349 sem_destroy(&m_operation_done);
Pavel Labath3a2da9e2015-02-06 11:32:52 +00002350 if (m_terminal_fd >= 0) {
2351 close(m_terminal_fd);
2352 m_terminal_fd = -1;
2353 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00002354}
2355
2356void
Greg Clayton743ecf42012-10-16 20:20:18 +00002357ProcessMonitor::StopOpThread()
2358{
Zachary Turneracee96a2014-09-23 18:32:09 +00002359 if (!m_operation_thread.IsJoinable())
Greg Clayton743ecf42012-10-16 20:20:18 +00002360 return;
2361
Zachary Turner39de3112014-09-09 20:54:56 +00002362 m_operation_thread.Cancel();
2363 m_operation_thread.Join(nullptr);
Greg Clayton743ecf42012-10-16 20:20:18 +00002364}