Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #if defined(HAVE_LIBUNWIND) |
| 4 | #include <libunwind.h> |
| 5 | #include <libunwind-ptrace.h> |
| 6 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 7 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <string.h> |
| 10 | #include <stdio.h> |
| 11 | #include <errno.h> |
| 12 | #include <stdlib.h> |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 13 | #include <assert.h> |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 14 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 15 | #include "common.h" |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 16 | |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 17 | Process * |
Petr Machata | c7585b6 | 2011-07-08 22:58:12 +0200 | [diff] [blame^] | 18 | open_program(char *filename, pid_t pid, int enable) { |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 19 | Process *proc; |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 20 | assert(pid != 0); |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 21 | proc = calloc(sizeof(Process), 1); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 22 | if (!proc) { |
| 23 | perror("malloc"); |
| 24 | exit(1); |
| 25 | } |
Juan Cespedes | e0660df | 2009-05-21 18:14:39 +0200 | [diff] [blame] | 26 | proc->filename = strdup(filename); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 27 | proc->breakpoints_enabled = -1; |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 28 | proc->pid = pid; |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 29 | #if defined(HAVE_LIBUNWIND) |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 30 | proc->unwind_priv = _UPT_create(pid); |
| 31 | proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0); |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 32 | #endif /* defined(HAVE_LIBUNWIND) */ |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 33 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 34 | |
| 35 | proc->next = list_of_processes; |
| 36 | list_of_processes = proc; |
Petr Machata | c7585b6 | 2011-07-08 22:58:12 +0200 | [diff] [blame^] | 37 | breakpoints_init(proc, enable); |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 38 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 39 | return proc; |
| 40 | } |
| 41 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 42 | void |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 43 | open_pid(pid_t pid) { |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 44 | Process *proc; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 45 | char *filename; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 46 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 47 | if (trace_pid(pid) < 0) { |
| 48 | fprintf(stderr, "Cannot attach to pid %u: %s\n", pid, |
| 49 | strerror(errno)); |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 53 | filename = pid2name(pid); |
| 54 | |
| 55 | if (!filename) { |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 56 | fprintf(stderr, "Cannot trace pid %u: %s\n", pid, |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 57 | strerror(errno)); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 58 | return; |
| 59 | } |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 60 | |
Petr Machata | c7585b6 | 2011-07-08 22:58:12 +0200 | [diff] [blame^] | 61 | proc = open_program(filename, pid, 1); |
Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 62 | continue_process(pid); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 63 | proc->breakpoints_enabled = 1; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 64 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 65 | |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 66 | Process * |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 67 | pid2proc(pid_t pid) { |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 68 | Process *tmp; |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 69 | |
| 70 | tmp = list_of_processes; |
| 71 | while (tmp) { |
| 72 | if (pid == tmp->pid) { |
| 73 | return tmp; |
| 74 | } |
| 75 | tmp = tmp->next; |
| 76 | } |
| 77 | return NULL; |
| 78 | } |