proc.c revision 5b3ffdf2e696273d38434ff7b3c26349fff5a0ea
1#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
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
15struct process * open_program(char * filename)
16{
17	struct process * proc;
18	struct library_symbol * sym;
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;
27	proc->callstack_depth = 0;
28	proc->breakpoint_being_enabled = NULL;
29	proc->next = NULL;
30	if (opt_L && filename) {
31		proc->list_of_symbols = read_elf(filename);
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		}
51	} else {
52		proc->list_of_symbols = NULL;
53	}
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	}
59
60	proc->next = list_of_processes;
61	list_of_processes = proc;
62	return proc;
63}
64
65void open_pid(pid_t pid, int verbose)
66{
67	struct process * proc;
68	char * filename;
69
70	if (trace_pid(pid)<0) {
71#if 0
72		if (verbose) {
73#endif
74			fprintf(stderr, "Cannot attach to pid %u: %s\n", pid, strerror(errno));
75#if 0
76		}
77#endif
78		return;
79	}
80
81	filename = pid2name(pid);
82
83#if 0
84	if (!filename) {
85		if (verbose) {
86			fprintf(stderr, "Cannot trace pid %u: %s\n", pid, strerror(errno));
87		}
88		return;
89	}
90#endif
91
92	proc = open_program(filename);
93	proc->pid = pid;
94}
95