blob: 6c0017d8907a2f17b359e8521b05df6fba1df0eb [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
15struct process * open_program(char * filename)
16{
17 struct process * proc;
18 proc = malloc(sizeof(struct process));
19 if (!proc) {
20 perror("malloc");
21 exit(1);
22 }
23 proc->filename = filename;
24 proc->pid = 0;
25 proc->breakpoints_enabled = -1;
26 proc->current_syscall = -1;
27 proc->current_symbol = NULL;
28 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 }
54
55 proc->next = list_of_processes;
56 list_of_processes = proc;
57 return proc;
58}
59
60void open_pid(pid_t pid, int verbose)
61{
62 struct process * proc;
63 char * filename;
64
Juan Cespedes35d70631998-03-15 14:05:40 +010065 if (trace_pid(pid)<0) {
66#if 0
67 if (verbose) {
68#endif
69 fprintf(stderr, "Cannot attach to pid %u: %s\n", pid, strerror(errno));
70#if 0
71 }
72#endif
73 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}