Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <string.h> |
| 7 | #include <stdio.h> |
| 8 | #include <errno.h> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | #include "ltrace.h" |
| 12 | #include "options.h" |
| 13 | #include "elf.h" |
| 14 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 15 | struct process *open_program(char *filename) |
| 16 | { |
| 17 | struct process *proc; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 18 | proc = malloc(sizeof(struct process)); |
| 19 | if (!proc) { |
| 20 | perror("malloc"); |
| 21 | exit(1); |
| 22 | } |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 23 | memset(proc, 0, sizeof(struct process)); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 24 | proc->filename = filename; |
| 25 | proc->pid = 0; |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 26 | proc->breakpoints = NULL; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 27 | proc->breakpoints_enabled = -1; |
Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 28 | proc->callstack_depth = 0; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 29 | proc->breakpoint_being_enabled = NULL; |
Juan Cespedes | 7186e2a | 2003-01-31 19:56:34 +0100 | [diff] [blame] | 30 | breakpoints_init(proc); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 31 | proc->next = NULL; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 32 | |
| 33 | proc->next = list_of_processes; |
| 34 | list_of_processes = proc; |
| 35 | return proc; |
| 36 | } |
| 37 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 38 | void open_pid(pid_t pid, int verbose) |
| 39 | { |
| 40 | struct process *proc; |
| 41 | char *filename; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 42 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 43 | if (trace_pid(pid) < 0) { |
| 44 | fprintf(stderr, "Cannot attach to pid %u: %s\n", pid, |
| 45 | strerror(errno)); |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 49 | filename = pid2name(pid); |
| 50 | |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 51 | #if 0 |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 52 | if (!filename) { |
| 53 | if (verbose) { |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 54 | fprintf(stderr, "Cannot trace pid %u: %s\n", pid, |
| 55 | strerror(errno)); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 56 | } |
| 57 | return; |
| 58 | } |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 59 | #endif |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 60 | |
| 61 | proc = open_program(filename); |
| 62 | proc->pid = pid; |
| 63 | } |