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