blob: 294340df4799471743ac6c78952000df4bdb44c8 [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;
Juan Cespedescac15c32003-01-31 18:58:58 +010026 proc->breakpoints = NULL;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010027 proc->breakpoints_enabled = -1;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020028 proc->callstack_depth = 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010029 proc->breakpoint_being_enabled = NULL;
30 proc->next = NULL;
Juan Cespedes35d70631998-03-15 14:05:40 +010031 if (opt_L && filename) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010032 proc->list_of_symbols = read_elf(filename);
Juan Cespedesac3db291998-04-25 14:31:58 +020033 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 Cespedes273ea6d1998-03-14 23:02:40 +010052 } else {
53 proc->list_of_symbols = NULL;
54 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020055 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 Cespedes273ea6d1998-03-14 23:02:40 +010060
61 proc->next = list_of_processes;
62 list_of_processes = proc;
63 return proc;
64}
65
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010066void
67open_pid(pid_t pid, int verbose) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010068 struct process * proc;
69 char * filename;
70
Juan Cespedes35d70631998-03-15 14:05:40 +010071 if (trace_pid(pid)<0) {
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010072 fprintf(stderr, "Cannot attach to pid %u: %s\n", pid, strerror(errno));
Juan Cespedes35d70631998-03-15 14:05:40 +010073 return;
74 }
75
Juan Cespedes273ea6d1998-03-14 23:02:40 +010076 filename = pid2name(pid);
77
Juan Cespedes35d70631998-03-15 14:05:40 +010078#if 0
Juan Cespedes273ea6d1998-03-14 23:02:40 +010079 if (!filename) {
80 if (verbose) {
81 fprintf(stderr, "Cannot trace pid %u: %s\n", pid, strerror(errno));
82 }
83 return;
84 }
Juan Cespedes35d70631998-03-15 14:05:40 +010085#endif
Juan Cespedes273ea6d1998-03-14 23:02:40 +010086
87 proc = open_program(filename);
88 proc->pid = pid;
89}