blob: 51833fe691616ff627dceea1b884eb58a61c38e3 [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 Machata44965c72012-04-06 19:59:20 +020052
Petr Machata2b46cfc2012-02-18 11:17:29 +010053static int
Petr Machata3d0c91c2012-04-14 02:37:38 +020054process_bare_init(struct Process *proc, const char *filename,
55 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010056{
Petr Machata3d0c91c2012-04-14 02:37:38 +020057 if (!was_exec) {
58 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020059
Petr Machata3d0c91c2012-04-14 02:37:38 +020060 proc->filename = strdup(filename);
61 if (proc->filename == NULL) {
62 fail:
63 free(proc->filename);
64 if (proc->breakpoints != NULL)
65 dict_clear(proc->breakpoints);
66 return -1;
67 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010068 }
69
70 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020071 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +020072 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +010073 if (proc->leader == NULL)
74 goto fail;
75
76 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +020077 proc->breakpoints = dict_init(target_address_hash,
78 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +010079 if (proc->breakpoints == NULL)
80 goto fail;
81 } else {
82 proc->breakpoints = NULL;
83 }
84
Joe Damatoab3b72c2010-10-31 00:21:53 -070085#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +020086 proc->unwind_priv = _UPT_create(pid);
87 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -070088#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -070089
Petr Machata2b46cfc2012-02-18 11:17:29 +010090 return 0;
91}
92
93static void
Petr Machata3d0c91c2012-04-14 02:37:38 +020094process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010095{
Petr Machata2b46cfc2012-02-18 11:17:29 +010096 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +020097 if (!was_exec) {
98 free(proc->filename);
99 remove_process(proc);
100 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100101}
102
Petr Machata3d0c91c2012-04-14 02:37:38 +0200103static int
104process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100105{
Petr Machata18bd8ff2012-04-10 04:32:39 +0200106 target_address_t entry;
107 target_address_t interp_bias;
108 if (process_get_entry(proc, &entry, &interp_bias) < 0) {
109 fprintf(stderr, "Couldn't get entry points of process %d\n",
Petr Machata2b46cfc2012-02-18 11:17:29 +0100110 proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100111 return -1;
112 }
113
Petr Machata3d0c91c2012-04-14 02:37:38 +0200114 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200115 fprintf(stderr, "failed to init breakpoints %d\n",
116 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200117 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200118 }
119
Petr Machata2b46cfc2012-02-18 11:17:29 +0100120 return 0;
121}
122
Petr Machata3d0c91c2012-04-14 02:37:38 +0200123int
124process_init(struct Process *proc, const char *filename, pid_t pid)
125{
126 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200127 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200128 fprintf(stderr, "failed to initialize process %d: %s\n",
129 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200130 return -1;
131 }
132
Petr Machata744f2552012-04-15 04:33:18 +0200133 if (arch_process_init(proc) < 0) {
134 process_bare_destroy(proc, 0);
135 goto fail;
136 }
137
Petr Machata218c5ff2012-04-15 04:22:39 +0200138 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200139 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200140 if (process_init_main(proc) < 0) {
141 process_bare_destroy(proc, 0);
142 goto fail;
143 }
144 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200145}
146
Petr Machata8ead1cd2012-04-24 18:13:09 +0200147static enum callback_status
148destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200149{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200150 breakpoint_destroy(bp);
151 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200152 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200153}
154
155static void
156private_process_destroy(struct Process *proc, int keep_filename)
157{
158 if (!keep_filename)
159 free(proc->filename);
160
Petr Machata8ead1cd2012-04-24 18:13:09 +0200161 /* Libraries and symbols. This is only relevant in
162 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200163 struct library *lib;
164 for (lib = proc->libraries; lib != NULL; ) {
165 struct library *next = lib->next;
166 library_destroy(lib);
167 free(lib);
168 lib = next;
169 }
170 proc->libraries = NULL;
171
172 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200173 if (proc->breakpoints != NULL) {
174 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
175 dict_clear(proc->breakpoints);
176 proc->breakpoints = NULL;
177 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200178}
179
180void
181process_destroy(struct Process *proc)
182{
183 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200184 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200185}
186
187int
188process_exec(struct Process *proc)
189{
Petr Machata744f2552012-04-15 04:33:18 +0200190 /* Call exec first, before we destroy the main state. */
191 if (arch_process_exec(proc) < 0)
192 return -1;
193
Petr Machata3d0c91c2012-04-14 02:37:38 +0200194 private_process_destroy(proc, 1);
195 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
196 return -1;
197 if (process_init_main(proc) < 0) {
198 process_bare_destroy(proc, 1);
199 return -1;
200 }
201 return 0;
202}
203
Petr Machata2b46cfc2012-02-18 11:17:29 +0100204struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200205open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100206{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100207 assert(pid != 0);
208 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200209 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200210 free(proc);
211 return NULL;
212 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100213 return proc;
214}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100215
Petr Machata2b46cfc2012-02-18 11:17:29 +0100216struct clone_single_bp_data {
217 struct Process *old_proc;
218 struct Process *new_proc;
219 int error;
220};
221
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222static void
223clone_single_bp(void *key, void *value, void *u)
224{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100225 struct breakpoint *bp = value;
226 struct clone_single_bp_data *data = u;
227
Petr Machatad3cc9882012-04-13 21:40:23 +0200228 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100229 struct breakpoint *clone = malloc(sizeof(*clone));
230 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200231 || breakpoint_clone(clone, data->new_proc,
232 bp, data->old_proc) < 0) {
233 fail:
234 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100235 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100236 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200237 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
238 breakpoint_destroy(clone);
239 goto fail;
240 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100241}
242
243int
244process_clone(struct Process *retp, struct Process *proc, pid_t pid)
245{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200246 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100247 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200248 fprintf(stderr, "failed to clone process %d->%d : %s\n",
249 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100250 return -1;
251 }
252
Petr Machatacf1679a2012-04-06 19:56:17 +0200253 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200254 retp->e_machine = proc->e_machine;
Petr Machatacf1679a2012-04-06 19:56:17 +0200255
Petr Machata2b46cfc2012-02-18 11:17:29 +0100256 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200257 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100258 return 0;
259
260 /* Clone symbols first so that we can clone and relink
261 * breakpoints. */
262 struct library *lib;
263 struct library **nlibp = &retp->libraries;
264 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
265 *nlibp = malloc(sizeof(**nlibp));
266 if (*nlibp == NULL
267 || library_clone(*nlibp, lib) < 0) {
268 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200269 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100270
271 /* Error when cloning. Unroll what was done. */
272 for (lib = retp->libraries; lib != NULL; ) {
273 struct library *next = lib->next;
274 library_destroy(lib);
275 free(lib);
276 lib = next;
277 }
278 goto fail;
279 }
280
281 nlibp = &(*nlibp)->next;
282 }
283
284 /* Now clone breakpoints. Symbol relinking is done in
285 * clone_single_bp. */
286 struct clone_single_bp_data data = {
287 .old_proc = proc,
288 .new_proc = retp,
289 .error = 0,
290 };
291 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
292
Petr Machataded6f972012-04-13 23:15:48 +0200293 /* And finally the call stack. */
294 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
295 retp->callstack_depth = proc->callstack_depth;
296
Petr Machata2b46cfc2012-02-18 11:17:29 +0100297 if (data.error < 0)
298 goto fail2;
299
Petr Machata744f2552012-04-15 04:33:18 +0200300 if (arch_process_clone(retp, proc) < 0)
301 goto fail2;
302
Petr Machata2b46cfc2012-02-18 11:17:29 +0100303 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100304}
305
Petr Machata3c516d52011-08-18 03:53:18 +0200306static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200307open_one_pid(pid_t pid)
308{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200309 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100310 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200311 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
312
Petr Machata1974dbc2011-08-19 18:58:01 +0200313 /* Get the filename first. Should the trace_pid fail, we can
314 * easily free it, untracing is more work. */
315 if ((filename = pid2name(pid)) == NULL
316 || trace_pid(pid) < 0) {
317 free(filename);
318 return -1;
319 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100320
Petr Machata75934ad2012-04-14 02:28:03 +0200321 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200322 if (proc == NULL)
323 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200324 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200325
Petr Machata1974dbc2011-08-19 18:58:01 +0200326 return 0;
327}
328
Petr Machata2b46cfc2012-02-18 11:17:29 +0100329static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200330start_one_pid(Process * proc, void * data)
331{
332 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100333 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100334}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100335
Petr Machata9a5420c2011-07-09 11:21:23 +0200336void
337open_pid(pid_t pid)
338{
339 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200340 /* If we are already tracing this guy, we should be seeing all
341 * his children via normal tracing route. */
342 if (pid2proc(pid) != NULL)
343 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200344
Petr Machata3c516d52011-08-18 03:53:18 +0200345 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200346 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200347 fprintf(stderr, "Cannot attach to pid %u: %s\n",
348 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200349 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200350 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200351 }
352
Petr Machata3c516d52011-08-18 03:53:18 +0200353 /* Now attach to all tasks that belong to that PID. There's a
354 * race between process_tasks and open_one_pid. So when we
355 * fail in open_one_pid below, we just do another round.
356 * Chances are that by then that PID will have gone away, and
357 * that's why we have seen the failure. The processes that we
358 * manage to open_one_pid are stopped, so we should eventually
359 * reach a point where process_tasks doesn't give any new
360 * processes (because there's nobody left to produce
361 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200362 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200363 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200364 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200365 pid_t *tasks;
366 size_t ntasks;
367 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200368
Petr Machata3c516d52011-08-18 03:53:18 +0200369 if (process_tasks(pid, &tasks, &ntasks) < 0) {
370 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
371 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100372 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200373 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200374
Petr Machata3c516d52011-08-18 03:53:18 +0200375 have_all = 1;
376 for (i = 0; i < ntasks; ++i)
377 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200378 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200379 have_all = 0;
380
Petr Machata9a5420c2011-07-09 11:21:23 +0200381 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200382
Petr Machata1974dbc2011-08-19 18:58:01 +0200383 if (have_all && old_ntasks == ntasks)
384 break;
385 old_ntasks = ntasks;
386 }
387
Petr Machata93d95df2012-04-17 05:16:19 +0200388 struct Process *leader = pid2proc(pid)->leader;
389
390 /* XXX Is there a way to figure out whether _start has
391 * actually already been hit? */
392 arch_dynlink_done(leader);
393
Petr Machata2f9b78e2012-04-16 21:08:54 +0200394 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200395 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200396}
397
Petr Machata2b46cfc2012-02-18 11:17:29 +0100398static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200399find_proc(Process * proc, void * data)
400{
401 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100402 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200403}
404
Juan Cespedesa8909f72009-04-28 20:02:41 +0200405Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100406pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200407 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
408}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100409
Petr Machatacebb8842011-07-09 11:14:11 +0200410static Process * list_of_processes = NULL;
411
Petr Machatacbe29c62011-09-27 02:27:58 +0200412static void
413unlist_process(Process * proc)
414{
415 Process *tmp;
416
417 if (list_of_processes == proc) {
418 list_of_processes = list_of_processes->next;
419 return;
420 }
421
422 for (tmp = list_of_processes; ; tmp = tmp->next) {
423 /* If the following assert fails, the process wasn't
424 * in the list. */
425 assert(tmp->next != NULL);
426
427 if (tmp->next == proc) {
428 tmp->next = tmp->next->next;
429 return;
430 }
431 }
432}
433
Petr Machata2b46cfc2012-02-18 11:17:29 +0100434struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100435each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100436 enum callback_status(*cb)(struct Process *proc, void *data),
437 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200438{
Petr Machata74132a42012-03-16 02:46:18 +0100439 struct Process *it = start_after == NULL ? list_of_processes
440 : start_after->next;
441
442 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200443 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100444 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100445 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200446 case CBS_FAIL:
447 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100448 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200449 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100450 case CBS_CONT:
451 break;
452 }
Petr Machatacebb8842011-07-09 11:14:11 +0200453 it = next;
454 }
455 return NULL;
456}
Petr Machata9a5420c2011-07-09 11:21:23 +0200457
458Process *
Petr Machata74132a42012-03-16 02:46:18 +0100459each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100460 enum callback_status(*cb)(struct Process *proc, void *data),
461 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200462{
Petr Machata74132a42012-03-16 02:46:18 +0100463 assert(proc != NULL);
464 struct Process *it = start_after == NULL ? proc->leader
465 : start_after->next;
466
Petr Machata9a5420c2011-07-09 11:21:23 +0200467 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100468 struct Process *leader = it->leader;
469 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200470 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100471 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100472 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200473 case CBS_FAIL:
474 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100475 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200476 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100477 case CBS_CONT:
478 break;
479 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200480 it = next;
481 }
482 }
483 return NULL;
484}
485
Petr Machata44965c72012-04-06 19:59:20 +0200486static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200487add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200488{
Petr Machata9a5420c2011-07-09 11:21:23 +0200489 Process ** leaderp = &list_of_processes;
490 if (proc->pid) {
491 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200492 if (tgid == 0)
493 /* Must have been terminated before we managed
494 * to fully attach. */
495 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200496 if (tgid == proc->pid)
497 proc->leader = proc;
498 else {
499 Process * leader = pid2proc(tgid);
500 proc->leader = leader;
501 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200502 leaderp = &leader->next;
503 }
504 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200505
506 if (!was_exec) {
507 proc->next = *leaderp;
508 *leaderp = proc;
509 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200510}
511
Petr Machatacbe29c62011-09-27 02:27:58 +0200512void
513change_process_leader(Process * proc, Process * leader)
514{
515 Process ** leaderp = &list_of_processes;
516 if (proc->leader == leader)
517 return;
518
519 assert(leader != NULL);
520 unlist_process(proc);
521 if (proc != leader)
522 leaderp = &leader->next;
523
524 proc->leader = leader;
525 proc->next = *leaderp;
526 *leaderp = proc;
527}
528
Petr Machata2b46cfc2012-02-18 11:17:29 +0100529static enum callback_status
530clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200531{
532 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
533 proc->pid, proc->leader->pid);
534 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100535 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200536}
537
Petr Machata69a03e62011-07-09 11:29:12 +0200538static enum ecb_status
539event_for_proc(Event * event, void * data)
540{
541 if (event->proc == data)
542 return ecb_deque;
543 else
544 return ecb_cont;
545}
546
547static void
548delete_events_for(Process * proc)
549{
550 Event * event;
551 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
552 free(event);
553}
554
Petr Machatacebb8842011-07-09 11:14:11 +0200555void
556remove_process(Process *proc)
557{
Petr Machatacebb8842011-07-09 11:14:11 +0200558 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
559
Petr Machata9a5420c2011-07-09 11:21:23 +0200560 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100561 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200562
Petr Machatacbe29c62011-09-27 02:27:58 +0200563 unlist_process(proc);
564 delete_events_for(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200565 process_destroy(proc);
566 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100567}
Petr Machata4007d742011-07-09 11:29:42 +0200568
569void
Petr Machata366c2f42012-02-09 19:34:36 +0100570install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200571{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200572 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200573 assert(proc->event_handler == NULL);
574 proc->event_handler = handler;
575}
576
577void
578destroy_event_handler(Process * proc)
579{
Petr Machata366c2f42012-02-09 19:34:36 +0100580 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200581 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200582 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200583 if (handler->destroy != NULL)
584 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200585 free(handler);
586 proc->event_handler = NULL;
587}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100588
589static enum callback_status
590breakpoint_for_symbol(struct library_symbol *libsym, void *data)
591{
592 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200593 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100594
Petr Machatad5e85562012-04-05 15:18:38 +0200595 /* If there is an artificial breakpoint on the same address,
596 * its libsym will be NULL, and we can smuggle our libsym
597 * there. That artificial breakpoint is there presumably for
598 * the callbacks, which we don't touch. If there is a real
599 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200600 * symbols and ignore extra symbol aliases.
601 *
602 * The other direction is more complicated and currently not
603 * supported. If a breakpoint has custom callbacks, it might
604 * be also custom-allocated, and we would really need to swap
605 * the two: delete the one now in the dictionary, swap values
606 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200607 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
608 libsym->enter_addr);
609 if (bp != NULL) {
610 assert(bp->libsym == NULL);
611 bp->libsym = libsym;
612 return CBS_CONT;
613 }
614
615 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200616 if (bp == NULL
617 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
618 fail:
619 free(bp);
620 return CBS_FAIL;
621 }
622 if (proc_add_breakpoint(proc, bp) < 0) {
623 breakpoint_destroy(bp);
624 goto fail;
625 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100626
Petr Machatafa0c5702012-04-13 18:43:40 +0200627 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200628 proc_remove_breakpoint(proc, bp);
629 breakpoint_destroy(bp);
630 goto fail;
631 }
632
Petr Machata2b46cfc2012-02-18 11:17:29 +0100633 return CBS_CONT;
634}
635
636void
637proc_add_library(struct Process *proc, struct library *lib)
638{
639 assert(lib->next == NULL);
640 lib->next = proc->libraries;
641 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200642 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
643 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100644
645 struct library_symbol *libsym = NULL;
646 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100647 proc)) != NULL)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200648 fprintf(stderr, "couldn't insert breakpoint for %s to %d: %s",
649 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100650}
651
652int
653proc_remove_library(struct Process *proc, struct library *lib)
654{
655 struct library **libp;
656 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
657 if (*libp == lib) {
658 *libp = lib->next;
659 return 0;
660 }
661 return -1;
662}
663
664struct library *
665proc_each_library(struct Process *proc, struct library *it,
666 enum callback_status (*cb)(struct Process *proc,
667 struct library *lib, void *data),
668 void *data)
669{
670 if (it == NULL)
671 it = proc->libraries;
672
673 while (it != NULL) {
674 struct library *next = it->next;
675
676 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200677 case CBS_FAIL:
678 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100679 case CBS_STOP:
680 return it;
681 case CBS_CONT:
682 break;
683 }
684
685 it = next;
686 }
687
688 return NULL;
689}
Petr Machata52dbfb12012-03-29 16:38:26 +0200690
Petr Machataf7fee432012-04-19 17:00:53 +0200691static void
692check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200693{
Petr Machata52dbfb12012-03-29 16:38:26 +0200694 /* Only the group leader should be getting the breakpoints and
695 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200696 assert(proc->leader != NULL);
697 assert(proc->leader == proc);
698 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200699}
Petr Machata52dbfb12012-03-29 16:38:26 +0200700
Petr Machataf7fee432012-04-19 17:00:53 +0200701int
702proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
703{
Petr Machatafa0c5702012-04-13 18:43:40 +0200704 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200705 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200706 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200707
Petr Machataa2416362012-04-06 02:43:34 +0200708 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200709 * assert, but that's not necessary right now. Read the
710 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200711 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200712
Petr Machatafa0c5702012-04-13 18:43:40 +0200713 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200714 fprintf(stderr,
715 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
716 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200717 return -1;
718 }
719
Petr Machata52dbfb12012-03-29 16:38:26 +0200720 return 0;
721}
722
Petr Machataf7fee432012-04-19 17:00:53 +0200723void
Petr Machata52dbfb12012-03-29 16:38:26 +0200724proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
725{
Petr Machataf7fee432012-04-19 17:00:53 +0200726 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
727 proc->pid, breakpoint_name(bp), bp->addr);
728 check_leader(proc);
729 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
730 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200731}
Petr Machatad3cc9882012-04-13 21:40:23 +0200732
733/* Dict doesn't support iteration restarts, so here's this contraption
734 * for now. XXX add restarts to dict. */
735struct each_breakpoint_data
736{
737 void *start;
738 void *end;
739 struct Process *proc;
740 enum callback_status (*cb)(struct Process *proc,
741 struct breakpoint *bp,
742 void *data);
743 void *cb_data;
744};
745
746static void
747each_breakpoint_cb(void *key, void *value, void *d)
748{
749 struct each_breakpoint_data *data = d;
750 if (data->end != NULL)
751 return;
752 if (data->start == key)
753 data->start = NULL;
754
755 if (data->start == NULL) {
756 switch (data->cb(data->proc, value, data->cb_data)) {
757 case CBS_FAIL:
758 /* XXX handle me */
759 case CBS_STOP:
760 data->end = key;
761 case CBS_CONT:
762 return;
763 }
764 }
765}
766
767void *
768proc_each_breakpoint(struct Process *proc, void *start,
769 enum callback_status (*cb)(struct Process *proc,
770 struct breakpoint *bp,
771 void *data), void *data)
772{
773 struct each_breakpoint_data dd = {
774 .start = start,
775 .proc = proc,
776 .cb = cb,
777 .cb_data = data,
778 };
779 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
780 return dd.end;
781}