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 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 15 | struct process * |
| 16 | open_program(char * filename) { |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 17 | struct process * proc; |
Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 18 | struct library_symbol * sym; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 19 | proc = malloc(sizeof(struct process)); |
| 20 | if (!proc) { |
| 21 | perror("malloc"); |
| 22 | exit(1); |
| 23 | } |
| 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; |
| 30 | proc->next = NULL; |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 31 | if (opt_L && filename) { |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 32 | proc->list_of_symbols = read_elf(filename); |
Juan Cespedes | ac3db29 | 1998-04-25 14:31:58 +0200 | [diff] [blame] | 33 | if (opt_e) { |
| 34 | struct library_symbol ** tmp1 = &(proc->list_of_symbols); |
| 35 | while(*tmp1) { |
| 36 | struct opt_e_t * tmp2 = opt_e; |
| 37 | int keep = !opt_e_enable; |
| 38 | |
| 39 | while(tmp2) { |
| 40 | if (!strcmp((*tmp1)->name, tmp2->name)) { |
| 41 | keep = opt_e_enable; |
| 42 | } |
| 43 | tmp2 = tmp2->next; |
| 44 | } |
| 45 | if (!keep) { |
| 46 | *tmp1 = (*tmp1)->next; |
| 47 | } else { |
| 48 | tmp1 = &((*tmp1)->next); |
| 49 | } |
| 50 | } |
| 51 | } |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 52 | } else { |
| 53 | proc->list_of_symbols = NULL; |
| 54 | } |
Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 55 | sym = proc->list_of_symbols; |
| 56 | while (sym) { |
| 57 | insert_breakpoint(proc, sym->enter_addr); /* proc->pid==0 delays enabling. */ |
| 58 | sym = sym->next; |
| 59 | } |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 60 | |
| 61 | proc->next = list_of_processes; |
| 62 | list_of_processes = proc; |
| 63 | return proc; |
| 64 | } |
| 65 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 66 | void |
| 67 | open_pid(pid_t pid, int verbose) { |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 68 | struct process * proc; |
| 69 | char * filename; |
| 70 | |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 71 | if (trace_pid(pid)<0) { |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 72 | fprintf(stderr, "Cannot attach to pid %u: %s\n", pid, strerror(errno)); |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 76 | filename = pid2name(pid); |
| 77 | |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 78 | #if 0 |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 79 | if (!filename) { |
| 80 | if (verbose) { |
| 81 | fprintf(stderr, "Cannot trace pid %u: %s\n", pid, strerror(errno)); |
| 82 | } |
| 83 | return; |
| 84 | } |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 85 | #endif |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 86 | |
| 87 | proc = open_program(filename); |
| 88 | proc->pid = pid; |
| 89 | } |