aboutsummaryrefslogtreecommitdiff
path: root/process.c
blob: 71f9791bc00675fc53ad64b9e470207d4a2c7bed (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
#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;

static void detach_process(int pid);
static struct process * pid2proc(int pid);
static void process_child(struct process * current_process);

int execute_process(const char * file, char * const argv[])
{
	struct process * tmp;
	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);
	}

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

static 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)) {
		disable_all_breakpoints(pid);
		untrace_pid(pid);
	}
}

static 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;
}

void wait_for_child(void)
{
	int pid;
	int status;
	struct process * current_process;

	pid = wait4(-1, &status, 0, NULL);
	if (pid==-1) {
		if (errno == ECHILD) {
			if (opt_d>0) {
				send_line("No more children");
			}
			exit(0);
		}
		perror("wait4");
		exit(1);
	}
	current_process = pid2proc(pid);
	if (!current_process) {
		fprintf(stderr, "wrong pid %d ???\n", pid);
		exit(1);
	}
	if (!current_process->breakpoints_enabled) {
		if (opt_d>0) {
			send_line("Enabling breakpoints for pid %d...", pid);
		}
		enable_all_breakpoints(pid);
		current_process->breakpoints_enabled=1;
	}
	if (WIFEXITED(status)) {
		send_line("pid %u exited", pid);
		detach_process(pid);
		return;
	}
	if (WIFSIGNALED(status)) {
		send_line("--- %s (%s) ---", signal_name[WSTOPSIG(status)], strsignal(WSTOPSIG(status)));
		send_line("+++ killed by %s +++", signal_name[WSTOPSIG(status)]);
		detach_process(pid);
		return;
	}
	if (!WIFSTOPPED(status)) {
		send_line("pid %u ???", pid);
		exit(1);
	}
	if (WSTOPSIG(status) != SIGTRAP) {
		send_line("--- %s (%s) ---", signal_name[WSTOPSIG(status)], strsignal(WSTOPSIG(status)));
		continue_process(pid, WSTOPSIG(status));
		return;
	}
	process_child(current_process);
}

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

	pid = current_process->pid;
	eip = get_eip(pid);
	instruction_pointer = eip;

	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()", 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", 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);
	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) {
		send_line("pid %u stopped; continuing it...", pid);
		continue_process(pid, 0);
	}
}