blob: 00d4f44cbe3012d616786f07c641ea90c0eb0f2c [file] [log] [blame]
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001//===-- ProcessMonitor.cpp ------------------------------------ -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Stephen Wilsone6f9f662010-07-24 02:19:04 +000012// C Includes
13#include <errno.h>
14#include <poll.h>
15#include <string.h>
Daniel Maleaa85e6b62012-12-07 22:21:08 +000016#include <stdint.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000017#include <unistd.h>
Todd Fiala42079682014-08-27 16:05:26 +000018#include <elf.h>
Stephen Wilsone6f9f662010-07-24 02:19:04 +000019
20// C++ Includes
21// Other libraries and framework includes
Johnny Chen0d5f2d42011-10-18 18:09:30 +000022#include "lldb/Core/Debugger.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000023#include "lldb/Core/Error.h"
Johnny Chen13e8e1c2011-05-13 21:29:50 +000024#include "lldb/Core/RegisterValue.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000025#include "lldb/Core/Scalar.h"
26#include "lldb/Host/Host.h"
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +000027#include "lldb/Host/HostNativeThread.h"
Zachary Turner39de3112014-09-09 20:54:56 +000028#include "lldb/Host/HostThread.h"
29#include "lldb/Host/ThreadLauncher.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000030#include "lldb/Target/Thread.h"
31#include "lldb/Target/RegisterContext.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000032#include "lldb/Target/UnixSignals.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000033#include "lldb/Utility/PseudoTerminal.h"
34
Chaoren Lin28e57422015-02-03 01:51:25 +000035#include "Plugins/Process/POSIX/CrashReason.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000036#include "Plugins/Process/POSIX/POSIXThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000037#include "ProcessLinux.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000038#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000039#include "ProcessMonitor.h"
40
Tamas Berghammerd8584872015-02-06 10:57:40 +000041// System includes - They have to be included after framework includes because they define some
42// macros which collide with variable names in other modules
43#ifndef __ANDROID__
44#include <sys/procfs.h>
45#endif
46#include <sys/personality.h>
47#include <sys/ptrace.h>
48#include <sys/socket.h>
49#include <sys/syscall.h>
50#include <sys/types.h>
51#include <sys/uio.h>
52#include <sys/user.h>
53#include <sys/wait.h>
54
Todd Fialacacde7d2014-09-27 16:54:22 +000055#ifdef __ANDROID__
56#define __ptrace_request int
57#define PT_DETACH PTRACE_DETACH
58#endif
59
Greg Clayton386ff182011-11-05 01:09:16 +000060#define DEBUG_PTRACE_MAXBYTES 20
61
Matt Kopec58c0b962013-03-20 20:34:35 +000062// Support ptrace extensions even when compiled without required kernel support
63#ifndef PTRACE_GETREGSET
64 #define PTRACE_GETREGSET 0x4204
65#endif
66#ifndef PTRACE_SETREGSET
67 #define PTRACE_SETREGSET 0x4205
68#endif
Richard Mitton0a558352013-10-17 21:14:00 +000069#ifndef PTRACE_GET_THREAD_AREA
70 #define PTRACE_GET_THREAD_AREA 25
71#endif
72#ifndef PTRACE_ARCH_PRCTL
73 #define PTRACE_ARCH_PRCTL 30
74#endif
75#ifndef ARCH_GET_FS
76 #define ARCH_SET_GS 0x1001
77 #define ARCH_SET_FS 0x1002
78 #define ARCH_GET_FS 0x1003
79 #define ARCH_GET_GS 0x1004
80#endif
81
Todd Fiala0bce1b62014-08-17 00:10:50 +000082#define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff
Matt Kopec58c0b962013-03-20 20:34:35 +000083
Todd Fialadbec1ff2014-09-04 16:08:20 +000084#define LLDB_PTRACE_NT_ARM_TLS 0x401 // ARM TLS register
85
Matt Kopece9ea0da2013-05-07 19:29:28 +000086// Support hardware breakpoints in case it has not been defined
87#ifndef TRAP_HWBKPT
88 #define TRAP_HWBKPT 4
89#endif
90
Andrew Kaylor93132f52013-05-28 23:04:25 +000091// Try to define a macro to encapsulate the tgkill syscall
92// fall back on kill() if tgkill isn't available
Chaoren Linc9346592015-02-28 00:20:16 +000093#define tgkill(pid, tid, sig) \
94 syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig)
Andrew Kaylor93132f52013-05-28 23:04:25 +000095
Stephen Wilsone6f9f662010-07-24 02:19:04 +000096using namespace lldb_private;
97
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +000098static Operation* EXIT_OPERATION = nullptr;
99
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000100// FIXME: this code is host-dependent with respect to types and
101// endianness and needs to be fixed. For example, lldb::addr_t is
102// hard-coded to uint64_t, but on a 32-bit Linux host, ptrace requires
103// 32-bit pointer arguments. This code uses casts to work around the
104// problem.
105
106// We disable the tracing of ptrace calls for integration builds to
107// avoid the additional indirection and checks.
108#ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION
109
Greg Clayton386ff182011-11-05 01:09:16 +0000110static void
111DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count)
112{
113 uint8_t *ptr = (uint8_t *)bytes;
114 const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count);
115 for(uint32_t i=0; i<loop_count; i++)
116 {
117 s.Printf ("[%x]", *ptr);
118 ptr++;
119 }
120}
121
Matt Kopec58c0b962013-03-20 20:34:35 +0000122static void PtraceDisplayBytes(int &req, void *data, size_t data_size)
Greg Clayton386ff182011-11-05 01:09:16 +0000123{
124 StreamString buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000125 Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (
Johnny Chen30213ff2012-01-05 19:17:38 +0000126 POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE));
Greg Clayton386ff182011-11-05 01:09:16 +0000127
128 if (verbose_log)
129 {
130 switch(req)
131 {
132 case PTRACE_POKETEXT:
133 {
134 DisplayBytes(buf, &data, 8);
135 verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData());
136 break;
137 }
Greg Clayton542e4072012-09-07 17:49:29 +0000138 case PTRACE_POKEDATA:
Greg Clayton386ff182011-11-05 01:09:16 +0000139 {
140 DisplayBytes(buf, &data, 8);
141 verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData());
142 break;
143 }
Greg Clayton542e4072012-09-07 17:49:29 +0000144 case PTRACE_POKEUSER:
Greg Clayton386ff182011-11-05 01:09:16 +0000145 {
146 DisplayBytes(buf, &data, 8);
147 verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData());
148 break;
149 }
Todd Fiala6ac1be42014-08-21 16:34:03 +0000150#if !defined (__arm64__) && !defined (__aarch64__)
Greg Clayton542e4072012-09-07 17:49:29 +0000151 case PTRACE_SETREGS:
Greg Clayton386ff182011-11-05 01:09:16 +0000152 {
Matt Kopec7de48462013-03-06 17:20:48 +0000153 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000154 verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData());
155 break;
156 }
157 case PTRACE_SETFPREGS:
158 {
Matt Kopec7de48462013-03-06 17:20:48 +0000159 DisplayBytes(buf, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000160 verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData());
161 break;
162 }
Todd Fialad35f2b92014-06-23 15:59:04 +0000163#endif
Greg Clayton542e4072012-09-07 17:49:29 +0000164 case PTRACE_SETSIGINFO:
Greg Clayton386ff182011-11-05 01:09:16 +0000165 {
166 DisplayBytes(buf, data, sizeof(siginfo_t));
167 verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData());
168 break;
169 }
Matt Kopec58c0b962013-03-20 20:34:35 +0000170 case PTRACE_SETREGSET:
171 {
172 // Extract iov_base from data, which is a pointer to the struct IOVEC
173 DisplayBytes(buf, *(void **)data, data_size);
174 verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData());
175 break;
176 }
Greg Clayton386ff182011-11-05 01:09:16 +0000177 default:
178 {
179 }
180 }
181 }
182}
183
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000184// Wrapper for ptrace to catch errors and log calls.
Ashok Thirumurthi762fbd02013-03-27 21:09:30 +0000185// 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 +0000186extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000187PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000188 const char* reqName, const char* file, int line)
189{
Greg Clayton386ff182011-11-05 01:09:16 +0000190 long int result;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000191
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000192 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE));
Greg Clayton386ff182011-11-05 01:09:16 +0000193
Matt Kopec7de48462013-03-06 17:20:48 +0000194 PtraceDisplayBytes(req, data, data_size);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000195
196 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000197 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
Todd Fiala4507f062014-02-27 20:46:12 +0000198 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), *(unsigned int *)addr, data);
Matt Kopec58c0b962013-03-20 20:34:35 +0000199 else
Todd Fiala4507f062014-02-27 20:46:12 +0000200 result = ptrace(static_cast<__ptrace_request>(req), static_cast<pid_t>(pid), addr, data);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000201
Ed Mastec099c952014-02-24 14:07:45 +0000202 if (log)
203 log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d",
204 reqName, pid, addr, data, data_size, result, file, line);
205
Matt Kopec7de48462013-03-06 17:20:48 +0000206 PtraceDisplayBytes(req, data, data_size);
Greg Clayton386ff182011-11-05 01:09:16 +0000207
Matt Kopec7de48462013-03-06 17:20:48 +0000208 if (log && errno != 0)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000209 {
210 const char* str;
211 switch (errno)
212 {
213 case ESRCH: str = "ESRCH"; break;
214 case EINVAL: str = "EINVAL"; break;
215 case EBUSY: str = "EBUSY"; break;
216 case EPERM: str = "EPERM"; break;
217 default: str = "<unknown>";
218 }
219 log->Printf("ptrace() failed; errno=%d (%s)", errno, str);
220 }
221
222 return result;
223}
224
Matt Kopec7de48462013-03-06 17:20:48 +0000225// Wrapper for ptrace when logging is not required.
226// Sets errno to 0 prior to calling ptrace.
227extern long
Matt Kopec58c0b962013-03-20 20:34:35 +0000228PtraceWrapper(int req, pid_t pid, void *addr, void *data, size_t data_size)
Matt Kopec7de48462013-03-06 17:20:48 +0000229{
Matt Kopec58c0b962013-03-20 20:34:35 +0000230 long result = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000231 errno = 0;
Matt Kopec58c0b962013-03-20 20:34:35 +0000232 if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET)
233 result = ptrace(static_cast<__ptrace_request>(req), pid, *(unsigned int *)addr, data);
234 else
235 result = ptrace(static_cast<__ptrace_request>(req), pid, addr, data);
Matt Kopec7de48462013-03-06 17:20:48 +0000236 return result;
237}
238
239#define PTRACE(req, pid, addr, data, data_size) \
240 PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000241#else
Matt Kopec7de48462013-03-06 17:20:48 +0000242 PtraceWrapper((req), (pid), (addr), (data), (data_size))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000243#endif
244
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000245//------------------------------------------------------------------------------
246// Static implementations of ProcessMonitor::ReadMemory and
247// ProcessMonitor::WriteMemory. This enables mutual recursion between these
248// functions without needed to go thru the thread funnel.
249
250static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000251DoReadMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000252 lldb::addr_t vm_addr, void *buf, size_t size, Error &error)
253{
Greg Clayton542e4072012-09-07 17:49:29 +0000254 // ptrace word size is determined by the host, not the child
255 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000256 unsigned char *dst = static_cast<unsigned char*>(buf);
257 size_t bytes_read;
258 size_t remainder;
259 long data;
260
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000261 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000262 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000263 ProcessPOSIXLog::IncNestLevel();
264 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000265 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000266 pid, word_size, (void*)vm_addr, buf, size);
267
268 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000269 for (bytes_read = 0; bytes_read < size; bytes_read += remainder)
270 {
271 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000272 data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0);
273 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000274 {
275 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000276 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000277 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000278 return bytes_read;
279 }
280
281 remainder = size - bytes_read;
282 remainder = remainder > word_size ? word_size : remainder;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000283
284 // Copy the data into our buffer
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000285 for (unsigned i = 0; i < remainder; ++i)
286 dst[i] = ((data >> i*8) & 0xFF);
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000287
Johnny Chen30213ff2012-01-05 19:17:38 +0000288 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
289 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
290 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
291 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Daniel Maleac63dddd2012-12-14 21:07:07 +0000292 {
293 uintptr_t print_dst = 0;
294 // Format bytes from data by moving into print_dst for log output
295 for (unsigned i = 0; i < remainder; ++i)
296 print_dst |= (((data >> i*8) & 0xFF) << i*8);
297 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
298 (void*)vm_addr, print_dst, (unsigned long)data);
299 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000300
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000301 vm_addr += word_size;
302 dst += word_size;
303 }
304
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000305 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000306 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000307 return bytes_read;
308}
309
310static size_t
Greg Clayton542e4072012-09-07 17:49:29 +0000311DoWriteMemory(lldb::pid_t pid,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000312 lldb::addr_t vm_addr, const void *buf, size_t size, Error &error)
313{
Greg Clayton542e4072012-09-07 17:49:29 +0000314 // ptrace word size is determined by the host, not the child
315 static const unsigned word_size = sizeof(void*);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000316 const unsigned char *src = static_cast<const unsigned char*>(buf);
317 size_t bytes_written = 0;
318 size_t remainder;
319
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000320 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL));
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000321 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000322 ProcessPOSIXLog::IncNestLevel();
323 if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY))
Daniel Malead01b2952012-11-29 21:49:15 +0000324 log->Printf ("ProcessMonitor::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__,
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000325 pid, word_size, (void*)vm_addr, buf, size);
326
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000327 for (bytes_written = 0; bytes_written < size; bytes_written += remainder)
328 {
329 remainder = size - bytes_written;
330 remainder = remainder > word_size ? word_size : remainder;
331
332 if (remainder == word_size)
333 {
334 unsigned long data = 0;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000335 assert(sizeof(data) >= word_size);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000336 for (unsigned i = 0; i < word_size; ++i)
337 data |= (unsigned long)src[i] << i*8;
338
Johnny Chen30213ff2012-01-05 19:17:38 +0000339 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
340 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
341 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
342 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000343 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
344 (void*)vm_addr, *(unsigned long*)src, data);
345
Matt Kopec7de48462013-03-06 17:20:48 +0000346 if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000347 {
348 error.SetErrorToErrno();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000349 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000350 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000351 return bytes_written;
352 }
353 }
354 else
355 {
356 unsigned char buff[8];
Greg Clayton542e4072012-09-07 17:49:29 +0000357 if (DoReadMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000358 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000359 {
360 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000361 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000362 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000363 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000364
365 memcpy(buff, src, remainder);
366
Greg Clayton542e4072012-09-07 17:49:29 +0000367 if (DoWriteMemory(pid, vm_addr,
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000368 buff, word_size, error) != word_size)
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000369 {
370 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000371 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000372 return bytes_written;
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000373 }
374
Johnny Chen30213ff2012-01-05 19:17:38 +0000375 if (log && ProcessPOSIXLog::AtTopNestLevel() &&
376 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) ||
377 (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) &&
378 size <= POSIX_LOG_MEMORY_SHORT_BYTES)))
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000379 log->Printf ("ProcessMonitor::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__,
380 (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000381 }
382
383 vm_addr += word_size;
384 src += word_size;
385 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000386 if (log)
Johnny Chen30213ff2012-01-05 19:17:38 +0000387 ProcessPOSIXLog::DecNestLevel();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000388 return bytes_written;
389}
390
Stephen Wilson26977162011-03-23 02:14:42 +0000391// Simple helper function to ensure flags are enabled on the given file
392// descriptor.
393static bool
394EnsureFDFlags(int fd, int flags, Error &error)
395{
396 int status;
397
398 if ((status = fcntl(fd, F_GETFL)) == -1)
399 {
400 error.SetErrorToErrno();
401 return false;
402 }
403
404 if (fcntl(fd, F_SETFL, status | flags) == -1)
405 {
406 error.SetErrorToErrno();
407 return false;
408 }
409
410 return true;
411}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000412
413//------------------------------------------------------------------------------
414/// @class Operation
415/// @brief Represents a ProcessMonitor operation.
416///
417/// Under Linux, it is not possible to ptrace() from any other thread but the
418/// one that spawned or attached to the process from the start. Therefore, when
419/// a ProcessMonitor is asked to deliver or change the state of an inferior
420/// process the operation must be "funneled" to a specific thread to perform the
421/// task. The Operation class provides an abstract base for all services the
422/// ProcessMonitor must perform via the single virtual function Execute, thus
423/// encapsulating the code that needs to run in the privileged context.
424class Operation
425{
426public:
Daniel Maleadd15b782013-05-13 17:32:07 +0000427 virtual ~Operation() {}
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000428 virtual void Execute(ProcessMonitor *monitor) = 0;
429};
430
431//------------------------------------------------------------------------------
432/// @class ReadOperation
433/// @brief Implements ProcessMonitor::ReadMemory.
434class ReadOperation : public Operation
435{
436public:
437 ReadOperation(lldb::addr_t addr, void *buff, size_t size,
438 Error &error, size_t &result)
439 : m_addr(addr), m_buff(buff), m_size(size),
440 m_error(error), m_result(result)
441 { }
442
443 void Execute(ProcessMonitor *monitor);
444
445private:
446 lldb::addr_t m_addr;
447 void *m_buff;
448 size_t m_size;
449 Error &m_error;
450 size_t &m_result;
451};
452
453void
454ReadOperation::Execute(ProcessMonitor *monitor)
455{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000456 lldb::pid_t pid = monitor->GetPID();
457
Greg Clayton542e4072012-09-07 17:49:29 +0000458 m_result = DoReadMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000459}
460
461//------------------------------------------------------------------------------
Ed Mastea56115f2013-07-17 14:30:26 +0000462/// @class WriteOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000463/// @brief Implements ProcessMonitor::WriteMemory.
464class WriteOperation : public Operation
465{
466public:
467 WriteOperation(lldb::addr_t addr, const void *buff, size_t size,
468 Error &error, size_t &result)
469 : m_addr(addr), m_buff(buff), m_size(size),
470 m_error(error), m_result(result)
471 { }
472
473 void Execute(ProcessMonitor *monitor);
474
475private:
476 lldb::addr_t m_addr;
477 const void *m_buff;
478 size_t m_size;
479 Error &m_error;
480 size_t &m_result;
481};
482
483void
484WriteOperation::Execute(ProcessMonitor *monitor)
485{
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000486 lldb::pid_t pid = monitor->GetPID();
487
Greg Clayton542e4072012-09-07 17:49:29 +0000488 m_result = DoWriteMemory(pid, m_addr, m_buff, m_size, m_error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000489}
490
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000491
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000492//------------------------------------------------------------------------------
493/// @class ReadRegOperation
494/// @brief Implements ProcessMonitor::ReadRegisterValue.
495class ReadRegOperation : public Operation
496{
497public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000498 ReadRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000499 RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000500 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000501 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000502 { }
503
504 void Execute(ProcessMonitor *monitor);
505
506private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000507 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000508 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000509 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000510 RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000511 bool &m_result;
512};
513
514void
515ReadRegOperation::Execute(ProcessMonitor *monitor)
516{
Todd Fiala49131cf2014-09-12 16:57:28 +0000517#if defined (__arm64__) || defined (__aarch64__)
518 if (m_offset > sizeof(struct user_pt_regs))
519 {
520 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
521 if (offset > sizeof(struct user_fpsimd_state))
522 {
523 m_result = false;
524 }
525 else
526 {
527 elf_fpregset_t regs;
528 int regset = NT_FPREGSET;
529 struct iovec ioVec;
530
531 ioVec.iov_base = &regs;
532 ioVec.iov_len = sizeof regs;
533 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
534 m_result = false;
535 else
536 {
537 m_result = true;
538 m_value.SetBytes((void *)(((unsigned char *)(&regs)) + offset), 16, monitor->GetProcess().GetByteOrder());
539 }
540 }
541 }
542 else
543 {
544 elf_gregset_t regs;
545 int regset = NT_PRSTATUS;
546 struct iovec ioVec;
547
548 ioVec.iov_base = &regs;
549 ioVec.iov_len = sizeof regs;
550 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
551 m_result = false;
552 else
553 {
554 m_result = true;
555 m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, monitor->GetProcess().GetByteOrder());
556 }
557 }
558#else
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000559 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000560
561 // Set errno to zero so that we can detect a failed peek.
562 errno = 0;
Matt Kopec7de48462013-03-06 17:20:48 +0000563 lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0);
564 if (errno)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000565 m_result = false;
566 else
567 {
568 m_value = data;
569 m_result = true;
570 }
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000571 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000572 log->Printf ("ProcessMonitor::%s() reg %s: 0x%" PRIx64, __FUNCTION__,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000573 m_reg_name, data);
Todd Fiala49131cf2014-09-12 16:57:28 +0000574#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000575}
576
577//------------------------------------------------------------------------------
578/// @class WriteRegOperation
579/// @brief Implements ProcessMonitor::WriteRegisterValue.
580class WriteRegOperation : public Operation
581{
582public:
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000583 WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name,
Daniel Maleaf0da3712012-12-18 19:50:15 +0000584 const RegisterValue &value, bool &result)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000585 : m_tid(tid), m_offset(offset), m_reg_name(reg_name),
Daniel Maleaf0da3712012-12-18 19:50:15 +0000586 m_value(value), m_result(result)
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000587 { }
588
589 void Execute(ProcessMonitor *monitor);
590
591private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000592 lldb::tid_t m_tid;
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000593 uintptr_t m_offset;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000594 const char *m_reg_name;
Johnny Chen13e8e1c2011-05-13 21:29:50 +0000595 const RegisterValue &m_value;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000596 bool &m_result;
597};
598
599void
600WriteRegOperation::Execute(ProcessMonitor *monitor)
601{
Todd Fiala49131cf2014-09-12 16:57:28 +0000602#if defined (__arm64__) || defined (__aarch64__)
603 if (m_offset > sizeof(struct user_pt_regs))
604 {
605 uintptr_t offset = m_offset - sizeof(struct user_pt_regs);
606 if (offset > sizeof(struct user_fpsimd_state))
607 {
608 m_result = false;
609 }
610 else
611 {
612 elf_fpregset_t regs;
613 int regset = NT_FPREGSET;
614 struct iovec ioVec;
615
616 ioVec.iov_base = &regs;
617 ioVec.iov_len = sizeof regs;
618 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
619 m_result = false;
620 else
621 {
622 ::memcpy((void *)(((unsigned char *)(&regs)) + offset), m_value.GetBytes(), 16);
623 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
624 m_result = false;
625 else
626 m_result = true;
627 }
628 }
629 }
630 else
631 {
632 elf_gregset_t regs;
633 int regset = NT_PRSTATUS;
634 struct iovec ioVec;
635
636 ioVec.iov_base = &regs;
637 ioVec.iov_len = sizeof regs;
638 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
639 m_result = false;
640 else
641 {
642 ::memcpy((void *)(((unsigned char *)(&regs)) + m_offset), m_value.GetBytes(), 8);
643 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, sizeof regs) < 0)
644 m_result = false;
645 else
646 m_result = true;
647 }
648 }
649#else
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000650 void* buf;
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000651 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000652
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000653 buf = (void*) m_value.GetAsUInt64();
Johnny Chen0d5f2d42011-10-18 18:09:30 +0000654
655 if (log)
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +0000656 log->Printf ("ProcessMonitor::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf);
Matt Kopec7de48462013-03-06 17:20:48 +0000657 if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000658 m_result = false;
659 else
660 m_result = true;
Todd Fiala49131cf2014-09-12 16:57:28 +0000661#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000662}
663
664//------------------------------------------------------------------------------
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000665/// @class ReadGPROperation
666/// @brief Implements ProcessMonitor::ReadGPR.
667class ReadGPROperation : public Operation
668{
669public:
Matt Kopec7de48462013-03-06 17:20:48 +0000670 ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
671 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000672 { }
673
674 void Execute(ProcessMonitor *monitor);
675
676private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000677 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000678 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000679 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000680 bool &m_result;
681};
682
683void
684ReadGPROperation::Execute(ProcessMonitor *monitor)
685{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000686#if defined (__arm64__) || defined (__aarch64__)
687 int regset = NT_PRSTATUS;
688 struct iovec ioVec;
689
690 ioVec.iov_base = m_buf;
691 ioVec.iov_len = m_buf_size;
692 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000693 m_result = false;
694 else
695 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000696#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000697 if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
698 m_result = false;
699 else
700 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000701#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000702}
703
704//------------------------------------------------------------------------------
705/// @class ReadFPROperation
706/// @brief Implements ProcessMonitor::ReadFPR.
707class ReadFPROperation : public Operation
708{
709public:
Matt Kopec7de48462013-03-06 17:20:48 +0000710 ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
711 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000712 { }
713
714 void Execute(ProcessMonitor *monitor);
715
716private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000717 lldb::tid_t m_tid;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000718 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000719 size_t m_buf_size;
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000720 bool &m_result;
721};
722
723void
724ReadFPROperation::Execute(ProcessMonitor *monitor)
725{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000726#if defined (__arm64__) || defined (__aarch64__)
727 int regset = NT_FPREGSET;
728 struct iovec ioVec;
729
730 ioVec.iov_base = m_buf;
731 ioVec.iov_len = m_buf_size;
732 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000733 m_result = false;
734 else
735 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000736#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000737 if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
738 m_result = false;
739 else
740 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000741#endif
Stephen Wilsonade1aea2011-01-19 01:31:38 +0000742}
743
744//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000745/// @class ReadRegisterSetOperation
746/// @brief Implements ProcessMonitor::ReadRegisterSet.
747class ReadRegisterSetOperation : public Operation
748{
749public:
750 ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
751 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
752 { }
753
754 void Execute(ProcessMonitor *monitor);
755
756private:
757 lldb::tid_t m_tid;
758 void *m_buf;
759 size_t m_buf_size;
760 const unsigned int m_regset;
761 bool &m_result;
762};
763
764void
765ReadRegisterSetOperation::Execute(ProcessMonitor *monitor)
766{
767 if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
768 m_result = false;
769 else
770 m_result = true;
771}
772
773//------------------------------------------------------------------------------
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000774/// @class WriteGPROperation
775/// @brief Implements ProcessMonitor::WriteGPR.
776class WriteGPROperation : public Operation
777{
778public:
Matt Kopec7de48462013-03-06 17:20:48 +0000779 WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
780 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000781 { }
782
783 void Execute(ProcessMonitor *monitor);
784
785private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000786 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000787 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000788 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000789 bool &m_result;
790};
791
792void
793WriteGPROperation::Execute(ProcessMonitor *monitor)
794{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000795#if defined (__arm64__) || defined (__aarch64__)
796 int regset = NT_PRSTATUS;
797 struct iovec ioVec;
798
799 ioVec.iov_base = m_buf;
800 ioVec.iov_len = m_buf_size;
801 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000802 m_result = false;
803 else
804 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000805#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000806 if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
807 m_result = false;
808 else
809 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000810#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000811}
812
813//------------------------------------------------------------------------------
814/// @class WriteFPROperation
815/// @brief Implements ProcessMonitor::WriteFPR.
816class WriteFPROperation : public Operation
817{
818public:
Matt Kopec7de48462013-03-06 17:20:48 +0000819 WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result)
820 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000821 { }
822
823 void Execute(ProcessMonitor *monitor);
824
825private:
Daniel Maleaf0da3712012-12-18 19:50:15 +0000826 lldb::tid_t m_tid;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000827 void *m_buf;
Matt Kopec7de48462013-03-06 17:20:48 +0000828 size_t m_buf_size;
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000829 bool &m_result;
830};
831
832void
833WriteFPROperation::Execute(ProcessMonitor *monitor)
834{
Todd Fiala6ac1be42014-08-21 16:34:03 +0000835#if defined (__arm64__) || defined (__aarch64__)
836 int regset = NT_FPREGSET;
837 struct iovec ioVec;
838
839 ioVec.iov_base = m_buf;
840 ioVec.iov_len = m_buf_size;
841 if (PTRACE(PTRACE_SETREGSET, m_tid, &regset, &ioVec, m_buf_size) < 0)
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000842 m_result = false;
843 else
844 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000845#else
Todd Fiala6ac1be42014-08-21 16:34:03 +0000846 if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0)
847 m_result = false;
848 else
849 m_result = true;
Todd Fialad35f2b92014-06-23 15:59:04 +0000850#endif
Peter Collingbourne10bc0102011-06-03 20:41:02 +0000851}
852
853//------------------------------------------------------------------------------
Matt Kopec58c0b962013-03-20 20:34:35 +0000854/// @class WriteRegisterSetOperation
855/// @brief Implements ProcessMonitor::WriteRegisterSet.
856class WriteRegisterSetOperation : public Operation
857{
858public:
859 WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result)
860 : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result)
861 { }
862
863 void Execute(ProcessMonitor *monitor);
864
865private:
866 lldb::tid_t m_tid;
867 void *m_buf;
868 size_t m_buf_size;
869 const unsigned int m_regset;
870 bool &m_result;
871};
872
873void
874WriteRegisterSetOperation::Execute(ProcessMonitor *monitor)
875{
876 if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0)
877 m_result = false;
878 else
879 m_result = true;
880}
881
882//------------------------------------------------------------------------------
Richard Mitton0a558352013-10-17 21:14:00 +0000883/// @class ReadThreadPointerOperation
884/// @brief Implements ProcessMonitor::ReadThreadPointer.
885class ReadThreadPointerOperation : public Operation
886{
887public:
888 ReadThreadPointerOperation(lldb::tid_t tid, lldb::addr_t *addr, bool &result)
889 : m_tid(tid), m_addr(addr), m_result(result)
890 { }
891
892 void Execute(ProcessMonitor *monitor);
893
894private:
895 lldb::tid_t m_tid;
896 lldb::addr_t *m_addr;
897 bool &m_result;
898};
899
900void
901ReadThreadPointerOperation::Execute(ProcessMonitor *monitor)
902{
903 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS));
904 if (log)
905 log->Printf ("ProcessMonitor::%s()", __FUNCTION__);
906
907 // The process for getting the thread area on Linux is
908 // somewhat... obscure. There's several different ways depending on
909 // what arch you're on, and what kernel version you have.
910
911 const ArchSpec& arch = monitor->GetProcess().GetTarget().GetArchitecture();
912 switch(arch.GetMachine())
913 {
Todd Fiala42079682014-08-27 16:05:26 +0000914 case llvm::Triple::aarch64:
915 {
Todd Fialadbec1ff2014-09-04 16:08:20 +0000916 int regset = LLDB_PTRACE_NT_ARM_TLS;
Todd Fiala42079682014-08-27 16:05:26 +0000917 struct iovec ioVec;
918
919 ioVec.iov_base = m_addr;
920 ioVec.iov_len = sizeof(lldb::addr_t);
921 if (PTRACE(PTRACE_GETREGSET, m_tid, &regset, &ioVec, ioVec.iov_len) < 0)
922 m_result = false;
923 else
924 m_result = true;
925 break;
926 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000927#if defined(__i386__) || defined(__x86_64__)
928 // Note that struct user below has a field named i387 which is x86-specific.
929 // Therefore, this case should be compiled only for x86-based systems.
Richard Mitton0a558352013-10-17 21:14:00 +0000930 case llvm::Triple::x86:
931 {
932 // Find the GS register location for our host architecture.
933 size_t gs_user_offset = offsetof(struct user, regs);
934#ifdef __x86_64__
935 gs_user_offset += offsetof(struct user_regs_struct, gs);
936#endif
937#ifdef __i386__
938 gs_user_offset += offsetof(struct user_regs_struct, xgs);
939#endif
940
941 // Read the GS register value to get the selector.
942 errno = 0;
943 long gs = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)gs_user_offset, NULL, 0);
944 if (errno)
945 {
946 m_result = false;
947 break;
948 }
949
950 // Read the LDT base for that selector.
951 uint32_t tmp[4];
952 m_result = (PTRACE(PTRACE_GET_THREAD_AREA, m_tid, (void *)(gs >> 3), &tmp, 0) == 0);
953 *m_addr = tmp[1];
954 break;
955 }
Todd Fiala720cd3f2014-06-16 14:49:28 +0000956#endif
Richard Mitton0a558352013-10-17 21:14:00 +0000957 case llvm::Triple::x86_64:
958 // Read the FS register base.
959 m_result = (PTRACE(PTRACE_ARCH_PRCTL, m_tid, m_addr, (void *)ARCH_GET_FS, 0) == 0);
960 break;
961 default:
962 m_result = false;
963 break;
964 }
965}
966
967//------------------------------------------------------------------------------
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000968/// @class ResumeOperation
969/// @brief Implements ProcessMonitor::Resume.
970class ResumeOperation : public Operation
971{
972public:
Stephen Wilson84ffe702011-03-30 15:55:52 +0000973 ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) :
974 m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000975
976 void Execute(ProcessMonitor *monitor);
977
978private:
979 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000980 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000981 bool &m_result;
982};
983
984void
985ResumeOperation::Execute(ProcessMonitor *monitor)
986{
Daniel Maleaa85e6b62012-12-07 22:21:08 +0000987 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +0000988
989 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
990 data = m_signo;
991
Matt Kopec7de48462013-03-06 17:20:48 +0000992 if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000993 {
994 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
995
996 if (log)
997 log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno));
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000998 m_result = false;
Andrew Kaylor93132f52013-05-28 23:04:25 +0000999 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001000 else
1001 m_result = true;
1002}
1003
1004//------------------------------------------------------------------------------
Ed Maste428a6782013-06-24 15:04:47 +00001005/// @class SingleStepOperation
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001006/// @brief Implements ProcessMonitor::SingleStep.
1007class SingleStepOperation : public Operation
1008{
1009public:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001010 SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result)
1011 : m_tid(tid), m_signo(signo), m_result(result) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001012
1013 void Execute(ProcessMonitor *monitor);
1014
1015private:
1016 lldb::tid_t m_tid;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001017 uint32_t m_signo;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001018 bool &m_result;
1019};
1020
1021void
1022SingleStepOperation::Execute(ProcessMonitor *monitor)
1023{
Daniel Maleaa85e6b62012-12-07 22:21:08 +00001024 intptr_t data = 0;
Stephen Wilson84ffe702011-03-30 15:55:52 +00001025
1026 if (m_signo != LLDB_INVALID_SIGNAL_NUMBER)
1027 data = m_signo;
1028
Matt Kopec7de48462013-03-06 17:20:48 +00001029 if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001030 m_result = false;
1031 else
1032 m_result = true;
1033}
1034
1035//------------------------------------------------------------------------------
1036/// @class SiginfoOperation
1037/// @brief Implements ProcessMonitor::GetSignalInfo.
1038class SiginfoOperation : public Operation
1039{
1040public:
Daniel Maleaa35970a2012-11-23 18:09:58 +00001041 SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err)
1042 : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001043
1044 void Execute(ProcessMonitor *monitor);
1045
1046private:
1047 lldb::tid_t m_tid;
1048 void *m_info;
1049 bool &m_result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001050 int &m_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001051};
1052
1053void
1054SiginfoOperation::Execute(ProcessMonitor *monitor)
1055{
Matt Kopec7de48462013-03-06 17:20:48 +00001056 if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001057 m_result = false;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001058 m_err = errno;
1059 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001060 else
1061 m_result = true;
1062}
1063
1064//------------------------------------------------------------------------------
1065/// @class EventMessageOperation
1066/// @brief Implements ProcessMonitor::GetEventMessage.
1067class EventMessageOperation : public Operation
1068{
1069public:
1070 EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result)
1071 : m_tid(tid), m_message(message), m_result(result) { }
1072
1073 void Execute(ProcessMonitor *monitor);
1074
1075private:
1076 lldb::tid_t m_tid;
1077 unsigned long *m_message;
1078 bool &m_result;
1079};
1080
1081void
1082EventMessageOperation::Execute(ProcessMonitor *monitor)
1083{
Matt Kopec7de48462013-03-06 17:20:48 +00001084 if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001085 m_result = false;
1086 else
1087 m_result = true;
1088}
1089
1090//------------------------------------------------------------------------------
Ed Maste263c9282014-03-17 17:45:53 +00001091/// @class DetachOperation
1092/// @brief Implements ProcessMonitor::Detach.
Greg Clayton28041352011-11-29 20:50:10 +00001093class DetachOperation : public Operation
1094{
1095public:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001096 DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { }
Greg Clayton28041352011-11-29 20:50:10 +00001097
1098 void Execute(ProcessMonitor *monitor);
1099
1100private:
Matt Kopec085d6ce2013-05-31 22:00:07 +00001101 lldb::tid_t m_tid;
Greg Clayton28041352011-11-29 20:50:10 +00001102 Error &m_error;
1103};
1104
1105void
1106DetachOperation::Execute(ProcessMonitor *monitor)
1107{
Matt Kopec085d6ce2013-05-31 22:00:07 +00001108 if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0)
Greg Clayton28041352011-11-29 20:50:10 +00001109 m_error.SetErrorToErrno();
Greg Clayton28041352011-11-29 20:50:10 +00001110}
1111
Johnny Chen25e68e32011-06-14 19:19:50 +00001112ProcessMonitor::OperationArgs::OperationArgs(ProcessMonitor *monitor)
1113 : m_monitor(monitor)
1114{
1115 sem_init(&m_semaphore, 0, 0);
1116}
1117
1118ProcessMonitor::OperationArgs::~OperationArgs()
1119{
1120 sem_destroy(&m_semaphore);
1121}
1122
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001123ProcessMonitor::LaunchArgs::LaunchArgs(ProcessMonitor *monitor,
1124 lldb_private::Module *module,
1125 char const **argv,
1126 char const **envp,
1127 const char *stdin_path,
1128 const char *stdout_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001129 const char *stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001130 const char *working_dir,
1131 const lldb_private::ProcessLaunchInfo &launch_info)
Johnny Chen25e68e32011-06-14 19:19:50 +00001132 : OperationArgs(monitor),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001133 m_module(module),
1134 m_argv(argv),
1135 m_envp(envp),
1136 m_stdin_path(stdin_path),
1137 m_stdout_path(stdout_path),
Daniel Malea6217d2a2013-01-08 14:49:22 +00001138 m_stderr_path(stderr_path),
Todd Fiala0bce1b62014-08-17 00:10:50 +00001139 m_working_dir(working_dir),
1140 m_launch_info(launch_info)
1141{
1142}
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001143
1144ProcessMonitor::LaunchArgs::~LaunchArgs()
Johnny Chen25e68e32011-06-14 19:19:50 +00001145{ }
1146
1147ProcessMonitor::AttachArgs::AttachArgs(ProcessMonitor *monitor,
1148 lldb::pid_t pid)
1149 : OperationArgs(monitor), m_pid(pid) { }
1150
1151ProcessMonitor::AttachArgs::~AttachArgs()
1152{ }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001153
1154//------------------------------------------------------------------------------
1155/// The basic design of the ProcessMonitor is built around two threads.
1156///
1157/// One thread (@see SignalThread) simply blocks on a call to waitpid() looking
1158/// for changes in the debugee state. When a change is detected a
1159/// ProcessMessage is sent to the associated ProcessLinux instance. This thread
1160/// "drives" state changes in the debugger.
1161///
1162/// The second thread (@see OperationThread) is responsible for two things 1)
Greg Clayton710dd5a2011-01-08 20:28:42 +00001163/// launching or attaching to the inferior process, and then 2) servicing
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001164/// operations such as register reads/writes, stepping, etc. See the comments
1165/// on the Operation class for more info as to why this is needed.
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001166ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001167 Module *module,
1168 const char *argv[],
1169 const char *envp[],
1170 const char *stdin_path,
1171 const char *stdout_path,
1172 const char *stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001173 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001174 const lldb_private::ProcessLaunchInfo &launch_info,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001175 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001176 : m_process(static_cast<ProcessLinux *>(process)),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001177 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001178 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001179 m_pid(LLDB_INVALID_PROCESS_ID),
1180 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001181 m_operation(0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001182{
Daniel Malea1efb4182013-09-16 23:12:18 +00001183 std::unique_ptr<LaunchArgs> args(new LaunchArgs(this, module, argv, envp,
1184 stdin_path, stdout_path, stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +00001185 working_dir, launch_info));
Stephen Wilson57740ec2011-01-15 00:12:41 +00001186
Daniel Malea1efb4182013-09-16 23:12:18 +00001187 sem_init(&m_operation_pending, 0, 0);
1188 sem_init(&m_operation_done, 0, 0);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001189
Johnny Chen25e68e32011-06-14 19:19:50 +00001190 StartLaunchOpThread(args.get(), error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001191 if (!error.Success())
1192 return;
1193
1194WAIT_AGAIN:
1195 // Wait for the operation thread to initialize.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001196 if (sem_wait(&args->m_semaphore))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001197 {
1198 if (errno == EINTR)
1199 goto WAIT_AGAIN;
1200 else
1201 {
1202 error.SetErrorToErrno();
1203 return;
1204 }
1205 }
1206
1207 // Check that the launch was a success.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001208 if (!args->m_error.Success())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001209 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001210 StopOpThread();
Stephen Wilson57740ec2011-01-15 00:12:41 +00001211 error = args->m_error;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001212 return;
1213 }
1214
1215 // Finally, start monitoring the child process for change in state.
Stephen Wilson57740ec2011-01-15 00:12:41 +00001216 m_monitor_thread = Host::StartMonitoringChildProcess(
1217 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001218 if (!m_monitor_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001219 {
1220 error.SetErrorToGenericError();
1221 error.SetErrorString("Process launch failed.");
1222 return;
1223 }
1224}
1225
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001226ProcessMonitor::ProcessMonitor(ProcessPOSIX *process,
Johnny Chen25e68e32011-06-14 19:19:50 +00001227 lldb::pid_t pid,
1228 lldb_private::Error &error)
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001229 : m_process(static_cast<ProcessLinux *>(process)),
Johnny Chen25e68e32011-06-14 19:19:50 +00001230 m_operation_thread(LLDB_INVALID_HOST_THREAD),
Matt Kopec7de48462013-03-06 17:20:48 +00001231 m_monitor_thread(LLDB_INVALID_HOST_THREAD),
Johnny Chen25e68e32011-06-14 19:19:50 +00001232 m_pid(LLDB_INVALID_PROCESS_ID),
1233 m_terminal_fd(-1),
Daniel Malea1efb4182013-09-16 23:12:18 +00001234 m_operation(0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001235{
Daniel Malea1efb4182013-09-16 23:12:18 +00001236 sem_init(&m_operation_pending, 0, 0);
1237 sem_init(&m_operation_done, 0, 0);
Johnny Chen25e68e32011-06-14 19:19:50 +00001238
Daniel Malea1efb4182013-09-16 23:12:18 +00001239 std::unique_ptr<AttachArgs> args(new AttachArgs(this, pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001240
1241 StartAttachOpThread(args.get(), error);
1242 if (!error.Success())
1243 return;
1244
1245WAIT_AGAIN:
1246 // Wait for the operation thread to initialize.
1247 if (sem_wait(&args->m_semaphore))
1248 {
1249 if (errno == EINTR)
1250 goto WAIT_AGAIN;
1251 else
1252 {
1253 error.SetErrorToErrno();
1254 return;
1255 }
1256 }
1257
Greg Clayton743ecf42012-10-16 20:20:18 +00001258 // Check that the attach was a success.
Johnny Chen25e68e32011-06-14 19:19:50 +00001259 if (!args->m_error.Success())
1260 {
Greg Clayton743ecf42012-10-16 20:20:18 +00001261 StopOpThread();
Johnny Chen25e68e32011-06-14 19:19:50 +00001262 error = args->m_error;
1263 return;
1264 }
1265
1266 // Finally, start monitoring the child process for change in state.
1267 m_monitor_thread = Host::StartMonitoringChildProcess(
1268 ProcessMonitor::MonitorCallback, this, GetPID(), true);
Zachary Turneracee96a2014-09-23 18:32:09 +00001269 if (!m_monitor_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001270 {
1271 error.SetErrorToGenericError();
1272 error.SetErrorString("Process attach failed.");
1273 return;
1274 }
1275}
1276
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001277ProcessMonitor::~ProcessMonitor()
1278{
Stephen Wilson84ffe702011-03-30 15:55:52 +00001279 StopMonitor();
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001280}
1281
1282//------------------------------------------------------------------------------
1283// Thread setup and tear down.
1284void
Johnny Chen25e68e32011-06-14 19:19:50 +00001285ProcessMonitor::StartLaunchOpThread(LaunchArgs *args, Error &error)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001286{
1287 static const char *g_thread_name = "lldb.process.linux.operation";
1288
Zachary Turneracee96a2014-09-23 18:32:09 +00001289 if (m_operation_thread.IsJoinable())
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001290 return;
1291
Zachary Turner39de3112014-09-09 20:54:56 +00001292 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001293}
1294
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001295void *
Johnny Chen25e68e32011-06-14 19:19:50 +00001296ProcessMonitor::LaunchOpThread(void *arg)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001297{
1298 LaunchArgs *args = static_cast<LaunchArgs*>(arg);
1299
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001300 if (!Launch(args)) {
1301 sem_post(&args->m_semaphore);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001302 return NULL;
Peter Collingbourne4aeb47e2011-06-14 03:55:49 +00001303 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001304
Stephen Wilson570243b2011-01-19 01:37:06 +00001305 ServeOperation(args);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001306 return NULL;
1307}
1308
1309bool
1310ProcessMonitor::Launch(LaunchArgs *args)
1311{
Todd Fiala0bce1b62014-08-17 00:10:50 +00001312 assert (args && "null args");
1313 if (!args)
1314 return false;
1315
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001316 ProcessMonitor *monitor = args->m_monitor;
1317 ProcessLinux &process = monitor->GetProcess();
1318 const char **argv = args->m_argv;
1319 const char **envp = args->m_envp;
1320 const char *stdin_path = args->m_stdin_path;
1321 const char *stdout_path = args->m_stdout_path;
1322 const char *stderr_path = args->m_stderr_path;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001323 const char *working_dir = args->m_working_dir;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001324
1325 lldb_utility::PseudoTerminal terminal;
1326 const size_t err_len = 1024;
1327 char err_str[err_len];
1328 lldb::pid_t pid;
1329
1330 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001331 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001332
Stephen Wilson57740ec2011-01-15 00:12:41 +00001333 // Propagate the environment if one is not supplied.
1334 if (envp == NULL || envp[0] == NULL)
1335 envp = const_cast<const char **>(environ);
1336
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001337 if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t>(-1))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001338 {
1339 args->m_error.SetErrorToGenericError();
1340 args->m_error.SetErrorString("Process fork failed.");
1341 goto FINISH;
1342 }
1343
Peter Collingbourne6a520222011-06-14 03:55:58 +00001344 // Recognized child exit status codes.
1345 enum {
1346 ePtraceFailed = 1,
1347 eDupStdinFailed,
1348 eDupStdoutFailed,
1349 eDupStderrFailed,
Daniel Malea6217d2a2013-01-08 14:49:22 +00001350 eChdirFailed,
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001351 eExecFailed,
1352 eSetGidFailed
Peter Collingbourne6a520222011-06-14 03:55:58 +00001353 };
1354
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001355 // Child process.
1356 if (pid == 0)
1357 {
1358 // Trace this process.
Matt Kopec7de48462013-03-06 17:20:48 +00001359 if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0)
Peter Collingbourne6a520222011-06-14 03:55:58 +00001360 exit(ePtraceFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001361
Pavel Labath493c3a12015-02-04 10:36:57 +00001362 // terminal has already dupped the tty descriptors to stdin/out/err.
1363 // This closes original fd from which they were copied (and avoids
1364 // leaking descriptors to the debugged process.
1365 terminal.CloseSlaveFileDescriptor();
1366
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001367 // Do not inherit setgid powers.
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001368 if (setgid(getgid()) != 0)
1369 exit(eSetGidFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001370
1371 // Let us have our own process group.
1372 setpgid(0, 0);
1373
Greg Clayton710dd5a2011-01-08 20:28:42 +00001374 // Dup file descriptors if needed.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001375 //
1376 // FIXME: If two or more of the paths are the same we needlessly open
1377 // the same file multiple times.
1378 if (stdin_path != NULL && stdin_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001379 if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001380 exit(eDupStdinFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001381
1382 if (stdout_path != NULL && stdout_path[0])
1383 if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001384 exit(eDupStdoutFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001385
1386 if (stderr_path != NULL && stderr_path[0])
Peter Collingbourne62343202011-06-14 03:55:54 +00001387 if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT))
Peter Collingbourne6a520222011-06-14 03:55:58 +00001388 exit(eDupStderrFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001389
Daniel Malea6217d2a2013-01-08 14:49:22 +00001390 // Change working directory
1391 if (working_dir != NULL && working_dir[0])
1392 if (0 != ::chdir(working_dir))
1393 exit(eChdirFailed);
1394
Todd Fiala0bce1b62014-08-17 00:10:50 +00001395 // Disable ASLR if requested.
1396 if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR))
1397 {
1398 const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS);
1399 if (old_personality == -1)
1400 {
1401 if (log)
1402 log->Printf ("ProcessMonitor::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno));
1403 }
1404 else
1405 {
1406 const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality);
1407 if (new_personality == -1)
1408 {
1409 if (log)
1410 log->Printf ("ProcessMonitor::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno));
1411
1412 }
1413 else
1414 {
1415 if (log)
Todd Fiala850f9a22014-09-19 18:27:45 +00001416 log->Printf ("ProcessMonitor::%s disabling ASLR: SUCCESS", __FUNCTION__);
Todd Fiala0bce1b62014-08-17 00:10:50 +00001417
1418 }
1419 }
1420 }
1421
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001422 // Execute. We should never return.
1423 execve(argv[0],
1424 const_cast<char *const *>(argv),
1425 const_cast<char *const *>(envp));
Peter Collingbourne6a520222011-06-14 03:55:58 +00001426 exit(eExecFailed);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001427 }
1428
1429 // Wait for the child process to to trap on its call to execve.
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001430 lldb::pid_t wpid;
Todd Fialaaf245d12014-06-30 21:05:18 +00001431 ::pid_t raw_pid;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001432 int status;
Todd Fialaaf245d12014-06-30 21:05:18 +00001433
1434 raw_pid = waitpid(pid, &status, 0);
1435 wpid = static_cast <lldb::pid_t> (raw_pid);
1436 if (raw_pid < 0)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001437 {
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001438 args->m_error.SetErrorToErrno();
1439 goto FINISH;
1440 }
Peter Collingbourne6a520222011-06-14 03:55:58 +00001441 else if (WIFEXITED(status))
1442 {
1443 // open, dup or execve likely failed for some reason.
1444 args->m_error.SetErrorToGenericError();
1445 switch (WEXITSTATUS(status))
1446 {
Greg Clayton542e4072012-09-07 17:49:29 +00001447 case ePtraceFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001448 args->m_error.SetErrorString("Child ptrace failed.");
1449 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001450 case eDupStdinFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001451 args->m_error.SetErrorString("Child open stdin failed.");
1452 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001453 case eDupStdoutFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001454 args->m_error.SetErrorString("Child open stdout failed.");
1455 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001456 case eDupStderrFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001457 args->m_error.SetErrorString("Child open stderr failed.");
1458 break;
Daniel Malea6217d2a2013-01-08 14:49:22 +00001459 case eChdirFailed:
1460 args->m_error.SetErrorString("Child failed to set working directory.");
1461 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001462 case eExecFailed:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001463 args->m_error.SetErrorString("Child exec failed.");
1464 break;
Sylvestre Ledru77c87c02013-09-28 15:47:38 +00001465 case eSetGidFailed:
1466 args->m_error.SetErrorString("Child setgid failed.");
1467 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001468 default:
Peter Collingbourne6a520222011-06-14 03:55:58 +00001469 args->m_error.SetErrorString("Child returned unknown exit status.");
1470 break;
1471 }
1472 goto FINISH;
1473 }
1474 assert(WIFSTOPPED(status) && wpid == pid &&
1475 "Could not sync with inferior process.");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001476
Matt Kopec085d6ce2013-05-31 22:00:07 +00001477 if (!SetDefaultPtraceOpts(pid))
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001478 {
1479 args->m_error.SetErrorToErrno();
1480 goto FINISH;
1481 }
1482
1483 // Release the master terminal descriptor and pass it off to the
1484 // ProcessMonitor instance. Similarly stash the inferior pid.
1485 monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor();
1486 monitor->m_pid = pid;
1487
Stephen Wilson26977162011-03-23 02:14:42 +00001488 // Set the terminal fd to be in non blocking mode (it simplifies the
1489 // implementation of ProcessLinux::GetSTDOUT to have a non-blocking
1490 // descriptor to read from).
1491 if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error))
1492 goto FINISH;
1493
Johnny Chen30213ff2012-01-05 19:17:38 +00001494 // Update the process thread list with this new thread.
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001495 // FIXME: should we be letting UpdateThreadList handle this?
1496 // FIXME: by using pids instead of tids, we can only support one thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001497 inferior.reset(process.CreateNewPOSIXThread(process, pid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001498
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001499 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001500 log->Printf ("ProcessMonitor::%s() adding pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001501 process.GetThreadList().AddThread(inferior);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001502
Matt Kopecb2910442013-07-09 15:09:45 +00001503 process.AddThreadForInitialStopIfNeeded(pid);
1504
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001505 // Let our process instance know the thread has stopped.
1506 process.SendMessage(ProcessMessage::Trace(pid));
1507
1508FINISH:
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001509 return args->m_error.Success();
1510}
1511
Johnny Chen25e68e32011-06-14 19:19:50 +00001512void
1513ProcessMonitor::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error)
1514{
1515 static const char *g_thread_name = "lldb.process.linux.operation";
1516
Zachary Turneracee96a2014-09-23 18:32:09 +00001517 if (m_operation_thread.IsJoinable())
Johnny Chen25e68e32011-06-14 19:19:50 +00001518 return;
1519
Zachary Turner39de3112014-09-09 20:54:56 +00001520 m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error);
Johnny Chen25e68e32011-06-14 19:19:50 +00001521}
1522
Johnny Chen25e68e32011-06-14 19:19:50 +00001523void *
1524ProcessMonitor::AttachOpThread(void *arg)
1525{
1526 AttachArgs *args = static_cast<AttachArgs*>(arg);
1527
Greg Clayton743ecf42012-10-16 20:20:18 +00001528 if (!Attach(args)) {
1529 sem_post(&args->m_semaphore);
Johnny Chen25e68e32011-06-14 19:19:50 +00001530 return NULL;
Greg Clayton743ecf42012-10-16 20:20:18 +00001531 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001532
1533 ServeOperation(args);
1534 return NULL;
1535}
1536
1537bool
1538ProcessMonitor::Attach(AttachArgs *args)
1539{
1540 lldb::pid_t pid = args->m_pid;
1541
1542 ProcessMonitor *monitor = args->m_monitor;
1543 ProcessLinux &process = monitor->GetProcess();
Johnny Chen25e68e32011-06-14 19:19:50 +00001544 lldb::ThreadSP inferior;
Ashok Thirumurthi01186352013-03-28 16:02:31 +00001545 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen25e68e32011-06-14 19:19:50 +00001546
Matt Kopec085d6ce2013-05-31 22:00:07 +00001547 // Use a map to keep track of the threads which we have attached/need to attach.
1548 Host::TidMap tids_to_attach;
Johnny Chen25e68e32011-06-14 19:19:50 +00001549 if (pid <= 1)
1550 {
1551 args->m_error.SetErrorToGenericError();
1552 args->m_error.SetErrorString("Attaching to process 1 is not allowed.");
1553 goto FINISH;
1554 }
1555
Matt Kopec085d6ce2013-05-31 22:00:07 +00001556 while (Host::FindProcessThreads(pid, tids_to_attach))
Johnny Chen25e68e32011-06-14 19:19:50 +00001557 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001558 for (Host::TidMap::iterator it = tids_to_attach.begin();
1559 it != tids_to_attach.end(); ++it)
1560 {
1561 if (it->second == false)
1562 {
1563 lldb::tid_t tid = it->first;
1564
1565 // Attach to the requested process.
1566 // An attach will cause the thread to stop with a SIGSTOP.
1567 if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0)
1568 {
1569 // No such thread. The thread may have exited.
1570 // More error handling may be needed.
1571 if (errno == ESRCH)
1572 {
1573 tids_to_attach.erase(it);
1574 continue;
1575 }
1576 else
1577 {
1578 args->m_error.SetErrorToErrno();
1579 goto FINISH;
1580 }
1581 }
1582
Todd Fiala9be50492014-07-01 16:30:53 +00001583 ::pid_t wpid;
Matt Kopec085d6ce2013-05-31 22:00:07 +00001584 // Need to use __WALL otherwise we receive an error with errno=ECHLD
1585 // At this point we should have a thread stopped if waitpid succeeds.
Todd Fiala9be50492014-07-01 16:30:53 +00001586 if ((wpid = waitpid(tid, NULL, __WALL)) < 0)
Matt Kopec085d6ce2013-05-31 22:00:07 +00001587 {
1588 // No such thread. The thread may have exited.
1589 // More error handling may be needed.
1590 if (errno == ESRCH)
1591 {
1592 tids_to_attach.erase(it);
1593 continue;
1594 }
1595 else
1596 {
1597 args->m_error.SetErrorToErrno();
1598 goto FINISH;
1599 }
1600 }
1601
1602 if (!SetDefaultPtraceOpts(tid))
1603 {
1604 args->m_error.SetErrorToErrno();
1605 goto FINISH;
1606 }
1607
1608 // Update the process thread list with the attached thread.
Michael Sartain9f822cd2013-07-31 23:27:46 +00001609 inferior.reset(process.CreateNewPOSIXThread(process, tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +00001610
Matt Kopec085d6ce2013-05-31 22:00:07 +00001611 if (log)
1612 log->Printf ("ProcessMonitor::%s() adding tid = %" PRIu64, __FUNCTION__, tid);
1613 process.GetThreadList().AddThread(inferior);
1614 it->second = true;
Matt Kopecb2910442013-07-09 15:09:45 +00001615 process.AddThreadForInitialStopIfNeeded(tid);
Matt Kopec085d6ce2013-05-31 22:00:07 +00001616 }
1617 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001618 }
1619
Matt Kopec085d6ce2013-05-31 22:00:07 +00001620 if (tids_to_attach.size() > 0)
Johnny Chen25e68e32011-06-14 19:19:50 +00001621 {
Matt Kopec085d6ce2013-05-31 22:00:07 +00001622 monitor->m_pid = pid;
1623 // Let our process instance know the thread has stopped.
1624 process.SendMessage(ProcessMessage::Trace(pid));
Johnny Chen25e68e32011-06-14 19:19:50 +00001625 }
Matt Kopec085d6ce2013-05-31 22:00:07 +00001626 else
1627 {
1628 args->m_error.SetErrorToGenericError();
1629 args->m_error.SetErrorString("No such process.");
1630 }
Johnny Chen25e68e32011-06-14 19:19:50 +00001631
1632 FINISH:
1633 return args->m_error.Success();
1634}
1635
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001636bool
Matt Kopec085d6ce2013-05-31 22:00:07 +00001637ProcessMonitor::SetDefaultPtraceOpts(lldb::pid_t pid)
1638{
1639 long ptrace_opts = 0;
1640
1641 // Have the child raise an event on exit. This is used to keep the child in
1642 // limbo until it is destroyed.
1643 ptrace_opts |= PTRACE_O_TRACEEXIT;
1644
1645 // Have the tracer trace threads which spawn in the inferior process.
1646 // TODO: if we want to support tracing the inferiors' child, add the
1647 // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK)
1648 ptrace_opts |= PTRACE_O_TRACECLONE;
1649
1650 // Have the tracer notify us before execve returns
1651 // (needed to disable legacy SIGTRAP generation)
1652 ptrace_opts |= PTRACE_O_TRACEEXEC;
1653
1654 return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0;
1655}
1656
1657bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001658ProcessMonitor::MonitorCallback(void *callback_baton,
1659 lldb::pid_t pid,
Peter Collingbourne2c67b9a2011-11-21 00:10:19 +00001660 bool exited,
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001661 int signal,
1662 int status)
1663{
1664 ProcessMessage message;
1665 ProcessMonitor *monitor = static_cast<ProcessMonitor*>(callback_baton);
Andrew Kaylor6578cb62013-07-09 22:36:48 +00001666 ProcessLinux *process = monitor->m_process;
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001667 assert(process);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001668 bool stop_monitoring;
1669 siginfo_t info;
Daniel Maleaa35970a2012-11-23 18:09:58 +00001670 int ptrace_err;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001671
Andrew Kaylor93132f52013-05-28 23:04:25 +00001672 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1673
1674 if (exited)
1675 {
1676 if (log)
1677 log->Printf ("ProcessMonitor::%s() got exit signal, tid = %" PRIu64, __FUNCTION__, pid);
1678 message = ProcessMessage::Exit(pid, status);
1679 process->SendMessage(message);
1680 return pid == process->GetID();
1681 }
1682
Daniel Maleaa35970a2012-11-23 18:09:58 +00001683 if (!monitor->GetSignalInfo(pid, &info, ptrace_err)) {
1684 if (ptrace_err == EINVAL) {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001685 if (log)
1686 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
Daniel Maleaa35970a2012-11-23 18:09:58 +00001687 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1688 if (!monitor->Resume(pid, SIGSTOP)) {
1689 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1690 }
1691 stop_monitoring = false;
1692 } else {
1693 // ptrace(GETSIGINFO) failed (but not due to group-stop). Most likely,
1694 // this means the child pid is gone (or not being debugged) therefore
Andrew Kaylor93132f52013-05-28 23:04:25 +00001695 // stop the monitor thread if this is the main pid.
1696 if (log)
1697 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d",
1698 __FUNCTION__, strerror(ptrace_err), pid, signal, status);
1699 stop_monitoring = pid == monitor->m_process->GetID();
Andrew Kaylor7d2abdf2013-09-04 16:06:04 +00001700 // If we are going to stop monitoring, we need to notify our process object
1701 if (stop_monitoring)
1702 {
1703 message = ProcessMessage::Exit(pid, status);
1704 process->SendMessage(message);
1705 }
Daniel Maleaa35970a2012-11-23 18:09:58 +00001706 }
1707 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00001708 else {
1709 switch (info.si_signo)
1710 {
1711 case SIGTRAP:
1712 message = MonitorSIGTRAP(monitor, &info, pid);
1713 break;
Greg Clayton542e4072012-09-07 17:49:29 +00001714
Stephen Wilson84ffe702011-03-30 15:55:52 +00001715 default:
1716 message = MonitorSignal(monitor, &info, pid);
1717 break;
1718 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001719
Stephen Wilson84ffe702011-03-30 15:55:52 +00001720 process->SendMessage(message);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001721 stop_monitoring = false;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001722 }
1723
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001724 return stop_monitoring;
1725}
1726
1727ProcessMessage
Stephen Wilson84ffe702011-03-30 15:55:52 +00001728ProcessMonitor::MonitorSIGTRAP(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001729 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001730{
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001731 ProcessMessage message;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001732
Andrew Kaylor93132f52013-05-28 23:04:25 +00001733 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1734
Johnny Chen0d5f2d42011-10-18 18:09:30 +00001735 assert(monitor);
1736 assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001737
Stephen Wilson84ffe702011-03-30 15:55:52 +00001738 switch (info->si_code)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001739 {
1740 default:
1741 assert(false && "Unexpected SIGTRAP code!");
1742 break;
1743
Matt Kopeca360d7e2013-05-17 19:27:47 +00001744 // TODO: these two cases are required if we want to support tracing
1745 // of the inferiors' children
1746 // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)):
1747 // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)):
1748
Matt Kopec650648f2013-01-08 16:30:18 +00001749 case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)):
1750 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001751 if (log)
1752 log->Printf ("ProcessMonitor::%s() received thread creation event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1753
Matt Kopec650648f2013-01-08 16:30:18 +00001754 unsigned long tid = 0;
1755 if (!monitor->GetEventMessage(pid, &tid))
1756 tid = -1;
1757 message = ProcessMessage::NewThread(pid, tid);
1758 break;
1759 }
1760
Matt Kopeca360d7e2013-05-17 19:27:47 +00001761 case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)):
Matt Kopec718be872013-10-09 19:39:55 +00001762 if (log)
1763 log->Printf ("ProcessMonitor::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP);
1764
1765 message = ProcessMessage::Exec(pid);
Matt Kopeca360d7e2013-05-17 19:27:47 +00001766 break;
1767
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001768 case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)):
1769 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001770 // The inferior process or one of its threads is about to exit.
1771 // Maintain the process or thread in a state of "limbo" until we are
1772 // explicitly commanded to detach, destroy, resume, etc.
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001773 unsigned long data = 0;
1774 if (!monitor->GetEventMessage(pid, &data))
1775 data = -1;
Andrew Kaylor93132f52013-05-28 23:04:25 +00001776 if (log)
Matt Kopecb2910442013-07-09 15:09:45 +00001777 log->Printf ("ProcessMonitor::%s() received limbo event, data = %lx, pid = %" PRIu64, __FUNCTION__, data, pid);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001778 message = ProcessMessage::Limbo(pid, (data >> 8));
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001779 break;
1780 }
1781
1782 case 0:
1783 case TRAP_TRACE:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001784 if (log)
1785 log->Printf ("ProcessMonitor::%s() received trace event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001786 message = ProcessMessage::Trace(pid);
1787 break;
1788
1789 case SI_KERNEL:
1790 case TRAP_BRKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001791 if (log)
1792 log->Printf ("ProcessMonitor::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001793 message = ProcessMessage::Break(pid);
1794 break;
Matt Kopece9ea0da2013-05-07 19:29:28 +00001795
1796 case TRAP_HWBKPT:
Andrew Kaylor93132f52013-05-28 23:04:25 +00001797 if (log)
1798 log->Printf ("ProcessMonitor::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid);
Matt Kopece9ea0da2013-05-07 19:29:28 +00001799 message = ProcessMessage::Watch(pid, (lldb::addr_t)info->si_addr);
1800 break;
Matt Kopec4a32bf52013-07-11 20:01:22 +00001801
1802 case SIGTRAP:
1803 case (SIGTRAP | 0x80):
1804 if (log)
1805 log->Printf ("ProcessMonitor::%s() received system call stop event, pid = %" PRIu64, __FUNCTION__, pid);
1806 // Ignore these signals until we know more about them
1807 monitor->Resume(pid, eResumeSignalNone);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001808 }
1809
1810 return message;
1811}
1812
Stephen Wilson84ffe702011-03-30 15:55:52 +00001813ProcessMessage
1814ProcessMonitor::MonitorSignal(ProcessMonitor *monitor,
Greg Clayton28041352011-11-29 20:50:10 +00001815 const siginfo_t *info, lldb::pid_t pid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00001816{
1817 ProcessMessage message;
1818 int signo = info->si_signo;
1819
Andrew Kaylor93132f52013-05-28 23:04:25 +00001820 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1821
Stephen Wilson84ffe702011-03-30 15:55:52 +00001822 // POSIX says that process behaviour is undefined after it ignores a SIGFPE,
1823 // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a
1824 // kill(2) or raise(3). Similarly for tgkill(2) on Linux.
1825 //
1826 // IOW, user generated signals never generate what we consider to be a
1827 // "crash".
1828 //
1829 // Similarly, ACK signals generated by this monitor.
1830 if (info->si_code == SI_TKILL || info->si_code == SI_USER)
1831 {
Andrew Kaylor93132f52013-05-28 23:04:25 +00001832 if (log)
Matt Kopecef143712013-06-03 18:00:07 +00001833 log->Printf ("ProcessMonitor::%s() received signal %s with code %s, pid = %d",
Andrew Kaylor93132f52013-05-28 23:04:25 +00001834 __FUNCTION__,
1835 monitor->m_process->GetUnixSignals().GetSignalAsCString (signo),
1836 (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"),
1837 info->si_pid);
1838
Stephen Wilson84ffe702011-03-30 15:55:52 +00001839 if (info->si_pid == getpid())
1840 return ProcessMessage::SignalDelivered(pid, signo);
1841 else
1842 return ProcessMessage::Signal(pid, signo);
1843 }
1844
Andrew Kaylor93132f52013-05-28 23:04:25 +00001845 if (log)
1846 log->Printf ("ProcessMonitor::%s() received signal %s", __FUNCTION__, monitor->m_process->GetUnixSignals().GetSignalAsCString (signo));
1847
Chaoren Lin28e57422015-02-03 01:51:25 +00001848 switch (signo)
1849 {
1850 case SIGSEGV:
1851 case SIGILL:
1852 case SIGFPE:
1853 case SIGBUS:
Stephen Wilson84ffe702011-03-30 15:55:52 +00001854 lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr);
Chaoren Lin28e57422015-02-03 01:51:25 +00001855 const auto reason = GetCrashReason(*info);
Stephen Wilson84ffe702011-03-30 15:55:52 +00001856 return ProcessMessage::Crash(pid, reason, signo, fault_addr);
1857 }
1858
1859 // Everything else is "normal" and does not require any special action on
1860 // our part.
1861 return ProcessMessage::Signal(pid, signo);
1862}
1863
Andrew Kaylord4d54992013-09-17 00:30:24 +00001864// On Linux, when a new thread is created, we receive to notifications,
1865// (1) a SIGTRAP|PTRACE_EVENT_CLONE from the main process thread with the
1866// child thread id as additional information, and (2) a SIGSTOP|SI_USER from
1867// the new child thread indicating that it has is stopped because we attached.
1868// We have no guarantee of the order in which these arrive, but we need both
1869// before we are ready to proceed. We currently keep a list of threads which
1870// have sent the initial SIGSTOP|SI_USER event. Then when we receive the
1871// SIGTRAP|PTRACE_EVENT_CLONE notification, if the initial stop has not occurred
1872// we call ProcessMonitor::WaitForInitialTIDStop() to wait for it.
1873
1874bool
1875ProcessMonitor::WaitForInitialTIDStop(lldb::tid_t tid)
1876{
1877 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1878 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001879 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waiting for thread to stop...", __FUNCTION__, tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001880
1881 // Wait for the thread to stop
1882 while (true)
1883 {
1884 int status = -1;
1885 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001886 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid...", __FUNCTION__, tid);
Todd Fiala9be50492014-07-01 16:30:53 +00001887 ::pid_t wait_pid = waitpid(tid, &status, __WALL);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001888 if (status == -1)
1889 {
1890 // If we got interrupted by a signal (in our process, not the
1891 // inferior) try again.
1892 if (errno == EINTR)
1893 continue;
1894 else
1895 {
1896 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001897 log->Printf("ProcessMonitor::%s(%" PRIu64 ") waitpid error -- %s", __FUNCTION__, tid, strerror(errno));
Andrew Kaylord4d54992013-09-17 00:30:24 +00001898 return false; // This is bad, but there's nothing we can do.
1899 }
1900 }
1901
1902 if (log)
Michael Sartainc258b302013-09-18 15:32:06 +00001903 log->Printf ("ProcessMonitor::%s(%" PRIu64 ") waitpid, status = %d", __FUNCTION__, tid, status);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001904
Todd Fiala9be50492014-07-01 16:30:53 +00001905 assert(static_cast<lldb::tid_t>(wait_pid) == tid);
Andrew Kaylord4d54992013-09-17 00:30:24 +00001906
1907 siginfo_t info;
1908 int ptrace_err;
1909 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1910 {
1911 if (log)
1912 {
1913 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed. errno=%d (%s)", __FUNCTION__, ptrace_err, strerror(ptrace_err));
1914 }
1915 return false;
1916 }
1917
1918 // If this is a thread exit, we won't get any more information.
1919 if (WIFEXITED(status))
1920 {
1921 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001922 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylord4d54992013-09-17 00:30:24 +00001923 return true;
1924 continue;
1925 }
1926
1927 assert(info.si_code == SI_USER);
1928 assert(WSTOPSIG(status) == SIGSTOP);
1929
1930 if (log)
1931 log->Printf ("ProcessMonitor::%s(bp) received thread stop signal", __FUNCTION__);
1932 m_process->AddThreadForInitialStopIfNeeded(wait_pid);
1933 return true;
1934 }
1935 return false;
1936}
1937
Andrew Kaylor93132f52013-05-28 23:04:25 +00001938bool
1939ProcessMonitor::StopThread(lldb::tid_t tid)
1940{
1941 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
1942
1943 // FIXME: Try to use tgkill or tkill
1944 int ret = tgkill(m_pid, tid, SIGSTOP);
1945 if (log)
1946 log->Printf ("ProcessMonitor::%s(bp) stopping thread, tid = %" PRIu64 ", ret = %d", __FUNCTION__, tid, ret);
1947
1948 // This can happen if a thread exited while we were trying to stop it. That's OK.
1949 // We'll get the signal for that later.
1950 if (ret < 0)
1951 return false;
1952
1953 // Wait for the thread to stop
1954 while (true)
1955 {
1956 int status = -1;
1957 if (log)
1958 log->Printf ("ProcessMonitor::%s(bp) waitpid...", __FUNCTION__);
Todd Fiala9be50492014-07-01 16:30:53 +00001959 ::pid_t wait_pid = ::waitpid (-1*getpgid(m_pid), &status, __WALL);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001960 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00001961 log->Printf ("ProcessMonitor::%s(bp) waitpid, pid = %" PRIu64 ", status = %d",
1962 __FUNCTION__, static_cast<lldb::pid_t>(wait_pid), status);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001963
Todd Fiala9be50492014-07-01 16:30:53 +00001964 if (wait_pid == -1)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001965 {
1966 // If we got interrupted by a signal (in our process, not the
1967 // inferior) try again.
1968 if (errno == EINTR)
1969 continue;
1970 else
1971 return false; // This is bad, but there's nothing we can do.
1972 }
1973
1974 // If this is a thread exit, we won't get any more information.
1975 if (WIFEXITED(status))
1976 {
1977 m_process->SendMessage(ProcessMessage::Exit(wait_pid, WEXITSTATUS(status)));
Todd Fiala9be50492014-07-01 16:30:53 +00001978 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001979 return true;
1980 continue;
1981 }
1982
1983 siginfo_t info;
1984 int ptrace_err;
1985 if (!GetSignalInfo(wait_pid, &info, ptrace_err))
1986 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001987 // another signal causing a StopAllThreads may have been received
1988 // before wait_pid's group-stop was processed, handle it now
1989 if (ptrace_err == EINVAL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00001990 {
Todd Fiala1b0539c2014-01-27 17:03:57 +00001991 assert(WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP);
Andrew Kaylor93132f52013-05-28 23:04:25 +00001992
Todd Fiala1b0539c2014-01-27 17:03:57 +00001993 if (log)
1994 log->Printf ("ProcessMonitor::%s() resuming from group-stop", __FUNCTION__);
1995 // inferior process is in 'group-stop', so deliver SIGSTOP signal
1996 if (!Resume(wait_pid, SIGSTOP)) {
1997 assert(0 && "SIGSTOP delivery failed while in 'group-stop' state");
1998 }
1999 continue;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002000 }
Todd Fiala1b0539c2014-01-27 17:03:57 +00002001
2002 if (log)
2003 log->Printf ("ProcessMonitor::%s() GetSignalInfo failed.", __FUNCTION__);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002004 return false;
2005 }
2006
2007 // Handle events from other threads
2008 if (log)
Todd Fiala9be50492014-07-01 16:30:53 +00002009 log->Printf ("ProcessMonitor::%s(bp) handling event, tid == %" PRIu64,
2010 __FUNCTION__, static_cast<lldb::tid_t>(wait_pid));
Andrew Kaylor93132f52013-05-28 23:04:25 +00002011
2012 ProcessMessage message;
2013 if (info.si_signo == SIGTRAP)
2014 message = MonitorSIGTRAP(this, &info, wait_pid);
2015 else
2016 message = MonitorSignal(this, &info, wait_pid);
2017
2018 POSIXThread *thread = static_cast<POSIXThread*>(m_process->GetThreadList().FindThreadByID(wait_pid).get());
2019
2020 // When a new thread is created, we may get a SIGSTOP for the new thread
2021 // just before we get the SIGTRAP that we use to add the thread to our
2022 // process thread list. We don't need to worry about that signal here.
2023 assert(thread || message.GetKind() == ProcessMessage::eSignalMessage);
2024
2025 if (!thread)
2026 {
2027 m_process->SendMessage(message);
2028 continue;
2029 }
2030
2031 switch (message.GetKind())
2032 {
Saleem Abdulrasool6747c7d2014-07-20 05:28:57 +00002033 case ProcessMessage::eExecMessage:
2034 llvm_unreachable("unexpected message");
Michael Sartainc258b302013-09-18 15:32:06 +00002035 case ProcessMessage::eAttachMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +00002036 case ProcessMessage::eInvalidMessage:
2037 break;
2038
2039 // These need special handling because we don't want to send a
2040 // resume even if we already sent a SIGSTOP to this thread. In
2041 // this case the resume will cause the thread to disappear. It is
2042 // unlikely that we'll ever get eExitMessage here, but the same
2043 // reasoning applies.
2044 case ProcessMessage::eLimboMessage:
2045 case ProcessMessage::eExitMessage:
2046 if (log)
2047 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2048 // SendMessage will set the thread state as needed.
2049 m_process->SendMessage(message);
2050 // If this is the thread we're waiting for, stop waiting. Even
2051 // though this wasn't the signal we expected, it's the last
2052 // signal we'll see while this thread is alive.
Todd Fiala9be50492014-07-01 16:30:53 +00002053 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002054 return true;
2055 break;
2056
Matt Kopecb2910442013-07-09 15:09:45 +00002057 case ProcessMessage::eSignalMessage:
2058 if (log)
2059 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2060 if (WSTOPSIG(status) == SIGSTOP)
2061 {
2062 m_process->AddThreadForInitialStopIfNeeded(tid);
2063 thread->SetState(lldb::eStateStopped);
2064 }
2065 else
2066 {
2067 m_process->SendMessage(message);
2068 // This isn't the stop we were expecting, but the thread is
2069 // stopped. SendMessage will handle processing of this event,
2070 // but we need to resume here to get the stop we are waiting
2071 // for (otherwise the thread will stop again immediately when
2072 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002073 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Matt Kopecb2910442013-07-09 15:09:45 +00002074 Resume(wait_pid, eResumeSignalNone);
2075 }
2076 break;
2077
Andrew Kaylor93132f52013-05-28 23:04:25 +00002078 case ProcessMessage::eSignalDeliveredMessage:
2079 // This is the stop we're expecting.
Todd Fiala9be50492014-07-01 16:30:53 +00002080 if (static_cast<lldb::tid_t>(wait_pid) == tid &&
2081 WIFSTOPPED(status) &&
2082 WSTOPSIG(status) == SIGSTOP &&
2083 info.si_code == SI_TKILL)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002084 {
2085 if (log)
2086 log->Printf ("ProcessMonitor::%s(bp) received signal, done waiting", __FUNCTION__);
2087 thread->SetState(lldb::eStateStopped);
2088 return true;
2089 }
2090 // else fall-through
Andrew Kaylor93132f52013-05-28 23:04:25 +00002091 case ProcessMessage::eBreakpointMessage:
2092 case ProcessMessage::eTraceMessage:
2093 case ProcessMessage::eWatchpointMessage:
2094 case ProcessMessage::eCrashMessage:
2095 case ProcessMessage::eNewThreadMessage:
2096 if (log)
2097 log->Printf ("ProcessMonitor::%s(bp) handling message", __FUNCTION__);
2098 // SendMessage will set the thread state as needed.
2099 m_process->SendMessage(message);
2100 // This isn't the stop we were expecting, but the thread is
2101 // stopped. SendMessage will handle processing of this event,
2102 // but we need to resume here to get the stop we are waiting
2103 // for (otherwise the thread will stop again immediately when
2104 // we try to resume).
Todd Fiala9be50492014-07-01 16:30:53 +00002105 if (static_cast<lldb::tid_t>(wait_pid) == tid)
Andrew Kaylor93132f52013-05-28 23:04:25 +00002106 Resume(wait_pid, eResumeSignalNone);
2107 break;
2108 }
2109 }
2110 return false;
2111}
2112
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002113void
Johnny Chen25e68e32011-06-14 19:19:50 +00002114ProcessMonitor::ServeOperation(OperationArgs *args)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002115{
Stephen Wilson570243b2011-01-19 01:37:06 +00002116 ProcessMonitor *monitor = args->m_monitor;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002117
Stephen Wilson570243b2011-01-19 01:37:06 +00002118 // We are finised with the arguments and are ready to go. Sync with the
2119 // parent thread and start serving operations on the inferior.
2120 sem_post(&args->m_semaphore);
2121
Michael Sartain704bf892013-10-09 01:28:57 +00002122 for(;;)
2123 {
Daniel Malea1efb4182013-09-16 23:12:18 +00002124 // wait for next pending operation
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002125 if (sem_wait(&monitor->m_operation_pending))
2126 {
2127 if (errno == EINTR)
2128 continue;
2129 assert(false && "Unexpected errno from sem_wait");
2130 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002131
Daniel Malea1efb4182013-09-16 23:12:18 +00002132 monitor->m_operation->Execute(monitor);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002133
Daniel Malea1efb4182013-09-16 23:12:18 +00002134 // notify calling thread that operation is complete
2135 sem_post(&monitor->m_operation_done);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002136 }
2137}
2138
2139void
2140ProcessMonitor::DoOperation(Operation *op)
2141{
Daniel Malea1efb4182013-09-16 23:12:18 +00002142 Mutex::Locker lock(m_operation_mutex);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002143
Daniel Malea1efb4182013-09-16 23:12:18 +00002144 m_operation = op;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002145
Daniel Malea1efb4182013-09-16 23:12:18 +00002146 // notify operation thread that an operation is ready to be processed
2147 sem_post(&m_operation_pending);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002148
Daniel Malea1efb4182013-09-16 23:12:18 +00002149 // wait for operation to complete
Todd Fiala8ce3dee2014-01-24 22:59:22 +00002150 while (sem_wait(&m_operation_done))
2151 {
2152 if (errno == EINTR)
2153 continue;
2154 assert(false && "Unexpected errno from sem_wait");
2155 }
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002156}
2157
2158size_t
2159ProcessMonitor::ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
2160 Error &error)
2161{
2162 size_t result;
2163 ReadOperation op(vm_addr, buf, size, error, result);
2164 DoOperation(&op);
2165 return result;
2166}
2167
2168size_t
2169ProcessMonitor::WriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size,
2170 lldb_private::Error &error)
2171{
2172 size_t result;
2173 WriteOperation op(vm_addr, buf, size, error, result);
2174 DoOperation(&op);
2175 return result;
2176}
2177
2178bool
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002179ProcessMonitor::ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char* reg_name,
Matt Kopec7de48462013-03-06 17:20:48 +00002180 unsigned size, RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002181{
2182 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002183 ReadRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002184 DoOperation(&op);
2185 return result;
2186}
2187
2188bool
Matt Kopec7de48462013-03-06 17:20:48 +00002189ProcessMonitor::WriteRegisterValue(lldb::tid_t tid, unsigned offset,
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002190 const char* reg_name, const RegisterValue &value)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002191{
2192 bool result;
Ashok Thirumurthiacbb1a52013-05-09 19:59:47 +00002193 WriteRegOperation op(tid, offset, reg_name, value, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002194 DoOperation(&op);
2195 return result;
2196}
2197
2198bool
Matt Kopec7de48462013-03-06 17:20:48 +00002199ProcessMonitor::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002200{
2201 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002202 ReadGPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002203 DoOperation(&op);
2204 return result;
2205}
2206
2207bool
Matt Kopec7de48462013-03-06 17:20:48 +00002208ProcessMonitor::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002209{
2210 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002211 ReadFPROperation op(tid, buf, buf_size, result);
Stephen Wilsonade1aea2011-01-19 01:31:38 +00002212 DoOperation(&op);
2213 return result;
2214}
2215
2216bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002217ProcessMonitor::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2218{
2219 bool result;
2220 ReadRegisterSetOperation op(tid, buf, buf_size, regset, result);
2221 DoOperation(&op);
2222 return result;
2223}
2224
2225bool
Matt Kopec7de48462013-03-06 17:20:48 +00002226ProcessMonitor::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002227{
2228 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002229 WriteGPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002230 DoOperation(&op);
2231 return result;
2232}
2233
2234bool
Matt Kopec7de48462013-03-06 17:20:48 +00002235ProcessMonitor::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size)
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002236{
2237 bool result;
Matt Kopec7de48462013-03-06 17:20:48 +00002238 WriteFPROperation op(tid, buf, buf_size, result);
Peter Collingbourne10bc0102011-06-03 20:41:02 +00002239 DoOperation(&op);
2240 return result;
2241}
2242
2243bool
Matt Kopec58c0b962013-03-20 20:34:35 +00002244ProcessMonitor::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset)
2245{
2246 bool result;
2247 WriteRegisterSetOperation op(tid, buf, buf_size, regset, result);
2248 DoOperation(&op);
2249 return result;
2250}
2251
2252bool
Richard Mitton0a558352013-10-17 21:14:00 +00002253ProcessMonitor::ReadThreadPointer(lldb::tid_t tid, lldb::addr_t &value)
2254{
2255 bool result;
2256 ReadThreadPointerOperation op(tid, &value, result);
2257 DoOperation(&op);
2258 return result;
2259}
2260
2261bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002262ProcessMonitor::Resume(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002263{
2264 bool result;
Andrew Kaylor93132f52013-05-28 23:04:25 +00002265 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
2266
2267 if (log)
2268 log->Printf ("ProcessMonitor::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid,
2269 m_process->GetUnixSignals().GetSignalAsCString (signo));
Stephen Wilson84ffe702011-03-30 15:55:52 +00002270 ResumeOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002271 DoOperation(&op);
Andrew Kaylor93132f52013-05-28 23:04:25 +00002272 if (log)
2273 log->Printf ("ProcessMonitor::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false");
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002274 return result;
2275}
2276
2277bool
Stephen Wilson84ffe702011-03-30 15:55:52 +00002278ProcessMonitor::SingleStep(lldb::tid_t tid, uint32_t signo)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002279{
2280 bool result;
Stephen Wilson84ffe702011-03-30 15:55:52 +00002281 SingleStepOperation op(tid, signo, result);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002282 DoOperation(&op);
2283 return result;
2284}
2285
2286bool
Ed Maste4e0999b2014-04-01 18:14:06 +00002287ProcessMonitor::Kill()
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002288{
Ed Maste4e0999b2014-04-01 18:14:06 +00002289 return kill(GetPID(), SIGKILL) == 0;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002290}
2291
2292bool
Daniel Maleaa35970a2012-11-23 18:09:58 +00002293ProcessMonitor::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err)
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002294{
2295 bool result;
Daniel Maleaa35970a2012-11-23 18:09:58 +00002296 SiginfoOperation op(tid, siginfo, result, ptrace_err);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002297 DoOperation(&op);
2298 return result;
2299}
2300
2301bool
2302ProcessMonitor::GetEventMessage(lldb::tid_t tid, unsigned long *message)
2303{
2304 bool result;
2305 EventMessageOperation op(tid, message, result);
2306 DoOperation(&op);
2307 return result;
2308}
2309
Greg Clayton743ecf42012-10-16 20:20:18 +00002310lldb_private::Error
Matt Kopec085d6ce2013-05-31 22:00:07 +00002311ProcessMonitor::Detach(lldb::tid_t tid)
Stephen Wilson84ffe702011-03-30 15:55:52 +00002312{
Greg Clayton28041352011-11-29 20:50:10 +00002313 lldb_private::Error error;
Matt Kopec085d6ce2013-05-31 22:00:07 +00002314 if (tid != LLDB_INVALID_THREAD_ID)
2315 {
2316 DetachOperation op(tid, error);
Greg Clayton743ecf42012-10-16 20:20:18 +00002317 DoOperation(&op);
2318 }
Greg Clayton743ecf42012-10-16 20:20:18 +00002319 return error;
Greg Clayton542e4072012-09-07 17:49:29 +00002320}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002321
2322bool
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002323ProcessMonitor::DupDescriptor(const char *path, int fd, int flags)
2324{
Peter Collingbourne62343202011-06-14 03:55:54 +00002325 int target_fd = open(path, flags, 0666);
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002326
2327 if (target_fd == -1)
2328 return false;
2329
Pavel Labath493c3a12015-02-04 10:36:57 +00002330 if (dup2(target_fd, fd) == -1)
2331 return false;
2332
2333 return (close(target_fd) == -1) ? false : true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +00002334}
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002335
2336void
2337ProcessMonitor::StopMonitoringChildProcess()
2338{
Zachary Turneracee96a2014-09-23 18:32:09 +00002339 if (m_monitor_thread.IsJoinable())
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002340 {
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +00002341 ::pthread_kill(m_monitor_thread.GetNativeThread().GetSystemHandle(), SIGUSR1);
Zachary Turner39de3112014-09-09 20:54:56 +00002342 m_monitor_thread.Join(nullptr);
Stephen Wilson9212d7f2011-01-04 21:40:25 +00002343 }
2344}
Stephen Wilson84ffe702011-03-30 15:55:52 +00002345
2346void
2347ProcessMonitor::StopMonitor()
2348{
2349 StopMonitoringChildProcess();
Greg Clayton743ecf42012-10-16 20:20:18 +00002350 StopOpThread();
Daniel Malea1efb4182013-09-16 23:12:18 +00002351 sem_destroy(&m_operation_pending);
2352 sem_destroy(&m_operation_done);
Pavel Labath3a2da9e2015-02-06 11:32:52 +00002353 if (m_terminal_fd >= 0) {
2354 close(m_terminal_fd);
2355 m_terminal_fd = -1;
2356 }
Stephen Wilson84ffe702011-03-30 15:55:52 +00002357}
2358
2359void
Greg Clayton743ecf42012-10-16 20:20:18 +00002360ProcessMonitor::StopOpThread()
2361{
Zachary Turneracee96a2014-09-23 18:32:09 +00002362 if (!m_operation_thread.IsJoinable())
Greg Clayton743ecf42012-10-16 20:20:18 +00002363 return;
2364
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +00002365 DoOperation(EXIT_OPERATION);
Zachary Turner39de3112014-09-09 20:54:56 +00002366 m_operation_thread.Join(nullptr);
Greg Clayton743ecf42012-10-16 20:20:18 +00002367}