blob: 5920e519ef12d99af127ceef8358afc997e56c64 [file] [log] [blame]
Juan Cespedes273ea6d1998-03-14 23:02:40 +01001#include <sys/types.h>
2#include <string.h>
3#include <stdio.h>
4#include <errno.h>
5#include <stdlib.h>
6
7#include "ltrace.h"
8#include "options.h"
9#include "elf.h"
10
11struct process * open_program(char * filename)
12{
13 struct process * proc;
14 proc = malloc(sizeof(struct process));
15 if (!proc) {
16 perror("malloc");
17 exit(1);
18 }
19 proc->filename = filename;
20 proc->pid = 0;
21 proc->breakpoints_enabled = -1;
22 proc->current_syscall = -1;
23 proc->current_symbol = NULL;
24 proc->breakpoint_being_enabled = NULL;
25 proc->next = NULL;
Juan Cespedes35d70631998-03-15 14:05:40 +010026 if (opt_L && filename) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010027 proc->list_of_symbols = read_elf(filename);
Juan Cespedesac3db291998-04-25 14:31:58 +020028 if (opt_e) {
29 struct library_symbol ** tmp1 = &(proc->list_of_symbols);
30 while(*tmp1) {
31 struct opt_e_t * tmp2 = opt_e;
32 int keep = !opt_e_enable;
33
34 while(tmp2) {
35 if (!strcmp((*tmp1)->name, tmp2->name)) {
36 keep = opt_e_enable;
37 }
38 tmp2 = tmp2->next;
39 }
40 if (!keep) {
41 *tmp1 = (*tmp1)->next;
42 } else {
43 tmp1 = &((*tmp1)->next);
44 }
45 }
46 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010047 } else {
48 proc->list_of_symbols = NULL;
49 }
50
51 proc->next = list_of_processes;
52 list_of_processes = proc;
53 return proc;
54}
55
56void open_pid(pid_t pid, int verbose)
57{
58 struct process * proc;
59 char * filename;
60
Juan Cespedes35d70631998-03-15 14:05:40 +010061 if (trace_pid(pid)<0) {
62#if 0
63 if (verbose) {
64#endif
65 fprintf(stderr, "Cannot attach to pid %u: %s\n", pid, strerror(errno));
66#if 0
67 }
68#endif
69 return;
70 }
71
Juan Cespedes273ea6d1998-03-14 23:02:40 +010072 filename = pid2name(pid);
73
Juan Cespedes35d70631998-03-15 14:05:40 +010074#if 0
Juan Cespedes273ea6d1998-03-14 23:02:40 +010075 if (!filename) {
76 if (verbose) {
77 fprintf(stderr, "Cannot trace pid %u: %s\n", pid, strerror(errno));
78 }
79 return;
80 }
Juan Cespedes35d70631998-03-15 14:05:40 +010081#endif
Juan Cespedes273ea6d1998-03-14 23:02:40 +010082
83 proc = open_program(filename);
84 proc->pid = pid;
85}