blob: 3c0a9360aa27a9d647b89f86467b07f1d1092275 [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes273ea6d1998-03-14 23:02:40 +01005#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 Cespedes8cc1b9d2002-03-01 19:54:23 +010015struct process *
16open_program(char * filename) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010017 struct process * proc;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020018 struct library_symbol * sym;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010019 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 Cespedes5b3ffdf2001-07-02 00:52:45 +020027 proc->callstack_depth = 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010028 proc->breakpoint_being_enabled = NULL;
29 proc->next = NULL;
Juan Cespedes35d70631998-03-15 14:05:40 +010030 if (opt_L && filename) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010031 proc->list_of_symbols = read_elf(filename);
Juan Cespedesac3db291998-04-25 14:31:58 +020032 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 Cespedes273ea6d1998-03-14 23:02:40 +010051 } else {
52 proc->list_of_symbols = NULL;
53 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020054 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 Cespedes273ea6d1998-03-14 23:02:40 +010059
60 proc->next = list_of_processes;
61 list_of_processes = proc;
62 return proc;
63}
64
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010065void
66open_pid(pid_t pid, int verbose) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010067 struct process * proc;
68 char * filename;
69
Juan Cespedes35d70631998-03-15 14:05:40 +010070 if (trace_pid(pid)<0) {
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010071 fprintf(stderr, "Cannot attach to pid %u: %s\n", pid, strerror(errno));
Juan Cespedes35d70631998-03-15 14:05:40 +010072 return;
73 }
74
Juan Cespedes273ea6d1998-03-14 23:02:40 +010075 filename = pid2name(pid);
76
Juan Cespedes35d70631998-03-15 14:05:40 +010077#if 0
Juan Cespedes273ea6d1998-03-14 23:02:40 +010078 if (!filename) {
79 if (verbose) {
80 fprintf(stderr, "Cannot trace pid %u: %s\n", pid, strerror(errno));
81 }
82 return;
83 }
Juan Cespedes35d70631998-03-15 14:05:40 +010084#endif
Juan Cespedes273ea6d1998-03-14 23:02:40 +010085
86 proc = open_program(filename);
87 proc->pid = pid;
88}