blob: 97969dcbcedc24f214144f68c053ec6fc0dd41cd [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 Machatacd972582012-01-07 03:02:07 +010018#include "config.h"
Petr Machataa611fc82012-02-07 13:27:04 +010019#include "common.h"
Petr Machata9294d822012-02-07 12:35:58 +010020#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010021#include "proc.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010022#include "library.h"
Petr Machatacd972582012-01-07 03:02:07 +010023#include "events.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010024
25/* /proc/pid doesn't exist just after the fork, and sometimes `ltrace'
26 * couldn't open it to find the executable. So it may be necessary to
27 * have a bit delay
28 */
29
Ian Wienand2d45b1a2006-02-20 22:48:07 +010030#define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */
Juan Cespedes1fe93d51998-03-13 00:29:21 +010031
Petr Machata9a5420c2011-07-09 11:21:23 +020032#define PROC_PID_FILE(VAR, FORMAT, PID) \
33 char VAR[strlen(FORMAT) + 6]; \
34 sprintf(VAR, FORMAT, PID)
35
Juan Cespedes1fe93d51998-03-13 00:29:21 +010036/*
Juan Cespedese0660df2009-05-21 18:14:39 +020037 * Returns a (malloc'd) file name corresponding to a running pid
Juan Cespedes1fe93d51998-03-13 00:29:21 +010038 */
Juan Cespedesf1350522008-12-16 18:19:58 +010039char *
40pid2name(pid_t pid) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +010041 if (!kill(pid, 0)) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010042 int delay = 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010043
Petr Machata9a5420c2011-07-09 11:21:23 +020044 PROC_PID_FILE(proc_exe, "/proc/%d/exe", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010045
Ian Wienand2d45b1a2006-02-20 22:48:07 +010046 while (delay < MAX_DELAY) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010047 if (!access(proc_exe, F_OK)) {
48 return strdup(proc_exe);
49 }
50 delay += 1000; /* 1 milisecond */
51 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010052 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010053 return NULL;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010054}
Joe Damato47cae1e2010-11-08 15:47:39 -080055
Petr Machata9a5420c2011-07-09 11:21:23 +020056static FILE *
57open_status_file(pid_t pid)
58{
59 PROC_PID_FILE(fn, "/proc/%d/status", pid);
60 /* Don't complain if we fail. This would typically happen
61 when the process is about to terminate, and these files are
62 not available anymore. This function is called from the
63 event loop, and we don't want to clutter the output just
64 because the process terminates. */
65 return fopen(fn, "r");
66}
67
68static char *
69find_line_starting(FILE * file, const char * prefix, size_t len)
70{
71 char * line = NULL;
72 size_t line_len = 0;
73 while (!feof(file)) {
74 if (getline(&line, &line_len, file) < 0)
75 return NULL;
76 if (strncmp(line, prefix, len) == 0)
77 return line;
78 }
79 return NULL;
80}
81
82static void
Petr Machata2b46cfc2012-02-18 11:17:29 +010083each_line_starting(FILE *file, const char *prefix,
84 enum callback_status (*cb)(const char *line,
85 const char *prefix,
86 void *data),
87 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +020088{
89 size_t len = strlen(prefix);
90 char * line;
91 while ((line = find_line_starting(file, prefix, len)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +010092 enum callback_status st = (*cb)(line, prefix, data);
Petr Machata9a5420c2011-07-09 11:21:23 +020093 free (line);
Petr Machata2b46cfc2012-02-18 11:17:29 +010094 if (st == CBS_STOP)
Petr Machata9a5420c2011-07-09 11:21:23 +020095 return;
96 }
97}
98
Petr Machata2b46cfc2012-02-18 11:17:29 +010099static enum callback_status
100process_leader_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200101{
102 pid_t * pidp = data;
103 *pidp = atoi(line + strlen(prefix));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100104 return CBS_STOP;
Petr Machata9a5420c2011-07-09 11:21:23 +0200105}
106
107pid_t
108process_leader(pid_t pid)
109{
Petr Machata1974dbc2011-08-19 18:58:01 +0200110 pid_t tgid = 0;
Petr Machata9a5420c2011-07-09 11:21:23 +0200111 FILE * file = open_status_file(pid);
112 if (file != NULL) {
113 each_line_starting(file, "Tgid:\t", &process_leader_cb, &tgid);
114 fclose(file);
115 }
116
117 return tgid;
118}
119
Petr Machata2b46cfc2012-02-18 11:17:29 +0100120static enum callback_status
121process_stopped_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200122{
123 char c = line[strlen(prefix)];
124 // t:tracing stop, T:job control stop
125 *(int *)data = (c == 't' || c == 'T');
Petr Machata2b46cfc2012-02-18 11:17:29 +0100126 return CBS_STOP;
Petr Machata9a5420c2011-07-09 11:21:23 +0200127}
128
129int
130process_stopped(pid_t pid)
131{
132 int is_stopped = -1;
133 FILE * file = open_status_file(pid);
134 if (file != NULL) {
135 each_line_starting(file, "State:\t", &process_stopped_cb,
136 &is_stopped);
137 fclose(file);
138 }
139 return is_stopped;
140}
141
Petr Machata2b46cfc2012-02-18 11:17:29 +0100142static enum callback_status
143process_status_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200144{
Petr Machata617ff0b2011-10-06 14:23:24 +0200145 const char * status = line + strlen(prefix);
146 const char c = *status;
147
148#define RETURN(C) do { \
149 *(enum process_status *)data = C; \
Petr Machata2b46cfc2012-02-18 11:17:29 +0100150 return CBS_STOP; \
Petr Machata617ff0b2011-10-06 14:23:24 +0200151 } while (0)
152
153 switch (c) {
154 case 'Z': RETURN(ps_zombie);
155 case 't': RETURN(ps_tracing_stop);
Petr Machatacbe29c62011-09-27 02:27:58 +0200156 case 'T':
Petr Machata617ff0b2011-10-06 14:23:24 +0200157 /* This can be either "T (stopped)" or, for older
158 * kernels, "T (tracing stop)". */
159 if (!strcmp(status, "T (stopped)\n"))
160 RETURN(ps_stop);
161 else if (!strcmp(status, "T (tracing stop)\n"))
162 RETURN(ps_tracing_stop);
163 else {
164 fprintf(stderr, "Unknown process status: %s",
165 status);
166 RETURN(ps_stop); /* Some sort of stop
167 * anyway. */
168 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200169 case 'D':
170 case 'S': RETURN(ps_sleeping);
Petr Machata617ff0b2011-10-06 14:23:24 +0200171 }
172
173 RETURN(ps_other);
174#undef RETURN
Petr Machata9a5420c2011-07-09 11:21:23 +0200175}
176
Petr Machata617ff0b2011-10-06 14:23:24 +0200177enum process_status
Petr Machata9a5420c2011-07-09 11:21:23 +0200178process_status(pid_t pid)
179{
Petr Machata617ff0b2011-10-06 14:23:24 +0200180 enum process_status ret = ps_invalid;
Petr Machata9a5420c2011-07-09 11:21:23 +0200181 FILE * file = open_status_file(pid);
182 if (file != NULL) {
183 each_line_starting(file, "State:\t", &process_status_cb, &ret);
184 fclose(file);
Petr Machata750ca8c2011-10-06 14:29:34 +0200185 if (ret == ps_invalid)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200186 fprintf(stderr, "process_status %d: %s", pid,
187 strerror(errno));
Petr Machata750ca8c2011-10-06 14:29:34 +0200188 } else
189 /* If the file is not present, the process presumably
190 * exited already. */
191 ret = ps_zombie;
192
Petr Machata9a5420c2011-07-09 11:21:23 +0200193 return ret;
194}
195
196static int
197all_digits(const char *str)
198{
199 while (isdigit(*str))
200 str++;
201 return !*str;
202}
203
204int
205process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n)
206{
207 PROC_PID_FILE(fn, "/proc/%d/task", pid);
208 DIR * d = opendir(fn);
209 if (d == NULL)
210 return -1;
211
Petr Machata9a5420c2011-07-09 11:21:23 +0200212 pid_t *tasks = NULL;
213 size_t n = 0;
214 size_t alloc = 0;
215
216 while (1) {
217 struct dirent entry;
218 struct dirent *result;
219 if (readdir_r(d, &entry, &result) != 0) {
220 free(tasks);
221 return -1;
222 }
223 if (result == NULL)
224 break;
225 if (result->d_type == DT_DIR && all_digits(result->d_name)) {
226 pid_t npid = atoi(result->d_name);
227 if (n >= alloc) {
228 alloc = alloc > 0 ? (2 * alloc) : 8;
229 pid_t *ntasks = realloc(tasks,
230 sizeof(*tasks) * alloc);
231 if (ntasks == NULL) {
232 free(tasks);
233 return -1;
234 }
235 tasks = ntasks;
236 }
237 if (n >= alloc)
238 abort();
239 tasks[n++] = npid;
240 }
241 }
242
243 closedir(d);
244
245 *ret_tasks = tasks;
246 *ret_n = n;
247 return 0;
248}
249
Petr Machata6a7997d2012-03-29 18:37:15 +0200250/* On native 64-bit system, we need to be careful when handling cross
251 * tracing. This select appropriate pointer depending on host and
252 * target architectures. XXX Really we should abstract this into the
253 * ABI object, as theorized about somewhere on pmachata/revamp
254 * branch. */
255static void *
256select_32_64(struct Process *proc, void *p32, void *p64)
257{
258 if (sizeof(long) == 4 || proc->mask_32bit)
259 return p32;
260 else
261 return p64;
262}
263
Joe Damato47cae1e2010-11-08 15:47:39 -0800264static int
Petr Machata6a7997d2012-03-29 18:37:15 +0200265fetch_dyn64(struct Process *proc, target_address_t *addr, Elf64_Dyn *ret)
266{
267 if (umovebytes(proc, *addr, ret, sizeof(*ret)) != sizeof(*ret))
268 return -1;
269 *addr += sizeof(*ret);
270 return 0;
271}
272
273static int
274fetch_dyn32(struct Process *proc, target_address_t *addr, Elf64_Dyn *ret)
275{
276 Elf32_Dyn dyn;
277 if (umovebytes(proc, *addr, &dyn, sizeof(dyn)) != sizeof(dyn))
278 return -1;
279
280 *addr += sizeof(dyn);
281 ret->d_tag = dyn.d_tag;
282 ret->d_un.d_val = dyn.d_un.d_val;
283
284 return 0;
285}
286
287static int (*
288dyn_fetcher(struct Process *proc))(struct Process *,
289 target_address_t *, Elf64_Dyn *)
290{
291 return select_32_64(proc, fetch_dyn32, fetch_dyn64);
292}
293
294static int
295find_dynamic_entry_addr(struct Process *proc, target_address_t src_addr,
296 int d_tag, target_address_t *ret)
297{
Joe Damato47cae1e2010-11-08 15:47:39 -0800298 debug(DEBUG_FUNCTION, "find_dynamic_entry()");
299
Petr Machata6a7997d2012-03-29 18:37:15 +0200300 if (ret == NULL || src_addr == 0 || d_tag < 0 || d_tag > DT_NUM)
Joe Damato47cae1e2010-11-08 15:47:39 -0800301 return -1;
Joe Damato47cae1e2010-11-08 15:47:39 -0800302
Petr Machata6a7997d2012-03-29 18:37:15 +0200303 int i = 0;
304 while (1) {
305 Elf64_Dyn entry;
306 if (dyn_fetcher(proc)(proc, &src_addr, &entry) < 0
307 || entry.d_tag == DT_NULL
308 || i++ > 100) { /* Arbitrary cut-off so that we
309 * don't loop forever if the
310 * binary is corrupted. */
311 debug(2, "Couldn't find address for dtag!");
312 return -1;
313 }
314
Joe Damato47cae1e2010-11-08 15:47:39 -0800315 if (entry.d_tag == d_tag) {
Petr Machataea8eb9a2012-04-17 01:32:07 +0200316 /* XXX The double cast should be removed when
317 * target_address_t becomes integral type. */
318 *ret = (target_address_t)(uintptr_t)entry.d_un.d_val;
Petr Machata8454bd72012-04-17 17:12:44 +0200319 debug(2, "found address: %p in dtag %d", *ret, d_tag);
Petr Machata17476b72012-02-23 18:50:37 +0100320 return 0;
Joe Damato47cae1e2010-11-08 15:47:39 -0800321 }
Petr Machata6a7997d2012-03-29 18:37:15 +0200322 }
323}
324
325/* Our own type for representing 32-bit linkmap. We can't rely on the
326 * definition in link.h, because that's only accurate for our host
327 * architecture, not for target architecture (where the traced process
328 * runs). */
329#define LT_LINK_MAP(BITS) \
330 { \
331 Elf##BITS##_Addr l_addr; \
332 Elf##BITS##_Addr l_name; \
333 Elf##BITS##_Addr l_ld; \
334 Elf##BITS##_Addr l_next; \
335 Elf##BITS##_Addr l_prev; \
336 }
337struct lt_link_map_32 LT_LINK_MAP(32);
338struct lt_link_map_64 LT_LINK_MAP(64);
339
340static int
341fetch_lm64(struct Process *proc, target_address_t addr,
342 struct lt_link_map_64 *ret)
343{
344 if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
345 return -1;
346 return 0;
347}
348
349static int
350fetch_lm32(struct Process *proc, target_address_t addr,
351 struct lt_link_map_64 *ret)
352{
353 struct lt_link_map_32 lm;
354 if (umovebytes(proc, addr, &lm, sizeof(lm)) != sizeof(lm))
355 return -1;
356
357 ret->l_addr = lm.l_addr;
358 ret->l_name = lm.l_name;
359 ret->l_ld = lm.l_ld;
360 ret->l_next = lm.l_next;
361 ret->l_prev = lm.l_prev;
362
363 return 0;
364}
365
366static int (*
367lm_fetcher(struct Process *proc))(struct Process *,
368 target_address_t, struct lt_link_map_64 *)
369{
370 return select_32_64(proc, fetch_lm32, fetch_lm64);
371}
372
373/* The same as above holds for struct r_debug. */
374#define LT_R_DEBUG(BITS) \
375 { \
376 int r_version; \
377 Elf##BITS##_Addr r_map; \
378 Elf##BITS##_Addr r_brk; \
379 int r_state; \
380 Elf##BITS##_Addr r_ldbase; \
Joe Damato47cae1e2010-11-08 15:47:39 -0800381 }
382
Petr Machata6a7997d2012-03-29 18:37:15 +0200383struct lt_r_debug_32 LT_R_DEBUG(32);
384struct lt_r_debug_64 LT_R_DEBUG(64);
385
386static int
387fetch_rd64(struct Process *proc, target_address_t addr,
388 struct lt_r_debug_64 *ret)
389{
390 if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
391 return -1;
392 return 0;
393}
394
395static int
396fetch_rd32(struct Process *proc, target_address_t addr,
397 struct lt_r_debug_64 *ret)
398{
399 struct lt_r_debug_32 rd;
400 if (umovebytes(proc, addr, &rd, sizeof(rd)) != sizeof(rd))
401 return -1;
402
403 ret->r_version = rd.r_version;
404 ret->r_map = rd.r_map;
405 ret->r_brk = rd.r_brk;
406 ret->r_state = rd.r_state;
407 ret->r_ldbase = rd.r_ldbase;
408
409 return 0;
410}
411
412static int (*
413rdebug_fetcher(struct Process *proc))(struct Process *,
414 target_address_t, struct lt_r_debug_64 *)
415{
416 return select_32_64(proc, fetch_rd32, fetch_rd64);
Joe Damato47cae1e2010-11-08 15:47:39 -0800417}
Joe Damatof0bd98b2010-11-08 15:47:42 -0800418
Joe Damatof0bd98b2010-11-08 15:47:42 -0800419static void
Petr Machata6a7997d2012-03-29 18:37:15 +0200420crawl_linkmap(struct Process *proc, struct lt_r_debug_64 *dbg)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100421{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800422 debug (DEBUG_FUNCTION, "crawl_linkmap()");
423
424 if (!dbg || !dbg->r_map) {
425 debug(2, "Debug structure or it's linkmap are NULL!");
426 return;
427 }
428
Petr Machataea8eb9a2012-04-17 01:32:07 +0200429 /* XXX The double cast should be removed when
430 * target_address_t becomes integral type. */
431 target_address_t addr = (target_address_t)(uintptr_t)dbg->r_map;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800432
Petr Machata6a7997d2012-03-29 18:37:15 +0200433 while (addr != 0) {
434 struct lt_link_map_64 rlm;
435 if (lm_fetcher(proc)(proc, addr, &rlm) < 0) {
Petr Machata74d80542012-04-03 12:40:59 +0200436 debug(2, "Unable to read link map");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800437 return;
438 }
439
Petr Machata89ac0392012-04-03 02:30:48 +0200440 target_address_t key = addr;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200441 /* XXX The double cast should be removed when
442 * target_address_t becomes integral type. */
443 addr = (target_address_t)(uintptr_t)rlm.l_next;
Petr Machata6a7997d2012-03-29 18:37:15 +0200444 if (rlm.l_name == 0) {
Petr Machata74d80542012-04-03 12:40:59 +0200445 debug(2, "Name of mapped library is NULL");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800446 return;
447 }
448
Petr Machata6a7997d2012-03-29 18:37:15 +0200449 char lib_name[BUFSIZ];
Petr Machataea8eb9a2012-04-17 01:32:07 +0200450 /* XXX The double cast should be removed when
451 * target_address_t becomes integral type. */
452 umovebytes(proc, (target_address_t)(uintptr_t)rlm.l_name,
Petr Machata6a7997d2012-03-29 18:37:15 +0200453 lib_name, sizeof(lib_name));
Joe Damatof0bd98b2010-11-08 15:47:42 -0800454
Petr Machata2b46cfc2012-02-18 11:17:29 +0100455 if (*lib_name == '\0') {
456 /* VDSO. No associated file, XXX but we might
457 * load it from the address space of the
458 * process. */
Joe Damatof0bd98b2010-11-08 15:47:42 -0800459 continue;
460 }
461
Petr Machata89ac0392012-04-03 02:30:48 +0200462 /* Do we have that library already? */
463 if (proc_each_library(proc, NULL, library_with_key_cb, &key))
464 continue;
465
Petr Machatab5f80ac2012-04-04 01:46:18 +0200466 struct library *lib = malloc(sizeof(*lib));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100467 if (lib == NULL) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200468 fail:
469 if (lib != NULL)
470 library_destroy(lib);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200471 fprintf(stderr, "Couldn't load ELF object %s: %s\n",
472 lib_name, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100473 continue;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800474 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200475 library_init(lib, LT_LIBTYPE_DSO);
476
477 if (ltelf_read_library(lib, proc, lib_name, rlm.l_addr) < 0)
478 goto fail;
479
Petr Machata89ac0392012-04-03 02:30:48 +0200480 lib->key = key;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100481 proc_add_library(proc, lib);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800482 }
483 return;
484}
485
Petr Machata029171f2012-04-05 02:14:30 +0200486/* A struct stored at proc->debug. */
487struct debug_struct
488{
489 target_address_t debug_addr;
490 int state;
491};
492
Petr Machata6a7997d2012-03-29 18:37:15 +0200493static int
494load_debug_struct(struct Process *proc, struct lt_r_debug_64 *ret)
495{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800496 debug(DEBUG_FUNCTION, "load_debug_struct");
497
Petr Machata029171f2012-04-05 02:14:30 +0200498 struct debug_struct *debug = proc->debug;
499
500 if (rdebug_fetcher(proc)(proc, debug->debug_addr, ret) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800501 debug(2, "This process does not have a debug structure!\n");
Petr Machata6a7997d2012-03-29 18:37:15 +0200502 return -1;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800503 }
504
Petr Machata6a7997d2012-03-29 18:37:15 +0200505 return 0;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800506}
507
508static void
Petr Machata12affff2012-03-29 18:33:03 +0200509rdebug_bp_on_hit(struct breakpoint *bp, struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100510{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800511 debug(DEBUG_FUNCTION, "arch_check_dbg");
512
Petr Machata6a7997d2012-03-29 18:37:15 +0200513 struct lt_r_debug_64 rdbg;
514 if (load_debug_struct(proc, &rdbg) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800515 debug(2, "Unable to load debug structure!");
516 return;
517 }
518
Petr Machata029171f2012-04-05 02:14:30 +0200519 struct debug_struct *debug = proc->debug;
Petr Machata6a7997d2012-03-29 18:37:15 +0200520 if (rdbg.r_state == RT_CONSISTENT) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800521 debug(2, "Linkmap is now consistent");
Petr Machata029171f2012-04-05 02:14:30 +0200522 if (debug->state == RT_ADD) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800523 debug(2, "Adding DSO to linkmap");
Petr Machata2b46cfc2012-02-18 11:17:29 +0100524 //data.proc = proc;
Petr Machata6a7997d2012-03-29 18:37:15 +0200525 crawl_linkmap(proc, &rdbg);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100526 //&data);
Petr Machata029171f2012-04-05 02:14:30 +0200527 } else if (debug->state == RT_DELETE) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800528 debug(2, "Removing DSO from linkmap");
529 } else {
530 debug(2, "Unexpected debug state!");
531 }
532 }
533
Petr Machata029171f2012-04-05 02:14:30 +0200534 debug->state = rdbg.r_state;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800535}
536
Joe Damatof0bd98b2010-11-08 15:47:42 -0800537int
Petr Machata52dbfb12012-03-29 16:38:26 +0200538linkmap_init(struct Process *proc, target_address_t dyn_addr)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100539{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800540 debug(DEBUG_FUNCTION, "linkmap_init()");
541
Petr Machata029171f2012-04-05 02:14:30 +0200542 struct debug_struct *debug = malloc(sizeof(*debug));
543 if (debug == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200544 fprintf(stderr, "couldn't allocate debug struct: %s\n",
545 strerror(errno));
Petr Machata89ac0392012-04-03 02:30:48 +0200546 fail:
Petr Machata029171f2012-04-05 02:14:30 +0200547 proc->debug = NULL;
548 free(debug);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800549 return -1;
550 }
Petr Machata029171f2012-04-05 02:14:30 +0200551 proc->debug = debug;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800552
Petr Machata029171f2012-04-05 02:14:30 +0200553 if (find_dynamic_entry_addr(proc, dyn_addr, DT_DEBUG,
554 &debug->debug_addr) == -1) {
555 debug(2, "Couldn't find debug structure!");
556 goto fail;
557 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800558
Petr Machata6a7997d2012-03-29 18:37:15 +0200559 int status;
560 struct lt_r_debug_64 rdbg;
561 if ((status = load_debug_struct(proc, &rdbg)) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800562 debug(2, "No debug structure or no memory to allocate one!");
Petr Machata6a7997d2012-03-29 18:37:15 +0200563 return status;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800564 }
565
Petr Machataea8eb9a2012-04-17 01:32:07 +0200566 /* XXX The double cast should be removed when
567 * target_address_t becomes integral type. */
568 target_address_t addr = (target_address_t)(uintptr_t)rdbg.r_brk;
Petr Machatab1492df2012-04-30 21:01:40 +0200569 if (arch_translate_address_dyn(proc, addr, &addr) < 0)
Petr Machata89ac0392012-04-03 02:30:48 +0200570 goto fail;
Petr Machata89ac0392012-04-03 02:30:48 +0200571
Petr Machata9df15012012-02-20 12:49:46 +0100572 struct breakpoint *rdebug_bp = insert_breakpoint(proc, addr, NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100573 static struct bp_callbacks rdebug_callbacks = {
Petr Machata12affff2012-03-29 18:33:03 +0200574 .on_hit = rdebug_bp_on_hit,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100575 };
576 rdebug_bp->cbs = &rdebug_callbacks;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800577
Petr Machata6a7997d2012-03-29 18:37:15 +0200578 crawl_linkmap(proc, &rdbg);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800579
Joe Damatof0bd98b2010-11-08 15:47:42 -0800580 return 0;
581}
Petr Machata9a5420c2011-07-09 11:21:23 +0200582
583int
584task_kill (pid_t pid, int sig)
585{
586 // Taken from GDB
587 int ret;
588
589 errno = 0;
590 ret = syscall (__NR_tkill, pid, sig);
591 return ret;
592}
Petr Machatacd972582012-01-07 03:02:07 +0100593
594void
595process_removed(struct Process *proc)
596{
597 delete_events_for(proc);
598}