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