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");
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));
741 memset(lstate,0,
sizeof(*lstate));
746 target->
state = lstate;
758 static struct target *linux_userproc_launch(
struct target_spec *spec,
759 struct evloop *evloop) {
761 struct target *target;
769 struct binfile *binfile;
771 int inpfd[2] = { -1,-1 };
772 int outpfd[2] = { -1,-1 };
773 int errpfd[2] = { -1,-1 };
779 #define LUP_SC_EXEC 59
780 #define LUP_SC_MPROTECT 10
781 #define LUP_SC_MMAP 9
782 #define LUP_SC_MUNMAP 11
783 #define LUP_SC_MMAP2 9
784 #define LUP_SC_PRCTL 157
785 #define LUP_SC_ARCH_PRCTL 158
786 #define LUP_SC_SET_THREAD_AREA 205
788 #define LUP_SC_EXEC 11
789 #define LUP_SC_MPROTECT 125
790 #define LUP_SC_MMAP 90
791 #define LUP_SC_MUNMAP 91
792 #define LUP_SC_MMAP2 192
793 #define LUP_SC_PRCTL 172
794 #define LUP_SC_ARCH_PRCTL 172
795 #define LUP_SC_SET_THREAD_AREA 243
798 struct user_regs_struct uregs;
800 unsigned long orig_eax;
805 char *argv_default[2] = { NULL,NULL };
813 if (argv == NULL || *argv == NULL) {
817 argv_default[0] = filename;
830 verror(
"binfile_open %s: %s\n",filename,strerror(errno));
849 verror(
"could not check if %s is static/dynamic exe; aborting!\n",
876 memset(lstate,0,
sizeof(*lstate));
883 target->
state = lstate;
892 verror(
"pipe(in): %s\n",strerror(errno));
896 else if (spec->
infile && strcmp(spec->
infile,
"-") != 0) {
897 infd = open(spec->
infile,O_RDONLY);
905 verror(
"pipe(out): %s\n",strerror(errno));
910 outfd = open(spec->
outfile,O_WRONLY | O_CREAT | O_APPEND,
911 S_IRUSR | S_IWUSR | S_IRGRP);
919 verror(
"pipe(err): %s\n",strerror(errno));
924 errfd = open(spec->
errfile,O_WRONLY | O_CREAT | O_APPEND,
925 S_IRUSR | S_IWUSR | S_IRGRP);
936 if ((pid = fork()) > 0) {
946 target->
infd = inpfd[1];
948 else if (infd > -1) {
952 if (outpfd[1] > -1) {
955 target->
outfd = outpfd[0];
957 else if (outfd > -1) {
961 if (errpfd[1] > -1) {
964 target->
errfd = errpfd[0];
966 else if (errfd > -1) {
975 if (waitpid(pid,&pstatus,0) < 0) {
976 if (errno == ECHILD || errno == EINVAL) {
977 verror(
"waitpid(%d): %s\n",pid,strerror(errno));
981 if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
982 verror(
"ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
988 if (WIFSTOPPED(pstatus)) {
992 if (WSTOPSIG(pstatus) == SIGTRAP) {
994 if (ptrace(PTRACE_GETREGS,pid,0,&uregs) < 0) {
995 vwarn(
"could not read EAX to deciper exec syscall!\n");
999 orig_eax = uregs.orig_rax;
1001 orig_eax = uregs.orig_eax;
1008 if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
1009 verror(
"ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
1016 if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
1017 verror(
"ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
1027 dup2(inpfd[0],STDIN_FILENO);
1029 else if (infd > 0) {
1030 dup2(infd,STDIN_FILENO);
1032 else if (!spec->
infile || strcmp(
"-",spec->
infile) != 0) {
1033 close(STDIN_FILENO);
1039 if (outpfd[1] > 0) {
1042 dup2(inpfd[1],STDOUT_FILENO);
1044 else if (outfd > 0) {
1045 dup2(outfd,STDOUT_FILENO);
1048 newfd = open(
"/dev/null",O_WRONLY);
1049 dup2(newfd,STDOUT_FILENO);
1055 if (errpfd[1] > 0) {
1058 dup2(inpfd[1],STDERR_FILENO);
1060 else if (errfd > 0) {
1061 dup2(errfd,STDERR_FILENO);
1064 newfd = open(
"/dev/null",O_WRONLY);
1065 dup2(newfd,STDERR_FILENO);
1073 ptrace(PTRACE_TRACEME,0,NULL,NULL);
1074 kill(getpid(),SIGINT);
1076 execve(filename,argv,envp);
1080 verror(
"fork: %s\n",strerror(errno));
1096 if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
1097 verror(
"ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
1101 if (waitpid(pid,&pstatus,0) < 0) {
1102 if (errno == ECHILD || errno == EINVAL) {
1103 verror(
"waitpid(%d): %s\n",pid,strerror(errno));
1110 if (WIFSTOPPED(pstatus)) {
1114 if (WSTOPSIG(pstatus) == SIGTRAP) {
1115 if (ptrace(PTRACE_GETREGS,pid,0,&uregs) < 0) {
1116 vwarn(
"could not read EAX to deciper syscall; skipping inference!\n");
1120 #if __WORDSIZE == 64
1121 orig_eax = uregs.orig_rax;
1123 orig_eax = uregs.orig_eax;
1181 else if (WIFCONTINUED(pstatus)) {
1184 else if (WIFSIGNALED(pstatus)) {
1185 verror(
"pid %d signaled (%d) in initial tracing!\n",
1186 pid,WTERMSIG(pstatus));
1189 else if (WIFEXITED(pstatus)) {
1194 verror(
"pid %d bailed in initial tracing!\n",pid);
1198 vwarn(
"pid %d: unhandled waitpid condition while waiting for load; trying again!\n",pid);
1219 if (ptrace(PTRACE_SETOPTIONS,pid,NULL,lstate->
ptrace_opts) < 0) {
1220 vwarn(
"ptrace setoptions failed: %s\n",strerror(errno));
1225 if (ptrace(PTRACE_POKEUSER,pid,offsetof(
struct user,u_debugreg[6]),0)) {
1226 verror(
"could not clear status debug reg, continuing anyway: %s!\n",
1232 "cleared status debug reg 6 for pid %d\n",pid);
1248 if (inpfd[1] > -1) {
1259 if (outpfd[0] > -1) {
1270 if (errpfd[0] > -1) {
1282 RPUT(binfile,binfile,target,trefcnt);
1287 static int __tid_exists(
int pid,
tid_t tid) {
1291 snprintf(buf,256,
"/proc/%d/task/%d",pid,tid);
1292 if (stat(buf,&sbuf)) {
1318 verror(
"cannot attach to thread until process is attached to!\n");
1325 if (!__tid_exists(pid,child)) {
1326 verror(
"thread %d in pid %d does not exist!\n",child,pid);
1353 (gpointer)(uintptr_t)child,NULL,&value)) {
1354 racy_status = (int)(uintptr_t)value;
1358 if (WIFSTOPPED(racy_status) && WSTOPSIG(racy_status) == SIGSTOP) {
1363 vwarn(
"new racy thread %d had status %d (but not SIGSTOP);"
1364 " assuming it is stopped though!\n",child,racy_status);
1379 vwarn(
"ptrace setoptions failed: %s\n",strerror(errno));
1403 rc = waitpid(child,&pstatus,WNOHANG | __WALL);
1406 "waitpid returned nothing for new non-racy tid %d!\n",child);
1409 verror(
"waitpid(%d): %s\n",child,strerror(errno));
1413 "waited for new non-racy tid %d successfully\n",child);
1435 linux_userproc_evloop_add_tid(target,child);
1441 static int __handle_internal_detaching(
struct target *target,
1447 #if __WORDSIZE == 64
1454 int pid = lstate->
pid;
1461 if (!WIFSTOPPED(pstatus)) {
1463 "pid %d thread %"PRIiTID" not stopped; ignoring\n",pid,tid);
1471 if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_CLONE << 8)) {
1472 ptrace(PTRACE_GETEVENTMSG,tid,NULL,&newstatus);
1473 newtid = (
tid_t)newstatus;
1475 "target %d thread %d cloned new thread %d; NOT attaching!\n",
1480 if (!linux_userproc_load_thread(target,tid,0)) {
1485 tstate->last_status = tstate->last_signo = WSTOPSIG(pstatus);
1486 if (tstate->last_status == (SIGTRAP | 0x80)) {
1488 "thread %"PRIiTID" stopped with syscall trap signo %d, ignoring\n",
1489 tid,tstate->last_status);
1490 tstate->last_signo = -1;
1492 else if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_EXIT << 8)) {
1494 pid,tstate->last_status);
1495 tstate->last_signo = -1;
1497 else if (tstate->last_status == SIGTRAP) {
1500 "thread %"PRIiTID" stopped with trap %d, minimal handling\n",
1501 tid,tstate->last_status);
1502 tstate->last_signo = -1;
1511 cdr = ptrace(PTRACE_PEEKUSER,tid,
1512 offsetof(
struct user,u_debugreg[6]),NULL);
1514 vwarn(
"could not read current val of status debug reg;"
1515 " don't know which handler to call; fatal!\n");
1521 "ignoring single step event pid %d thread %"PRIiTID"\n",
1541 ipval = ptrace(PTRACE_PEEKUSER,tid,
1542 offsetof(
struct user,u_debugreg[dreg]),NULL );
1544 verror(
"could not read current val of debug reg %d after up status!\n",dreg);
1549 "found hw break (status) in dreg %d on 0x%"PRIxADDR"\n",
1553 ipval = linux_userproc_read_reg(target,tid,target->
ipregno);
1555 verror(
"could not read EIP while finding probepoint: %s\n",
1571 "found hw break (eip) in dreg %d on 0x%"PRIxADDR"\n",
1583 " in debug reg %d, BUT no probepoint!\n",
1589 "hw bp %d pid %d thread %"PRIiTID", not resetting EIP\n",
1597 "sw bp pid %d thread %"PRIiTID", resetting EIP\n",
1604 verror(
"could not reset EIP; thread will crash\n");
1610 vwarn(
"could not find bp and not in sstep; letting thread"
1611 " pid %d thread %"PRIiTID" detach without handling"
1618 else if (WIFCONTINUED(pstatus)) {
1620 "waitpid CONT event pid %d thread %"PRIiTID" (status 0x%x); ignoring\n",
1622 tstate->last_signo = -1;
1623 tstate->last_status = -1;
1625 else if (WIFEXITED(pstatus)) {
1631 "waitpid EXITED event pid %d thread %"PRIiTID" (status 0x%x); ignoring\n",
1633 tstate->last_signo = -1;
1634 tstate->last_status = -1;
1638 else if (WIFSIGNALED(pstatus)) {
1640 "waitpid SIGNALED event pid %d thread %"PRIiTID" (status 0x%x); ignoring\n",
1642 tstate->last_signo = -1;
1643 tstate->last_status = -1;
1649 "unknown waitpid event pid %d thread %"PRIiTID" (status 0x%x)\n",
1670 retval = waitpid(tid,&status,WNOHANG | __WALL);
1673 "pid %d thread %"PRIiTID" running; not handling\n",pid,tid);
1676 else if (retval < 0) {
1677 verror(
"waitpid pid %d thread %"PRIiTID": %s\n",pid,tid,strerror(errno));
1690 return __handle_internal_detaching(target,tthread,status);
1694 int detaching_all,
int stay_paused) {
1711 verror(
"cannot detach from thread until process is attached to!\n");
1723 linux_userproc_evloop_del_tid(target,tid);
1728 snprintf(buf,256,
"/proc/%d/task/%d",pid,tid);
1729 if (stat(buf,&sbuf) == 0) {
1747 ptrace(PTRACE_DETACH,tid,NULL,(
void *)(uintptr_t)(stay_paused ? SIGSTOP : 0));
1753 if (!detaching_all && !stay_paused)
1767 static int linux_userproc_snprintf(
struct target *target,
1768 char *buf,
int bufsiz) {
1773 return snprintf(buf,bufsiz,
"ptrace(%d,%s)",lspec->
pid,lspec->
program);
1775 return snprintf(buf,bufsiz,
"ptrace(%d)",lspec->
pid);
1778 static tid_t linux_userproc_gettid(
struct target *target) {
1789 tthread = linux_userproc_load_current_thread(target,0);
1791 verror(
"could not load current thread to get TID!\n");
1795 return tthread->
tid;
1798 static void linux_userproc_free_thread_state(
struct target *target,
void *
state) {
1802 static int __linux_userproc_load_thread_status(
struct target_thread *tthread,
1803 tid_t tid,
int force) {
1817 "pid %d thread %"PRIiTID" already valid\n",pid,tid);
1822 pid,tid,tthread->
tid);
1825 snprintf(buf,64,
"/proc/%d/task/%d/stat",pid,tthread->
tid);
1827 statf = fopen(buf,
"r");
1830 "fopen(%s): %s; UNKNOWN!\n",buf,strerror(errno));
1835 if ((rc = fscanf(statf,
"%d (%s %c",&rtid,buf,&pstate))) {
1836 if (pstate ==
'R' || pstate ==
'r')
1838 else if (pstate ==
'W' || pstate ==
'w')
1840 else if (pstate ==
'S' || pstate ==
's')
1842 else if (pstate ==
'D' || pstate ==
'd')
1844 else if (pstate ==
'Z' || pstate ==
'z')
1846 else if (pstate ==
'T' || pstate ==
't')
1848 else if (pstate ==
'X' || pstate ==
'x')
1851 verror(
"fscanf returned %d; read tid %d (%s) %c; UNKNOWN!\n",
1852 rc,rtid,buf,pstate);
1857 else if (rc < 0 && errno == EINTR) {
1872 static struct target_thread *__linux_userproc_load_thread(
struct target *target,
1873 tid_t tid,
int force,
1884 verror(
"thread %"PRIiTID" does not exist; forgot to attach?\n",tid);
1897 pid,tid,tthread->
tid);
1900 __linux_userproc_load_thread_status(tthread,tid,force);
1907 if (ptrace(PTRACE_GETREGS,tthread->
tid,NULL,&(tstate->
regs)) == -1) {
1908 verror(
"ptrace(GETREGS): %s\n",strerror(errno));
1916 memset(&tstate->
regs,0,
sizeof(tstate->
regs));
1925 static struct target_thread *linux_userproc_load_thread(
struct target *target,
1926 tid_t tid,
int force) {
1927 return __linux_userproc_load_thread(target,tid,force,0);
1930 static struct target_thread *linux_userproc_load_current_thread(
struct target *target,
1935 return linux_userproc_load_thread(target,lstate->
current_tid,force);
1938 static int linux_userproc_load_all_threads(
struct target *target,
int force) {
1940 GHashTableIter iter;
1944 g_hash_table_iter_init(&iter,target->
threads);
1945 while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
1949 if (!linux_userproc_load_thread(target,tthread->tid,force)) {
1958 static int linux_userproc_flush_thread(
struct target *target,
tid_t tid) {
1968 verror(
"cannot flush unknown thread %"PRIiTID"; forgot to load?\n",tid);
1977 verror(
"target %s not writeable!\n",target->
name);
1983 if (ptrace(PTRACE_SETREGS,tthread->
tid,NULL,&(tstate->
regs)) == -1) {
1984 verror(
"ptrace(SETREGS): %s\n",strerror(errno));
1992 static int linux_userproc_flush_current_thread(
struct target *target) {
1993 if (!target->
current_thread && linux_userproc_load_current_thread(target,0))
1999 static int linux_userproc_flush_all_threads(
struct target *target) {
2001 GHashTableIter iter;
2005 g_hash_table_iter_init(&iter,target->
threads);
2006 while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
2007 if (key == (gpointer)TID_GLOBAL)
2010 rc = linux_userproc_flush_thread(target,tthread->tid);
2020 static int linux_userproc_thread_snprintf(
struct target *target,
2022 char *buf,
int bufsiz,
2023 int detail,
char *sep,
char *kvsep) {
2025 struct user_regs_struct *r;
2034 #
if __WORDSIZE == 64
2035 #define __V_LINUX_PTRACE_REG_LARGE_SIZE
2036 #ifdef __V_LINUX_PTRACE_REG_LARGE_SIZE
2049 rc += snprintf((rc >= bufsiz) ? NULL : buf + rc,
2050 (rc >= bufsiz) ? 0 :bufsiz - rc,
2051 "%s" "ip%s%"RF "%s" "bp%s%"RF "%s" "sp%s%"RF "%s"
2052 "flags%s%"RF "%s" "ax%s%"RF "%s" "bx%s%"RF "%s"
2053 "cx%s%"RF "%s" "dx%s%"RF "%s" "di%s%"RF "%s"
2054 "si%s%"RF "%s" "cs%s%"RF "%s" "ss%s%"RF "%s"
2055 "ds%s%"RF "%s" "es%s%"RF "%s"
2056 "fs%s%"RF "%s" "gs%s%"RF,
2057 #
if __WORDSIZE == 64
2058 sep,kvsep,r->rip,sep,kvsep,r->rbp,sep,kvsep,r->rsp,sep,
2059 kvsep,r->eflags,sep,kvsep,r->rax,sep,kvsep,r->rbx,sep,
2060 kvsep,r->rcx,sep,kvsep,r->rdx,sep,kvsep,r->rdi,sep,
2061 kvsep,r->rsi,sep,kvsep,r->cs,sep,kvsep,r->ss,sep,
2062 kvsep,r->ds,sep,kvsep,r->es,sep,
2063 kvsep,r->fs,sep,kvsep,r->gs
2065 sep,kvsep,r->eip,sep,kvsep,r->ebp,sep,kvsep,r->esp,sep,
2066 kvsep,r->eflags,sep,kvsep,r->eax,sep,kvsep,r->ebx,sep,
2067 kvsep,r->ecx,sep,kvsep,r->edx,sep,kvsep,r->edi,sep,
2068 kvsep,r->esi,sep,kvsep,r->cs,sep,kvsep,r->ss,sep,
2069 kvsep,r->ds,sep,kvsep,r->es,sep,
2070 kvsep,r->fs,sep,kvsep,r->gs
2074 rc += snprintf((rc >= bufsiz) ? NULL : buf + rc,
2075 (rc >= bufsiz) ? 0 :bufsiz - rc,
2076 "%s" "dr0%s%"DRF "%s" "dr1%s%"DRF
2077 "%s" "dr2%s%"DRF "%s" "dr3%s%"DRF
2078 "%s" "dr6%s%"DRF "%s" "dr7%s%"DRF,
2079 sep,kvsep,tstate->
dr[0],sep,kvsep,tstate->
dr[1],
2080 sep,kvsep,tstate->
dr[1],sep,kvsep,tstate->
dr[2],
2081 sep,kvsep,tstate->
dr[6],sep,kvsep,tstate->
dr[7]);
2086 static int linux_userproc_init(
struct target *target) {
2132 linux_userproc_evloop_add_tid(target,tthread->
tid);
2137 static int linux_userproc_postloadinit(
struct target *target) {
2141 static int linux_userproc_attach_internal(
struct target *target) {
2146 struct dirent *dirent;
2169 if (ptrace(PTRACE_ATTACH,pid,NULL,NULL) < 0) {
2170 verror(
"ptrace attach pid %d failed: %s\n",pid,strerror(errno));
2175 snprintf(buf,256,
"/proc/%d/mem",pid);
2176 if ((lstate->
memfd = open(buf,O_LARGEFILE,O_RDWR)) < 0) {
2177 verror(
"open %s failed, detaching: %s!\n",buf,strerror(errno));
2178 ptrace(PTRACE_DETACH,pid,NULL,NULL);
2192 if (waitpid(pid,&pstatus,0) < 0) {
2193 if (errno == ECHILD || errno == EINVAL)
2204 if (ptrace(PTRACE_SETOPTIONS,pid,NULL,lstate->
ptrace_opts) < 0) {
2205 vwarn(
"ptrace setoptions failed: %s\n",strerror(errno));
2212 snprintf(buf,256,
"/proc/%d/task",pid);
2213 if (!(dirp = opendir(buf))) {
2214 verror(
"could not opendir %s to attach to threads: %s!\n",
2215 buf,strerror(errno));
2220 while ((dirent = readdir(dirp))) {
2221 if (dirent->d_name[0] ==
'.')
2224 tid = (
tid_t)strtol(dirent->d_name,&endp,10);
2225 if (endp == dirent->d_name || errno == ERANGE || errno == EINVAL) {
2226 verror(
"weird error parsing thread id out of '%s': %s; skipping!\n",
2227 dirent->d_name,strerror(errno));
2235 if (ptrace(PTRACE_ATTACH,tid,NULL,NULL) < 0) {
2236 verror(
"ptrace attach tid %d failed: %s\n",tid,strerror(errno));
2244 if (waitpid(tid,&pstatus,__WALL) < 0) {
2245 if (errno == ECHILD || errno == EINVAL) {
2246 verror(
"waitpid tid %d failed: %s\n",tid,strerror(errno));
2255 if (ptrace(PTRACE_SETOPTIONS,tid,NULL,lstate->
ptrace_opts) < 0) {
2256 vwarn(
"ptrace setoptions failed, continuing: %s\n",strerror(errno));
2277 linux_userproc_evloop_add_tid(target,tid);
2285 static int linux_userproc_detach(
struct target *target,
int stay_paused) {
2288 GHashTableIter iter;
2309 threadlist = array_list_create(g_hash_table_size(target->
threads));
2310 g_hash_table_iter_init(&iter,target->
threads);
2311 while (g_hash_table_iter_next(&iter,NULL,(gpointer)&tthread)) {
2315 array_list_append(threadlist,tthread);
2317 for (i = 0; i < array_list_len(threadlist); ++
i) {
2318 tthread = (
struct target_thread *)array_list_item(threadlist,i);
2322 verror(
"could not detach thread %"PRIiTID"\n",tthread->tid);
2326 array_list_free(threadlist);
2335 verror(
"could not detach global thread %"PRIiTID"\n",tthread->tid);
2358 kill(lstate->
pid,SIGCONT);
2360 if (lstate->
memfd > 0)
2361 close(lstate->
memfd);
2368 static int linux_userproc_fini(
struct target *target) {
2392 static int linux_userproc_kill(
struct target *target,
int sig) {
2403 if (kill(lstate->
pid,sig))
2409 static int linux_userproc_loadspaces(
struct target *target) {
2418 static int linux_userproc_loadregions(
struct target *target,
2420 char buf[PATH_MAX*2];
2421 char main_exe[PATH_MAX];
2436 snprintf(buf,PATH_MAX*2,
"/proc/%d/exe",lstate->
pid);
2437 if ((rc = readlink(buf,main_exe,PATH_MAX - 1)) < 1)
2439 main_exe[rc] =
'\0';
2441 snprintf(buf,PATH_MAX,
"/proc/%d/maps",lstate->
pid);
2448 if (!(ret = fgets(buf,PATH_MAX*2,f)) && !errno)
2450 else if (!ret && errno) {
2451 verror(
"fgets: %s",strerror(errno));
2457 rc = sscanf(buf,
"%Lx-%Lx %c%c%c%c %Lx %*x:%*x %*d %s",&start,&end,
2458 &p[0],&p[1],&p[2],&p[3],&offset,buf);
2459 if (rc == 8 || rc == 7) {
2462 if (strncmp(main_exe,buf,PATH_MAX) == 0)
2464 else if (strcmp(buf,
"[heap]") == 0)
2466 else if (strcmp(buf,
"[stack]") == 0)
2468 else if (strcmp(buf,
"[vdso]") == 0)
2470 else if (strcmp(buf,
"[vsyscall]") == 0)
2516 else if (rc > 0 && !errno) {
2517 vwarn(
"weird content in /proc/pid/maps (%d)!\n",rc);
2519 else if (rc > 0 && errno) {
2520 vwarn(
"weird content in /proc/pid/maps (%d): %s!\n",rc,strerror(errno));
2533 static int linux_userproc_updateregions(
struct target *target,
2535 char buf[PATH_MAX*2];
2536 char main_exe[PATH_MAX];
2541 GList *t1,*t2,*t3,*t4;
2548 uint32_t prot_flags;
2556 snprintf(buf,PATH_MAX*2,
"/proc/%d/exe",lstate->
pid);
2557 if ((rc = readlink(buf,main_exe,PATH_MAX - 1)) < 1)
2559 main_exe[rc] =
'\0';
2561 snprintf(buf,PATH_MAX,
"/proc/%d/maps",lstate->
pid);
2575 if (!(ret = fgets(buf,PATH_MAX*2,f)) && !errno)
2577 else if (!ret && errno) {
2578 verror(
"fgets: %s",strerror(errno));
2584 rc = sscanf(buf,
"%Lx-%Lx %c%c%c%c %Lx %*x:%*x %*d %s",&start,&end,
2585 &p[0],&p[1],&p[2],&p[3],&offset,buf);
2586 if (rc == 8 || rc == 7) {
2589 if (strncmp(main_exe,buf,PATH_MAX) == 0)
2591 else if (strcmp(buf,
"[heap]") == 0)
2593 else if (strcmp(buf,
"[stack]") == 0)
2595 else if (strcmp(buf,
"[vdso]") == 0)
2597 else if (strcmp(buf,
"[vsyscall]") == 0)
2637 if (range->
end == end
2638 && range->
offset == offset
2648 if (start < region->base_load_addr)
2656 else if (rc > 0 && !errno) {
2657 vwarn(
"weird content in /proc/pid/maps (%d)!\n",rc);
2659 else if (rc > 0 && errno) {
2660 vwarn(
"weird content in /proc/pid/maps (%d): %s!\n",rc,strerror(errno));
2691 else if (
OBJMOD(range)) {
2716 if (!exists || !region->
ranges) {
2731 else if (
OBJNEW(region)) {
2739 if (linux_userproc_loaddebugfiles(target,space,region)) {
2740 vwarn(
"could not load debugfile for new region (%s:%s:%s)\n",
2758 static int linux_userproc_loaddebugfiles(
struct target *target,
2777 if (!region->
name || strlen(region->
name) == 0)
2820 static struct target *
2821 linux_userproc_instantiate_overlay(
struct target *target,
2825 struct target *overlay;
2841 linux_userproc_lookup_overlay_thread_by_id(
struct target *target,
int id) {
2847 retval = linux_userproc_load_thread(target,
id,0);
2855 "found overlay thread %d\n",
id);
2861 linux_userproc_lookup_overlay_thread_by_name(
struct target *target,
char *
name) {
2865 GHashTableIter iter;
2867 if ((rc = linux_userproc_load_all_threads(target,0)))
2868 vwarn(
"could not load %d threads; continuing anyway!\n",-rc);
2870 g_hash_table_iter_init(&iter,target->
threads);
2871 while (g_hash_table_iter_next(&iter,NULL,(gpointer)&tthread)) {
2874 else if (tthread->name && strcmp(tthread->name,name) == 0) {
2882 "found overlay thread %"PRIiTID"\n",retval->
tid);
2899 int pid = lstate->
pid;
2904 snprintf(buf,256,
"/proc/%d/stat",pid);
2905 statf = fopen(buf,
"r");
2907 verror(
"statf(%s): %s\n",buf,strerror(errno));
2911 if ((rc = fscanf(statf,
"%d (%s %c",&pid,buf,&pstate))) {
2912 if (pstate ==
'R' || pstate ==
'r' || pstate ==
'W' || pstate ==
'w')
2914 else if (pstate ==
'S' || pstate ==
's' || pstate ==
'D' || pstate ==
'd')
2916 else if (pstate ==
'Z' || pstate ==
'z')
2918 else if (pstate ==
'T' || pstate ==
't')
2921 vwarn(
"fscanf returned %d; read %d (%s) %c; returning TSTATUS_UNKNOWN!\n",
2926 else if (rc < 0 && errno == EINTR) {
2937 static int linux_userproc_pause(
struct target *target,
int nowait) {
2938 GHashTableIter iter;
2959 g_hash_table_iter_init(&iter,target->
threads);
2960 while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
2961 if (key == (gpointer)TID_GLOBAL)
2964 if (!linux_userproc_load_thread(target,tthread->tid,0)) {
2965 verror(
"could not load thread %"PRIiTID"; pausing anyway!\n",
2978 else if (waitid(P_PID,tthread->tid,&sinfo,WNOHANG | WNOWAIT) == 0
2979 && sinfo.si_pid == tthread->tid) {
2981 "tid %d has pending siginfo to waitpid on; not pausing here\n",
2998 if (kill(tthread->tid,SIGSTOP) < 0) {
2999 verror(
"kill(%d,SIGSTOP): %s\n",tthread->tid,strerror(errno));
3013 if (waitpid(tthread->tid,&pstatus,__WALL) < 0) {
3014 if (errno == ECHILD || errno == EINVAL) {
3015 verror(
"waitpid(%"PRIiTID"): %s\n",tthread->tid,strerror(errno));
3033 static int linux_userproc_pause_thread(
struct target *target,
tid_t tid,
3043 if (tid == TID_GLOBAL)
3065 if (!linux_userproc_load_thread(target,tthread->
tid,0)) {
3066 verror(
"could not load thread %"PRIiTID"; pausing anyway!\n",
3074 if (kill(tthread->
tid,SIGSTOP) < 0) {
3075 verror(
"kill(%d,SIGSTOP): %s\n",tthread->
tid,strerror(errno));
3087 if (waitpid(tthread->
tid,&pstatus,__WALL) < 0) {
3088 if (errno == ECHILD || errno == EINVAL) {
3102 static int linux_userproc_resume(
struct target *target) {
3104 GHashTableIter iter;
3133 g_hash_table_iter_init(&iter,target->
threads);
3134 while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
3135 if (key == (gpointer)TID_GLOBAL)
3162 if (ptrace(PTRACE_SETOPTIONS,tthread->tid,NULL,
3164 vwarn(
"ptrace setoptions on tid %"PRIiTID" failed: %s\n",
3165 tthread->tid,strerror(errno));
3173 verror(
"ptrace signo %d restart of tid %"PRIiTID" failed: %s\n",
3174 tstate->
last_signo,tthread->tid,strerror(errno));
3180 if (ptrace(lstate->
ptrace_type,tthread->tid,NULL,NULL) < 0) {
3181 verror(
"ptrace restart of tid %"PRIiTID" (status %d) failed: %s\n",
3182 tthread->tid,tthread->status,strerror(errno));
3203 g_hash_table_iter_init(&iter,target->
threads);
3204 while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
3205 if (key == (gpointer)TID_GLOBAL)
3231 static target_status_t linux_userproc_handle_exception(
struct target *target,
3233 int *again,
void *priv) {
3238 #if __WORDSIZE == 64
3262 if (__tid_exists(pid,tid)) {
3264 "thread %d does not YET exist; might be new!\n",tid);
3266 (gpointer)(uintptr_t)tid,
3267 (gpointer)(uintptr_t)pstatus);
3276 if (WIFSTOPPED(pstatus)) {
3299 if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_CLONE << 8)) {
3300 ptrace(PTRACE_GETEVENTMSG,tid,NULL,&newstatus);
3301 newtid = (
tid_t)newstatus;
3303 "target %d thread %d cloned new thread %d; attaching now.\n",
3321 linux_userproc_updateregions(target,space);
3325 if (!__linux_userproc_load_thread(target,tid,0,1)) {
3336 "thread %"PRIiTID" stopped with syscall trap signo %d\n",
3340 else if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_EXIT << 8)) {
3342 "target %d tid %d exiting (%d)! will detach at next resume.\n",
3384 cdr = ptrace(PTRACE_PEEKUSER,tid,
3385 offsetof(
struct user,u_debugreg[6]),NULL);
3387 vwarn(
"could not read current val of status debug reg;"
3388 " don't know which handler to call; fatal!\n");
3411 ipval = ptrace(PTRACE_PEEKUSER,tid,
3412 offsetof(
struct user,u_debugreg[dreg]),NULL );
3414 verror(
"could not read current val of debug reg %d after up status!\n",dreg);
3419 "found hw break (status) in dreg %d on 0x%"PRIxADDR"\n",
3423 ipval = linux_userproc_read_reg(target,tid,target->
ipregno);
3425 verror(
"could not read EIP while finding probepoint: %s\n",
3441 "found hw break (eip) in dreg %d on 0x%"PRIxADDR"\n",
3445 "checking for SS or sw break on 0x%"PRIxADDR"\n",
3456 if (ptrace(PTRACE_POKEUSER,tid,
3457 offsetof(
struct user,u_debugreg[6]),0)) {
3458 verror(
"could not clear status debug reg, continuing"
3459 " anyway: %s!\n",strerror(errno));
3472 " in debug reg %d, BUT no probepoint!\n",
3499 else if (cdr & 0x4000) {
3523 vwarn(
"could not find hardware bp and not sstep'ing;"
3524 " letting user handle fault at 0x%"PRIxADDR"!\n",
3531 "thread %"PRIiTID" stopped with (our) signo %d\n",
3550 if (ptrace(lstate->
ptrace_type,tid,NULL,NULL) < 0) {
3552 tid,strerror(errno));
3560 "thread %"PRIiTID" stopped with (ext) signo %d\n",
3566 else if (WIFCONTINUED(pstatus)) {
3575 else if (WIFSIGNALED(pstatus) || WIFEXITED(pstatus)) {
3587 tthread,(
void *)(uintptr_t)pstatus);
3594 if (g_hash_table_size(target->
threads) == 2) {
3600 linux_userproc_detach(target,0);
3610 vwarn(
"unexpected child process status event: %08x; bailing!\n",
3638 struct target *target = (
struct target *)state;
3644 verror(
"could not find thread tid for readfd %d!\n",readfd);
3650 verror(
"waitpipe_drain: %s\n",strerror(errno));
3655 verror(
"cound not find thread %d!\n",tid);
3664 "synchronous ctl sig sent to tid %d; not calling waitpid; ignoring"
3665 " (probable multithread bug!)\n",
3672 retval = waitpid(tid,&status,WNOHANG | __WALL);
3674 if (errno == ECHILD || errno == EINVAL) {
3681 else if (retval == 0) {
3683 "tid %"PRIiTID" running; waitpid has nothing to report; ignoring!\n",
3689 "tid %"PRIiTID" running; handling evloop sig\n",tid);
3697 retval = linux_userproc_handle_exception(target,0,&again,&exst);
3700 verror(
"thread-specific status %d tid %d; bad!\n",retval,tid);
3705 verror(
"unexpected error on thread %d; bad!\n",tid);
3710 verror(
"unexpected unknown on thread %d; bad!\n",tid);
3730 "tid %"PRIiTID" done; removing its fd (%d) from evloop\n",
3755 "tid %"PRIiTID" signaled with %d; resuming; signal will hit tid\n",
3762 vwarn(
"unexpected pause on thread %d; bad!\n",tid);
3767 verror(
"unexpected error on thread %d; bad!\n",tid);
3778 static int linux_userproc_evloop_add_tid(
struct target *target,
int tid) {
3782 verror(
"no evloop attached!\n");
3788 "not adding waitpipe readfd %d for tid %d\n",readfd,tid);
3793 verror(
"could not add tid %d to waitpipe!\n",tid);
3801 "added waitpipe/evloop readfd %d for tid %d\n",readfd,tid);
3810 static int linux_userproc_evloop_del_tid(
struct target *target,
int tid) {
3814 verror(
"no evloop attached!\n");
3823 "removed waitpipe/evloop readfd %d for tid %d\n",
3828 "did not find valid readfd (%d) to remove tid %d from evloop!\n",
3832 if (target->
infd > -1)
3834 if (target->
outfd > -1)
3836 if (target->
errfd > -1)
3850 if (target->
infd > -1)
3853 if (target->
outfd > -1)
3856 if (target->
errfd > -1)
3865 linux_userproc_evloop_add_tid(target,tid);
3868 array_list_free(tids);
3886 linux_userproc_evloop_del_tid(target,tid);
3889 array_list_free(tids);
3903 struct timespec req, rem;
3904 unsigned int usec_thresh = 100;
3906 uint64_t total_ns = 0;
3910 total_us = tv->tv_sec * 1000000 + tv->tv_usec;
3911 total_ns = total_us * 1000;
3913 if (total_us < usec_thresh) {
3915 req.tv_nsec = total_ns;
3919 req.tv_nsec = usec_thresh * 1000;
3925 tid = waitpid(-lstate->
pid,&status,WNOHANG | __WALL);
3930 if (1 || errno == ECHILD || errno == EINVAL) {
3936 else if (tid == 0) {
3947 nanosleep(&req,&rem);
3949 total_ns -= req.tv_nsec - rem.tv_nsec;
3951 total_ns -= req.tv_nsec;
3954 if (total_ns > usec_thresh * 1000)
3955 req.tv_nsec = usec_thresh * 1000;
3957 req.tv_nsec = total_ns;
3981 retval = linux_userproc_handle_exception(target,0,NULL,&exst);
3984 vwarn(
"unhandled thread-specific status %d!\n",retval);
3996 static target_status_t linux_userproc_monitor(
struct target *target) {
4012 tid = waitpid(-1,&pstatus,__WALL);
4014 if (errno == ECHILD || errno == EINVAL)
4022 retval = linux_userproc_handle_exception(target,0,&again,&exst);
4031 vwarn(
"unhandled thread-specific status %d!\n",retval);
4037 static unsigned char *linux_userproc_read(
struct target *target,
4039 unsigned long length,
4040 unsigned char *buf) {
4052 unsigned long linux_userproc_write(
struct target *target,
4054 unsigned long length,
4055 unsigned char *buf) {
4058 #if __WORDSIZE == 64
4068 for (j = 0; j < length && j < 16; ++j)
4073 verror(
"target %s not writeable!\n",target->
name);
4100 if (length % (__WORDSIZE / 8)) {
4102 word = ptrace(PTRACE_PEEKTEXT,lstate->
current_tid,
4103 (addr + length) - (length % (__WORDSIZE / 8)),
4106 verror(
"ptrace(PEEKTEXT) last word: %s\n",strerror(errno));
4111 for (j = 0; j < __WORDSIZE / 8; ++j)
4115 memcpy(&word,(buf + length) - (length % (__WORDSIZE / 8)),
4116 length % (__WORDSIZE / 8));
4119 for (j = 0; j < __WORDSIZE / 8; ++j)
4124 if (length / (__WORDSIZE / 8)) {
4125 for (i = 0; i < length; i += (__WORDSIZE / 8)) {
4128 #
if __WORDSIZE == 64
4129 addr + i,*(uint64_t *)(buf + i)) == -1) {
4131 addr +
i,*(uint32_t *)(buf + i)) == -1) {
4133 verror(
"ptrace(POKETEXT): %s\n",strerror(errno));
4139 if (length % (__WORDSIZE / 8)) {
4142 (i) ? addr + i - (__WORDSIZE / 8) : addr,
4144 verror(
"ptrace(POKETEXT) last word: %s\n",strerror(errno));
4208 #if __WORDSIZE == 64
4210 10, 12, 11, 5, 13, 14, 4, 19,
4211 9, 8, 7, 6, 3, 2, 1, 0,
4213 -1, -1, -1, -1, -1, -1, -1, -1,
4214 -1, -1, -1, -1, -1, -1, -1, -1,
4215 -1, -1, -1, -1, -1, -1, -1, -1,
4216 -1, -1, -1, -1, -1, -1, -1, -1,
4217 18, 24, 17, 20, 23, 25, 26,
4223 -1, -1, -1, -1,-1, -1, -1, -1,-1, -1,
4224 -1, -1,-1, -1, -1, -1,-1, -1, -1, -1,
4229 6, 1, 2, 0, 15, 5, 3, 4,
4232 -1, -1, -1, -1, -1, -1, -1, -1,
4234 -1, -1, -1, -1, -1, -1, -1, -1,
4235 -1, -1, -1, -1, -1, -1, -1, -1,
4238 8, 13, 16, 7, 9, 10,
4239 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4246 REGVAL linux_userproc_read_reg(
struct target *target,
tid_t tid,
REG reg) {
4251 tthread = linux_userproc_load_thread(target,tid,0);
4253 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4262 if (reg >= arch_regcount(target->
arch)) {
4263 verror(
"regnum %d does not have a target mapping!\n",reg);
4269 ptrace_idx = dreg_to_ptrace_idx64[reg];
4270 return (
REGVAL)(((
unsigned long *)&(tstate->
regs))[ptrace_idx]);
4273 ptrace_idx = dreg_to_ptrace_idx32[reg];
4274 return (
REGVAL)(((
long int *)&(tstate->
regs))[ptrace_idx]);
4278 int linux_userproc_write_reg(
struct target *target,
tid_t tid,
REG reg,
4285 verror(
"target %s not writeable!\n",target->
name);
4290 tthread = linux_userproc_load_thread(target,tid,0);
4292 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4301 if (reg >= arch_regcount(target->
arch)) {
4302 verror(
"regnum %d does not have a target mapping!\n",reg);
4307 ptrace_idx = dreg_to_ptrace_idx64[reg];
4308 ((
unsigned long *)&(tstate->
regs))[ptrace_idx] = (
unsigned long)value;
4311 ptrace_idx = dreg_to_ptrace_idx32[reg];
4312 ((
long int*)&(tstate->
regs))[ptrace_idx] = (
long int)value;
4324 static REG linux_userproc_get_unused_debug_reg(
struct target *target,
tid_t tid) {
4329 tthread = linux_userproc_load_thread(target,tid,0);
4331 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4337 if (!tstate->
dr[0]) { retval = 0; }
4338 else if (!tstate->
dr[1]) { retval = 1; }
4339 else if (!tstate->
dr[2]) { retval = 2; }
4340 else if (!tstate->
dr[3]) { retval = 3; }
4347 #define VWORDBYTESIZE __WORDSIZE / 8
4349 #if __WORDSIZE == 64
4350 static int read_ptrace_debug_reg(
int pid,
unsigned long *array) {
4352 static int read_ptrace_debug_reg(
int pid,
int *array) {
4357 for ( ; i < 8; ++
i) {
4358 #if __WORDSIZE == 64
4360 (
unsigned long)ptrace(PTRACE_PEEKUSER,pid,
4361 offsetof(
struct user,u_debugreg[i]),NULL);
4364 (int)ptrace(PTRACE_PEEKUSER,pid,
4365 offsetof(
struct user,u_debugreg[i]),NULL);
4368 verror(
"ptrace(PEEKUSER): %s\n",strerror(errno));
4400 static int linux_userproc_set_hw_breakpoint(
struct target *target,
tid_t tid,
4402 #if __WORDSIZE == 64
4410 tthread = linux_userproc_load_thread(target,tid,0);
4412 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4418 if (reg < 0 || reg > 3) {
4424 ptrace(PTRACE_PEEKUSER,tid,
4425 offsetof(
struct user,u_debugreg[reg]),(
void *)&cdr);
4427 vwarn(
"could not read current val of debug reg %"PRIiREG": %s!\n",
4428 reg,strerror(errno));
4430 else if (cdr != 0) {
4438 tstate->
dr[reg] = addr;
4444 tstate->
dr[7] |= (1 << (reg * 2));
4445 tstate->
dr[7] &= ~(1 << (reg * 2 + 1));
4447 tstate->
dr[7] &= ~(3 << (16 + (reg * 4)));
4460 ptrace(PTRACE_POKEUSER,tid,
4461 offsetof(
struct user,u_debugreg[reg]),(
void *)(tstate->
dr[reg]));
4463 verror(
"could not update debug reg %"PRIiREG", aborting: %s!\n",
4464 reg,strerror(errno));
4468 ptrace(PTRACE_POKEUSER,tid,
4469 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4471 verror(
"could not update status debug reg, aborting: %s!\n",
4475 ptrace(PTRACE_POKEUSER,tid,
4476 offsetof(
struct user,u_debugreg[7]),(
void *)(tstate->
dr[7]));
4478 verror(
"could not update control debug reg, aborting: %s!\n",
4486 tstate->
dr[reg] = 0;
4491 static int linux_userproc_set_hw_watchpoint(
struct target *target,
tid_t tid,
4495 #if __WORDSIZE == 64
4503 tthread = linux_userproc_load_thread(target,tid,0);
4505 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4511 if (reg < 0 || reg > 3) {
4517 ptrace(PTRACE_PEEKUSER,tid,
4518 offsetof(
struct user,u_debugreg[reg]),(
void *)&cdr);
4520 vwarn(
"could not read current val of debug reg %"PRIiREG"!\n",reg);
4522 else if (cdr != 0) {
4530 tstate->
dr[reg] = addr;
4536 tstate->
dr[7] |= (1 << (reg * 2));
4537 tstate->
dr[7] &= ~(1 << (reg * 2 + 1));
4539 tstate->
dr[7] &= ~(3 << (16 + (reg * 4)));
4540 tstate->
dr[7] |= (whence << (16 + (reg * 4)));
4542 tstate->
dr[7] &= ~(3 << (18 + (reg * 4)));
4543 tstate->
dr[7] |= (watchsize << (18 + (reg * 4)));
4546 tstate->
dr[7] |= (1 << 8);
4551 tstate->
dr[6],tstate->
dr[7],whence,watchsize);
4555 ptrace(PTRACE_POKEUSER,tid,
4556 offsetof(
struct user,u_debugreg[reg]),(
void *)(tstate->
dr[reg]));
4558 verror(
"could not update debug reg %"PRIiREG" (%p), aborting: %s!\n",reg,
4559 (
void *)(tstate->
dr[reg]),strerror(errno));
4563 ptrace(PTRACE_POKEUSER,tid,
4564 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4566 verror(
"could not update status debug reg (%p), aborting: %s!\n",
4567 (
void *)(tstate->
dr[6]),strerror(errno));
4570 ptrace(PTRACE_POKEUSER,tid,
4571 offsetof(
struct user,u_debugreg[7]),(
void *)(tstate->
dr[7]));
4573 verror(
"could not update control debug reg (%p), aborting: %s!\n",
4574 (
void *)(tstate->
dr[7]),strerror(errno));
4581 tstate->
dr[reg] = 0;
4586 static int linux_userproc_unset_hw_breakpoint(
struct target *target,
tid_t tid,
4591 tthread = linux_userproc_load_thread(target,tid,0);
4593 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4599 if (reg < 0 || reg > 3) {
4605 tstate->
dr[reg] = 0;
4611 tstate->
dr[7] &= ~(3 << (reg * 2));
4615 ptrace(PTRACE_POKEUSER,tid,
4616 offsetof(
struct user,u_debugreg[reg]),(
void *)(tstate->
dr[reg]));
4618 verror(
"could not update debug reg %"PRIiREG", aborting: %s!\n",
4619 reg,strerror(errno));
4623 ptrace(PTRACE_POKEUSER,tid,
4624 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4626 verror(
"could not update status debug reg, aborting: %s!\n",
4630 ptrace(PTRACE_POKEUSER,tid,
4631 offsetof(
struct user,u_debugreg[7]),(
void *)(tstate->
dr[7]));
4633 verror(
"could not update control debug reg,aborting: %s!\n",
4644 static int linux_userproc_unset_hw_watchpoint(
struct target *target,
tid_t tid,
4647 return linux_userproc_unset_hw_breakpoint(target,tid,reg);
4651 ptrace(PTRACE_POKEUSER,tid,
4652 offsetof(
struct user,u_debugreg[7]),(
void *)0);
4654 verror(
"could not update control debug reg, aborting: %s!\n",
4665 tthread = linux_userproc_load_thread(target,tid,0);
4667 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4673 ptrace(PTRACE_POKEUSER,tid,
4674 offsetof(
struct user,u_debugreg[7]),(
void *)tstate->
dr[7]);
4676 verror(
"could not update control debug reg, aborting: %s!\n",
4688 tthread = linux_userproc_load_thread(target,tid,0);
4690 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4696 if (dreg < 0 || dreg > 3) {
4705 tstate->
dr[7] &= ~(3 << (dreg * 2));
4709 ptrace(PTRACE_POKEUSER,tid,
4710 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4712 verror(
"could not update status debug reg, aborting: %s!\n",
4716 ptrace(PTRACE_POKEUSER,tid,
4717 offsetof(
struct user,u_debugreg[7]),(
void *)(tstate->
dr[7]));
4719 verror(
"could not update control debug reg,aborting: %s!\n",
4735 tthread = linux_userproc_load_thread(target,tid,0);
4737 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4743 if (dreg < 0 || dreg > 3) {
4752 tstate->
dr[7] |= (1 << (dreg * 2));
4753 tstate->
dr[7] &= ~(1 << (dreg * 2 + 1));
4757 ptrace(PTRACE_POKEUSER,tid,
4758 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4760 verror(
"could not update status debug reg, aborting: %s!\n",
4764 ptrace(PTRACE_POKEUSER,tid,
4765 offsetof(
struct user,u_debugreg[7]),(
void *)(tstate->
dr[7]));
4767 verror(
"could not update control debug reg, aborting: %s!\n",
4784 struct target *overlay) {
4788 tthread = linux_userproc_load_thread(target,tid,0);
4790 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4801 ptrace(PTRACE_POKEUSER,tid,
4802 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4804 verror(
"could not update status debug reg, aborting: %s!\n",
4828 if (linux_userproc_flush_thread(target,tid) < 0) {
4829 verror(
"could not flush thread; not single stepping!\n");
4833 ptrace(PTRACE_SINGLESTEP,tid,NULL,NULL);
4835 verror(
"could not ptrace single step thread %"PRIiTID": %s\n",
4836 tid,strerror(errno));
4851 struct target *overlay) {
4855 tthread = linux_userproc_load_thread(target,tid,0);
4857 verror(
"thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4868 ptrace(PTRACE_POKEUSER,tid,
4869 offsetof(
struct user,u_debugreg[6]),(
void *)(tstate->
dr[6]));
4871 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)
#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
static uint64_t unsigned int i
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)
char * linux_userproc_argp_header
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
void * calloc(size_t nmemb, size_t size)
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)
struct argp linux_userproc_argp
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)
void * malloc(size_t size)
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 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)