22 #include <sys/types.h>
28 #include <sys/ptrace.h>
31 #include <bits/wordsize.h>
58 #define EF_RF (0x00010000)
71 static int linux_userproc_snprintf(
struct target *
target,
72 char *buf,
int bufsiz);
74 static int linux_userproc_postloadinit(
struct target *
target);
75 static int linux_userproc_attach_internal(
struct target *
target);
76 static int linux_userproc_detach(
struct target *
target,
int stay_paused);
78 static int linux_userproc_kill(
struct target *
target,
int sig);
79 static int linux_userproc_loadspaces(
struct target *
target);
80 static int linux_userproc_loadregions(
struct target *
target,
82 static int linux_userproc_loaddebugfiles(
struct target *
target,
88 int *again,
void *priv);
91 linux_userproc_instantiate_overlay(
struct target *
target,
96 linux_userproc_lookup_overlay_thread_by_id(
struct target *
target,
int id);
98 linux_userproc_lookup_overlay_thread_by_name(
struct target *
target,
char *
name);
101 static int linux_userproc_pause(
struct target *
target,
int nowait);
102 static int linux_userproc_resume(
struct target *
target);
110 static unsigned char *linux_userproc_read(
struct target *
target,
112 unsigned long length,
114 static unsigned long linux_userproc_write(
struct target *
target,
116 unsigned long length,
125 static int linux_userproc_load_all_threads(
struct target *
target,
int force);
129 static int linux_userproc_flush_current_thread(
struct target *
target);
130 static int linux_userproc_flush_all_threads(
struct target *
target);
131 static int linux_userproc_invalidate_all_threads(
struct target *
target);
132 static int linux_userproc_thread_snprintf(
struct target *
target,
134 char *buf,
int bufsiz,
135 int detail,
char *sep,
char *kvsep);
161 struct target *overlay);
163 struct target *overlay);
166 static int linux_userproc_evloop_add_tid(
struct target *
target,
int tid);
167 static int linux_userproc_evloop_del_tid(
struct target *
target,
int tid);
173 .
snprintf = linux_userproc_snprintf,
175 .init = linux_userproc_init,
176 .fini = linux_userproc_fini,
177 .attach = linux_userproc_attach_internal,
178 .detach = linux_userproc_detach,
179 .kill = linux_userproc_kill,
180 .loadspaces = linux_userproc_loadspaces,
181 .loadregions = linux_userproc_loadregions,
182 .loaddebugfiles = linux_userproc_loaddebugfiles,
183 .postloadinit = linux_userproc_postloadinit,
185 .handle_exception = linux_userproc_handle_exception,
188 .handle_interrupted_step = NULL,
190 .instantiate_overlay = linux_userproc_instantiate_overlay,
191 .lookup_overlay_thread_by_id = linux_userproc_lookup_overlay_thread_by_id,
192 .lookup_overlay_thread_by_name = linux_userproc_lookup_overlay_thread_by_name,
194 .
status = linux_userproc_status,
195 .pause = linux_userproc_pause,
196 .resume = linux_userproc_resume,
197 .monitor = linux_userproc_monitor,
198 .poll = linux_userproc_poll,
199 .read = linux_userproc_read,
200 .write = linux_userproc_write,
202 .gettid = linux_userproc_gettid,
203 .free_thread_state = linux_userproc_free_thread_state,
207 .load_available_threads = linux_userproc_load_all_threads,
208 .load_thread = linux_userproc_load_thread,
209 .load_current_thread = linux_userproc_load_current_thread,
210 .load_all_threads = linux_userproc_load_all_threads,
211 .pause_thread = linux_userproc_pause_thread,
212 .flush_thread = linux_userproc_flush_thread,
213 .flush_current_thread = linux_userproc_flush_current_thread,
214 .flush_all_threads = linux_userproc_flush_all_threads,
215 .thread_snprintf = linux_userproc_thread_snprintf,
220 .readreg = linux_userproc_read_reg,
221 .writereg = linux_userproc_write_reg,
222 .get_unused_debug_reg = linux_userproc_get_unused_debug_reg,
223 .set_hw_breakpoint = linux_userproc_set_hw_breakpoint,
224 .set_hw_watchpoint = linux_userproc_set_hw_watchpoint,
225 .unset_hw_breakpoint = linux_userproc_unset_hw_breakpoint,
226 .unset_hw_watchpoint = linux_userproc_unset_hw_watchpoint,
238 {
"pid",
'p',
"PID",0,
"A target process to attach to.",-4 },
239 {
"program",
'b',
"FILE",0,
"A program to launch as the target.",-4 },
240 {
"args",
'a',
"LIST",0,
"A comma-separated argument list.",-4 },
241 {
"envvars",
'e',
"LIST",0,
"A comma-separated envvar list.",-4 },
268 for (i = 0; lspec->
argv[i] != NULL; ++i,++ac) ;
273 for (i = 0; lspec->
envp[i] != NULL; ++i,++ac)
274 envstrlen += strlen(lspec->
envp[i]) + 1;
277 av = calloc(ac + 1,
sizeof(
char *));
282 av[j++] = strdup(
"-e");
284 av[j] = malloc(envstrlen);
287 while (
p < (av[j] + envstrlen)) {
288 rc = snprintf(
p,(av[j] + envstrlen) -
p,
"%s",lspec->
envp[i]);
301 av[j++] = strdup(
"--");
302 av[j++] = strdup(lspec->
program);
305 for (i = 0; lspec->
argv[i] != NULL; ++i) {
306 av[j++] = strdup(lspec->
argv[i]);
312 else if (lspec->
pid > -1) {
313 av = calloc(3,
sizeof(
char *));
314 av[0] = strdup(
"-p");
316 snprintf(av[1],11,
"%d",lspec->
pid);
333 struct argp_option *opti;
340 if (key == ARGP_KEY_INIT)
342 else if (!state->input)
343 return ARGP_ERR_UNKNOWN;
359 if (key == opti->key) {
373 verror(
"cannot mix arguments for ptrace target (%c) with non-ptrace"
394 return ARGP_ERR_UNKNOWN;
398 case ARGP_KEY_NO_ARGS:
399 case ARGP_KEY_SUCCESS:
404 if (lspec->program) {
405 verror(
"cannot specify both binary to launch and an argv!\n");
409 lspec->argv = calloc(tstate->
quoted_argc + 1,
sizeof(
char *));
420 if (lspec && lspec->pid > -1 && lspec->program) {
421 verror(
"cannot specify both pid (to attach) and binary (to launch!)\n");
427 lspec->pid = atoi(arg);
430 lspec->program = strdup(arg);
432 lspec->argv[0] = strdup(arg);
436 for (i = 0; arg[i] !=
'\0'; ++i) {
440 lspec->argv = calloc(count+2,
sizeof(
char *));
442 lspec->argv[0] = strdup(lspec->program);
445 argdup = strdup(arg);
446 for (i = 0; argdup[i] !=
'\0'; ++i) {
447 if (argdup[i] ==
',') {
449 lspec->argv[count++] = strdup(&argdup[previ]);
454 lspec->argv[count+1] = NULL;
458 for (i = 0; arg[i] !=
'\0'; ++i) {
462 lspec->envp = calloc(count+1,
sizeof(
char *));
463 lspec->envp[0] = arg;
466 argdup = strdup(arg);
467 for (i = 0; argdup[i] !=
'\0'; ++i) {
468 if (argdup[i] ==
',') {
470 lspec->envp[count] = strdup(&argdup[previ]);
475 lspec->envp[count] = NULL;
479 return ARGP_ERR_UNKNOWN;
495 #define INITIAL_PTRACE_OPTS \
496 PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXIT
510 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
526 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
542 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
559 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
565 == (SIGTRAP | (PTRACE_EVENT_EXEC << 8));
576 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
582 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8));
600 if (lspec->
pid > -1) {
601 return linux_userproc_attach(spec,evloop);
604 return linux_userproc_launch(spec,evloop);
611 lspec = calloc(1,
sizeof(*lspec));
653 char pbuf[PATH_MAX*2];
654 char main_exe[PATH_MAX];
666 if (geteuid() != 0) {
667 verror(
"must be root!\n");
672 snprintf(buf,256,
"/proc/%d/stat",
pid);
673 if (stat(buf,&sbuf)) {
674 verror(
"stat %s: %s\n",buf,strerror(errno));
679 stfile = fopen(buf,
"r");
681 verror(
"fopen %s: %s\n",buf,strerror(errno));
684 else if (!fgets(buf,256,stfile)) {
685 verror(
"fgets %s: %s\n",buf,strerror(errno));
689 if (strlen(buf) && buf[strlen(buf)-1] ==
'\n')
690 buf[strlen(buf)-1] =
'\0';
698 snprintf(pbuf,PATH_MAX*2,
"/proc/%d/exe",
pid);
699 if ((rc = readlink(pbuf,main_exe,PATH_MAX - 1)) < 1)
704 verror(
"binfile_open %s: %s\n",pbuf,strerror(errno));
742 memset(lstate,0,
sizeof(*lstate));
747 target->
state = lstate;
759 static struct target *linux_userproc_launch(
struct target_spec *spec,
760 struct evloop *evloop) {
762 struct target *target;
770 struct binfile *binfile;
772 int inpfd[2] = { -1,-1 };
773 int outpfd[2] = { -1,-1 };
774 int errpfd[2] = { -1,-1 };
780 #define LUP_SC_EXEC 59
781 #define LUP_SC_MPROTECT 10
782 #define LUP_SC_MMAP 9
783 #define LUP_SC_MUNMAP 11
784 #define LUP_SC_MMAP2 9
785 #define LUP_SC_PRCTL 157
786 #define LUP_SC_ARCH_PRCTL 158
787 #define LUP_SC_SET_THREAD_AREA 205
789 #define LUP_SC_EXEC 11
790 #define LUP_SC_MPROTECT 125
791 #define LUP_SC_MMAP 90
792 #define LUP_SC_MUNMAP 91
793 #define LUP_SC_MMAP2 192
794 #define LUP_SC_PRCTL 172
795 #define LUP_SC_ARCH_PRCTL 172
796 #define LUP_SC_SET_THREAD_AREA 243
799 struct user_regs_struct uregs;
801 unsigned long orig_eax;
806 char *argv_default[2] = { NULL,NULL };
814 if (argv == NULL || *argv == NULL) {
818 argv_default[0] = filename;
831 verror(
"binfile_open %s: %s\n",filename,strerror(errno));
851 verror(
"could not check if %s is static/dynamic exe; aborting!\n",
878 memset(lstate,0,
sizeof(*lstate));
885 target->
state = lstate;
894 verror(
"pipe(in): %s\n",strerror(errno));
898 else if (spec->
infile && strcmp(spec->
infile,
"-") != 0) {
899 infd = open(spec->
infile,O_RDONLY);
907 verror(
"pipe(out): %s\n",strerror(errno));
912 outfd = open(spec->
outfile,O_WRONLY | O_CREAT | O_APPEND,
913 S_IRUSR | S_IWUSR | S_IRGRP);
921 verror(
"pipe(err): %s\n",strerror(errno));
926 errfd = open(spec->
errfile,O_WRONLY | O_CREAT | O_APPEND,
927 S_IRUSR | S_IWUSR | S_IRGRP);
938 if ((pid = fork()) > 0) {
948 target->
infd = inpfd[1];
950 else if (infd > -1) {
954 if (outpfd[1] > -1) {
957 target->
outfd = outpfd[0];
959 else if (outfd > -1) {
963 if (errpfd[1] > -1) {
966 target->
errfd = errpfd[0];
968 else if (errfd > -1) {
977 if (waitpid(pid,&pstatus,0) < 0) {
978 if (errno == ECHILD || errno == EINVAL) {
979 verror(
"waitpid(%d): %s\n",pid,strerror(errno));
983 if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
984 verror(
"ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
990 if (WIFSTOPPED(pstatus)) {
994 if (WSTOPSIG(pstatus) == SIGTRAP) {
996 if (ptrace(PTRACE_GETREGS,pid,0,&uregs) < 0) {
997 vwarn(
"could not read EAX to deciper exec syscall!\n");
1000 #if __WORDSIZE == 64
1001 orig_eax = uregs.orig_rax;
1003 orig_eax = uregs.orig_eax;
1010 if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
1011 verror(
"ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
1018 if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
1019 verror(
"ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
1029 dup2(inpfd[0],STDIN_FILENO);
1031 else if (infd > 0) {
1032 dup2(infd,STDIN_FILENO);
1034 else if (!spec->
infile || strcmp(
"-",spec->
infile) != 0) {
1035 close(STDIN_FILENO);
1041 if (outpfd[1] > 0) {
1044 dup2(inpfd[1],STDOUT_FILENO);
1046 else if (outfd > 0) {
1047 dup2(outfd,STDOUT_FILENO);
1050 newfd = open(
"/dev/null",O_WRONLY);
1051 dup2(newfd,STDOUT_FILENO);
1057 if (errpfd[1] > 0) {
1060 dup2(inpfd[1],STDERR_FILENO);
1062 else if (errfd > 0) {
1063 dup2(errfd,STDERR_FILENO);
1066 newfd = open(
"/dev/null",O_WRONLY);
1067 dup2(newfd,STDERR_FILENO);
1075 ptrace(PTRACE_TRACEME,0,NULL,NULL);
1076 kill(getpid(),SIGINT);
1078 execve(filename,argv,envp);
1082 verror(
"fork: %s\n",strerror(errno));
1098 if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
1099 verror(
"ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
1103 if (waitpid(pid,&pstatus,0) < 0) {
1104 if (errno == ECHILD || errno == EINVAL) {
1105 verror(
"waitpid(%d): %s\n",pid,strerror(errno));
1112 if (WIFSTOPPED(pstatus)) {
1116 if (WSTOPSIG(pstatus) == SIGTRAP) {
1117 if (ptrace(PTRACE_GETREGS,pid,0,&uregs) < 0) {
1118 vwarn(
"could not read EAX to deciper syscall; skipping inference!\n");
1122 #if __WORDSIZE == 64
1123 orig_eax = uregs.orig_rax;
1125 orig_eax = uregs.orig_eax;
1183 else if (WIFCONTINUED(pstatus)) {
1186 else if (WIFSIGNALED(pstatus)) {
1187 verror(
"pid %d signaled (%d) in initial tracing!\n",
1188 pid,WTERMSIG(pstatus));
1191 else if (WIFEXITED(pstatus)) {
1196 verror(
"pid %d bailed in initial tracing!\n",pid);
1200 vwarn(
"pid %d: unhandled waitpid condition while waiting for load; trying again!\n",pid);
1221 if (ptrace(PTRACE_SETOPTIONS,pid,NULL,lstate->
ptrace_opts) < 0) {
1222 vwarn(
"ptrace setoptions failed: %s\n",strerror(errno));
1227 if (ptrace(PTRACE_POKEUSER,pid,offsetof(
struct user,u_debugreg[6]),0)) {
1228 verror(
"could not clear status debug reg, continuing anyway: %s!\n",
1234 "cleared status debug reg 6 for pid %d\n",pid);
1250 if (inpfd[1] > -1) {
1261 if (outpfd[0] > -1) {
1272 if (errpfd[0] > -1) {
1284 RPUT(binfile,binfile,target,trefcnt);
1289 static int __tid_exists(
int pid,
tid_t tid) {
1293 snprintf(buf,256,
"/proc/%d/task/%d",pid,tid);
1294 if (stat(buf,&sbuf)) {
1320 verror(
"cannot attach to thread until process is attached to!\n");
1327 if (!__tid_exists(pid,child)) {
1328 verror(
"thread %d in pid %d does not exist!\n",child,pid);
1355 (gpointer)(uintptr_t)child,NULL,&value)) {
1356 racy_status = (int)(uintptr_t)value;
1360 if (WIFSTOPPED(racy_status) && WSTOPSIG(racy_status) == SIGSTOP) {
1365 vwarn(
"new racy thread %d had status %d (but not SIGSTOP);"
1366 " assuming it is stopped though!\n",child,racy_status);
1381 vwarn(
"ptrace setoptions failed: %s\n",strerror(errno));
1405 rc = waitpid(child,&pstatus,WNOHANG | __WALL);
1408 "waitpid returned nothing for new non-racy tid %d!\n",child);
1411 verror(
"waitpid(%d): %s\n",child,strerror(errno));
1415 "waited for new non-racy tid %d successfully\n",child);
1437 linux_userproc_evloop_add_tid(target,child);
1443 static int __handle_internal_detaching(
struct target *target,
1449 #if __WORDSIZE == 64
1456 int pid = lstate->
pid;
1463 if (!WIFSTOPPED(pstatus)) {
1465 "pid %d thread %"PRIiTID" not stopped; ignoring\n",pid,tid);
1473 if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_CLONE << 8)) {
1474 ptrace(PTRACE_GETEVENTMSG,tid,NULL,&newstatus);
1475 newtid = (
tid_t)newstatus;
1477 "target %d thread %d cloned new thread %d; NOT attaching!\n",
1482 if (!linux_userproc_load_thread(target,tid,0)) {
1487 tstate->last_status = tstate->last_signo = WSTOPSIG(pstatus);
1488 if (tstate->last_status == (SIGTRAP | 0x80)) {
1490 "thread %"PRIiTID" stopped with syscall trap signo %d, ignoring\n",
1491 tid,tstate->last_status);
1492 tstate->last_signo = -1;
1494 else if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_EXIT << 8)) {
1496 pid,tstate->last_status);
1497 tstate->last_signo = -1;
1499 else if (tstate->last_status == SIGTRAP) {
1502 "thread %"PRIiTID" stopped with trap %d, minimal handling\n",
1503 tid,tstate->last_status);
1504 tstate->last_signo = -1;
1513 cdr = ptrace(PTRACE_PEEKUSER,tid,
1514 offsetof(
struct user,u_debugreg[6]),NULL);
1516 vwarn(
"could not read current val of status debug reg;"
1517 " don't know which handler to call; fatal!\n");
1523 "ignoring single step event pid %d thread %"PRIiTID"\n",
1543 ipval = ptrace(PTRACE_PEEKUSER,tid,
1544 offsetof(
struct user,u_debugreg[dreg]),NULL );
1546 verror(
"could not read current val of debug reg %d after up status!\n",dreg);
1551 "found hw break (status) in dreg %d on 0x%"PRIxADDR"\n",
1555 ipval = linux_userproc_read_reg(target,tid,target->
ipregno);
1557 verror(
"could not read EIP while finding probepoint: %s\n",
1573 "found hw break (eip) in dreg %d on 0x%"PRIxADDR"\n",
1585 " in debug reg %d, BUT no probepoint!\n",
1591 "hw bp %d pid %d thread %"PRIiTID", not resetting EIP\n",
1599 "sw bp pid %d thread %"PRIiTID", resetting EIP\n",
1606 verror(
"could not reset EIP; thread will crash\n");
1612 vwarn(
"could not find bp and not in sstep; letting thread"
1613 " pid %d thread %"PRIiTID" detach without handling"
1620 else if (WIFCONTINUED(pstatus)) {
1622 "waitpid CONT event pid %d thread %"PRIiTID" (status 0x%x); ignoring\n",
1624 tstate->last_signo = -1;
1625 tstate->last_status = -1;
1627 else if (WIFEXITED(pstatus)) {
1633 "waitpid EXITED event pid %d thread %"PRIiTID" (status 0x%x); ignoring\n",
1635 tstate->last_signo = -1;
1636 tstate->last_status = -1;
1640 else if (WIFSIGNALED(pstatus)) {
1642 "waitpid SIGNALED event pid %d thread %"PRIiTID" (status 0x%x); ignoring\n",
1644 tstate->last_signo = -1;
1645 tstate->last_status = -1;
1651 "unknown waitpid event pid %d thread %"PRIiTID" (status 0x%x)\n",
1672 retval = waitpid(tid,&status,WNOHANG | __WALL);
1675 "pid %d thread %"PRIiTID" running; not handling\n",pid,tid);
1678 else if (retval < 0) {
1679 verror(
"waitpid pid %d thread %"PRIiTID": %s\n",pid,tid,strerror(errno));
1692 return __handle_internal_detaching(target,tthread,status);
1696 int detaching_all,
int stay_paused) {
1713 verror(
"cannot detach from thread until process is attached to!\n");
1725 linux_userproc_evloop_del_tid(target,tid);
1730 snprintf(buf,256,
"/proc/%d/task/%d",pid,tid);
1731 if (stat(buf,&sbuf) == 0) {
1749 ptrace(PTRACE_DETACH,tid,NULL,(
void *)(uintptr_t)(stay_paused ? SIGSTOP : 0));
1755 if (!detaching_all && !stay_paused)
1769 static int linux_userproc_snprintf(
struct target *target,
1770 char *buf,
int bufsiz) {
1775 return snprintf(buf,bufsiz,
"ptrace(%d,%s)",lspec->
pid,lspec->
program);
1777 return snprintf(buf,bufsiz,
"ptrace(%d)",lspec->
pid);
1780 static tid_t linux_userproc_gettid(
struct target *target) {
1791 tthread = linux_userproc_load_current_thread(target,0);
1793 verror(
"could not load current thread to get TID!\n");
1797 return tthread->
tid;
1800 static void linux_userproc_free_thread_state(
struct target *target,
void *
state) {
1804 static int __linux_userproc_load_thread_status(
struct target_thread *tthread,
1805 tid_t tid,
int force) {
1819 "pid %d thread %"PRIiTID" already valid\n",pid,tid);
1824 pid,tid,tthread->
tid);
1827 snprintf(buf,64,
"/proc/%d/task/%d/stat",pid,tthread->
tid);
1829 statf = fopen(buf,
"r");
1832 "fopen(%s): %s; UNKNOWN!\n",buf,strerror(errno));
1837 if ((rc = fscanf(statf,
"%d (%s %c",&rtid,buf,&pstate))) {
1838 if (pstate ==
'R' || pstate ==
'r')
1840 else if (pstate ==
'W' || pstate ==
'w')
1842 else if (pstate ==
'S' || pstate ==
's')
1844 else if (pstate ==
'D' || pstate ==
'd')
1846 else if (pstate ==
'Z' || pstate ==
'z')
1848 else if (pstate ==
'T' || pstate ==
't')
1850 else if (pstate ==
'X' || pstate ==
'x')
1853 verror(
"fscanf returned %d; read tid %d (%s) %c; UNKNOWN!\n",
1854 rc,rtid,buf,pstate);
1859 else if (rc < 0 && errno == EINTR) {
1874 static struct target_thread *__linux_userproc_load_thread(
struct target *target,
1875 tid_t tid,
int force,
1886 verror(
"thread %"PRIiTID" does not exist; forgot to attach?\n",tid);
1899 pid,tid,tthread->
tid);
1902 __linux_userproc_load_thread_status(tthread,tid,force);
1909 if (ptrace(PTRACE_GETREGS,tthread->
tid,NULL,&(tstate->
regs)) == -1) {
1910 verror(
"ptrace(GETREGS): %s\n",strerror(errno));
1918 memset(&tstate->
regs,0,
sizeof(tstate->
regs));
1927 static struct target_thread *linux_userproc_load_thread(
struct target *target,
1928 tid_t tid,
int force) {
1929 return __linux_userproc_load_thread(target,tid,force,0);
1932 static struct target_thread *linux_userproc_load_current_thread(
struct target *target,
1937 return linux_userproc_load_thread(target,lstate->
current_tid,force);
1940 static int linux_userproc_load_all_threads(
struct target *target,
int force) {
1942 GHashTableIter iter;
1946 g_hash_table_iter_init(&iter,target->
threads);
1947 while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
1951 if (!linux_userproc_load_thread(target,tthread->tid,force)) {
1960 static int linux_userproc_flush_thread(
struct target *target,
tid_t tid) {
1970 verror(
"cannot flush unknown thread %"PRIiTID"; forgot to load?\n",tid);
1979 if (ptrace(PTRACE_SETREGS,tthread->
tid,NULL,&(tstate->
regs)) == -1) {
1980 verror(
"ptrace(SETREGS): %s\n",strerror(errno));
1988 static int linux_userproc_flush_current_thread(
struct target *target) {
1989 if (!target->
current_thread && linux_userproc_load_current_thread(target,0))
1995 static int linux_userproc_flush_all_threads(
struct target *target) {
1997 GHashTableIter iter;
2001 g_hash_table_iter_init(&iter,target->
threads);
2002 while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
2003 if (key == (gpointer)TID_GLOBAL)
2006 rc = linux_userproc_flush_thread(target,tthread->tid);
2016 static int linux_userproc_thread_snprintf(
struct target *target,
2018 char *buf,
int bufsiz,
2019 int detail,
char *sep,
char *kvsep) {
2021 struct user_regs_struct *r;
2030 #
if __WORDSIZE == 64
2031 #define __V_LINUX_PTRACE_REG_LARGE_SIZE
2032 #ifdef __V_LINUX_PTRACE_REG_LARGE_SIZE
2045 rc += snprintf((rc >= bufsiz) ? NULL : buf + rc,
2046 (rc >= bufsiz) ? 0 :bufsiz - rc,
2047 "%s" "ip%s%"RF "%s" "bp%s%"RF "%s" "sp%s%"RF "%s"
2048 "flags%s%"RF "%s" "ax%s%"RF "%s" "bx%s%"RF "%s"
2049 "cx%s%"RF "%s" "dx%s%"RF "%s" "di%s%"RF "%s"
2050 "si%s%"RF "%s" "cs%s%"RF "%s" "ss%s%"RF "%s"
2051 "ds%s%"RF "%s" "es%s%"RF "%s"
2052 "fs%s%"RF "%s" "gs%s%"RF,
2053 #
if __WORDSIZE == 64
2054 sep,kvsep,r->rip,sep,kvsep,r->rbp,sep,kvsep,r->rsp,sep,
2055 kvsep,r->eflags,sep,kvsep,r->rax,sep,kvsep,r->rbx,sep,
2056 kvsep,r->rcx,sep,kvsep,r->rdx,sep,kvsep,r->rdi,sep,
2057 kvsep,r->rsi,sep,kvsep,r->cs,sep,kvsep,r->ss,sep,
2058 kvsep,r->ds,sep,kvsep,r->es,sep,
2059 kvsep,r->fs,sep,kvsep,r->gs
2061 sep,kvsep,r->eip,sep,kvsep,r->ebp,sep,kvsep,r->esp,sep,
2062 kvsep,r->eflags,sep,kvsep,r->eax,sep,kvsep,r->ebx,sep,
2063 kvsep,r->ecx,sep,kvsep,r->edx,sep,kvsep,r->edi,sep,
2064 kvsep,r->esi,sep,kvsep,r->cs,sep,kvsep,r->ss,sep,
2065 kvsep,r->ds,sep,kvsep,r->es,sep,
2066 kvsep,r->fs,sep,kvsep,r->gs
2070 rc += snprintf((rc >= bufsiz) ? NULL : buf + rc,
2071 (rc >= bufsiz) ? 0 :bufsiz - rc,
2072 "%s" "dr0%s%"DRF "%s" "dr1%s%"DRF
2073 "%s" "dr2%s%"DRF "%s" "dr3%s%"DRF
2074 "%s" "dr6%s%"DRF "%s" "dr7%s%"DRF,
2075 sep,kvsep,tstate->
dr[0],sep,kvsep,tstate->
dr[1],
2076 sep,kvsep,tstate->
dr[1],sep,kvsep,tstate->
dr[2],
2077 sep,kvsep,tstate->
dr[6],sep,kvsep,tstate->
dr[7]);
2082 static int linux_userproc_init(
struct target *target) {
2128 linux_userproc_evloop_add_tid(target,tthread->
tid);
2133 static int linux_userproc_postloadinit(
struct target *target) {
2137 static int linux_userproc_attach_internal(
struct target *target) {
2142 struct dirent *dirent;
2165 if (ptrace(PTRACE_ATTACH,pid,NULL,NULL) < 0) {
2166 verror(
"ptrace attach pid %d failed: %s\n",pid,strerror(errno));
2171 snprintf(buf,256,
"/proc/%d/mem",pid);
2172 if ((lstate->
memfd = open(buf,O_LARGEFILE,O_RDWR)) < 0) {
2173 verror(
"open %s failed, detaching: %s!\n",buf,strerror(errno));
2174 ptrace(PTRACE_DETACH,pid,NULL,NULL);
2188 if (waitpid(pid,&pstatus,0) < 0) {
2189 if (errno == ECHILD || errno == EINVAL)
2200 if (ptrace(PTRACE_SETOPTIONS,pid,NULL,lstate->
ptrace_opts) < 0) {
2201 vwarn(
"ptrace setoptions failed: %s\n",strerror(errno));
2208 snprintf(buf,256,
"/proc/%d/task",pid);
2209 if (!(dirp = opendir(buf))) {
2210 verror(
"could not opendir %s to attach to threads: %s!\n",
2211 buf,strerror(errno));
2216 while ((dirent = readdir(dirp))) {
2217 if (dirent->d_name[0] ==
'.')
2220 tid = (
tid_t)strtol(dirent->d_name,&endp,10);
2221 if (endp == dirent->d_name || errno == ERANGE || errno == EINVAL) {
2222 verror(
"weird error parsing thread id out of '%s': %s; skipping!\n",
2223 dirent->d_name,strerror(errno));
2231 if (ptrace(PTRACE_ATTACH,tid,NULL,NULL) < 0) {
2232 verror(
"ptrace attach tid %d failed: %s\n",tid,strerror(errno));
2240 if (waitpid(tid,&pstatus,__WALL) < 0) {
2241 if (errno == ECHILD || errno == EINVAL) {
2242 verror(
"waitpid tid %d failed: %s\n",tid,strerror(errno));
2251 if (ptrace(PTRACE_SETOPTIONS,tid,NULL,lstate->
ptrace_opts) < 0) {
2252 vwarn(
"ptrace setoptions failed, continuing: %s\n",strerror(errno));
2273 linux_userproc_evloop_add_tid(target,tid);
2281 static int linux_userproc_detach(
struct target *target,
int stay_paused) {
2284 GHashTableIter iter;
2305 threadlist = array_list_create(g_hash_table_size(target->
threads));
2306 g_hash_table_iter_init(&iter,target->
threads);
2307 while (g_hash_table_iter_next(&iter,NULL,(gpointer)&tthread)) {
2311 array_list_append(threadlist,tthread);
2313 for (i = 0; i < array_list_len(threadlist); ++i) {
2314 tthread = (
struct target_thread *)array_list_item(threadlist,i);
2318 verror(
"could not detach thread %"PRIiTID"\n",tthread->tid);
2322 array_list_free(threadlist);
2331 verror(
"could not detach global thread %"PRIiTID"\n",tthread->tid);
2354 kill(lstate->
pid,SIGCONT);
2356 if (lstate->
memfd > 0)
2357 close(lstate->
memfd);
2364 static int linux_userproc_fini(
struct target *target) {
2383 free(target->
state);
2388 static int linux_userproc_kill(
struct target *target,
int sig) {
2399 if (kill(lstate->
pid,sig))
2405 static int linux_userproc_loadspaces(
struct target *target) {
2414 static int linux_userproc_loadregions(
struct target *target,
2416 char buf[PATH_MAX*2];
2417 char main_exe[PATH_MAX];
2432 snprintf(buf,PATH_MAX*2,
"/proc/%d/exe",lstate->
pid);
2433 if ((rc = readlink(buf,main_exe,PATH_MAX - 1)) < 1)
2435 main_exe[rc] =
'\0';
2437 snprintf(buf,PATH_MAX,
"/proc/%d/maps",lstate->
pid);
2444 if (!(ret = fgets(buf,PATH_MAX*2,f)) && !errno)
2446 else if (!ret && errno) {
2447 verror(
"fgets: %s",strerror(errno));
2453 rc = sscanf(buf,
"%Lx-%Lx %c%c%c%c %Lx %*x:%*x %*d %s",&start,&end,
2454 &p[0],&p[1],&p[2],&p[3],&offset,buf);
2455 if (rc == 8 || rc == 7) {
2458 if (strncmp(main_exe,buf,PATH_MAX) == 0)
2460 else if (strcmp(buf,
"[heap]") == 0)
2462 else if (strcmp(buf,
"[stack]") == 0)
2464 else if (strcmp(buf,
"[vdso]") == 0)
2466 else if (strcmp(buf,
"[vsyscall]") == 0)
2512 else if (rc > 0 && !errno) {
2513 vwarn(
"weird content in /proc/pid/maps (%d)!\n",rc);
2515 else if (rc > 0 && errno) {
2516 vwarn(
"weird content in /proc/pid/maps (%d): %s!\n",rc,strerror(errno));
2529 static int linux_userproc_updateregions(
struct target *target,
2531 char buf[PATH_MAX*2];
2532 char main_exe[PATH_MAX];
2537 GList *t1,*t2,*t3,*t4;
2544 uint32_t prot_flags;
2552 snprintf(buf,PATH_MAX*2,
"/proc/%d/exe",lstate->
pid);
2553 if ((rc = readlink(buf,main_exe,PATH_MAX - 1)) < 1)
2555 main_exe[rc] =
'\0';
2557 snprintf(buf,PATH_MAX,
"/proc/%d/maps",lstate->
pid);
2571 if (!(ret = fgets(buf,PATH_MAX*2,f)) && !errno)
2573 else if (!ret && errno) {
2574 verror(
"fgets: %s",strerror(errno));
2580 rc = sscanf(buf,
"%Lx-%Lx %c%c%c%c %Lx %*x:%*x %*d %s",&start,&end,
2581 &p[0],&p[1],&p[2],&p[3],&offset,buf);
2582 if (rc == 8 || rc == 7) {
2585 if (strncmp(main_exe,buf,PATH_MAX) == 0)
2587 else if (strcmp(buf,
"[heap]") == 0)
2589 else if (strcmp(buf,
"[stack]") == 0)
2591 else if (strcmp(buf,
"[vdso]") == 0)
2593 else if (strcmp(buf,
"[vsyscall]") == 0)
2633 if (range->
end == end
2634 && range->
offset == offset
2644 if (start < region->base_load_addr)
2652 else if (rc > 0 && !errno) {
2653 vwarn(
"weird content in /proc/pid/maps (%d)!\n",rc);
2655 else if (rc > 0 && errno) {
2656 vwarn(
"weird content in /proc/pid/maps (%d): %s!\n",rc,strerror(errno));
2687 else if (
OBJMOD(range)) {
2712 if (!exists || !region->
ranges) {
2727 else if (
OBJNEW(region)) {
2735 if (linux_userproc_loaddebugfiles(target,space,region)) {
2736 vwarn(
"could not load debugfile for new region (%s:%s:%s)\n",
2754 static int linux_userproc_loaddebugfiles(
struct target *target,
2773 if (!region->
name || strlen(region->
name) == 0)
2816 static struct target *
2817 linux_userproc_instantiate_overlay(
struct target *target,
2821 struct target *overlay;
2837 linux_userproc_lookup_overlay_thread_by_id(
struct target *target,
int id) {
2843 retval = linux_userproc_load_thread(target,
id,0);
2851 "found overlay thread %d\n",
id);
2857 linux_userproc_lookup_overlay_thread_by_name(
struct target *target,
char *
name) {
2861 GHashTableIter iter;
2863 if ((rc = linux_userproc_load_all_threads(target,0)))
2864 vwarn(
"could not load %d threads; continuing anyway!\n",-rc);
2866 g_hash_table_iter_init(&iter,target->
threads);
2867 while (g_hash_table_iter_next(&iter,NULL,(gpointer)&tthread)) {
2870 else if (tthread->name && strcmp(tthread->name,name) == 0) {
2878 "found overlay thread %"PRIiTID"\n",retval->
tid);
2895 int pid = lstate->
pid;
2900 snprintf(buf,256,
"/proc/%d/stat",pid);
2901 statf = fopen(buf,
"r");
2903 verror(
"statf(%s): %s\n",buf,strerror(errno));
2907 if ((rc = fscanf(statf,
"%d (%s %c",&pid,buf,&pstate))) {
2908 if (pstate ==
'R' || pstate ==
'r' || pstate ==
'W' || pstate ==
'w')
2910 else if (pstate ==
'S' || pstate ==
's' || pstate ==
'D' || pstate ==
'd')
2912 else if (pstate ==
'Z' || pstate ==
'z')
2914 else if (pstate ==
'T' || pstate ==
't')
2917 vwarn(
"fscanf returned %d; read %d (%s) %c; returning TSTATUS_UNKNOWN!\n",
2922 else if (rc < 0 && errno == EINTR) {
2933 static int linux_userproc_pause(
struct target *target,
int nowait) {
2934 GHashTableIter iter;
2955 g_hash_table_iter_init(&iter,target->
threads);
2956 while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
2957 if (key == (gpointer)TID_GLOBAL)
2960 if (!linux_userproc_load_thread(target,tthread->tid,0)) {
2961 verror(
"could not load thread %"PRIiTID"; pausing anyway!\n",
2974 else if (waitid(P_PID,tthread->tid,&sinfo,WNOHANG | WNOWAIT) == 0
2975 && sinfo.si_pid == tthread->tid) {
2977 "tid %d has pending siginfo to waitpid on; not pausing here\n",
2994 if (kill(tthread->tid,SIGSTOP) < 0) {
2995 verror(
"kill(%d,SIGSTOP): %s\n",tthread->tid,strerror(errno));
3009 if (waitpid(tthread->tid,&pstatus,__WALL) < 0) {
3010 if (errno == ECHILD || errno == EINVAL) {
3011 verror(
"waitpid(%"PRIiTID"): %s\n",tthread->tid,strerror(errno));
3029 static int linux_userproc_pause_thread(
struct target *target,
tid_t tid,
3039 if (tid == TID_GLOBAL)
3061 if (!linux_userproc_load_thread(target,tthread->
tid,0)) {
3062 verror(
"could not load thread %"PRIiTID"; pausing anyway!\n",
3070 if (kill(tthread->
tid,SIGSTOP) < 0) {
3071 verror(
"kill(%d,SIGSTOP): %s\n",tthread->
tid,strerror(errno));
3083 if (waitpid(tthread->
tid,&pstatus,__WALL) < 0) {
3084 if (errno == ECHILD || errno == EINVAL) {
3098 static int linux_userproc_resume(
struct target *target) {
3100 GHashTableIter iter;
3129 g_hash_table_iter_init(&iter,target->
threads);
3130 while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
3131 if (key == (gpointer)TID_GLOBAL)
3158 if (ptrace(PTRACE_SETOPTIONS,tthread->tid,NULL,
3160 vwarn(
"ptrace setoptions on tid %"PRIiTID" failed: %s\n",
3161 tthread->tid,strerror(errno));
3169 verror(
"ptrace signo %d restart of tid %"PRIiTID" failed: %s\n",
3170 tstate->
last_signo,tthread->tid,strerror(errno));
3176 if (ptrace(lstate->
ptrace_type,tthread->tid,NULL,NULL) < 0) {
3177 verror(
"ptrace restart of tid %"PRIiTID" (status %d) failed: %s\n",
3178 tthread->tid,tthread->status,strerror(errno));
3185 lstate->
pid,tthread->tid);
3199 g_hash_table_iter_init(&iter,target->
threads);
3200 while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
3201 if (key == (gpointer)TID_GLOBAL)
3227 static target_status_t linux_userproc_handle_exception(
struct target *target,
3229 int *again,
void *priv) {
3234 #if __WORDSIZE == 64
3258 if (__tid_exists(pid,tid)) {
3260 "thread %d does not YET exist; might be new!\n",tid);
3262 (gpointer)(uintptr_t)tid,
3263 (gpointer)(uintptr_t)pstatus);
3272 if (WIFSTOPPED(pstatus)) {
3295 if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_CLONE << 8)) {
3296 ptrace(PTRACE_GETEVENTMSG,tid,NULL,&newstatus);
3297 newtid = (
tid_t)newstatus;
3299 "target %d thread %d cloned new thread %d; attaching now.\n",
3317 linux_userproc_updateregions(target,space);
3321 if (!__linux_userproc_load_thread(target,tid,0,1)) {
3332 "thread %"PRIiTID" stopped with syscall trap signo %d\n",
3336 else if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_EXIT << 8)) {
3338 "target %d tid %d exiting (%d)! will detach at next resume.\n",
3380 cdr = ptrace(PTRACE_PEEKUSER,tid,
3381 offsetof(
struct user,u_debugreg[6]),NULL);
3383 vwarn(
"could not read current val of status debug reg;"
3384 " don't know which handler to call; fatal!\n");
3407 ipval = ptrace(PTRACE_PEEKUSER,tid,
3408 offsetof(
struct user,u_debugreg[dreg]),NULL );
3410 verror(
"could not read current val of debug reg %d after up status!\n",dreg);
3415 "found hw break (status) in dreg %d on 0x%"PRIxADDR"\n",
3419 ipval = linux_userproc_read_reg(target,tid,target->
ipregno);
3421 verror(
"could not read EIP while finding probepoint: %s\n",
3437 "found hw break (eip) in dreg %d on 0x%"PRIxADDR"\n",
3441 "checking for SS or sw break on 0x%"PRIxADDR"\n",
3452 if (ptrace(PTRACE_POKEUSER,tid,
3453 offsetof(
struct user,u_debugreg[6]),0)) {
3454 verror(
"could not clear status debug reg, continuing"
3455 " anyway: %s!\n",strerror(errno));
3468 " in debug reg %d, BUT no probepoint!\n",
3495 else if (cdr & 0x4000) {
3519 vwarn(
"could not find hardware bp and not sstep'ing;"
3520 " letting user handle fault at 0x%"PRIxADDR"!\n",
3527 "thread %"PRIiTID" stopped with (our) signo %d\n",
3546 if (ptrace(lstate->
ptrace_type,tid,NULL,NULL) < 0) {
3548 tid,strerror(errno));
3556 "thread %"PRIiTID" stopped with (ext) signo %d\n",
3562 else if (WIFCONTINUED(pstatus)) {
3571 else if (WIFSIGNALED(pstatus) || WIFEXITED(pstatus)) {
3583 tthread,(
void *)(uintptr_t)pstatus);
3590 if (g_hash_table_size(target->
threads) == 2) {
3596 linux_userproc_detach(target,0);
3606 vwarn(
"unexpected child process status event: %08x; bailing!\n",
3634 struct target *target = (
struct target *)state;
3640 verror(
"could not find thread tid for readfd %d!\n",readfd);
3646 verror(
"waitpipe_drain: %s\n",strerror(errno));
3651 verror(
"cound not find thread %d!\n",tid);
3660 "synchronous ctl sig sent to tid %d; not calling waitpid; ignoring"
3661 " (probable multithread bug!)\n",
3668 retval = waitpid(tid,&status,WNOHANG | __WALL);
3670 if (errno == ECHILD || errno == EINVAL) {
3677 else if (retval == 0) {
3679 "tid %"PRIiTID" running; waitpid has nothing to report; ignoring!\n",
3685 "tid %"PRIiTID" running; handling evloop sig\n",tid);
3693 retval = linux_userproc_handle_exception(target,0,&again,&exst);
3696 verror(
"thread-specific status %d tid %d; bad!\n",retval,tid);
3701 verror(
"unexpected error on thread %d; bad!\n",tid);
3706 verror(
"unexpected unknown on thread %d; bad!\n",tid);
3726 "tid %"PRIiTID" done; removing its fd (%d) from evloop\n",
3751 "tid %"PRIiTID" signaled with %d; resuming; signal will hit tid\n",
3758 vwarn(
"unexpected pause on thread %d; bad!\n",tid);
3763 verror(
"unexpected error on thread %d; bad!\n",tid);
3774 static int linux_userproc_evloop_add_tid(
struct target *target,
int tid) {
3778 verror(
"no evloop attached!\n");
3784 "not adding waitpipe readfd %d for tid %d\n",readfd,tid);
3789 verror(
"could not add tid %d to waitpipe!\n",tid);
3797 "added waitpipe/evloop readfd %d for tid %d\n",readfd,tid);
3806 static int linux_userproc_evloop_del_tid(
struct target *target,
int tid) {
3810 verror(
"no evloop attached!\n");
3819 "removed waitpipe/evloop readfd %d for tid %d\n",
3824 "did not find valid readfd (%d) to remove tid %d from evloop!\n",
3828 if (target->
infd > -1)
3830 if (target->
outfd > -1)
3832 if (target->
errfd > -1)
3846 if (target->
infd > -1)
3849 if (target->
outfd > -1)
3852 if (target->
errfd > -1)
3861 linux_userproc_evloop_add_tid(target,tid);
3864 array_list_free(tids);
3882 linux_userproc_evloop_del_tid(target,tid);
3885 array_list_free(tids);
3899 struct timespec req, rem;
3900 unsigned int usec_thresh = 100;
3902 uint64_t total_ns = 0;
3906 total_us = tv->tv_sec * 1000000 + tv->tv_usec;
3907 total_ns = total_us * 1000;
3909 if (total_us < usec_thresh) {
3911 req.tv_nsec = total_ns;
3915 req.tv_nsec = usec_thresh * 1000;
3921 tid = waitpid(-lstate->
pid,&status,WNOHANG | __WALL);
3926 if (1 || errno == ECHILD || errno == EINVAL) {
3932 else if (tid == 0) {
3943 nanosleep(&req,&rem);
3945 total_ns -= req.tv_nsec - rem.tv_nsec;
3947 total_ns -= req.tv_nsec;
3950 if (total_ns > usec_thresh * 1000)
3951 req.tv_nsec = usec_thresh * 1000;
3953 req.tv_nsec = total_ns;
3977 retval = linux_userproc_handle_exception(target,0,NULL,&exst);
3980 vwarn(
"unhandled thread-specific status %d!\n",retval);
3992 static target_status_t linux_userproc_monitor(
struct target *target) {
4008 tid = waitpid(-1,&pstatus,__WALL);
4010 if (errno == ECHILD || errno == EINVAL)
4018 retval = linux_userproc_handle_exception(target,0,&again,&exst);
4027 vwarn(
"unhandled thread-specific status %d!\n",retval);
4033 static unsigned char *linux_userproc_read(
struct target *target,
4035 unsigned long length,
4036 unsigned char *buf) {
4048 unsigned long linux_userproc_write(
struct target *target,
4050 unsigned long length,
4051 unsigned char *buf) {
4054 #if __WORDSIZE == 64
4064 for (j = 0; j < length && j < 16; ++j)
4090 if (length % (__WORDSIZE / 8)) {
4092 word = ptrace(PTRACE_PEEKTEXT,lstate->
current_tid,
4093 (addr + length) - (length % (__WORDSIZE / 8)),
4096 verror(
"ptrace(PEEKTEXT) last word: %s\n",strerror(errno));
4101 for (j = 0; j < __WORDSIZE / 8; ++j)
4105 memcpy(&word,(buf + length) - (length % (__WORDSIZE / 8)),
4106 length % (__WORDSIZE / 8));
4109 for (j = 0; j < __WORDSIZE / 8; ++j)
4114 if (length / (__WORDSIZE / 8)) {
4115 for (i = 0; i < length; i += (__WORDSIZE / 8)) {
4118 #
if __WORDSIZE == 64
4119 addr + i,*(uint64_t *)(buf + i)) == -1) {
4121 addr + i,*(uint32_t *)(buf + i)) == -1) {
4123 verror(
"ptrace(POKETEXT): %s\n",strerror(errno));
4129 if (length % (__WORDSIZE / 8)) {
4132 (i) ? addr + i - (__WORDSIZE / 8) : addr,
4134 verror(
"ptrace(POKETEXT) last word: %s\n",strerror(errno));
4198 #if __WORDSIZE == 64
4200 10, 12, 11, 5, 13, 14, 4, 19,
4201 9, 8, 7, 6, 3, 2, 1, 0,
4203 -1, -1, -1, -1, -1, -1, -1, -1,
4204 -1, -1, -1, -1, -1, -1, -1, -1,
4205 -1, -1, -1, -1, -1, -1, -1, -1,
4206 -1, -1, -1, -1, -1, -1, -1, -1,
4207 18, 24, 17, 20, 23, 25, 26,
4213 -1, -1, -1, -1,-1, -1, -1, -1,-1, -1,
4214 -1, -1,-1, -1, -1, -1,-1, -1, -1, -1,
4219 6, 1, 2, 0, 15, 5, 3, 4,
4222 -1, -1, -1, -1, -1, -1, -1, -1,
4224 -1, -1, -1, -1, -1, -1, -1, -1,
4225 -1, -1, -1, -1, -1, -1, -1, -1,
4228 8, 13, 16, 7, 9, 10,
4229 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4236 REGVAL linux_userproc_read_reg(
struct target *target,
tid_t tid,
REG reg) {
4241 tthread = linux_userproc_load_thread(target,tid,0);
4243 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4252 if (reg >= arch_regcount(target->
arch)) {
4253 verror(
"regnum %d does not have a target mapping!\n",reg);
4259 ptrace_idx = dreg_to_ptrace_idx64[reg];
4260 return (
REGVAL)(((
unsigned long *)&(tstate->
regs))[ptrace_idx]);
4263 ptrace_idx = dreg_to_ptrace_idx32[reg];
4264 return (
REGVAL)(((
long int *)&(tstate->
regs))[ptrace_idx]);
4268 int linux_userproc_write_reg(
struct target *target,
tid_t tid,
REG reg,
4274 tthread = linux_userproc_load_thread(target,tid,0);
4276 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4285 if (reg >= arch_regcount(target->
arch)) {
4286 verror(
"regnum %d does not have a target mapping!\n",reg);
4291 ptrace_idx = dreg_to_ptrace_idx64[reg];
4292 ((
unsigned long *)&(tstate->
regs))[ptrace_idx] = (
unsigned long)value;
4295 ptrace_idx = dreg_to_ptrace_idx32[reg];
4296 ((
long int*)&(tstate->
regs))[ptrace_idx] = (
long int)value;
4308 static REG linux_userproc_get_unused_debug_reg(
struct target *target,
tid_t tid) {
4313 tthread = linux_userproc_load_thread(target,tid,0);
4315 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4321 if (!tstate->
dr[0]) { retval = 0; }
4322 else if (!tstate->
dr[1]) { retval = 1; }
4323 else if (!tstate->
dr[2]) { retval = 2; }
4324 else if (!tstate->
dr[3]) { retval = 3; }
4331 #define VWORDBYTESIZE __WORDSIZE / 8
4333 #if __WORDSIZE == 64
4334 static int read_ptrace_debug_reg(
int pid,
unsigned long *array) {
4336 static int read_ptrace_debug_reg(
int pid,
int *array) {
4341 for ( ; i < 8; ++i) {
4342 #if __WORDSIZE == 64
4344 (
unsigned long)ptrace(PTRACE_PEEKUSER,pid,
4345 offsetof(
struct user,u_debugreg[i]),NULL);
4348 (int)ptrace(PTRACE_PEEKUSER,pid,
4349 offsetof(
struct user,u_debugreg[i]),NULL);
4352 verror(
"ptrace(PEEKUSER): %s\n",strerror(errno));
4384 static int linux_userproc_set_hw_breakpoint(
struct target *target,
tid_t tid,
4386 #if __WORDSIZE == 64
4394 tthread = linux_userproc_load_thread(target,tid,0);
4396 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4402 if (reg < 0 || reg > 3) {
4408 ptrace(PTRACE_PEEKUSER,tid,
4409 offsetof(
struct user,u_debugreg[reg]),(
void *)&cdr);
4411 vwarn(
"could not read current val of debug reg %"PRIiREG": %s!\n",
4412 reg,strerror(errno));
4414 else if (cdr != 0) {
4422 tstate->
dr[reg] = addr;
4428 tstate->
dr[7] |= (1 << (reg * 2));
4429 tstate->
dr[7] &= ~(1 << (reg * 2 + 1));
4431 tstate->
dr[7] &= ~(3 << (16 + (reg * 4)));
4444 ptrace(PTRACE_POKEUSER,tid,
4445 offsetof(
struct user,u_debugreg[reg]),(
void *)(tstate->
dr[reg]));
4447 verror(
"could not update debug reg %"PRIiREG", aborting: %s!\n",
4448 reg,strerror(errno));
4452 ptrace(PTRACE_POKEUSER,tid,
4453 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4455 verror(
"could not update status debug reg, aborting: %s!\n",
4459 ptrace(PTRACE_POKEUSER,tid,
4460 offsetof(
struct user,u_debugreg[7]),(
void *)(tstate->
dr[7]));
4462 verror(
"could not update control debug reg, aborting: %s!\n",
4470 tstate->
dr[reg] = 0;
4475 static int linux_userproc_set_hw_watchpoint(
struct target *target,
tid_t tid,
4479 #if __WORDSIZE == 64
4487 tthread = linux_userproc_load_thread(target,tid,0);
4489 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4495 if (reg < 0 || reg > 3) {
4501 ptrace(PTRACE_PEEKUSER,tid,
4502 offsetof(
struct user,u_debugreg[reg]),(
void *)&cdr);
4504 vwarn(
"could not read current val of debug reg %"PRIiREG"!\n",reg);
4506 else if (cdr != 0) {
4514 tstate->
dr[reg] = addr;
4520 tstate->
dr[7] |= (1 << (reg * 2));
4521 tstate->
dr[7] &= ~(1 << (reg * 2 + 1));
4523 tstate->
dr[7] &= ~(3 << (16 + (reg * 4)));
4524 tstate->
dr[7] |= (whence << (16 + (reg * 4)));
4526 tstate->
dr[7] &= ~(3 << (18 + (reg * 4)));
4527 tstate->
dr[7] |= (watchsize << (18 + (reg * 4)));
4530 tstate->
dr[7] |= (1 << 8);
4535 tstate->
dr[6],tstate->
dr[7],whence,watchsize);
4539 ptrace(PTRACE_POKEUSER,tid,
4540 offsetof(
struct user,u_debugreg[reg]),(
void *)(tstate->
dr[reg]));
4542 verror(
"could not update debug reg %"PRIiREG" (%p), aborting: %s!\n",reg,
4543 (
void *)(tstate->
dr[reg]),strerror(errno));
4547 ptrace(PTRACE_POKEUSER,tid,
4548 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4550 verror(
"could not update status debug reg (%p), aborting: %s!\n",
4551 (
void *)(tstate->
dr[6]),strerror(errno));
4554 ptrace(PTRACE_POKEUSER,tid,
4555 offsetof(
struct user,u_debugreg[7]),(
void *)(tstate->
dr[7]));
4557 verror(
"could not update control debug reg (%p), aborting: %s!\n",
4558 (
void *)(tstate->
dr[7]),strerror(errno));
4565 tstate->
dr[reg] = 0;
4570 static int linux_userproc_unset_hw_breakpoint(
struct target *target,
tid_t tid,
4575 tthread = linux_userproc_load_thread(target,tid,0);
4577 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4583 if (reg < 0 || reg > 3) {
4589 tstate->
dr[reg] = 0;
4595 tstate->
dr[7] &= ~(3 << (reg * 2));
4599 ptrace(PTRACE_POKEUSER,tid,
4600 offsetof(
struct user,u_debugreg[reg]),(
void *)(tstate->
dr[reg]));
4602 verror(
"could not update debug reg %"PRIiREG", aborting: %s!\n",
4603 reg,strerror(errno));
4607 ptrace(PTRACE_POKEUSER,tid,
4608 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4610 verror(
"could not update status debug reg, aborting: %s!\n",
4614 ptrace(PTRACE_POKEUSER,tid,
4615 offsetof(
struct user,u_debugreg[7]),(
void *)(tstate->
dr[7]));
4617 verror(
"could not update control debug reg,aborting: %s!\n",
4628 static int linux_userproc_unset_hw_watchpoint(
struct target *target,
tid_t tid,
4631 return linux_userproc_unset_hw_breakpoint(target,tid,reg);
4635 ptrace(PTRACE_POKEUSER,tid,
4636 offsetof(
struct user,u_debugreg[7]),(
void *)0);
4638 verror(
"could not update control debug reg, aborting: %s!\n",
4649 tthread = linux_userproc_load_thread(target,tid,0);
4651 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4657 ptrace(PTRACE_POKEUSER,tid,
4658 offsetof(
struct user,u_debugreg[7]),(
void *)tstate->
dr[7]);
4660 verror(
"could not update control debug reg, aborting: %s!\n",
4672 tthread = linux_userproc_load_thread(target,tid,0);
4674 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4680 if (dreg < 0 || dreg > 3) {
4689 tstate->
dr[7] &= ~(3 << (dreg * 2));
4693 ptrace(PTRACE_POKEUSER,tid,
4694 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4696 verror(
"could not update status debug reg, aborting: %s!\n",
4700 ptrace(PTRACE_POKEUSER,tid,
4701 offsetof(
struct user,u_debugreg[7]),(
void *)(tstate->
dr[7]));
4703 verror(
"could not update control debug reg,aborting: %s!\n",
4719 tthread = linux_userproc_load_thread(target,tid,0);
4721 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4727 if (dreg < 0 || dreg > 3) {
4736 tstate->
dr[7] |= (1 << (dreg * 2));
4737 tstate->
dr[7] &= ~(1 << (dreg * 2 + 1));
4741 ptrace(PTRACE_POKEUSER,tid,
4742 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4744 verror(
"could not update status debug reg, aborting: %s!\n",
4748 ptrace(PTRACE_POKEUSER,tid,
4749 offsetof(
struct user,u_debugreg[7]),(
void *)(tstate->
dr[7]));
4751 verror(
"could not update control debug reg, aborting: %s!\n",
4768 struct target *overlay) {
4772 tthread = linux_userproc_load_thread(target,tid,0);
4774 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4785 ptrace(PTRACE_POKEUSER,tid,
4786 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4788 verror(
"could not update status debug reg, aborting: %s!\n",
4812 if (linux_userproc_flush_thread(target,tid) < 0) {
4813 verror(
"could not flush thread; not single stepping!\n");
4817 ptrace(PTRACE_SINGLESTEP,tid,NULL,NULL);
4819 verror(
"could not ptrace single step thread %"PRIiTID": %s\n",
4820 tid,strerror(errno));
4835 struct target *overlay) {
4839 tthread = linux_userproc_load_thread(target,tid,0);
4841 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4852 ptrace(PTRACE_POKEUSER,tid,
4853 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4855 verror(
"could not update status debug reg, aborting: %s!\n",
int waitpipe_add(int pid)
unsigned long target_generic_fd_write(int fd, ADDR addr, unsigned long length, unsigned char *buf)
int target_flush_all_threads(struct target *target)
int addrspace_detach_region(struct addrspace *space, struct memregion *region)
int linux_userproc_detach_evloop(struct target *target)
#define EVLOOP_HRET_REMOVEALLTYPES
#define vwarnopt(level, area, flags, format,...)
error_t linux_userproc_argp_parse_opt(int key, char *arg, struct argp_state *state)
char * linux_userproc_argp_header
#define INITIAL_PTRACE_OPTS
int waitpipe_init_auto(void(*alt_handler)(int, siginfo_t *, void *))
void target_broadcast_event(struct target *target, struct target_event *event)
int evloop_unset_fd(struct evloop *evloop, int fd, int fdtype)
struct debugfile * debugfile_from_file(char *filename, char *root_prefix, struct array_list *debugfile_load_opts_list)
int8_t ctl_sig_pausing_all
int waitpipe_get(int readfd)
struct target_event * target_create_event_2(struct target *target, struct target_thread *thread, target_event_t type, void *priv, void *priv2)
result_t probepoint_ss_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
int linux_userproc_singlestep_end(struct target *target, tid_t tid, struct target *overlay)
int addrspace_find_range_real(struct addrspace *space, ADDR addr, struct memregion **region_saveptr, struct memrange **range_saveptr)
int linux_userproc_at_exit(struct target *target, tid_t tid)
int linux_userproc_disable_hw_breakpoints(struct target *target, tid_t tid)
struct linux_userproc_spec * linux_userproc_build_spec(void)
GHashTable * soft_probepoints
int linux_userproc_enable_hw_breakpoint(struct target *target, tid_t tid, REG dreg)
int memregion_detach_range(struct memregion *region, struct memrange *range)
int target_resume(struct target *target)
#define v_g_list_foreach(glhead, glcur, elm)
int waitpipe_is_initialized(void)
#define OBJSLIVE(obj, type)
target_debug_bp_handler_t handle_break
struct target_thread * global_thread
struct target * target_create(char *type, struct target_spec *spec)
int target_flush_thread(struct target *target, tid_t tid)
int target_associate_debugfile(struct target *target, struct memregion *region, struct debugfile *debugfile)
struct target_ops linux_userspace_process_ops
#define verror(format,...)
#define LUP_SC_SET_THREAD_AREA
int target_find_memory_real(struct target *target, ADDR addr, struct addrspace **space_saveptr, struct memregion **region_saveptr, struct memrange **range_saveptr)
void linux_userproc_free_spec(struct linux_userproc_spec *lspec)
int linux_userproc_spec_to_argv(struct target_spec *spec, int *argc, char ***argv)
void target_detach_thread(struct target *target, struct target_thread *tthread)
int waitpipe_get_pid(int readfd)
struct memregion * memregion_create(struct addrspace *space, region_type_t type, char *name)
int target_invalidate_thread(struct target *target, struct target_thread *tthread)
#define vwarn(format,...)
GHashTable * new_racy_threads
result_t probepoint_bp_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint, int was_stepping)
int linux_userproc_enable_hw_breakpoints(struct target *target, tid_t tid)
#define LUP_SC_ARCH_PRCTL
enum __ptrace_request ptrace_type
struct argp_option linux_userproc_argp_opts[]
struct memrange * memrange_create(struct memregion *region, ADDR start, ADDR end, OFFSET offset, unsigned int prot_flags)
struct target_thread * current_thread
struct binfile * binfile_open__int(char *filename, char *root_prefix, struct binfile_instance *bfinst)
int linux_userproc_attach_thread(struct target *target, tid_t parent, tid_t child)
struct memregion * addrspace_match_region_start(struct addrspace *space, region_type_t rtype, ADDR start)
struct array_list * target_list_tids(struct target *target)
int linux_userproc_last_status(struct target *target, tid_t tid)
int waitpipe_remove(int pid)
#define THREAD_SPECIFIC_STATUS(status)
struct target * linux_userproc_instantiate(struct target_spec *spec, struct evloop *evloop)
struct target_location_ctxt * global_tlctxt
const char * target_regname(struct target *target, REG reg)
struct memregion * addrspace_match_region_name(struct addrspace *space, region_type_t rtype, char *name)
struct memrange * memregion_match_range(struct memregion *region, ADDR start)
int linux_userproc_notify_sw_breakpoint(struct target *target, ADDR addr, int notification)
struct target_spec * spec
target_type_t target_type
unsigned char * target_generic_fd_read(int fd, ADDR addr, unsigned long length, unsigned char *buf)
int linux_userproc_detach_thread(struct target *target, tid_t tid, int detaching_all, int stay_paused)
#define v_g_list_foreach_safe(glhead, glcur, glnext, elm)
#define vdebug(devel, areas, flags, format,...)
int evloop_set_fd(struct evloop *evloop, int fd, int fdtype, evloop_handler_t handler, void *state)
struct thread_probepoint_context * tpc
#define EVLOOP_HRET_ERROR
int linux_userproc_singlestep(struct target *target, tid_t tid, int isbp, struct target *overlay)
#define vdebugc(devel, areas, flags, format,...)
int __poll_and_handle_detaching(struct target *target, struct target_thread *tthread)
result_t probepoint_resumeat_handler(struct target *target, struct target_thread *tthread)
#define ARCH_X86_64_REG_COUNT
int unlink(const char *pathname)
struct array_list * debugfile_load_opts_list
#define ARCH_X86_REG_COUNT
void target_thread_set_status(struct target_thread *tthread, thread_status_t status)
#define EVLOOP_HRET_SUCCESS
int target_invalidate_all_threads(struct target *target)
int linux_userproc_last_signo(struct target *target, tid_t tid)
struct binfile * binfile_pointing
void target_reuse_thread_as_global(struct target *target, struct target_thread *thread)
struct target_location_ctxt * target_location_ctxt_create(struct target *target, tid_t tid, struct memregion *region)
int target_finalize(struct target *target)
unsigned int breakpoint_instrs_len
GHashTable * hard_probepoints
int linux_userproc_pid(struct target *target)
int linux_userproc_disable_hw_breakpoint(struct target *target, tid_t tid, REG dreg)
int waitpipe_drain(int pid)
int binfile_get_root_scope_sizes(struct binfile *binfile, int *named, int *duplicated, int *anon, int *numscopes)
int(* snprintf)(struct target *target, char *buf, int bufsiz)
void target_tid_set_status(struct target *target, tid_t tid, thread_status_t status)
struct target_spec * spec
int linux_userproc_at_syscall(struct target *target, tid_t tid)
#define EVLOOP_HRET_BADERROR
struct user_regs_struct regs
uint32_t nodisablehwbponss
#define RPUT(x, objtype, hx, rc)
char * debugfile_root_prefix
int linux_userproc_evloop_handler(int readfd, int fdtype, void *state)
struct target_thread * target_lookup_thread(struct target *target, tid_t tid)
void target_set_status(struct target *target, target_status_t status)
struct target_event * target_create_event(struct target *target, struct target_thread *thread, target_event_t type, void *priv)
struct target_thread * blocking_thread
target_debug_handler_t handle_step
#define array_list_foreach_fakeptr_t(alist, lpc, placeholder, intertype)
int linux_userproc_attach_evloop(struct target *target, struct evloop *evloop)
struct memregion * addrspace_find_region(struct addrspace *space, char *name)
struct argp linux_userproc_argp
struct target_thread * target_create_thread(struct target *target, tid_t tid, void *tstate, void *tpstate)
int target_write_reg(struct target *target, tid_t tid, REG reg, REGVAL value)
struct probepoint * probepoint
#define OBJSDEAD(obj, type)
struct addrspace * addrspace_create(struct target *target, char *name, ADDR tag)
target_type_t supported_overlay_types
int linux_userproc_at_exec(struct target *target, tid_t tid)