aboutsummaryrefslogtreecommitdiff
path: root/process.c
blob: 4f557bed5e3e3e02ec6220fea45372caae9183b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/wait.h>

#include <asm/unistd.h>

#include "i386.h"
#include "ltrace.h"
#include "process.h"
#include "symbols.h"
#include "functions.h"
#include "syscall.h"
#include "signal.h"
#include "output.h"

struct process * list_of_processes = NULL;

unsigned int instruction_pointer;

int execute_process(const char * file, char * const argv[])
{
	int pid = fork();

	if (pid<0) {
		perror("fork");
		exit(1);
	} else if (!pid) {
		trace_me();
		execvp(file, argv);
		fprintf(stderr, "Can't execute \"%s\": %s\n", argv[1], sys_errlist[errno]);
		exit(1);
	}
	
	return pid;
}

struct process * attach_process(int pid)
{
	struct process * tmp;

	tmp = (struct process *)malloc(sizeof(struct process));
	tmp->pid = pid;
	tmp->syscall_number = -1;
	tmp->next = list_of_processes;
	list_of_processes = tmp;

	return tmp;
}

/*
 * TODO: All the breakpoints should be disabled.
 */
void detach_process(int pid)
{
	struct process *tmp, *tmp2;

	if (list_of_processes && (pid == list_of_processes->pid)) {
		tmp2 = list_of_processes->next;
		free(list_of_processes);
		list_of_processes = tmp2;
	} else {
		tmp = list_of_processes;
		while(tmp && tmp->next) {
			if (pid == tmp->next->pid) {
				tmp2 = tmp->next->next;
				free(tmp->next);
				tmp->next = tmp2;
			}
			tmp = tmp->next;
		}
	}
	if (!kill(pid,0)) {
		untrace_pid(pid);
	}
}

struct process * pid2proc(int pid)
{
	struct process * tmp;

	tmp = list_of_processes;
	while(tmp) {
		if (pid == tmp->pid) {
			return tmp;
		}
		tmp = tmp->next;
	}
	return NULL;
}

static void chld_handler();
void init_sighandler(void)
{
	signal(SIGCHLD, chld_handler);
}

static void chld_handler()
{
	int pid;
	unsigned long eip;
	int esp;
	int function_seen;
	int status;
	struct library_symbol * tmp = NULL;
	struct process * current_process;

	signal(SIGCHLD, chld_handler);

	pid = wait4(-1, &status, WNOHANG, NULL);
	if (!pid) {
		if (errno!=0) {
			perror("wait4");
			exit(1);
		}
		return;
	}
	if (pid==-1) {
		perror("wait4");
		exit(1);
	}
	current_process = pid2proc(pid);
	if (!current_process) {
		/*
 		* I should assume here that this process is new, so I
		* have to enable breakpoints
 		*/
		if (opt_d>0) {
			fprintf(output, "new process %d. Attaching it...\n", pid);
		}
		current_process = attach_process(pid);
		if (opt_d>0) {
			fprintf(output, "Enabling breakpoints for pid %d...\n", pid);
		}
		enable_all_breakpoints(pid);
	}
	if (WIFEXITED(status)) {
		fprintf(output, "pid %u exited\n", pid);
		detach_process(pid);
		return;
	}
	if (WIFSIGNALED(status)) {
		fprintf(output, "pid %u exited on signal %u\n", pid, WTERMSIG(status));
		detach_process(pid);
		return;
	}
	if (!WIFSTOPPED(status)) {
		fprintf(output, "pid %u ???\n", pid);
		exit(1);
	}
	eip = get_eip(pid);
	instruction_pointer = eip;
	if (WSTOPSIG(status) != SIGTRAP) {
		send_line("--- %s (%s) ---\n", signal_name[WSTOPSIG(status)], strsignal(WSTOPSIG(status)));
		continue_process(pid, WSTOPSIG(status));
		return;
	}
	if (opt_d>0) {
		fprintf(output, "<pid:%u> ", pid);
	}
	switch (type_of_stop(current_process, &status)) {
		case PROC_SYSCALL:
			if (status==__NR_fork) {
				disable_all_breakpoints(pid);
			}
			if (opt_S) {
				send_line("SYSCALL: %s()\n", syscall_list[status]);
			}
			continue_process(pid, 0);
			return;
		case PROC_SYSRET:
			if (status==__NR_fork) {
				enable_all_breakpoints(pid);
			}
			if (opt_S && (opt_d>0)) {
				send_line("SYSRET:  %u\n", status);
			}
			continue_process(pid, 0);
			return;
		case PROC_BREAKPOINT:
		default:
	}
	/* pid is breakpointed... */
	/* TODO: I may be here after a PTRACE_SINGLESTEP ... */
	esp = get_esp(pid);
	instruction_pointer = get_return(pid, esp);
	send_line("");
	tmp = library_symbols;
	function_seen = 0;
	while(tmp) {
		if (eip == tmp->addr) {
			function_seen = 1;
			print_function(tmp->name, pid, esp);
			continue_after_breakpoint(pid, eip, tmp->old_value, 0);
			break;
		}
		tmp = tmp->next;
	}
	if (!function_seen) {
		fprintf(output, "pid %u stopped; continuing it...\n", pid);
		continue_process(pid, 0);
	}
}