blob: 7672ae804c0f85d537d08f630b3d02625dc78ba0 [file] [log] [blame]
Joe Damatoab3b72c2010-10-31 00:21:53 -07001#include "config.h"
2
3#if defined(HAVE_LIBUNWIND)
4#include <libunwind.h>
5#include <libunwind-ptrace.h>
6#endif /* defined(HAVE_LIBUNWIND) */
7
Juan Cespedes273ea6d1998-03-14 23:02:40 +01008#include <sys/types.h>
9#include <string.h>
10#include <stdio.h>
11#include <errno.h>
12#include <stdlib.h>
Petr Machata1b17dbf2011-07-08 19:22:52 +020013#include <assert.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +010014
Juan Cespedesf7281232009-06-25 16:11:21 +020015#include "common.h"
Petr Machata9294d822012-02-07 12:35:58 +010016#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010017#include "proc.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010018
Petr Machata744f2552012-04-15 04:33:18 +020019#ifndef ARCH_HAVE_PROCESS_DATA
20int
21arch_process_init(struct Process *proc)
22{
23 return 0;
24}
25
26void
27arch_process_destroy(struct Process *proc)
28{
29}
30
31int
32arch_process_clone(struct Process *retp, struct Process *proc)
33{
34 return 0;
35}
36
37int
38arch_process_exec(struct Process *proc)
39{
40 return 0;
41}
42#endif
43
Petr Machata93d95df2012-04-17 05:16:19 +020044#ifndef ARCH_HAVE_DYNLINK_DONE
45void
46arch_dynlink_done(struct Process *proc)
47{
48}
49#endif
50
Petr Machata3d0c91c2012-04-14 02:37:38 +020051static void add_process(struct Process *proc, int was_exec);
Petr Machata61686c22012-05-03 18:39:49 +020052static void unlist_process(struct Process *proc);
Petr Machata44965c72012-04-06 19:59:20 +020053
Petr Machata2b46cfc2012-02-18 11:17:29 +010054static int
Petr Machata3d0c91c2012-04-14 02:37:38 +020055process_bare_init(struct Process *proc, const char *filename,
56 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010057{
Petr Machata3d0c91c2012-04-14 02:37:38 +020058 if (!was_exec) {
59 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020060
Petr Machata3d0c91c2012-04-14 02:37:38 +020061 proc->filename = strdup(filename);
62 if (proc->filename == NULL) {
63 fail:
64 free(proc->filename);
65 if (proc->breakpoints != NULL)
66 dict_clear(proc->breakpoints);
67 return -1;
68 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010069 }
70
71 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020072 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +020073 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +010074 if (proc->leader == NULL)
75 goto fail;
76
77 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +020078 proc->breakpoints = dict_init(target_address_hash,
79 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +010080 if (proc->breakpoints == NULL)
81 goto fail;
82 } else {
83 proc->breakpoints = NULL;
84 }
85
Joe Damatoab3b72c2010-10-31 00:21:53 -070086#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +020087 proc->unwind_priv = _UPT_create(pid);
88 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -070089#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -070090
Petr Machata2b46cfc2012-02-18 11:17:29 +010091 return 0;
92}
93
94static void
Petr Machata3d0c91c2012-04-14 02:37:38 +020095process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010096{
Petr Machata2b46cfc2012-02-18 11:17:29 +010097 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +020098 if (!was_exec) {
99 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200100 unlist_process(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200101 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100102}
103
Petr Machata3d0c91c2012-04-14 02:37:38 +0200104static int
105process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100106{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200107 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200108 fprintf(stderr, "failed to init breakpoints %d\n",
109 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200110 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200111 }
112
Petr Machata2b46cfc2012-02-18 11:17:29 +0100113 return 0;
114}
115
Petr Machata3d0c91c2012-04-14 02:37:38 +0200116int
117process_init(struct Process *proc, const char *filename, pid_t pid)
118{
119 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200120 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200121 fprintf(stderr, "failed to initialize process %d: %s\n",
122 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200123 return -1;
124 }
125
Petr Machata744f2552012-04-15 04:33:18 +0200126 if (arch_process_init(proc) < 0) {
127 process_bare_destroy(proc, 0);
128 goto fail;
129 }
130
Petr Machata218c5ff2012-04-15 04:22:39 +0200131 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200132 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200133 if (process_init_main(proc) < 0) {
134 process_bare_destroy(proc, 0);
135 goto fail;
136 }
137 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200138}
139
Petr Machata8ead1cd2012-04-24 18:13:09 +0200140static enum callback_status
141destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200142{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200143 breakpoint_destroy(bp);
144 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200145 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200146}
147
148static void
149private_process_destroy(struct Process *proc, int keep_filename)
150{
151 if (!keep_filename)
152 free(proc->filename);
153
Petr Machata8ead1cd2012-04-24 18:13:09 +0200154 /* Libraries and symbols. This is only relevant in
155 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200156 struct library *lib;
157 for (lib = proc->libraries; lib != NULL; ) {
158 struct library *next = lib->next;
159 library_destroy(lib);
160 free(lib);
161 lib = next;
162 }
163 proc->libraries = NULL;
164
165 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200166 if (proc->breakpoints != NULL) {
167 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
168 dict_clear(proc->breakpoints);
169 proc->breakpoints = NULL;
170 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200171}
172
173void
174process_destroy(struct Process *proc)
175{
176 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200177 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200178}
179
180int
181process_exec(struct Process *proc)
182{
Petr Machata744f2552012-04-15 04:33:18 +0200183 /* Call exec first, before we destroy the main state. */
184 if (arch_process_exec(proc) < 0)
185 return -1;
186
Petr Machata3d0c91c2012-04-14 02:37:38 +0200187 private_process_destroy(proc, 1);
188 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
189 return -1;
190 if (process_init_main(proc) < 0) {
191 process_bare_destroy(proc, 1);
192 return -1;
193 }
194 return 0;
195}
196
Petr Machata2b46cfc2012-02-18 11:17:29 +0100197struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200198open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100199{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100200 assert(pid != 0);
201 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200202 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200203 free(proc);
204 return NULL;
205 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100206 return proc;
207}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100208
Petr Machata2b46cfc2012-02-18 11:17:29 +0100209struct clone_single_bp_data {
210 struct Process *old_proc;
211 struct Process *new_proc;
212 int error;
213};
214
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215static void
216clone_single_bp(void *key, void *value, void *u)
217{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100218 struct breakpoint *bp = value;
219 struct clone_single_bp_data *data = u;
220
Petr Machatad3cc9882012-04-13 21:40:23 +0200221 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222 struct breakpoint *clone = malloc(sizeof(*clone));
223 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200224 || breakpoint_clone(clone, data->new_proc,
225 bp, data->old_proc) < 0) {
226 fail:
227 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100228 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100229 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200230 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
231 breakpoint_destroy(clone);
232 goto fail;
233 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234}
235
236int
237process_clone(struct Process *retp, struct Process *proc, pid_t pid)
238{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200239 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100240 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200241 fprintf(stderr, "failed to clone process %d->%d : %s\n",
242 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100243 return -1;
244 }
245
Petr Machatacf1679a2012-04-06 19:56:17 +0200246 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200247 retp->e_machine = proc->e_machine;
Petr Machatacf1679a2012-04-06 19:56:17 +0200248
Petr Machata2b46cfc2012-02-18 11:17:29 +0100249 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200250 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251 return 0;
252
253 /* Clone symbols first so that we can clone and relink
254 * breakpoints. */
255 struct library *lib;
256 struct library **nlibp = &retp->libraries;
257 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
258 *nlibp = malloc(sizeof(**nlibp));
259 if (*nlibp == NULL
260 || library_clone(*nlibp, lib) < 0) {
261 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200262 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100263
264 /* Error when cloning. Unroll what was done. */
265 for (lib = retp->libraries; lib != NULL; ) {
266 struct library *next = lib->next;
267 library_destroy(lib);
268 free(lib);
269 lib = next;
270 }
271 goto fail;
272 }
273
274 nlibp = &(*nlibp)->next;
275 }
276
277 /* Now clone breakpoints. Symbol relinking is done in
278 * clone_single_bp. */
279 struct clone_single_bp_data data = {
280 .old_proc = proc,
281 .new_proc = retp,
282 .error = 0,
283 };
284 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
Petr Machata94078ec2012-01-05 18:07:02 +0100285 if (data.error < 0)
286 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100287
Petr Machataded6f972012-04-13 23:15:48 +0200288 /* And finally the call stack. */
289 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
290 retp->callstack_depth = proc->callstack_depth;
291
Petr Machata94078ec2012-01-05 18:07:02 +0100292 size_t i;
293 for (i = 0; i < retp->callstack_depth; ++i) {
294 struct value_dict *args = retp->callstack[i].arguments;
295 if (args != NULL) {
296 fail3:
297 struct value_dict *nargs = malloc(sizeof(*nargs));
298 fprintf(stderr, "{A:%p->%p}", args, nargs);
299 if (nargs == NULL
300 || val_dict_clone(nargs, args) < 0) {
301
302 int j;
303 for (j = 0; j < i; ++j) {
304 nargs = p->callstack[i].arguments;
305 val_dict_destroy(nargs);
306 free(nargs);
307 p->callstack[i].arguments = NULL;
308 }
309 goto fail2;
310 }
311 retp->callstack[i].arguments = nargs;
312 }
313 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100314
Petr Machata744f2552012-04-15 04:33:18 +0200315 if (arch_process_clone(retp, proc) < 0)
Petr Machata94078ec2012-01-05 18:07:02 +0100316 goto fail3;
Petr Machata744f2552012-04-15 04:33:18 +0200317
Petr Machata2b46cfc2012-02-18 11:17:29 +0100318 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100319}
320
Petr Machata3c516d52011-08-18 03:53:18 +0200321static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200322open_one_pid(pid_t pid)
323{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200324 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100325 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200326 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
327
Petr Machata1974dbc2011-08-19 18:58:01 +0200328 /* Get the filename first. Should the trace_pid fail, we can
329 * easily free it, untracing is more work. */
330 if ((filename = pid2name(pid)) == NULL
331 || trace_pid(pid) < 0) {
332 free(filename);
333 return -1;
334 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100335
Petr Machata75934ad2012-04-14 02:28:03 +0200336 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200337 if (proc == NULL)
338 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200339 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200340
Petr Machata1974dbc2011-08-19 18:58:01 +0200341 return 0;
342}
343
Petr Machata2b46cfc2012-02-18 11:17:29 +0100344static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200345start_one_pid(Process * proc, void * data)
346{
347 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100348 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100349}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100350
Petr Machata9a5420c2011-07-09 11:21:23 +0200351void
352open_pid(pid_t pid)
353{
354 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200355 /* If we are already tracing this guy, we should be seeing all
356 * his children via normal tracing route. */
357 if (pid2proc(pid) != NULL)
358 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200359
Petr Machata3c516d52011-08-18 03:53:18 +0200360 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200361 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200362 fprintf(stderr, "Cannot attach to pid %u: %s\n",
363 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200364 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200365 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200366 }
367
Petr Machata3c516d52011-08-18 03:53:18 +0200368 /* Now attach to all tasks that belong to that PID. There's a
369 * race between process_tasks and open_one_pid. So when we
370 * fail in open_one_pid below, we just do another round.
371 * Chances are that by then that PID will have gone away, and
372 * that's why we have seen the failure. The processes that we
373 * manage to open_one_pid are stopped, so we should eventually
374 * reach a point where process_tasks doesn't give any new
375 * processes (because there's nobody left to produce
376 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200377 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200378 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200379 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200380 pid_t *tasks;
381 size_t ntasks;
382 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200383
Petr Machata3c516d52011-08-18 03:53:18 +0200384 if (process_tasks(pid, &tasks, &ntasks) < 0) {
385 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
386 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100387 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200388 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200389
Petr Machata3c516d52011-08-18 03:53:18 +0200390 have_all = 1;
391 for (i = 0; i < ntasks; ++i)
392 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200393 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200394 have_all = 0;
395
Petr Machata9a5420c2011-07-09 11:21:23 +0200396 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200397
Petr Machata1974dbc2011-08-19 18:58:01 +0200398 if (have_all && old_ntasks == ntasks)
399 break;
400 old_ntasks = ntasks;
401 }
402
Petr Machata93d95df2012-04-17 05:16:19 +0200403 struct Process *leader = pid2proc(pid)->leader;
404
405 /* XXX Is there a way to figure out whether _start has
406 * actually already been hit? */
407 arch_dynlink_done(leader);
408
Petr Machata2f9b78e2012-04-16 21:08:54 +0200409 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200410 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200411}
412
Petr Machata2b46cfc2012-02-18 11:17:29 +0100413static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200414find_proc(Process * proc, void * data)
415{
416 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100417 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200418}
419
Juan Cespedesa8909f72009-04-28 20:02:41 +0200420Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100421pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200422 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
423}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100424
Petr Machatacebb8842011-07-09 11:14:11 +0200425static Process * list_of_processes = NULL;
426
Petr Machatacbe29c62011-09-27 02:27:58 +0200427static void
428unlist_process(Process * proc)
429{
430 Process *tmp;
431
432 if (list_of_processes == proc) {
433 list_of_processes = list_of_processes->next;
434 return;
435 }
436
437 for (tmp = list_of_processes; ; tmp = tmp->next) {
438 /* If the following assert fails, the process wasn't
439 * in the list. */
440 assert(tmp->next != NULL);
441
442 if (tmp->next == proc) {
443 tmp->next = tmp->next->next;
444 return;
445 }
446 }
447}
448
Petr Machata2b46cfc2012-02-18 11:17:29 +0100449struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100450each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100451 enum callback_status(*cb)(struct Process *proc, void *data),
452 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200453{
Petr Machata74132a42012-03-16 02:46:18 +0100454 struct Process *it = start_after == NULL ? list_of_processes
455 : start_after->next;
456
457 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200458 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100459 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100460 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200461 case CBS_FAIL:
462 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100463 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200464 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100465 case CBS_CONT:
466 break;
467 }
Petr Machatacebb8842011-07-09 11:14:11 +0200468 it = next;
469 }
470 return NULL;
471}
Petr Machata9a5420c2011-07-09 11:21:23 +0200472
473Process *
Petr Machata74132a42012-03-16 02:46:18 +0100474each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100475 enum callback_status(*cb)(struct Process *proc, void *data),
476 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200477{
Petr Machata74132a42012-03-16 02:46:18 +0100478 assert(proc != NULL);
479 struct Process *it = start_after == NULL ? proc->leader
480 : start_after->next;
481
Petr Machata9a5420c2011-07-09 11:21:23 +0200482 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100483 struct Process *leader = it->leader;
484 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200485 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100486 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100487 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200488 case CBS_FAIL:
489 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100490 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200491 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100492 case CBS_CONT:
493 break;
494 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200495 it = next;
496 }
497 }
498 return NULL;
499}
500
Petr Machata44965c72012-04-06 19:59:20 +0200501static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200502add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200503{
Petr Machata9a5420c2011-07-09 11:21:23 +0200504 Process ** leaderp = &list_of_processes;
505 if (proc->pid) {
506 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200507 if (tgid == 0)
508 /* Must have been terminated before we managed
509 * to fully attach. */
510 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200511 if (tgid == proc->pid)
512 proc->leader = proc;
513 else {
514 Process * leader = pid2proc(tgid);
515 proc->leader = leader;
516 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200517 leaderp = &leader->next;
518 }
519 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200520
521 if (!was_exec) {
522 proc->next = *leaderp;
523 *leaderp = proc;
524 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200525}
526
Petr Machatacbe29c62011-09-27 02:27:58 +0200527void
528change_process_leader(Process * proc, Process * leader)
529{
530 Process ** leaderp = &list_of_processes;
531 if (proc->leader == leader)
532 return;
533
534 assert(leader != NULL);
535 unlist_process(proc);
536 if (proc != leader)
537 leaderp = &leader->next;
538
539 proc->leader = leader;
540 proc->next = *leaderp;
541 *leaderp = proc;
542}
543
Petr Machata2b46cfc2012-02-18 11:17:29 +0100544static enum callback_status
545clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200546{
547 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
548 proc->pid, proc->leader->pid);
549 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100550 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200551}
552
Petr Machata69a03e62011-07-09 11:29:12 +0200553static enum ecb_status
554event_for_proc(Event * event, void * data)
555{
556 if (event->proc == data)
557 return ecb_deque;
558 else
559 return ecb_cont;
560}
561
562static void
563delete_events_for(Process * proc)
564{
565 Event * event;
566 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
567 free(event);
568}
569
Petr Machatacebb8842011-07-09 11:14:11 +0200570void
571remove_process(Process *proc)
572{
Petr Machatacebb8842011-07-09 11:14:11 +0200573 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
574
Petr Machata9a5420c2011-07-09 11:21:23 +0200575 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100576 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200577
Petr Machatacbe29c62011-09-27 02:27:58 +0200578 unlist_process(proc);
579 delete_events_for(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200580 process_destroy(proc);
581 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100582}
Petr Machata4007d742011-07-09 11:29:42 +0200583
584void
Petr Machata366c2f42012-02-09 19:34:36 +0100585install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200586{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200587 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200588 assert(proc->event_handler == NULL);
589 proc->event_handler = handler;
590}
591
592void
593destroy_event_handler(Process * proc)
594{
Petr Machata366c2f42012-02-09 19:34:36 +0100595 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200596 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200597 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200598 if (handler->destroy != NULL)
599 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200600 free(handler);
601 proc->event_handler = NULL;
602}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100603
604static enum callback_status
605breakpoint_for_symbol(struct library_symbol *libsym, void *data)
606{
607 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200608 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100609
Petr Machatad5e85562012-04-05 15:18:38 +0200610 /* If there is an artificial breakpoint on the same address,
611 * its libsym will be NULL, and we can smuggle our libsym
612 * there. That artificial breakpoint is there presumably for
613 * the callbacks, which we don't touch. If there is a real
614 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200615 * symbols and ignore extra symbol aliases.
616 *
617 * The other direction is more complicated and currently not
618 * supported. If a breakpoint has custom callbacks, it might
619 * be also custom-allocated, and we would really need to swap
620 * the two: delete the one now in the dictionary, swap values
621 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200622 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
623 libsym->enter_addr);
624 if (bp != NULL) {
625 assert(bp->libsym == NULL);
626 bp->libsym = libsym;
627 return CBS_CONT;
628 }
629
630 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200631 if (bp == NULL
632 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
633 fail:
634 free(bp);
635 return CBS_FAIL;
636 }
637 if (proc_add_breakpoint(proc, bp) < 0) {
638 breakpoint_destroy(bp);
639 goto fail;
640 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100641
Petr Machatafa0c5702012-04-13 18:43:40 +0200642 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200643 proc_remove_breakpoint(proc, bp);
644 breakpoint_destroy(bp);
645 goto fail;
646 }
647
Petr Machata2b46cfc2012-02-18 11:17:29 +0100648 return CBS_CONT;
649}
650
651void
652proc_add_library(struct Process *proc, struct library *lib)
653{
654 assert(lib->next == NULL);
655 lib->next = proc->libraries;
656 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200657 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
658 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100659
660 struct library_symbol *libsym = NULL;
661 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100662 proc)) != NULL)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200663 fprintf(stderr, "couldn't insert breakpoint for %s to %d: %s",
664 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100665}
666
667int
668proc_remove_library(struct Process *proc, struct library *lib)
669{
670 struct library **libp;
671 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
672 if (*libp == lib) {
673 *libp = lib->next;
674 return 0;
675 }
676 return -1;
677}
678
679struct library *
680proc_each_library(struct Process *proc, struct library *it,
681 enum callback_status (*cb)(struct Process *proc,
682 struct library *lib, void *data),
683 void *data)
684{
685 if (it == NULL)
686 it = proc->libraries;
687
688 while (it != NULL) {
689 struct library *next = it->next;
690
691 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200692 case CBS_FAIL:
693 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100694 case CBS_STOP:
695 return it;
696 case CBS_CONT:
697 break;
698 }
699
700 it = next;
701 }
702
703 return NULL;
704}
Petr Machata52dbfb12012-03-29 16:38:26 +0200705
Petr Machataf7fee432012-04-19 17:00:53 +0200706static void
707check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200708{
Petr Machata52dbfb12012-03-29 16:38:26 +0200709 /* Only the group leader should be getting the breakpoints and
710 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200711 assert(proc->leader != NULL);
712 assert(proc->leader == proc);
713 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200714}
Petr Machata52dbfb12012-03-29 16:38:26 +0200715
Petr Machataf7fee432012-04-19 17:00:53 +0200716int
717proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
718{
Petr Machatafa0c5702012-04-13 18:43:40 +0200719 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200720 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200721 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200722
Petr Machataa2416362012-04-06 02:43:34 +0200723 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200724 * assert, but that's not necessary right now. Read the
725 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200726 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200727
Petr Machatafa0c5702012-04-13 18:43:40 +0200728 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200729 fprintf(stderr,
730 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
731 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200732 return -1;
733 }
734
Petr Machata52dbfb12012-03-29 16:38:26 +0200735 return 0;
736}
737
Petr Machataf7fee432012-04-19 17:00:53 +0200738void
Petr Machata52dbfb12012-03-29 16:38:26 +0200739proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
740{
Petr Machataf7fee432012-04-19 17:00:53 +0200741 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
742 proc->pid, breakpoint_name(bp), bp->addr);
743 check_leader(proc);
744 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
745 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200746}
Petr Machatad3cc9882012-04-13 21:40:23 +0200747
748/* Dict doesn't support iteration restarts, so here's this contraption
749 * for now. XXX add restarts to dict. */
750struct each_breakpoint_data
751{
752 void *start;
753 void *end;
754 struct Process *proc;
755 enum callback_status (*cb)(struct Process *proc,
756 struct breakpoint *bp,
757 void *data);
758 void *cb_data;
759};
760
761static void
762each_breakpoint_cb(void *key, void *value, void *d)
763{
764 struct each_breakpoint_data *data = d;
765 if (data->end != NULL)
766 return;
767 if (data->start == key)
768 data->start = NULL;
769
770 if (data->start == NULL) {
771 switch (data->cb(data->proc, value, data->cb_data)) {
772 case CBS_FAIL:
773 /* XXX handle me */
774 case CBS_STOP:
775 data->end = key;
776 case CBS_CONT:
777 return;
778 }
779 }
780}
781
782void *
783proc_each_breakpoint(struct Process *proc, void *start,
784 enum callback_status (*cb)(struct Process *proc,
785 struct breakpoint *bp,
786 void *data), void *data)
787{
788 struct each_breakpoint_data dd = {
789 .start = start,
790 .proc = proc,
791 .cb = cb,
792 .cb_data = data,
793 };
794 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
795 return dd.end;
796}