blob: b3b41c9303773f88e7df2a868202a8bfd3d6b1f7 [file] [log] [blame]
Petr Machata750ca8c2011-10-06 14:29:34 +02001#define _GNU_SOURCE /* For getline. */
Juan Cespedesd44c6b81998-09-25 14:48:42 +02002#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02003
Petr Machata9a5420c2011-07-09 11:21:23 +02004#include <sys/stat.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +02005#include <sys/syscall.h>
6#include <sys/types.h>
7#include <ctype.h>
8#include <dirent.h>
9#include <errno.h>
Petr Machata9a5420c2011-07-09 11:21:23 +020010#include <fcntl.h>
Zachary T Welchbfb26c72010-12-06 23:21:00 -080011#include <inttypes.h>
Joe Damato47cae1e2010-11-08 15:47:39 -080012#include <link.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +020013#include <signal.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010014#include <stdio.h>
15#include <string.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +010016#include <unistd.h>
Petr Machata9a5420c2011-07-09 11:21:23 +020017
Petr Machataa611fc82012-02-07 13:27:04 +010018#include "common.h"
Petr Machata9294d822012-02-07 12:35:58 +010019#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010020#include "proc.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010021#include "library.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010022
23/* /proc/pid doesn't exist just after the fork, and sometimes `ltrace'
24 * couldn't open it to find the executable. So it may be necessary to
25 * have a bit delay
26 */
27
Ian Wienand2d45b1a2006-02-20 22:48:07 +010028#define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */
Juan Cespedes1fe93d51998-03-13 00:29:21 +010029
Petr Machata9a5420c2011-07-09 11:21:23 +020030#define PROC_PID_FILE(VAR, FORMAT, PID) \
31 char VAR[strlen(FORMAT) + 6]; \
32 sprintf(VAR, FORMAT, PID)
33
Juan Cespedes1fe93d51998-03-13 00:29:21 +010034/*
Juan Cespedese0660df2009-05-21 18:14:39 +020035 * Returns a (malloc'd) file name corresponding to a running pid
Juan Cespedes1fe93d51998-03-13 00:29:21 +010036 */
Juan Cespedesf1350522008-12-16 18:19:58 +010037char *
38pid2name(pid_t pid) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +010039 if (!kill(pid, 0)) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010040 int delay = 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010041
Petr Machata9a5420c2011-07-09 11:21:23 +020042 PROC_PID_FILE(proc_exe, "/proc/%d/exe", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010043
Ian Wienand2d45b1a2006-02-20 22:48:07 +010044 while (delay < MAX_DELAY) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010045 if (!access(proc_exe, F_OK)) {
46 return strdup(proc_exe);
47 }
48 delay += 1000; /* 1 milisecond */
49 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010050 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010051 return NULL;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010052}
Joe Damato47cae1e2010-11-08 15:47:39 -080053
Petr Machata9a5420c2011-07-09 11:21:23 +020054static FILE *
55open_status_file(pid_t pid)
56{
57 PROC_PID_FILE(fn, "/proc/%d/status", pid);
58 /* Don't complain if we fail. This would typically happen
59 when the process is about to terminate, and these files are
60 not available anymore. This function is called from the
61 event loop, and we don't want to clutter the output just
62 because the process terminates. */
63 return fopen(fn, "r");
64}
65
66static char *
67find_line_starting(FILE * file, const char * prefix, size_t len)
68{
69 char * line = NULL;
70 size_t line_len = 0;
71 while (!feof(file)) {
72 if (getline(&line, &line_len, file) < 0)
73 return NULL;
74 if (strncmp(line, prefix, len) == 0)
75 return line;
76 }
77 return NULL;
78}
79
80static void
Petr Machata2b46cfc2012-02-18 11:17:29 +010081each_line_starting(FILE *file, const char *prefix,
82 enum callback_status (*cb)(const char *line,
83 const char *prefix,
84 void *data),
85 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +020086{
87 size_t len = strlen(prefix);
88 char * line;
89 while ((line = find_line_starting(file, prefix, len)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +010090 enum callback_status st = (*cb)(line, prefix, data);
Petr Machata9a5420c2011-07-09 11:21:23 +020091 free (line);
Petr Machata2b46cfc2012-02-18 11:17:29 +010092 if (st == CBS_STOP)
Petr Machata9a5420c2011-07-09 11:21:23 +020093 return;
94 }
95}
96
Petr Machata2b46cfc2012-02-18 11:17:29 +010097static enum callback_status
98process_leader_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +020099{
100 pid_t * pidp = data;
101 *pidp = atoi(line + strlen(prefix));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100102 return CBS_STOP;
Petr Machata9a5420c2011-07-09 11:21:23 +0200103}
104
105pid_t
106process_leader(pid_t pid)
107{
Petr Machata1974dbc2011-08-19 18:58:01 +0200108 pid_t tgid = 0;
Petr Machata9a5420c2011-07-09 11:21:23 +0200109 FILE * file = open_status_file(pid);
110 if (file != NULL) {
111 each_line_starting(file, "Tgid:\t", &process_leader_cb, &tgid);
112 fclose(file);
113 }
114
115 return tgid;
116}
117
Petr Machata2b46cfc2012-02-18 11:17:29 +0100118static enum callback_status
119process_stopped_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200120{
121 char c = line[strlen(prefix)];
122 // t:tracing stop, T:job control stop
123 *(int *)data = (c == 't' || c == 'T');
Petr Machata2b46cfc2012-02-18 11:17:29 +0100124 return CBS_STOP;
Petr Machata9a5420c2011-07-09 11:21:23 +0200125}
126
127int
128process_stopped(pid_t pid)
129{
130 int is_stopped = -1;
131 FILE * file = open_status_file(pid);
132 if (file != NULL) {
133 each_line_starting(file, "State:\t", &process_stopped_cb,
134 &is_stopped);
135 fclose(file);
136 }
137 return is_stopped;
138}
139
Petr Machata2b46cfc2012-02-18 11:17:29 +0100140static enum callback_status
141process_status_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200142{
Petr Machata617ff0b2011-10-06 14:23:24 +0200143 const char * status = line + strlen(prefix);
144 const char c = *status;
145
146#define RETURN(C) do { \
147 *(enum process_status *)data = C; \
Petr Machata2b46cfc2012-02-18 11:17:29 +0100148 return CBS_STOP; \
Petr Machata617ff0b2011-10-06 14:23:24 +0200149 } while (0)
150
151 switch (c) {
152 case 'Z': RETURN(ps_zombie);
153 case 't': RETURN(ps_tracing_stop);
Petr Machatacbe29c62011-09-27 02:27:58 +0200154 case 'T':
Petr Machata617ff0b2011-10-06 14:23:24 +0200155 /* This can be either "T (stopped)" or, for older
156 * kernels, "T (tracing stop)". */
157 if (!strcmp(status, "T (stopped)\n"))
158 RETURN(ps_stop);
159 else if (!strcmp(status, "T (tracing stop)\n"))
160 RETURN(ps_tracing_stop);
161 else {
162 fprintf(stderr, "Unknown process status: %s",
163 status);
164 RETURN(ps_stop); /* Some sort of stop
165 * anyway. */
166 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200167 case 'D':
168 case 'S': RETURN(ps_sleeping);
Petr Machata617ff0b2011-10-06 14:23:24 +0200169 }
170
171 RETURN(ps_other);
172#undef RETURN
Petr Machata9a5420c2011-07-09 11:21:23 +0200173}
174
Petr Machata617ff0b2011-10-06 14:23:24 +0200175enum process_status
Petr Machata9a5420c2011-07-09 11:21:23 +0200176process_status(pid_t pid)
177{
Petr Machata617ff0b2011-10-06 14:23:24 +0200178 enum process_status ret = ps_invalid;
Petr Machata9a5420c2011-07-09 11:21:23 +0200179 FILE * file = open_status_file(pid);
180 if (file != NULL) {
181 each_line_starting(file, "State:\t", &process_status_cb, &ret);
182 fclose(file);
Petr Machata750ca8c2011-10-06 14:29:34 +0200183 if (ret == ps_invalid)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200184 fprintf(stderr, "process_status %d: %s", pid,
185 strerror(errno));
Petr Machata750ca8c2011-10-06 14:29:34 +0200186 } else
187 /* If the file is not present, the process presumably
188 * exited already. */
189 ret = ps_zombie;
190
Petr Machata9a5420c2011-07-09 11:21:23 +0200191 return ret;
192}
193
194static int
195all_digits(const char *str)
196{
197 while (isdigit(*str))
198 str++;
199 return !*str;
200}
201
202int
203process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n)
204{
205 PROC_PID_FILE(fn, "/proc/%d/task", pid);
206 DIR * d = opendir(fn);
207 if (d == NULL)
208 return -1;
209
Petr Machata9a5420c2011-07-09 11:21:23 +0200210 pid_t *tasks = NULL;
211 size_t n = 0;
212 size_t alloc = 0;
213
214 while (1) {
215 struct dirent entry;
216 struct dirent *result;
217 if (readdir_r(d, &entry, &result) != 0) {
218 free(tasks);
219 return -1;
220 }
221 if (result == NULL)
222 break;
223 if (result->d_type == DT_DIR && all_digits(result->d_name)) {
224 pid_t npid = atoi(result->d_name);
225 if (n >= alloc) {
226 alloc = alloc > 0 ? (2 * alloc) : 8;
227 pid_t *ntasks = realloc(tasks,
228 sizeof(*tasks) * alloc);
229 if (ntasks == NULL) {
230 free(tasks);
231 return -1;
232 }
233 tasks = ntasks;
234 }
235 if (n >= alloc)
236 abort();
237 tasks[n++] = npid;
238 }
239 }
240
241 closedir(d);
242
243 *ret_tasks = tasks;
244 *ret_n = n;
245 return 0;
246}
247
Petr Machata6a7997d2012-03-29 18:37:15 +0200248/* On native 64-bit system, we need to be careful when handling cross
249 * tracing. This select appropriate pointer depending on host and
250 * target architectures. XXX Really we should abstract this into the
251 * ABI object, as theorized about somewhere on pmachata/revamp
252 * branch. */
253static void *
254select_32_64(struct Process *proc, void *p32, void *p64)
255{
256 if (sizeof(long) == 4 || proc->mask_32bit)
257 return p32;
258 else
259 return p64;
260}
261
Joe Damato47cae1e2010-11-08 15:47:39 -0800262static int
Petr Machata6a7997d2012-03-29 18:37:15 +0200263fetch_dyn64(struct Process *proc, target_address_t *addr, Elf64_Dyn *ret)
264{
265 if (umovebytes(proc, *addr, ret, sizeof(*ret)) != sizeof(*ret))
266 return -1;
267 *addr += sizeof(*ret);
268 return 0;
269}
270
271static int
272fetch_dyn32(struct Process *proc, target_address_t *addr, Elf64_Dyn *ret)
273{
274 Elf32_Dyn dyn;
275 if (umovebytes(proc, *addr, &dyn, sizeof(dyn)) != sizeof(dyn))
276 return -1;
277
278 *addr += sizeof(dyn);
279 ret->d_tag = dyn.d_tag;
280 ret->d_un.d_val = dyn.d_un.d_val;
281
282 return 0;
283}
284
285static int (*
286dyn_fetcher(struct Process *proc))(struct Process *,
287 target_address_t *, Elf64_Dyn *)
288{
289 return select_32_64(proc, fetch_dyn32, fetch_dyn64);
290}
291
292static int
293find_dynamic_entry_addr(struct Process *proc, target_address_t src_addr,
294 int d_tag, target_address_t *ret)
295{
Joe Damato47cae1e2010-11-08 15:47:39 -0800296 debug(DEBUG_FUNCTION, "find_dynamic_entry()");
297
Petr Machata6a7997d2012-03-29 18:37:15 +0200298 if (ret == NULL || src_addr == 0 || d_tag < 0 || d_tag > DT_NUM)
Joe Damato47cae1e2010-11-08 15:47:39 -0800299 return -1;
Joe Damato47cae1e2010-11-08 15:47:39 -0800300
Petr Machata6a7997d2012-03-29 18:37:15 +0200301 int i = 0;
302 while (1) {
303 Elf64_Dyn entry;
304 if (dyn_fetcher(proc)(proc, &src_addr, &entry) < 0
305 || entry.d_tag == DT_NULL
306 || i++ > 100) { /* Arbitrary cut-off so that we
307 * don't loop forever if the
308 * binary is corrupted. */
309 debug(2, "Couldn't find address for dtag!");
310 return -1;
311 }
312
Joe Damato47cae1e2010-11-08 15:47:39 -0800313 if (entry.d_tag == d_tag) {
Petr Machataea8eb9a2012-04-17 01:32:07 +0200314 /* XXX The double cast should be removed when
315 * target_address_t becomes integral type. */
316 *ret = (target_address_t)(uintptr_t)entry.d_un.d_val;
Petr Machata8454bd72012-04-17 17:12:44 +0200317 debug(2, "found address: %p in dtag %d", *ret, d_tag);
Petr Machata17476b72012-02-23 18:50:37 +0100318 return 0;
Joe Damato47cae1e2010-11-08 15:47:39 -0800319 }
Petr Machata6a7997d2012-03-29 18:37:15 +0200320 }
321}
322
323/* Our own type for representing 32-bit linkmap. We can't rely on the
324 * definition in link.h, because that's only accurate for our host
325 * architecture, not for target architecture (where the traced process
326 * runs). */
327#define LT_LINK_MAP(BITS) \
328 { \
329 Elf##BITS##_Addr l_addr; \
330 Elf##BITS##_Addr l_name; \
331 Elf##BITS##_Addr l_ld; \
332 Elf##BITS##_Addr l_next; \
333 Elf##BITS##_Addr l_prev; \
334 }
335struct lt_link_map_32 LT_LINK_MAP(32);
336struct lt_link_map_64 LT_LINK_MAP(64);
337
338static int
339fetch_lm64(struct Process *proc, target_address_t addr,
340 struct lt_link_map_64 *ret)
341{
342 if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
343 return -1;
344 return 0;
345}
346
347static int
348fetch_lm32(struct Process *proc, target_address_t addr,
349 struct lt_link_map_64 *ret)
350{
351 struct lt_link_map_32 lm;
352 if (umovebytes(proc, addr, &lm, sizeof(lm)) != sizeof(lm))
353 return -1;
354
355 ret->l_addr = lm.l_addr;
356 ret->l_name = lm.l_name;
357 ret->l_ld = lm.l_ld;
358 ret->l_next = lm.l_next;
359 ret->l_prev = lm.l_prev;
360
361 return 0;
362}
363
364static int (*
365lm_fetcher(struct Process *proc))(struct Process *,
366 target_address_t, struct lt_link_map_64 *)
367{
368 return select_32_64(proc, fetch_lm32, fetch_lm64);
369}
370
371/* The same as above holds for struct r_debug. */
372#define LT_R_DEBUG(BITS) \
373 { \
374 int r_version; \
375 Elf##BITS##_Addr r_map; \
376 Elf##BITS##_Addr r_brk; \
377 int r_state; \
378 Elf##BITS##_Addr r_ldbase; \
Joe Damato47cae1e2010-11-08 15:47:39 -0800379 }
380
Petr Machata6a7997d2012-03-29 18:37:15 +0200381struct lt_r_debug_32 LT_R_DEBUG(32);
382struct lt_r_debug_64 LT_R_DEBUG(64);
383
384static int
385fetch_rd64(struct Process *proc, target_address_t addr,
386 struct lt_r_debug_64 *ret)
387{
388 if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
389 return -1;
390 return 0;
391}
392
393static int
394fetch_rd32(struct Process *proc, target_address_t addr,
395 struct lt_r_debug_64 *ret)
396{
397 struct lt_r_debug_32 rd;
398 if (umovebytes(proc, addr, &rd, sizeof(rd)) != sizeof(rd))
399 return -1;
400
401 ret->r_version = rd.r_version;
402 ret->r_map = rd.r_map;
403 ret->r_brk = rd.r_brk;
404 ret->r_state = rd.r_state;
405 ret->r_ldbase = rd.r_ldbase;
406
407 return 0;
408}
409
410static int (*
411rdebug_fetcher(struct Process *proc))(struct Process *,
412 target_address_t, struct lt_r_debug_64 *)
413{
414 return select_32_64(proc, fetch_rd32, fetch_rd64);
Joe Damato47cae1e2010-11-08 15:47:39 -0800415}
Joe Damatof0bd98b2010-11-08 15:47:42 -0800416
Joe Damatof0bd98b2010-11-08 15:47:42 -0800417static void
Petr Machata6a7997d2012-03-29 18:37:15 +0200418crawl_linkmap(struct Process *proc, struct lt_r_debug_64 *dbg)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100419{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800420 debug (DEBUG_FUNCTION, "crawl_linkmap()");
421
422 if (!dbg || !dbg->r_map) {
423 debug(2, "Debug structure or it's linkmap are NULL!");
424 return;
425 }
426
Petr Machataea8eb9a2012-04-17 01:32:07 +0200427 /* XXX The double cast should be removed when
428 * target_address_t becomes integral type. */
429 target_address_t addr = (target_address_t)(uintptr_t)dbg->r_map;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800430
Petr Machata6a7997d2012-03-29 18:37:15 +0200431 while (addr != 0) {
432 struct lt_link_map_64 rlm;
433 if (lm_fetcher(proc)(proc, addr, &rlm) < 0) {
Petr Machata74d80542012-04-03 12:40:59 +0200434 debug(2, "Unable to read link map");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800435 return;
436 }
437
Petr Machata89ac0392012-04-03 02:30:48 +0200438 target_address_t key = addr;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200439 /* XXX The double cast should be removed when
440 * target_address_t becomes integral type. */
441 addr = (target_address_t)(uintptr_t)rlm.l_next;
Petr Machata6a7997d2012-03-29 18:37:15 +0200442 if (rlm.l_name == 0) {
Petr Machata74d80542012-04-03 12:40:59 +0200443 debug(2, "Name of mapped library is NULL");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800444 return;
445 }
446
Petr Machata6a7997d2012-03-29 18:37:15 +0200447 char lib_name[BUFSIZ];
Petr Machataea8eb9a2012-04-17 01:32:07 +0200448 /* XXX The double cast should be removed when
449 * target_address_t becomes integral type. */
450 umovebytes(proc, (target_address_t)(uintptr_t)rlm.l_name,
Petr Machata6a7997d2012-03-29 18:37:15 +0200451 lib_name, sizeof(lib_name));
Joe Damatof0bd98b2010-11-08 15:47:42 -0800452
Petr Machata2b46cfc2012-02-18 11:17:29 +0100453 if (*lib_name == '\0') {
454 /* VDSO. No associated file, XXX but we might
455 * load it from the address space of the
456 * process. */
Joe Damatof0bd98b2010-11-08 15:47:42 -0800457 continue;
458 }
459
Petr Machata89ac0392012-04-03 02:30:48 +0200460 /* Do we have that library already? */
461 if (proc_each_library(proc, NULL, library_with_key_cb, &key))
462 continue;
463
Petr Machatab5f80ac2012-04-04 01:46:18 +0200464 struct library *lib = malloc(sizeof(*lib));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100465 if (lib == NULL) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200466 fail:
467 if (lib != NULL)
468 library_destroy(lib);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200469 fprintf(stderr, "Couldn't load ELF object %s: %s\n",
470 lib_name, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100471 continue;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800472 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200473 library_init(lib, LT_LIBTYPE_DSO);
474
475 if (ltelf_read_library(lib, proc, lib_name, rlm.l_addr) < 0)
476 goto fail;
477
Petr Machata89ac0392012-04-03 02:30:48 +0200478 lib->key = key;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100479 proc_add_library(proc, lib);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800480 }
481 return;
482}
483
Petr Machata029171f2012-04-05 02:14:30 +0200484/* A struct stored at proc->debug. */
485struct debug_struct
486{
487 target_address_t debug_addr;
488 int state;
489};
490
Petr Machata6a7997d2012-03-29 18:37:15 +0200491static int
492load_debug_struct(struct Process *proc, struct lt_r_debug_64 *ret)
493{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800494 debug(DEBUG_FUNCTION, "load_debug_struct");
495
Petr Machata029171f2012-04-05 02:14:30 +0200496 struct debug_struct *debug = proc->debug;
497
498 if (rdebug_fetcher(proc)(proc, debug->debug_addr, ret) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800499 debug(2, "This process does not have a debug structure!\n");
Petr Machata6a7997d2012-03-29 18:37:15 +0200500 return -1;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800501 }
502
Petr Machata6a7997d2012-03-29 18:37:15 +0200503 return 0;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800504}
505
506static void
Petr Machata12affff2012-03-29 18:33:03 +0200507rdebug_bp_on_hit(struct breakpoint *bp, struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100508{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800509 debug(DEBUG_FUNCTION, "arch_check_dbg");
510
Petr Machata6a7997d2012-03-29 18:37:15 +0200511 struct lt_r_debug_64 rdbg;
512 if (load_debug_struct(proc, &rdbg) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800513 debug(2, "Unable to load debug structure!");
514 return;
515 }
516
Petr Machata029171f2012-04-05 02:14:30 +0200517 struct debug_struct *debug = proc->debug;
Petr Machata6a7997d2012-03-29 18:37:15 +0200518 if (rdbg.r_state == RT_CONSISTENT) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800519 debug(2, "Linkmap is now consistent");
Petr Machata029171f2012-04-05 02:14:30 +0200520 if (debug->state == RT_ADD) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800521 debug(2, "Adding DSO to linkmap");
Petr Machata2b46cfc2012-02-18 11:17:29 +0100522 //data.proc = proc;
Petr Machata6a7997d2012-03-29 18:37:15 +0200523 crawl_linkmap(proc, &rdbg);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100524 //&data);
Petr Machata029171f2012-04-05 02:14:30 +0200525 } else if (debug->state == RT_DELETE) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800526 debug(2, "Removing DSO from linkmap");
527 } else {
528 debug(2, "Unexpected debug state!");
529 }
530 }
531
Petr Machata029171f2012-04-05 02:14:30 +0200532 debug->state = rdbg.r_state;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800533}
534
Joe Damatof0bd98b2010-11-08 15:47:42 -0800535int
Petr Machata52dbfb12012-03-29 16:38:26 +0200536linkmap_init(struct Process *proc, target_address_t dyn_addr)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100537{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800538 debug(DEBUG_FUNCTION, "linkmap_init()");
539
Petr Machata029171f2012-04-05 02:14:30 +0200540 struct debug_struct *debug = malloc(sizeof(*debug));
541 if (debug == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200542 fprintf(stderr, "couldn't allocate debug struct: %s\n",
543 strerror(errno));
Petr Machata89ac0392012-04-03 02:30:48 +0200544 fail:
Petr Machata029171f2012-04-05 02:14:30 +0200545 proc->debug = NULL;
546 free(debug);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800547 return -1;
548 }
Petr Machata029171f2012-04-05 02:14:30 +0200549 proc->debug = debug;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800550
Petr Machata029171f2012-04-05 02:14:30 +0200551 if (find_dynamic_entry_addr(proc, dyn_addr, DT_DEBUG,
552 &debug->debug_addr) == -1) {
553 debug(2, "Couldn't find debug structure!");
554 goto fail;
555 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800556
Petr Machata6a7997d2012-03-29 18:37:15 +0200557 int status;
558 struct lt_r_debug_64 rdbg;
559 if ((status = load_debug_struct(proc, &rdbg)) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800560 debug(2, "No debug structure or no memory to allocate one!");
Petr Machata6a7997d2012-03-29 18:37:15 +0200561 return status;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800562 }
563
Petr Machataea8eb9a2012-04-17 01:32:07 +0200564 /* XXX The double cast should be removed when
565 * target_address_t becomes integral type. */
566 target_address_t addr = (target_address_t)(uintptr_t)rdbg.r_brk;
Petr Machata89ac0392012-04-03 02:30:48 +0200567 if (arch_translate_address(proc, addr, &addr) < 0)
568 goto fail;
Petr Machata89ac0392012-04-03 02:30:48 +0200569
Petr Machata9df15012012-02-20 12:49:46 +0100570 struct breakpoint *rdebug_bp = insert_breakpoint(proc, addr, NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100571 static struct bp_callbacks rdebug_callbacks = {
Petr Machata12affff2012-03-29 18:33:03 +0200572 .on_hit = rdebug_bp_on_hit,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100573 };
574 rdebug_bp->cbs = &rdebug_callbacks;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800575
Petr Machata6a7997d2012-03-29 18:37:15 +0200576 crawl_linkmap(proc, &rdbg);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800577
Joe Damatof0bd98b2010-11-08 15:47:42 -0800578 return 0;
579}
Petr Machata9a5420c2011-07-09 11:21:23 +0200580
Petr Machata18bd8ff2012-04-10 04:32:39 +0200581static int
582fetch_auxv64_entry(int fd, Elf64_auxv_t *ret)
583{
584 /* Reaching EOF is as much problem as not reading whole
585 * entry. */
586 return read(fd, ret, sizeof(*ret)) == sizeof(*ret) ? 0 : -1;
587}
588
589static int
590fetch_auxv32_entry(int fd, Elf64_auxv_t *ret)
591{
592 Elf32_auxv_t auxv;
593 if (read(fd, &auxv, sizeof(auxv)) != sizeof(auxv))
594 return -1;
595
596 ret->a_type = auxv.a_type;
597 ret->a_un.a_val = auxv.a_un.a_val;
598 return 0;
599}
600
601static int (*
602auxv_fetcher(struct Process *proc))(int, Elf64_auxv_t *)
603{
604 return select_32_64(proc, fetch_auxv32_entry, fetch_auxv64_entry);
605}
606
607int
608process_get_entry(struct Process *proc,
609 target_address_t *entryp,
610 target_address_t *interp_biasp)
611{
612 PROC_PID_FILE(fn, "/proc/%d/auxv", proc->pid);
613 int fd = open(fn, O_RDONLY);
614 if (fd == -1) {
615 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200616 fprintf(stderr, "couldn't read %s: %s", fn, strerror(errno));
Petr Machata18bd8ff2012-04-10 04:32:39 +0200617 done:
618 if (fd != -1)
619 close(fd);
620 return fd == -1 ? -1 : 0;
621 }
622
623 target_address_t at_entry = 0;
624 target_address_t at_bias = 0;
625 while (1) {
626 Elf64_auxv_t entry;
627 if (auxv_fetcher(proc)(fd, &entry) < 0)
628 goto fail;
629
630 switch (entry.a_type) {
631 case AT_BASE:
Petr Machataea8eb9a2012-04-17 01:32:07 +0200632 /* XXX The double cast should be removed when
633 * target_address_t becomes integral type. */
634 at_bias = (target_address_t)
635 (uintptr_t)entry.a_un.a_val;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200636 continue;
637
638 case AT_ENTRY:
Petr Machataea8eb9a2012-04-17 01:32:07 +0200639 /* XXX The double cast should be removed when
640 * target_address_t becomes integral type. */
641 at_entry = (target_address_t)
642 (uintptr_t)entry.a_un.a_val;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200643 default:
644 continue;
645
646 case AT_NULL:
647 break;
648 }
649 break;
650 }
651
652 *entryp = at_entry;
653 *interp_biasp = at_bias;
654 goto done;
655}
656
Petr Machata9a5420c2011-07-09 11:21:23 +0200657int
658task_kill (pid_t pid, int sig)
659{
660 // Taken from GDB
661 int ret;
662
663 errno = 0;
664 ret = syscall (__NR_tkill, pid, sig);
665 return ret;
666}