blob: 3e6d5cbfe6da6e22660b0149505100d34da69141 [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);
285
Petr Machataded6f972012-04-13 23:15:48 +0200286 /* And finally the call stack. */
287 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
288 retp->callstack_depth = proc->callstack_depth;
289
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290 if (data.error < 0)
291 goto fail2;
292
Petr Machata744f2552012-04-15 04:33:18 +0200293 if (arch_process_clone(retp, proc) < 0)
294 goto fail2;
295
Petr Machata2b46cfc2012-02-18 11:17:29 +0100296 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100297}
298
Petr Machata3c516d52011-08-18 03:53:18 +0200299static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200300open_one_pid(pid_t pid)
301{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200302 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100303 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200304 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
305
Petr Machata1974dbc2011-08-19 18:58:01 +0200306 /* Get the filename first. Should the trace_pid fail, we can
307 * easily free it, untracing is more work. */
308 if ((filename = pid2name(pid)) == NULL
309 || trace_pid(pid) < 0) {
310 free(filename);
311 return -1;
312 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100313
Petr Machata75934ad2012-04-14 02:28:03 +0200314 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200315 if (proc == NULL)
316 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200317 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200318
Petr Machata1974dbc2011-08-19 18:58:01 +0200319 return 0;
320}
321
Petr Machata2b46cfc2012-02-18 11:17:29 +0100322static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200323start_one_pid(Process * proc, void * data)
324{
325 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100326 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100327}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100328
Petr Machata9a5420c2011-07-09 11:21:23 +0200329void
330open_pid(pid_t pid)
331{
332 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200333 /* If we are already tracing this guy, we should be seeing all
334 * his children via normal tracing route. */
335 if (pid2proc(pid) != NULL)
336 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200337
Petr Machata3c516d52011-08-18 03:53:18 +0200338 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200339 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200340 fprintf(stderr, "Cannot attach to pid %u: %s\n",
341 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200342 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200343 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200344 }
345
Petr Machata3c516d52011-08-18 03:53:18 +0200346 /* Now attach to all tasks that belong to that PID. There's a
347 * race between process_tasks and open_one_pid. So when we
348 * fail in open_one_pid below, we just do another round.
349 * Chances are that by then that PID will have gone away, and
350 * that's why we have seen the failure. The processes that we
351 * manage to open_one_pid are stopped, so we should eventually
352 * reach a point where process_tasks doesn't give any new
353 * processes (because there's nobody left to produce
354 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200355 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200356 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200357 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200358 pid_t *tasks;
359 size_t ntasks;
360 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200361
Petr Machata3c516d52011-08-18 03:53:18 +0200362 if (process_tasks(pid, &tasks, &ntasks) < 0) {
363 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
364 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100365 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200366 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200367
Petr Machata3c516d52011-08-18 03:53:18 +0200368 have_all = 1;
369 for (i = 0; i < ntasks; ++i)
370 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200371 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200372 have_all = 0;
373
Petr Machata9a5420c2011-07-09 11:21:23 +0200374 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200375
Petr Machata1974dbc2011-08-19 18:58:01 +0200376 if (have_all && old_ntasks == ntasks)
377 break;
378 old_ntasks = ntasks;
379 }
380
Petr Machata93d95df2012-04-17 05:16:19 +0200381 struct Process *leader = pid2proc(pid)->leader;
382
383 /* XXX Is there a way to figure out whether _start has
384 * actually already been hit? */
385 arch_dynlink_done(leader);
386
Petr Machata2f9b78e2012-04-16 21:08:54 +0200387 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200388 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200389}
390
Petr Machata2b46cfc2012-02-18 11:17:29 +0100391static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200392find_proc(Process * proc, void * data)
393{
394 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100395 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200396}
397
Juan Cespedesa8909f72009-04-28 20:02:41 +0200398Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100399pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200400 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
401}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100402
Petr Machatacebb8842011-07-09 11:14:11 +0200403static Process * list_of_processes = NULL;
404
Petr Machatacbe29c62011-09-27 02:27:58 +0200405static void
406unlist_process(Process * proc)
407{
408 Process *tmp;
409
410 if (list_of_processes == proc) {
411 list_of_processes = list_of_processes->next;
412 return;
413 }
414
415 for (tmp = list_of_processes; ; tmp = tmp->next) {
416 /* If the following assert fails, the process wasn't
417 * in the list. */
418 assert(tmp->next != NULL);
419
420 if (tmp->next == proc) {
421 tmp->next = tmp->next->next;
422 return;
423 }
424 }
425}
426
Petr Machata2b46cfc2012-02-18 11:17:29 +0100427struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100428each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100429 enum callback_status(*cb)(struct Process *proc, void *data),
430 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200431{
Petr Machata74132a42012-03-16 02:46:18 +0100432 struct Process *it = start_after == NULL ? list_of_processes
433 : start_after->next;
434
435 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200436 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100437 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100438 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200439 case CBS_FAIL:
440 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100441 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200442 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100443 case CBS_CONT:
444 break;
445 }
Petr Machatacebb8842011-07-09 11:14:11 +0200446 it = next;
447 }
448 return NULL;
449}
Petr Machata9a5420c2011-07-09 11:21:23 +0200450
451Process *
Petr Machata74132a42012-03-16 02:46:18 +0100452each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100453 enum callback_status(*cb)(struct Process *proc, void *data),
454 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200455{
Petr Machata74132a42012-03-16 02:46:18 +0100456 assert(proc != NULL);
457 struct Process *it = start_after == NULL ? proc->leader
458 : start_after->next;
459
Petr Machata9a5420c2011-07-09 11:21:23 +0200460 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100461 struct Process *leader = it->leader;
462 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200463 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100464 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100465 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200466 case CBS_FAIL:
467 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100468 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200469 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100470 case CBS_CONT:
471 break;
472 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200473 it = next;
474 }
475 }
476 return NULL;
477}
478
Petr Machata44965c72012-04-06 19:59:20 +0200479static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200480add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200481{
Petr Machata9a5420c2011-07-09 11:21:23 +0200482 Process ** leaderp = &list_of_processes;
483 if (proc->pid) {
484 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200485 if (tgid == 0)
486 /* Must have been terminated before we managed
487 * to fully attach. */
488 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200489 if (tgid == proc->pid)
490 proc->leader = proc;
491 else {
492 Process * leader = pid2proc(tgid);
493 proc->leader = leader;
494 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200495 leaderp = &leader->next;
496 }
497 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200498
499 if (!was_exec) {
500 proc->next = *leaderp;
501 *leaderp = proc;
502 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200503}
504
Petr Machatacbe29c62011-09-27 02:27:58 +0200505void
506change_process_leader(Process * proc, Process * leader)
507{
508 Process ** leaderp = &list_of_processes;
509 if (proc->leader == leader)
510 return;
511
512 assert(leader != NULL);
513 unlist_process(proc);
514 if (proc != leader)
515 leaderp = &leader->next;
516
517 proc->leader = leader;
518 proc->next = *leaderp;
519 *leaderp = proc;
520}
521
Petr Machata2b46cfc2012-02-18 11:17:29 +0100522static enum callback_status
523clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200524{
525 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
526 proc->pid, proc->leader->pid);
527 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100528 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200529}
530
Petr Machata69a03e62011-07-09 11:29:12 +0200531static enum ecb_status
532event_for_proc(Event * event, void * data)
533{
534 if (event->proc == data)
535 return ecb_deque;
536 else
537 return ecb_cont;
538}
539
540static void
541delete_events_for(Process * proc)
542{
543 Event * event;
544 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
545 free(event);
546}
547
Petr Machatacebb8842011-07-09 11:14:11 +0200548void
549remove_process(Process *proc)
550{
Petr Machatacebb8842011-07-09 11:14:11 +0200551 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
552
Petr Machata9a5420c2011-07-09 11:21:23 +0200553 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100554 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200555
Petr Machatacbe29c62011-09-27 02:27:58 +0200556 unlist_process(proc);
557 delete_events_for(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200558 process_destroy(proc);
559 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100560}
Petr Machata4007d742011-07-09 11:29:42 +0200561
562void
Petr Machata366c2f42012-02-09 19:34:36 +0100563install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200564{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200565 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200566 assert(proc->event_handler == NULL);
567 proc->event_handler = handler;
568}
569
570void
571destroy_event_handler(Process * proc)
572{
Petr Machata366c2f42012-02-09 19:34:36 +0100573 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200574 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200575 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200576 if (handler->destroy != NULL)
577 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200578 free(handler);
579 proc->event_handler = NULL;
580}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100581
582static enum callback_status
583breakpoint_for_symbol(struct library_symbol *libsym, void *data)
584{
585 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200586 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100587
Petr Machatad5e85562012-04-05 15:18:38 +0200588 /* If there is an artificial breakpoint on the same address,
589 * its libsym will be NULL, and we can smuggle our libsym
590 * there. That artificial breakpoint is there presumably for
591 * the callbacks, which we don't touch. If there is a real
592 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200593 * symbols and ignore extra symbol aliases.
594 *
595 * The other direction is more complicated and currently not
596 * supported. If a breakpoint has custom callbacks, it might
597 * be also custom-allocated, and we would really need to swap
598 * the two: delete the one now in the dictionary, swap values
599 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200600 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
601 libsym->enter_addr);
602 if (bp != NULL) {
603 assert(bp->libsym == NULL);
604 bp->libsym = libsym;
605 return CBS_CONT;
606 }
607
608 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200609 if (bp == NULL
610 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
611 fail:
612 free(bp);
613 return CBS_FAIL;
614 }
615 if (proc_add_breakpoint(proc, bp) < 0) {
616 breakpoint_destroy(bp);
617 goto fail;
618 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100619
Petr Machatafa0c5702012-04-13 18:43:40 +0200620 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200621 proc_remove_breakpoint(proc, bp);
622 breakpoint_destroy(bp);
623 goto fail;
624 }
625
Petr Machata2b46cfc2012-02-18 11:17:29 +0100626 return CBS_CONT;
627}
628
629void
630proc_add_library(struct Process *proc, struct library *lib)
631{
632 assert(lib->next == NULL);
633 lib->next = proc->libraries;
634 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200635 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
636 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100637
638 struct library_symbol *libsym = NULL;
639 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100640 proc)) != NULL)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200641 fprintf(stderr, "couldn't insert breakpoint for %s to %d: %s",
642 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100643}
644
645int
646proc_remove_library(struct Process *proc, struct library *lib)
647{
648 struct library **libp;
649 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
650 if (*libp == lib) {
651 *libp = lib->next;
652 return 0;
653 }
654 return -1;
655}
656
657struct library *
658proc_each_library(struct Process *proc, struct library *it,
659 enum callback_status (*cb)(struct Process *proc,
660 struct library *lib, void *data),
661 void *data)
662{
663 if (it == NULL)
664 it = proc->libraries;
665
666 while (it != NULL) {
667 struct library *next = it->next;
668
669 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200670 case CBS_FAIL:
671 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100672 case CBS_STOP:
673 return it;
674 case CBS_CONT:
675 break;
676 }
677
678 it = next;
679 }
680
681 return NULL;
682}
Petr Machata52dbfb12012-03-29 16:38:26 +0200683
Petr Machataf7fee432012-04-19 17:00:53 +0200684static void
685check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200686{
Petr Machata52dbfb12012-03-29 16:38:26 +0200687 /* Only the group leader should be getting the breakpoints and
688 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200689 assert(proc->leader != NULL);
690 assert(proc->leader == proc);
691 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200692}
Petr Machata52dbfb12012-03-29 16:38:26 +0200693
Petr Machataf7fee432012-04-19 17:00:53 +0200694int
695proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
696{
Petr Machatafa0c5702012-04-13 18:43:40 +0200697 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200698 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200699 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200700
Petr Machataa2416362012-04-06 02:43:34 +0200701 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200702 * assert, but that's not necessary right now. Read the
703 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200704 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200705
Petr Machatafa0c5702012-04-13 18:43:40 +0200706 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200707 fprintf(stderr,
708 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
709 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200710 return -1;
711 }
712
Petr Machata52dbfb12012-03-29 16:38:26 +0200713 return 0;
714}
715
Petr Machataf7fee432012-04-19 17:00:53 +0200716void
Petr Machata52dbfb12012-03-29 16:38:26 +0200717proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
718{
Petr Machataf7fee432012-04-19 17:00:53 +0200719 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
720 proc->pid, breakpoint_name(bp), bp->addr);
721 check_leader(proc);
722 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
723 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200724}
Petr Machatad3cc9882012-04-13 21:40:23 +0200725
726/* Dict doesn't support iteration restarts, so here's this contraption
727 * for now. XXX add restarts to dict. */
728struct each_breakpoint_data
729{
730 void *start;
731 void *end;
732 struct Process *proc;
733 enum callback_status (*cb)(struct Process *proc,
734 struct breakpoint *bp,
735 void *data);
736 void *cb_data;
737};
738
739static void
740each_breakpoint_cb(void *key, void *value, void *d)
741{
742 struct each_breakpoint_data *data = d;
743 if (data->end != NULL)
744 return;
745 if (data->start == key)
746 data->start = NULL;
747
748 if (data->start == NULL) {
749 switch (data->cb(data->proc, value, data->cb_data)) {
750 case CBS_FAIL:
751 /* XXX handle me */
752 case CBS_STOP:
753 data->end = key;
754 case CBS_CONT:
755 return;
756 }
757 }
758}
759
760void *
761proc_each_breakpoint(struct Process *proc, void *start,
762 enum callback_status (*cb)(struct Process *proc,
763 struct breakpoint *bp,
764 void *data), void *data)
765{
766 struct each_breakpoint_data dd = {
767 .start = start,
768 .proc = proc,
769 .cb = cb,
770 .cb_data = data,
771 };
772 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
773 return dd.end;
774}