blob: 15502207af7895228ef07f45fe0de23926e245a1 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Zachary T Welch, CodeSourcery
5 * Copyright (C) 2010 Joe Damato
6 * Copyright (C) 1998,2008,2009 Juan Cespedes
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
Petr Machata750ca8c2011-10-06 14:29:34 +020024#define _GNU_SOURCE /* For getline. */
Juan Cespedesd44c6b81998-09-25 14:48:42 +020025#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +020026
Petr Machata9a5420c2011-07-09 11:21:23 +020027#include <sys/stat.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +020028#include <sys/syscall.h>
29#include <sys/types.h>
30#include <ctype.h>
31#include <dirent.h>
32#include <errno.h>
Petr Machata9a5420c2011-07-09 11:21:23 +020033#include <fcntl.h>
Zachary T Welchbfb26c72010-12-06 23:21:00 -080034#include <inttypes.h>
Joe Damato47cae1e2010-11-08 15:47:39 -080035#include <link.h>
Petr Machatacc0e1e42012-04-25 13:42:07 +020036#include <signal.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010037#include <stdio.h>
38#include <string.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +010039#include <unistd.h>
Petr Machata9a5420c2011-07-09 11:21:23 +020040
Petr Machatacd972582012-01-07 03:02:07 +010041#include "config.h"
Petr Machata9294d822012-02-07 12:35:58 +010042#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010043#include "proc.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010044#include "library.h"
Petr Machata64262602012-01-07 03:41:36 +010045#include "backend.h"
Petr Machatacd972582012-01-07 03:02:07 +010046#include "events.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010047
48/* /proc/pid doesn't exist just after the fork, and sometimes `ltrace'
49 * couldn't open it to find the executable. So it may be necessary to
50 * have a bit delay
51 */
52
Ian Wienand2d45b1a2006-02-20 22:48:07 +010053#define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */
Juan Cespedes1fe93d51998-03-13 00:29:21 +010054
Petr Machata9a5420c2011-07-09 11:21:23 +020055#define PROC_PID_FILE(VAR, FORMAT, PID) \
56 char VAR[strlen(FORMAT) + 6]; \
57 sprintf(VAR, FORMAT, PID)
58
Juan Cespedes1fe93d51998-03-13 00:29:21 +010059/*
Juan Cespedese0660df2009-05-21 18:14:39 +020060 * Returns a (malloc'd) file name corresponding to a running pid
Juan Cespedes1fe93d51998-03-13 00:29:21 +010061 */
Juan Cespedesf1350522008-12-16 18:19:58 +010062char *
63pid2name(pid_t pid) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +010064 if (!kill(pid, 0)) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010065 int delay = 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010066
Petr Machata9a5420c2011-07-09 11:21:23 +020067 PROC_PID_FILE(proc_exe, "/proc/%d/exe", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010068
Ian Wienand2d45b1a2006-02-20 22:48:07 +010069 while (delay < MAX_DELAY) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010070 if (!access(proc_exe, F_OK)) {
71 return strdup(proc_exe);
72 }
73 delay += 1000; /* 1 milisecond */
74 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010075 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010076 return NULL;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010077}
Joe Damato47cae1e2010-11-08 15:47:39 -080078
Petr Machata9a5420c2011-07-09 11:21:23 +020079static FILE *
80open_status_file(pid_t pid)
81{
82 PROC_PID_FILE(fn, "/proc/%d/status", pid);
83 /* Don't complain if we fail. This would typically happen
84 when the process is about to terminate, and these files are
85 not available anymore. This function is called from the
86 event loop, and we don't want to clutter the output just
87 because the process terminates. */
88 return fopen(fn, "r");
89}
90
91static char *
92find_line_starting(FILE * file, const char * prefix, size_t len)
93{
94 char * line = NULL;
95 size_t line_len = 0;
96 while (!feof(file)) {
97 if (getline(&line, &line_len, file) < 0)
98 return NULL;
99 if (strncmp(line, prefix, len) == 0)
100 return line;
101 }
102 return NULL;
103}
104
105static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100106each_line_starting(FILE *file, const char *prefix,
107 enum callback_status (*cb)(const char *line,
108 const char *prefix,
109 void *data),
110 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200111{
112 size_t len = strlen(prefix);
113 char * line;
114 while ((line = find_line_starting(file, prefix, len)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100115 enum callback_status st = (*cb)(line, prefix, data);
Petr Machata9a5420c2011-07-09 11:21:23 +0200116 free (line);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100117 if (st == CBS_STOP)
Petr Machata9a5420c2011-07-09 11:21:23 +0200118 return;
119 }
120}
121
Petr Machata2b46cfc2012-02-18 11:17:29 +0100122static enum callback_status
123process_leader_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200124{
125 pid_t * pidp = data;
126 *pidp = atoi(line + strlen(prefix));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100127 return CBS_STOP;
Petr Machata9a5420c2011-07-09 11:21:23 +0200128}
129
130pid_t
131process_leader(pid_t pid)
132{
Petr Machata1974dbc2011-08-19 18:58:01 +0200133 pid_t tgid = 0;
Petr Machata9a5420c2011-07-09 11:21:23 +0200134 FILE * file = open_status_file(pid);
135 if (file != NULL) {
136 each_line_starting(file, "Tgid:\t", &process_leader_cb, &tgid);
137 fclose(file);
138 }
139
140 return tgid;
141}
142
Petr Machata2b46cfc2012-02-18 11:17:29 +0100143static enum callback_status
144process_stopped_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200145{
146 char c = line[strlen(prefix)];
147 // t:tracing stop, T:job control stop
148 *(int *)data = (c == 't' || c == 'T');
Petr Machata2b46cfc2012-02-18 11:17:29 +0100149 return CBS_STOP;
Petr Machata9a5420c2011-07-09 11:21:23 +0200150}
151
152int
153process_stopped(pid_t pid)
154{
155 int is_stopped = -1;
156 FILE * file = open_status_file(pid);
157 if (file != NULL) {
158 each_line_starting(file, "State:\t", &process_stopped_cb,
159 &is_stopped);
160 fclose(file);
161 }
162 return is_stopped;
163}
164
Petr Machata2b46cfc2012-02-18 11:17:29 +0100165static enum callback_status
166process_status_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200167{
Petr Machata617ff0b2011-10-06 14:23:24 +0200168 const char * status = line + strlen(prefix);
169 const char c = *status;
170
171#define RETURN(C) do { \
172 *(enum process_status *)data = C; \
Petr Machata2b46cfc2012-02-18 11:17:29 +0100173 return CBS_STOP; \
Petr Machata617ff0b2011-10-06 14:23:24 +0200174 } while (0)
175
176 switch (c) {
177 case 'Z': RETURN(ps_zombie);
178 case 't': RETURN(ps_tracing_stop);
Petr Machatacbe29c62011-09-27 02:27:58 +0200179 case 'T':
Petr Machata617ff0b2011-10-06 14:23:24 +0200180 /* This can be either "T (stopped)" or, for older
181 * kernels, "T (tracing stop)". */
182 if (!strcmp(status, "T (stopped)\n"))
183 RETURN(ps_stop);
184 else if (!strcmp(status, "T (tracing stop)\n"))
185 RETURN(ps_tracing_stop);
186 else {
187 fprintf(stderr, "Unknown process status: %s",
188 status);
189 RETURN(ps_stop); /* Some sort of stop
190 * anyway. */
191 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200192 case 'D':
193 case 'S': RETURN(ps_sleeping);
Petr Machata617ff0b2011-10-06 14:23:24 +0200194 }
195
196 RETURN(ps_other);
197#undef RETURN
Petr Machata9a5420c2011-07-09 11:21:23 +0200198}
199
Petr Machata617ff0b2011-10-06 14:23:24 +0200200enum process_status
Petr Machata9a5420c2011-07-09 11:21:23 +0200201process_status(pid_t pid)
202{
Petr Machata617ff0b2011-10-06 14:23:24 +0200203 enum process_status ret = ps_invalid;
Petr Machata9a5420c2011-07-09 11:21:23 +0200204 FILE * file = open_status_file(pid);
205 if (file != NULL) {
206 each_line_starting(file, "State:\t", &process_status_cb, &ret);
207 fclose(file);
Petr Machata750ca8c2011-10-06 14:29:34 +0200208 if (ret == ps_invalid)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200209 fprintf(stderr, "process_status %d: %s", pid,
210 strerror(errno));
Petr Machata750ca8c2011-10-06 14:29:34 +0200211 } else
212 /* If the file is not present, the process presumably
213 * exited already. */
214 ret = ps_zombie;
215
Petr Machata9a5420c2011-07-09 11:21:23 +0200216 return ret;
217}
218
219static int
220all_digits(const char *str)
221{
222 while (isdigit(*str))
223 str++;
224 return !*str;
225}
226
227int
228process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n)
229{
230 PROC_PID_FILE(fn, "/proc/%d/task", pid);
231 DIR * d = opendir(fn);
232 if (d == NULL)
233 return -1;
234
Petr Machata9a5420c2011-07-09 11:21:23 +0200235 pid_t *tasks = NULL;
236 size_t n = 0;
237 size_t alloc = 0;
238
239 while (1) {
240 struct dirent entry;
241 struct dirent *result;
242 if (readdir_r(d, &entry, &result) != 0) {
243 free(tasks);
244 return -1;
245 }
246 if (result == NULL)
247 break;
248 if (result->d_type == DT_DIR && all_digits(result->d_name)) {
249 pid_t npid = atoi(result->d_name);
250 if (n >= alloc) {
251 alloc = alloc > 0 ? (2 * alloc) : 8;
252 pid_t *ntasks = realloc(tasks,
253 sizeof(*tasks) * alloc);
254 if (ntasks == NULL) {
255 free(tasks);
256 return -1;
257 }
258 tasks = ntasks;
259 }
260 if (n >= alloc)
261 abort();
262 tasks[n++] = npid;
263 }
264 }
265
266 closedir(d);
267
268 *ret_tasks = tasks;
269 *ret_n = n;
270 return 0;
271}
272
Petr Machata6a7997d2012-03-29 18:37:15 +0200273/* On native 64-bit system, we need to be careful when handling cross
274 * tracing. This select appropriate pointer depending on host and
275 * target architectures. XXX Really we should abstract this into the
276 * ABI object, as theorized about somewhere on pmachata/revamp
277 * branch. */
278static void *
279select_32_64(struct Process *proc, void *p32, void *p64)
280{
281 if (sizeof(long) == 4 || proc->mask_32bit)
282 return p32;
283 else
284 return p64;
285}
286
Joe Damato47cae1e2010-11-08 15:47:39 -0800287static int
Petr Machata6a7997d2012-03-29 18:37:15 +0200288fetch_dyn64(struct Process *proc, target_address_t *addr, Elf64_Dyn *ret)
289{
290 if (umovebytes(proc, *addr, ret, sizeof(*ret)) != sizeof(*ret))
291 return -1;
292 *addr += sizeof(*ret);
293 return 0;
294}
295
296static int
297fetch_dyn32(struct Process *proc, target_address_t *addr, Elf64_Dyn *ret)
298{
299 Elf32_Dyn dyn;
300 if (umovebytes(proc, *addr, &dyn, sizeof(dyn)) != sizeof(dyn))
301 return -1;
302
303 *addr += sizeof(dyn);
304 ret->d_tag = dyn.d_tag;
305 ret->d_un.d_val = dyn.d_un.d_val;
306
307 return 0;
308}
309
310static int (*
311dyn_fetcher(struct Process *proc))(struct Process *,
312 target_address_t *, Elf64_Dyn *)
313{
314 return select_32_64(proc, fetch_dyn32, fetch_dyn64);
315}
316
317static int
318find_dynamic_entry_addr(struct Process *proc, target_address_t src_addr,
319 int d_tag, target_address_t *ret)
320{
Joe Damato47cae1e2010-11-08 15:47:39 -0800321 debug(DEBUG_FUNCTION, "find_dynamic_entry()");
322
Petr Machata6a7997d2012-03-29 18:37:15 +0200323 if (ret == NULL || src_addr == 0 || d_tag < 0 || d_tag > DT_NUM)
Joe Damato47cae1e2010-11-08 15:47:39 -0800324 return -1;
Joe Damato47cae1e2010-11-08 15:47:39 -0800325
Petr Machata6a7997d2012-03-29 18:37:15 +0200326 int i = 0;
327 while (1) {
328 Elf64_Dyn entry;
329 if (dyn_fetcher(proc)(proc, &src_addr, &entry) < 0
330 || entry.d_tag == DT_NULL
331 || i++ > 100) { /* Arbitrary cut-off so that we
332 * don't loop forever if the
333 * binary is corrupted. */
334 debug(2, "Couldn't find address for dtag!");
335 return -1;
336 }
337
Joe Damato47cae1e2010-11-08 15:47:39 -0800338 if (entry.d_tag == d_tag) {
Petr Machataea8eb9a2012-04-17 01:32:07 +0200339 /* XXX The double cast should be removed when
340 * target_address_t becomes integral type. */
341 *ret = (target_address_t)(uintptr_t)entry.d_un.d_val;
Petr Machata8454bd72012-04-17 17:12:44 +0200342 debug(2, "found address: %p in dtag %d", *ret, d_tag);
Petr Machata17476b72012-02-23 18:50:37 +0100343 return 0;
Joe Damato47cae1e2010-11-08 15:47:39 -0800344 }
Petr Machata6a7997d2012-03-29 18:37:15 +0200345 }
346}
347
348/* Our own type for representing 32-bit linkmap. We can't rely on the
349 * definition in link.h, because that's only accurate for our host
350 * architecture, not for target architecture (where the traced process
351 * runs). */
352#define LT_LINK_MAP(BITS) \
353 { \
354 Elf##BITS##_Addr l_addr; \
355 Elf##BITS##_Addr l_name; \
356 Elf##BITS##_Addr l_ld; \
357 Elf##BITS##_Addr l_next; \
358 Elf##BITS##_Addr l_prev; \
359 }
360struct lt_link_map_32 LT_LINK_MAP(32);
361struct lt_link_map_64 LT_LINK_MAP(64);
362
363static int
364fetch_lm64(struct Process *proc, target_address_t addr,
365 struct lt_link_map_64 *ret)
366{
367 if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
368 return -1;
369 return 0;
370}
371
372static int
373fetch_lm32(struct Process *proc, target_address_t addr,
374 struct lt_link_map_64 *ret)
375{
376 struct lt_link_map_32 lm;
377 if (umovebytes(proc, addr, &lm, sizeof(lm)) != sizeof(lm))
378 return -1;
379
380 ret->l_addr = lm.l_addr;
381 ret->l_name = lm.l_name;
382 ret->l_ld = lm.l_ld;
383 ret->l_next = lm.l_next;
384 ret->l_prev = lm.l_prev;
385
386 return 0;
387}
388
389static int (*
390lm_fetcher(struct Process *proc))(struct Process *,
391 target_address_t, struct lt_link_map_64 *)
392{
393 return select_32_64(proc, fetch_lm32, fetch_lm64);
394}
395
396/* The same as above holds for struct r_debug. */
397#define LT_R_DEBUG(BITS) \
398 { \
399 int r_version; \
400 Elf##BITS##_Addr r_map; \
401 Elf##BITS##_Addr r_brk; \
402 int r_state; \
403 Elf##BITS##_Addr r_ldbase; \
Joe Damato47cae1e2010-11-08 15:47:39 -0800404 }
405
Petr Machata6a7997d2012-03-29 18:37:15 +0200406struct lt_r_debug_32 LT_R_DEBUG(32);
407struct lt_r_debug_64 LT_R_DEBUG(64);
408
409static int
410fetch_rd64(struct Process *proc, target_address_t addr,
411 struct lt_r_debug_64 *ret)
412{
413 if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
414 return -1;
415 return 0;
416}
417
418static int
419fetch_rd32(struct Process *proc, target_address_t addr,
420 struct lt_r_debug_64 *ret)
421{
422 struct lt_r_debug_32 rd;
423 if (umovebytes(proc, addr, &rd, sizeof(rd)) != sizeof(rd))
424 return -1;
425
426 ret->r_version = rd.r_version;
427 ret->r_map = rd.r_map;
428 ret->r_brk = rd.r_brk;
429 ret->r_state = rd.r_state;
430 ret->r_ldbase = rd.r_ldbase;
431
432 return 0;
433}
434
435static int (*
436rdebug_fetcher(struct Process *proc))(struct Process *,
437 target_address_t, struct lt_r_debug_64 *)
438{
439 return select_32_64(proc, fetch_rd32, fetch_rd64);
Joe Damato47cae1e2010-11-08 15:47:39 -0800440}
Joe Damatof0bd98b2010-11-08 15:47:42 -0800441
Joe Damatof0bd98b2010-11-08 15:47:42 -0800442static void
Petr Machata6a7997d2012-03-29 18:37:15 +0200443crawl_linkmap(struct Process *proc, struct lt_r_debug_64 *dbg)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100444{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800445 debug (DEBUG_FUNCTION, "crawl_linkmap()");
446
447 if (!dbg || !dbg->r_map) {
448 debug(2, "Debug structure or it's linkmap are NULL!");
449 return;
450 }
451
Petr Machataea8eb9a2012-04-17 01:32:07 +0200452 /* XXX The double cast should be removed when
453 * target_address_t becomes integral type. */
454 target_address_t addr = (target_address_t)(uintptr_t)dbg->r_map;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800455
Petr Machata6a7997d2012-03-29 18:37:15 +0200456 while (addr != 0) {
457 struct lt_link_map_64 rlm;
458 if (lm_fetcher(proc)(proc, addr, &rlm) < 0) {
Petr Machata74d80542012-04-03 12:40:59 +0200459 debug(2, "Unable to read link map");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800460 return;
461 }
462
Petr Machata89ac0392012-04-03 02:30:48 +0200463 target_address_t key = addr;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200464 /* XXX The double cast should be removed when
465 * target_address_t becomes integral type. */
466 addr = (target_address_t)(uintptr_t)rlm.l_next;
Petr Machata6a7997d2012-03-29 18:37:15 +0200467 if (rlm.l_name == 0) {
Petr Machata74d80542012-04-03 12:40:59 +0200468 debug(2, "Name of mapped library is NULL");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800469 return;
470 }
471
Petr Machata6a7997d2012-03-29 18:37:15 +0200472 char lib_name[BUFSIZ];
Petr Machataea8eb9a2012-04-17 01:32:07 +0200473 /* XXX The double cast should be removed when
474 * target_address_t becomes integral type. */
475 umovebytes(proc, (target_address_t)(uintptr_t)rlm.l_name,
Petr Machata6a7997d2012-03-29 18:37:15 +0200476 lib_name, sizeof(lib_name));
Joe Damatof0bd98b2010-11-08 15:47:42 -0800477
Petr Machata2b46cfc2012-02-18 11:17:29 +0100478 if (*lib_name == '\0') {
479 /* VDSO. No associated file, XXX but we might
480 * load it from the address space of the
481 * process. */
Joe Damatof0bd98b2010-11-08 15:47:42 -0800482 continue;
483 }
484
Petr Machata89ac0392012-04-03 02:30:48 +0200485 /* Do we have that library already? */
486 if (proc_each_library(proc, NULL, library_with_key_cb, &key))
487 continue;
488
Petr Machatab5f80ac2012-04-04 01:46:18 +0200489 struct library *lib = malloc(sizeof(*lib));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100490 if (lib == NULL) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200491 fail:
492 if (lib != NULL)
493 library_destroy(lib);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200494 fprintf(stderr, "Couldn't load ELF object %s: %s\n",
495 lib_name, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100496 continue;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800497 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200498 library_init(lib, LT_LIBTYPE_DSO);
499
500 if (ltelf_read_library(lib, proc, lib_name, rlm.l_addr) < 0)
501 goto fail;
502
Petr Machata89ac0392012-04-03 02:30:48 +0200503 lib->key = key;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100504 proc_add_library(proc, lib);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800505 }
506 return;
507}
508
Petr Machata029171f2012-04-05 02:14:30 +0200509/* A struct stored at proc->debug. */
510struct debug_struct
511{
512 target_address_t debug_addr;
513 int state;
514};
515
Petr Machata6a7997d2012-03-29 18:37:15 +0200516static int
517load_debug_struct(struct Process *proc, struct lt_r_debug_64 *ret)
518{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800519 debug(DEBUG_FUNCTION, "load_debug_struct");
520
Petr Machata029171f2012-04-05 02:14:30 +0200521 struct debug_struct *debug = proc->debug;
522
523 if (rdebug_fetcher(proc)(proc, debug->debug_addr, ret) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800524 debug(2, "This process does not have a debug structure!\n");
Petr Machata6a7997d2012-03-29 18:37:15 +0200525 return -1;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800526 }
527
Petr Machata6a7997d2012-03-29 18:37:15 +0200528 return 0;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800529}
530
531static void
Petr Machata12affff2012-03-29 18:33:03 +0200532rdebug_bp_on_hit(struct breakpoint *bp, struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100533{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800534 debug(DEBUG_FUNCTION, "arch_check_dbg");
535
Petr Machata6a7997d2012-03-29 18:37:15 +0200536 struct lt_r_debug_64 rdbg;
537 if (load_debug_struct(proc, &rdbg) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800538 debug(2, "Unable to load debug structure!");
539 return;
540 }
541
Petr Machata029171f2012-04-05 02:14:30 +0200542 struct debug_struct *debug = proc->debug;
Petr Machata6a7997d2012-03-29 18:37:15 +0200543 if (rdbg.r_state == RT_CONSISTENT) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800544 debug(2, "Linkmap is now consistent");
Petr Machata029171f2012-04-05 02:14:30 +0200545 if (debug->state == RT_ADD) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800546 debug(2, "Adding DSO to linkmap");
Petr Machata2b46cfc2012-02-18 11:17:29 +0100547 //data.proc = proc;
Petr Machata6a7997d2012-03-29 18:37:15 +0200548 crawl_linkmap(proc, &rdbg);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100549 //&data);
Petr Machata029171f2012-04-05 02:14:30 +0200550 } else if (debug->state == RT_DELETE) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800551 debug(2, "Removing DSO from linkmap");
552 } else {
553 debug(2, "Unexpected debug state!");
554 }
555 }
556
Petr Machata029171f2012-04-05 02:14:30 +0200557 debug->state = rdbg.r_state;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800558}
559
Joe Damatof0bd98b2010-11-08 15:47:42 -0800560int
Petr Machata52dbfb12012-03-29 16:38:26 +0200561linkmap_init(struct Process *proc, target_address_t dyn_addr)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100562{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800563 debug(DEBUG_FUNCTION, "linkmap_init()");
564
Petr Machata029171f2012-04-05 02:14:30 +0200565 struct debug_struct *debug = malloc(sizeof(*debug));
566 if (debug == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200567 fprintf(stderr, "couldn't allocate debug struct: %s\n",
568 strerror(errno));
Petr Machata89ac0392012-04-03 02:30:48 +0200569 fail:
Petr Machata029171f2012-04-05 02:14:30 +0200570 proc->debug = NULL;
571 free(debug);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800572 return -1;
573 }
Petr Machata029171f2012-04-05 02:14:30 +0200574 proc->debug = debug;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800575
Petr Machata029171f2012-04-05 02:14:30 +0200576 if (find_dynamic_entry_addr(proc, dyn_addr, DT_DEBUG,
577 &debug->debug_addr) == -1) {
578 debug(2, "Couldn't find debug structure!");
579 goto fail;
580 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800581
Petr Machata6a7997d2012-03-29 18:37:15 +0200582 int status;
583 struct lt_r_debug_64 rdbg;
584 if ((status = load_debug_struct(proc, &rdbg)) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800585 debug(2, "No debug structure or no memory to allocate one!");
Petr Machata6a7997d2012-03-29 18:37:15 +0200586 return status;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800587 }
588
Petr Machataea8eb9a2012-04-17 01:32:07 +0200589 /* XXX The double cast should be removed when
590 * target_address_t becomes integral type. */
591 target_address_t addr = (target_address_t)(uintptr_t)rdbg.r_brk;
Petr Machatab1492df2012-04-30 21:01:40 +0200592 if (arch_translate_address_dyn(proc, addr, &addr) < 0)
Petr Machata89ac0392012-04-03 02:30:48 +0200593 goto fail;
Petr Machata89ac0392012-04-03 02:30:48 +0200594
Petr Machata9df15012012-02-20 12:49:46 +0100595 struct breakpoint *rdebug_bp = insert_breakpoint(proc, addr, NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100596 static struct bp_callbacks rdebug_callbacks = {
Petr Machata12affff2012-03-29 18:33:03 +0200597 .on_hit = rdebug_bp_on_hit,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100598 };
599 rdebug_bp->cbs = &rdebug_callbacks;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800600
Petr Machata6a7997d2012-03-29 18:37:15 +0200601 crawl_linkmap(proc, &rdbg);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800602
Joe Damatof0bd98b2010-11-08 15:47:42 -0800603 return 0;
604}
Petr Machata9a5420c2011-07-09 11:21:23 +0200605
606int
607task_kill (pid_t pid, int sig)
608{
609 // Taken from GDB
610 int ret;
611
612 errno = 0;
613 ret = syscall (__NR_tkill, pid, sig);
614 return ret;
615}
Petr Machatacd972582012-01-07 03:02:07 +0100616
617void
618process_removed(struct Process *proc)
619{
620 delete_events_for(proc);
621}