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 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame^] | 34 | add_process(proc); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 35 | |
Petr Machata | c7585b6 | 2011-07-08 22:58:12 +0200 | [diff] [blame] | 36 | breakpoints_init(proc, enable); |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 37 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 38 | return proc; |
| 39 | } |
| 40 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 41 | void |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 42 | open_pid(pid_t pid) { |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 43 | Process *proc; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 44 | char *filename; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 45 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 46 | if (trace_pid(pid) < 0) { |
| 47 | fprintf(stderr, "Cannot attach to pid %u: %s\n", pid, |
| 48 | strerror(errno)); |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 52 | filename = pid2name(pid); |
| 53 | |
| 54 | if (!filename) { |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 55 | fprintf(stderr, "Cannot trace pid %u: %s\n", pid, |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 56 | strerror(errno)); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 57 | return; |
| 58 | } |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 59 | |
Petr Machata | c7585b6 | 2011-07-08 22:58:12 +0200 | [diff] [blame] | 60 | proc = open_program(filename, pid, 1); |
Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 61 | continue_process(pid); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 62 | proc->breakpoints_enabled = 1; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 63 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 64 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame^] | 65 | static enum pcb_status |
| 66 | find_proc(Process * proc, void * data) |
| 67 | { |
| 68 | pid_t pid = (pid_t)(uintptr_t)data; |
| 69 | return proc->pid == pid ? pcb_stop : pcb_cont; |
| 70 | } |
| 71 | |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 72 | Process * |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 73 | pid2proc(pid_t pid) { |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame^] | 74 | return each_process(NULL, &find_proc, (void *)(uintptr_t)pid); |
| 75 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 76 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame^] | 77 | |
| 78 | static Process * list_of_processes = NULL; |
| 79 | |
| 80 | Process * |
| 81 | each_process(Process * proc, |
| 82 | enum pcb_status (* cb)(Process * proc, void * data), |
| 83 | void * data) |
| 84 | { |
| 85 | Process * it = proc ?: list_of_processes; |
| 86 | for (; it != NULL; ) { |
| 87 | /* Callback might call remove_process. */ |
| 88 | Process * next = it->next; |
| 89 | if ((*cb) (it, data) == pcb_stop) |
| 90 | return it; |
| 91 | it = next; |
| 92 | } |
| 93 | return NULL; |
| 94 | } |
| 95 | void |
| 96 | add_process(Process * proc) |
| 97 | { |
| 98 | proc->next = list_of_processes; |
| 99 | list_of_processes = proc; |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | remove_process(Process *proc) |
| 104 | { |
| 105 | Process *tmp, *tmp2; |
| 106 | |
| 107 | debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid); |
| 108 | |
| 109 | if (list_of_processes == proc) { |
| 110 | tmp = list_of_processes; |
| 111 | list_of_processes = list_of_processes->next; |
| 112 | free(tmp); |
| 113 | return; |
| 114 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 115 | tmp = list_of_processes; |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame^] | 116 | while (tmp->next) { |
| 117 | if (tmp->next == proc) { |
| 118 | tmp2 = tmp->next; |
| 119 | tmp->next = tmp->next->next; |
| 120 | free(tmp2); |
| 121 | return; |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 122 | } |
| 123 | tmp = tmp->next; |
| 124 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 125 | } |