blob: e7556f54fe5e02cf70a395d14c822b8109a7ef62 [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>
Petr Machataba1664b2012-04-28 14:59:05 +020038#include <stdlib.h>
Juan Cespedes1fe93d51998-03-13 00:29:21 +010039#include <string.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +010040#include <unistd.h>
Petr Machata9a5420c2011-07-09 11:21:23 +020041
Petr Machata64262602012-01-07 03:41:36 +010042#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020043#include "breakpoint.h"
44#include "config.h"
45#include "debug.h"
Petr Machatacd972582012-01-07 03:02:07 +010046#include "events.h"
Petr Machataba1664b2012-04-28 14:59:05 +020047#include "library.h"
48#include "ltrace-elf.h"
49#include "proc.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010050
51/* /proc/pid doesn't exist just after the fork, and sometimes `ltrace'
52 * couldn't open it to find the executable. So it may be necessary to
53 * have a bit delay
54 */
55
Ian Wienand2d45b1a2006-02-20 22:48:07 +010056#define MAX_DELAY 100000 /* 100000 microseconds = 0.1 seconds */
Juan Cespedes1fe93d51998-03-13 00:29:21 +010057
Petr Machata9a5420c2011-07-09 11:21:23 +020058#define PROC_PID_FILE(VAR, FORMAT, PID) \
59 char VAR[strlen(FORMAT) + 6]; \
60 sprintf(VAR, FORMAT, PID)
61
Juan Cespedes1fe93d51998-03-13 00:29:21 +010062/*
Juan Cespedese0660df2009-05-21 18:14:39 +020063 * Returns a (malloc'd) file name corresponding to a running pid
Juan Cespedes1fe93d51998-03-13 00:29:21 +010064 */
Juan Cespedesf1350522008-12-16 18:19:58 +010065char *
66pid2name(pid_t pid) {
Juan Cespedes1fe93d51998-03-13 00:29:21 +010067 if (!kill(pid, 0)) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010068 int delay = 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010069
Petr Machata9a5420c2011-07-09 11:21:23 +020070 PROC_PID_FILE(proc_exe, "/proc/%d/exe", pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010071
Ian Wienand2d45b1a2006-02-20 22:48:07 +010072 while (delay < MAX_DELAY) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010073 if (!access(proc_exe, F_OK)) {
74 return strdup(proc_exe);
75 }
76 delay += 1000; /* 1 milisecond */
77 }
Juan Cespedes1fe93d51998-03-13 00:29:21 +010078 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010079 return NULL;
Juan Cespedes1fe93d51998-03-13 00:29:21 +010080}
Joe Damato47cae1e2010-11-08 15:47:39 -080081
Petr Machata9a5420c2011-07-09 11:21:23 +020082static FILE *
83open_status_file(pid_t pid)
84{
85 PROC_PID_FILE(fn, "/proc/%d/status", pid);
86 /* Don't complain if we fail. This would typically happen
87 when the process is about to terminate, and these files are
88 not available anymore. This function is called from the
89 event loop, and we don't want to clutter the output just
90 because the process terminates. */
91 return fopen(fn, "r");
92}
93
94static char *
95find_line_starting(FILE * file, const char * prefix, size_t len)
96{
97 char * line = NULL;
98 size_t line_len = 0;
99 while (!feof(file)) {
100 if (getline(&line, &line_len, file) < 0)
101 return NULL;
102 if (strncmp(line, prefix, len) == 0)
103 return line;
104 }
105 return NULL;
106}
107
108static void
Petr Machata2b46cfc2012-02-18 11:17:29 +0100109each_line_starting(FILE *file, const char *prefix,
110 enum callback_status (*cb)(const char *line,
111 const char *prefix,
112 void *data),
113 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200114{
115 size_t len = strlen(prefix);
116 char * line;
117 while ((line = find_line_starting(file, prefix, len)) != NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100118 enum callback_status st = (*cb)(line, prefix, data);
Petr Machataba1664b2012-04-28 14:59:05 +0200119 free(line);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100120 if (st == CBS_STOP)
Petr Machata9a5420c2011-07-09 11:21:23 +0200121 return;
122 }
123}
124
Petr Machata2b46cfc2012-02-18 11:17:29 +0100125static enum callback_status
126process_leader_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200127{
128 pid_t * pidp = data;
129 *pidp = atoi(line + strlen(prefix));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100130 return CBS_STOP;
Petr Machata9a5420c2011-07-09 11:21:23 +0200131}
132
133pid_t
134process_leader(pid_t pid)
135{
Petr Machata1974dbc2011-08-19 18:58:01 +0200136 pid_t tgid = 0;
Petr Machata9a5420c2011-07-09 11:21:23 +0200137 FILE * file = open_status_file(pid);
138 if (file != NULL) {
139 each_line_starting(file, "Tgid:\t", &process_leader_cb, &tgid);
140 fclose(file);
141 }
142
143 return tgid;
144}
145
Petr Machata2b46cfc2012-02-18 11:17:29 +0100146static enum callback_status
147process_stopped_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200148{
149 char c = line[strlen(prefix)];
150 // t:tracing stop, T:job control stop
151 *(int *)data = (c == 't' || c == 'T');
Petr Machata2b46cfc2012-02-18 11:17:29 +0100152 return CBS_STOP;
Petr Machata9a5420c2011-07-09 11:21:23 +0200153}
154
155int
156process_stopped(pid_t pid)
157{
158 int is_stopped = -1;
159 FILE * file = open_status_file(pid);
160 if (file != NULL) {
161 each_line_starting(file, "State:\t", &process_stopped_cb,
162 &is_stopped);
163 fclose(file);
164 }
165 return is_stopped;
166}
167
Petr Machata2b46cfc2012-02-18 11:17:29 +0100168static enum callback_status
169process_status_cb(const char *line, const char *prefix, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200170{
Petr Machata617ff0b2011-10-06 14:23:24 +0200171 const char * status = line + strlen(prefix);
172 const char c = *status;
173
174#define RETURN(C) do { \
175 *(enum process_status *)data = C; \
Petr Machata2b46cfc2012-02-18 11:17:29 +0100176 return CBS_STOP; \
Petr Machata617ff0b2011-10-06 14:23:24 +0200177 } while (0)
178
179 switch (c) {
180 case 'Z': RETURN(ps_zombie);
181 case 't': RETURN(ps_tracing_stop);
Petr Machatacbe29c62011-09-27 02:27:58 +0200182 case 'T':
Petr Machata617ff0b2011-10-06 14:23:24 +0200183 /* This can be either "T (stopped)" or, for older
184 * kernels, "T (tracing stop)". */
185 if (!strcmp(status, "T (stopped)\n"))
186 RETURN(ps_stop);
187 else if (!strcmp(status, "T (tracing stop)\n"))
188 RETURN(ps_tracing_stop);
189 else {
190 fprintf(stderr, "Unknown process status: %s",
191 status);
192 RETURN(ps_stop); /* Some sort of stop
193 * anyway. */
194 }
Petr Machatacbe29c62011-09-27 02:27:58 +0200195 case 'D':
196 case 'S': RETURN(ps_sleeping);
Petr Machata617ff0b2011-10-06 14:23:24 +0200197 }
198
199 RETURN(ps_other);
200#undef RETURN
Petr Machata9a5420c2011-07-09 11:21:23 +0200201}
202
Petr Machata617ff0b2011-10-06 14:23:24 +0200203enum process_status
Petr Machata9a5420c2011-07-09 11:21:23 +0200204process_status(pid_t pid)
205{
Petr Machata617ff0b2011-10-06 14:23:24 +0200206 enum process_status ret = ps_invalid;
Petr Machata9a5420c2011-07-09 11:21:23 +0200207 FILE * file = open_status_file(pid);
208 if (file != NULL) {
209 each_line_starting(file, "State:\t", &process_status_cb, &ret);
210 fclose(file);
Petr Machata750ca8c2011-10-06 14:29:34 +0200211 if (ret == ps_invalid)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200212 fprintf(stderr, "process_status %d: %s", pid,
213 strerror(errno));
Petr Machata750ca8c2011-10-06 14:29:34 +0200214 } else
215 /* If the file is not present, the process presumably
216 * exited already. */
217 ret = ps_zombie;
218
Petr Machata9a5420c2011-07-09 11:21:23 +0200219 return ret;
220}
221
222static int
223all_digits(const char *str)
224{
225 while (isdigit(*str))
226 str++;
227 return !*str;
228}
229
230int
231process_tasks(pid_t pid, pid_t **ret_tasks, size_t *ret_n)
232{
233 PROC_PID_FILE(fn, "/proc/%d/task", pid);
234 DIR * d = opendir(fn);
235 if (d == NULL)
236 return -1;
237
Petr Machata9a5420c2011-07-09 11:21:23 +0200238 pid_t *tasks = NULL;
239 size_t n = 0;
240 size_t alloc = 0;
241
242 while (1) {
243 struct dirent entry;
244 struct dirent *result;
245 if (readdir_r(d, &entry, &result) != 0) {
246 free(tasks);
247 return -1;
248 }
249 if (result == NULL)
250 break;
251 if (result->d_type == DT_DIR && all_digits(result->d_name)) {
252 pid_t npid = atoi(result->d_name);
253 if (n >= alloc) {
254 alloc = alloc > 0 ? (2 * alloc) : 8;
255 pid_t *ntasks = realloc(tasks,
256 sizeof(*tasks) * alloc);
257 if (ntasks == NULL) {
258 free(tasks);
259 return -1;
260 }
261 tasks = ntasks;
262 }
263 if (n >= alloc)
264 abort();
265 tasks[n++] = npid;
266 }
267 }
268
269 closedir(d);
270
271 *ret_tasks = tasks;
272 *ret_n = n;
273 return 0;
274}
275
Petr Machata6a7997d2012-03-29 18:37:15 +0200276/* On native 64-bit system, we need to be careful when handling cross
277 * tracing. This select appropriate pointer depending on host and
278 * target architectures. XXX Really we should abstract this into the
279 * ABI object, as theorized about somewhere on pmachata/revamp
280 * branch. */
281static void *
282select_32_64(struct Process *proc, void *p32, void *p64)
283{
284 if (sizeof(long) == 4 || proc->mask_32bit)
285 return p32;
286 else
287 return p64;
288}
289
Joe Damato47cae1e2010-11-08 15:47:39 -0800290static int
Petr Machata6a7997d2012-03-29 18:37:15 +0200291fetch_dyn64(struct Process *proc, target_address_t *addr, Elf64_Dyn *ret)
292{
293 if (umovebytes(proc, *addr, ret, sizeof(*ret)) != sizeof(*ret))
294 return -1;
295 *addr += sizeof(*ret);
296 return 0;
297}
298
299static int
300fetch_dyn32(struct Process *proc, target_address_t *addr, Elf64_Dyn *ret)
301{
302 Elf32_Dyn dyn;
303 if (umovebytes(proc, *addr, &dyn, sizeof(dyn)) != sizeof(dyn))
304 return -1;
305
306 *addr += sizeof(dyn);
307 ret->d_tag = dyn.d_tag;
308 ret->d_un.d_val = dyn.d_un.d_val;
309
310 return 0;
311}
312
313static int (*
314dyn_fetcher(struct Process *proc))(struct Process *,
315 target_address_t *, Elf64_Dyn *)
316{
317 return select_32_64(proc, fetch_dyn32, fetch_dyn64);
318}
319
320static int
321find_dynamic_entry_addr(struct Process *proc, target_address_t src_addr,
322 int d_tag, target_address_t *ret)
323{
Joe Damato47cae1e2010-11-08 15:47:39 -0800324 debug(DEBUG_FUNCTION, "find_dynamic_entry()");
325
Petr Machata6a7997d2012-03-29 18:37:15 +0200326 if (ret == NULL || src_addr == 0 || d_tag < 0 || d_tag > DT_NUM)
Joe Damato47cae1e2010-11-08 15:47:39 -0800327 return -1;
Joe Damato47cae1e2010-11-08 15:47:39 -0800328
Petr Machata6a7997d2012-03-29 18:37:15 +0200329 int i = 0;
330 while (1) {
331 Elf64_Dyn entry;
332 if (dyn_fetcher(proc)(proc, &src_addr, &entry) < 0
333 || entry.d_tag == DT_NULL
334 || i++ > 100) { /* Arbitrary cut-off so that we
335 * don't loop forever if the
336 * binary is corrupted. */
337 debug(2, "Couldn't find address for dtag!");
338 return -1;
339 }
340
Joe Damato47cae1e2010-11-08 15:47:39 -0800341 if (entry.d_tag == d_tag) {
Petr Machataea8eb9a2012-04-17 01:32:07 +0200342 /* XXX The double cast should be removed when
343 * target_address_t becomes integral type. */
344 *ret = (target_address_t)(uintptr_t)entry.d_un.d_val;
Petr Machata8454bd72012-04-17 17:12:44 +0200345 debug(2, "found address: %p in dtag %d", *ret, d_tag);
Petr Machata17476b72012-02-23 18:50:37 +0100346 return 0;
Joe Damato47cae1e2010-11-08 15:47:39 -0800347 }
Petr Machata6a7997d2012-03-29 18:37:15 +0200348 }
349}
350
351/* Our own type for representing 32-bit linkmap. We can't rely on the
352 * definition in link.h, because that's only accurate for our host
353 * architecture, not for target architecture (where the traced process
354 * runs). */
355#define LT_LINK_MAP(BITS) \
356 { \
357 Elf##BITS##_Addr l_addr; \
358 Elf##BITS##_Addr l_name; \
359 Elf##BITS##_Addr l_ld; \
360 Elf##BITS##_Addr l_next; \
361 Elf##BITS##_Addr l_prev; \
362 }
363struct lt_link_map_32 LT_LINK_MAP(32);
364struct lt_link_map_64 LT_LINK_MAP(64);
365
366static int
367fetch_lm64(struct Process *proc, target_address_t addr,
368 struct lt_link_map_64 *ret)
369{
370 if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
371 return -1;
372 return 0;
373}
374
375static int
376fetch_lm32(struct Process *proc, target_address_t addr,
377 struct lt_link_map_64 *ret)
378{
379 struct lt_link_map_32 lm;
380 if (umovebytes(proc, addr, &lm, sizeof(lm)) != sizeof(lm))
381 return -1;
382
383 ret->l_addr = lm.l_addr;
384 ret->l_name = lm.l_name;
385 ret->l_ld = lm.l_ld;
386 ret->l_next = lm.l_next;
387 ret->l_prev = lm.l_prev;
388
389 return 0;
390}
391
392static int (*
393lm_fetcher(struct Process *proc))(struct Process *,
394 target_address_t, struct lt_link_map_64 *)
395{
396 return select_32_64(proc, fetch_lm32, fetch_lm64);
397}
398
399/* The same as above holds for struct r_debug. */
400#define LT_R_DEBUG(BITS) \
401 { \
402 int r_version; \
403 Elf##BITS##_Addr r_map; \
404 Elf##BITS##_Addr r_brk; \
405 int r_state; \
406 Elf##BITS##_Addr r_ldbase; \
Joe Damato47cae1e2010-11-08 15:47:39 -0800407 }
408
Petr Machata6a7997d2012-03-29 18:37:15 +0200409struct lt_r_debug_32 LT_R_DEBUG(32);
410struct lt_r_debug_64 LT_R_DEBUG(64);
411
412static int
413fetch_rd64(struct Process *proc, target_address_t addr,
414 struct lt_r_debug_64 *ret)
415{
416 if (umovebytes(proc, addr, ret, sizeof(*ret)) != sizeof(*ret))
417 return -1;
418 return 0;
419}
420
421static int
422fetch_rd32(struct Process *proc, target_address_t addr,
423 struct lt_r_debug_64 *ret)
424{
425 struct lt_r_debug_32 rd;
426 if (umovebytes(proc, addr, &rd, sizeof(rd)) != sizeof(rd))
427 return -1;
428
429 ret->r_version = rd.r_version;
430 ret->r_map = rd.r_map;
431 ret->r_brk = rd.r_brk;
432 ret->r_state = rd.r_state;
433 ret->r_ldbase = rd.r_ldbase;
434
435 return 0;
436}
437
438static int (*
439rdebug_fetcher(struct Process *proc))(struct Process *,
440 target_address_t, struct lt_r_debug_64 *)
441{
442 return select_32_64(proc, fetch_rd32, fetch_rd64);
Joe Damato47cae1e2010-11-08 15:47:39 -0800443}
Joe Damatof0bd98b2010-11-08 15:47:42 -0800444
Joe Damatof0bd98b2010-11-08 15:47:42 -0800445static void
Petr Machata6a7997d2012-03-29 18:37:15 +0200446crawl_linkmap(struct Process *proc, struct lt_r_debug_64 *dbg)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100447{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800448 debug (DEBUG_FUNCTION, "crawl_linkmap()");
449
450 if (!dbg || !dbg->r_map) {
451 debug(2, "Debug structure or it's linkmap are NULL!");
452 return;
453 }
454
Petr Machataea8eb9a2012-04-17 01:32:07 +0200455 /* XXX The double cast should be removed when
456 * target_address_t becomes integral type. */
457 target_address_t addr = (target_address_t)(uintptr_t)dbg->r_map;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800458
Petr Machata6a7997d2012-03-29 18:37:15 +0200459 while (addr != 0) {
460 struct lt_link_map_64 rlm;
461 if (lm_fetcher(proc)(proc, addr, &rlm) < 0) {
Petr Machata74d80542012-04-03 12:40:59 +0200462 debug(2, "Unable to read link map");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800463 return;
464 }
465
Petr Machata89ac0392012-04-03 02:30:48 +0200466 target_address_t key = addr;
Petr Machataea8eb9a2012-04-17 01:32:07 +0200467 /* XXX The double cast should be removed when
468 * target_address_t becomes integral type. */
469 addr = (target_address_t)(uintptr_t)rlm.l_next;
Petr Machata6a7997d2012-03-29 18:37:15 +0200470 if (rlm.l_name == 0) {
Petr Machata74d80542012-04-03 12:40:59 +0200471 debug(2, "Name of mapped library is NULL");
Joe Damatof0bd98b2010-11-08 15:47:42 -0800472 return;
473 }
474
Petr Machata6a7997d2012-03-29 18:37:15 +0200475 char lib_name[BUFSIZ];
Petr Machataea8eb9a2012-04-17 01:32:07 +0200476 /* XXX The double cast should be removed when
477 * target_address_t becomes integral type. */
478 umovebytes(proc, (target_address_t)(uintptr_t)rlm.l_name,
Petr Machata6a7997d2012-03-29 18:37:15 +0200479 lib_name, sizeof(lib_name));
Joe Damatof0bd98b2010-11-08 15:47:42 -0800480
Petr Machata2b46cfc2012-02-18 11:17:29 +0100481 if (*lib_name == '\0') {
482 /* VDSO. No associated file, XXX but we might
483 * load it from the address space of the
484 * process. */
Joe Damatof0bd98b2010-11-08 15:47:42 -0800485 continue;
486 }
487
Petr Machata89ac0392012-04-03 02:30:48 +0200488 /* Do we have that library already? */
489 if (proc_each_library(proc, NULL, library_with_key_cb, &key))
490 continue;
491
Petr Machatab5f80ac2012-04-04 01:46:18 +0200492 struct library *lib = malloc(sizeof(*lib));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100493 if (lib == NULL) {
Petr Machatab5f80ac2012-04-04 01:46:18 +0200494 fail:
495 if (lib != NULL)
496 library_destroy(lib);
Petr Machatacc0e1e42012-04-25 13:42:07 +0200497 fprintf(stderr, "Couldn't load ELF object %s: %s\n",
498 lib_name, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100499 continue;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800500 }
Petr Machatab5f80ac2012-04-04 01:46:18 +0200501 library_init(lib, LT_LIBTYPE_DSO);
502
503 if (ltelf_read_library(lib, proc, lib_name, rlm.l_addr) < 0)
504 goto fail;
505
Petr Machata89ac0392012-04-03 02:30:48 +0200506 lib->key = key;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100507 proc_add_library(proc, lib);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800508 }
509 return;
510}
511
Petr Machata029171f2012-04-05 02:14:30 +0200512/* A struct stored at proc->debug. */
513struct debug_struct
514{
515 target_address_t debug_addr;
516 int state;
517};
518
Petr Machata6a7997d2012-03-29 18:37:15 +0200519static int
520load_debug_struct(struct Process *proc, struct lt_r_debug_64 *ret)
521{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800522 debug(DEBUG_FUNCTION, "load_debug_struct");
523
Petr Machata029171f2012-04-05 02:14:30 +0200524 struct debug_struct *debug = proc->debug;
525
526 if (rdebug_fetcher(proc)(proc, debug->debug_addr, ret) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800527 debug(2, "This process does not have a debug structure!\n");
Petr Machata6a7997d2012-03-29 18:37:15 +0200528 return -1;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800529 }
530
Petr Machata6a7997d2012-03-29 18:37:15 +0200531 return 0;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800532}
533
534static void
Petr Machata12affff2012-03-29 18:33:03 +0200535rdebug_bp_on_hit(struct breakpoint *bp, struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100536{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800537 debug(DEBUG_FUNCTION, "arch_check_dbg");
538
Petr Machata6a7997d2012-03-29 18:37:15 +0200539 struct lt_r_debug_64 rdbg;
540 if (load_debug_struct(proc, &rdbg) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800541 debug(2, "Unable to load debug structure!");
542 return;
543 }
544
Petr Machata029171f2012-04-05 02:14:30 +0200545 struct debug_struct *debug = proc->debug;
Petr Machata6a7997d2012-03-29 18:37:15 +0200546 if (rdbg.r_state == RT_CONSISTENT) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800547 debug(2, "Linkmap is now consistent");
Petr Machata029171f2012-04-05 02:14:30 +0200548 if (debug->state == RT_ADD) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800549 debug(2, "Adding DSO to linkmap");
Petr Machata2b46cfc2012-02-18 11:17:29 +0100550 //data.proc = proc;
Petr Machata6a7997d2012-03-29 18:37:15 +0200551 crawl_linkmap(proc, &rdbg);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100552 //&data);
Petr Machata029171f2012-04-05 02:14:30 +0200553 } else if (debug->state == RT_DELETE) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800554 debug(2, "Removing DSO from linkmap");
555 } else {
556 debug(2, "Unexpected debug state!");
557 }
558 }
559
Petr Machata029171f2012-04-05 02:14:30 +0200560 debug->state = rdbg.r_state;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800561}
562
Joe Damatof0bd98b2010-11-08 15:47:42 -0800563int
Petr Machata52dbfb12012-03-29 16:38:26 +0200564linkmap_init(struct Process *proc, target_address_t dyn_addr)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100565{
Joe Damatof0bd98b2010-11-08 15:47:42 -0800566 debug(DEBUG_FUNCTION, "linkmap_init()");
567
Petr Machata029171f2012-04-05 02:14:30 +0200568 struct debug_struct *debug = malloc(sizeof(*debug));
569 if (debug == NULL) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200570 fprintf(stderr, "couldn't allocate debug struct: %s\n",
571 strerror(errno));
Petr Machata89ac0392012-04-03 02:30:48 +0200572 fail:
Petr Machata029171f2012-04-05 02:14:30 +0200573 proc->debug = NULL;
574 free(debug);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800575 return -1;
576 }
Petr Machata029171f2012-04-05 02:14:30 +0200577 proc->debug = debug;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800578
Petr Machata029171f2012-04-05 02:14:30 +0200579 if (find_dynamic_entry_addr(proc, dyn_addr, DT_DEBUG,
580 &debug->debug_addr) == -1) {
581 debug(2, "Couldn't find debug structure!");
582 goto fail;
583 }
Joe Damatof0bd98b2010-11-08 15:47:42 -0800584
Petr Machata6a7997d2012-03-29 18:37:15 +0200585 int status;
586 struct lt_r_debug_64 rdbg;
587 if ((status = load_debug_struct(proc, &rdbg)) < 0) {
Joe Damatof0bd98b2010-11-08 15:47:42 -0800588 debug(2, "No debug structure or no memory to allocate one!");
Petr Machata6a7997d2012-03-29 18:37:15 +0200589 return status;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800590 }
591
Petr Machataea8eb9a2012-04-17 01:32:07 +0200592 /* XXX The double cast should be removed when
593 * target_address_t becomes integral type. */
594 target_address_t addr = (target_address_t)(uintptr_t)rdbg.r_brk;
Petr Machatab1492df2012-04-30 21:01:40 +0200595 if (arch_translate_address_dyn(proc, addr, &addr) < 0)
Petr Machata89ac0392012-04-03 02:30:48 +0200596 goto fail;
Petr Machata89ac0392012-04-03 02:30:48 +0200597
Petr Machata9df15012012-02-20 12:49:46 +0100598 struct breakpoint *rdebug_bp = insert_breakpoint(proc, addr, NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100599 static struct bp_callbacks rdebug_callbacks = {
Petr Machata12affff2012-03-29 18:33:03 +0200600 .on_hit = rdebug_bp_on_hit,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100601 };
602 rdebug_bp->cbs = &rdebug_callbacks;
Joe Damatof0bd98b2010-11-08 15:47:42 -0800603
Petr Machata6a7997d2012-03-29 18:37:15 +0200604 crawl_linkmap(proc, &rdbg);
Joe Damatof0bd98b2010-11-08 15:47:42 -0800605
Joe Damatof0bd98b2010-11-08 15:47:42 -0800606 return 0;
607}
Petr Machata9a5420c2011-07-09 11:21:23 +0200608
609int
610task_kill (pid_t pid, int sig)
611{
612 // Taken from GDB
613 int ret;
614
615 errno = 0;
616 ret = syscall (__NR_tkill, pid, sig);
617 return ret;
618}
Petr Machatacd972582012-01-07 03:02:07 +0100619
620void
621process_removed(struct Process *proc)
622{
623 delete_events_for(proc);
624}