blob: 7ec05c3c061031c083942a5017ea300e0a9f0530 [file] [log] [blame]
Petr Machatae99af272012-10-26 00:29:52 +02001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2006,2007,2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2009 Juan Cespedes
5 * Copyright (C) 1998,2001,2002,2003,2007,2008,2009 Juan Cespedes
6 * Copyright (C) 2006 Ian Wienand
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
Juan Cespedesd44c6b81998-09-25 14:48:42 +020024#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +020025
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020026#include <assert.h>
Petr Machata2b46cfc2012-02-18 11:17:29 +010027#include <errno.h>
Petr Machataba1664b2012-04-28 14:59:05 +020028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020031
Juan Cespedesf1bfe202002-03-27 00:22:23 +010032#ifdef __powerpc__
33#include <sys/ptrace.h>
34#endif
35
Petr Machata64262602012-01-07 03:41:36 +010036#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020037#include "breakpoint.h"
38#include "debug.h"
39#include "library.h"
40#include "ltrace-elf.h"
41#include "proc.h"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020042
Petr Machatac67a6e62012-03-28 02:39:49 +020043#ifndef ARCH_HAVE_TRANSLATE_ADDRESS
44int
Petr Machata929bd572012-12-17 03:20:34 +010045arch_translate_address_dyn(struct process *proc,
Petr Machatabac2da52012-05-29 00:42:59 +020046 arch_addr_t addr, arch_addr_t *ret)
Petr Machatab1492df2012-04-30 21:01:40 +020047{
48 *ret = addr;
49 return 0;
50}
51
52struct ltelf;
53int
54arch_translate_address(struct ltelf *lte,
Petr Machatabac2da52012-05-29 00:42:59 +020055 arch_addr_t addr, arch_addr_t *ret)
Petr Machatac67a6e62012-03-28 02:39:49 +020056{
57 *ret = addr;
58 return 0;
59}
60#endif
61
Petr Machataa9fd8f42012-02-07 13:25:56 +010062void
Petr Machata929bd572012-12-17 03:20:34 +010063breakpoint_on_hit(struct breakpoint *bp, struct process *proc)
Petr Machataa9fd8f42012-02-07 13:25:56 +010064{
65 assert(bp != NULL);
66 if (bp->cbs != NULL && bp->cbs->on_hit != NULL)
Petr Machata55ac9322012-03-27 03:07:35 +020067 (bp->cbs->on_hit)(bp, proc);
68}
69
70void
Petr Machata929bd572012-12-17 03:20:34 +010071breakpoint_on_continue(struct breakpoint *bp, struct process *proc)
Petr Machata55ac9322012-03-27 03:07:35 +020072{
73 assert(bp != NULL);
74 if (bp->cbs != NULL && bp->cbs->on_continue != NULL)
75 (bp->cbs->on_continue)(bp, proc);
76 else
77 continue_after_breakpoint(proc, bp);
Petr Machataa9fd8f42012-02-07 13:25:56 +010078}
79
Petr Machata86d38282012-04-24 18:09:01 +020080void
Petr Machata929bd572012-12-17 03:20:34 +010081breakpoint_on_retract(struct breakpoint *bp, struct process *proc)
Petr Machata86d38282012-04-24 18:09:01 +020082{
83 assert(bp != NULL);
84 if (bp->cbs != NULL && bp->cbs->on_retract != NULL)
85 (bp->cbs->on_retract)(bp, proc);
86}
87
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020088/*****************************************************************************/
89
Petr Machata9294d822012-02-07 12:35:58 +010090struct breakpoint *
Petr Machatad7e4ca82012-11-28 03:38:47 +010091address2bpstruct(struct process *proc, arch_addr_t addr)
Petr Machatafed1e8d2012-02-07 02:06:29 +010092{
Petr Machata26627682011-07-08 18:15:32 +020093 assert(proc != NULL);
94 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020095 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020096 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Petr Machatad7e4ca82012-11-28 03:38:47 +010097
98 struct breakpoint **found
99 = DICT_FIND(proc->breakpoints, &addr, struct breakpoint *);
100 if (found == NULL)
101 return NULL;
102 return *found;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200103}
104
Petr Machata8cce1192012-03-25 01:37:19 +0100105#ifndef ARCH_HAVE_BREAKPOINT_DATA
Petr Machata2b46cfc2012-02-18 11:17:29 +0100106int
Petr Machata929bd572012-12-17 03:20:34 +0100107arch_breakpoint_init(struct process *proc, struct breakpoint *sbp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100108{
109 return 0;
110}
Petr Machata8cce1192012-03-25 01:37:19 +0100111
112void
113arch_breakpoint_destroy(struct breakpoint *sbp)
114{
115}
Petr Machatad3cc9882012-04-13 21:40:23 +0200116
117int
118arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp)
119{
120 return 0;
121}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100122#endif
123
Petr Machatad3cc9882012-04-13 21:40:23 +0200124static void
Petr Machata929bd572012-12-17 03:20:34 +0100125breakpoint_init_base(struct breakpoint *bp, struct process *proc,
Petr Machatabac2da52012-05-29 00:42:59 +0200126 arch_addr_t addr, struct library_symbol *libsym)
Petr Machatad3cc9882012-04-13 21:40:23 +0200127{
128 bp->cbs = NULL;
129 bp->addr = addr;
130 memset(bp->orig_value, 0, sizeof(bp->orig_value));
131 bp->enabled = 0;
132 bp->libsym = libsym;
133}
134
Petr Machata52dbfb12012-03-29 16:38:26 +0200135/* On second thought, I don't think we need PROC. All the translation
136 * (arch_translate_address in particular) should be doable using
137 * static lookups of various sections in the ELF file. We shouldn't
138 * need process for anything. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100139int
Petr Machata929bd572012-12-17 03:20:34 +0100140breakpoint_init(struct breakpoint *bp, struct process *proc,
Petr Machatabac2da52012-05-29 00:42:59 +0200141 arch_addr_t addr, struct library_symbol *libsym)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100142{
Petr Machatad3cc9882012-04-13 21:40:23 +0200143 breakpoint_init_base(bp, proc, addr, libsym);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100144 return arch_breakpoint_init(proc, bp);
145}
146
Petr Machata8cce1192012-03-25 01:37:19 +0100147void
Petr Machata55ac9322012-03-27 03:07:35 +0200148breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs)
149{
150 if (bp->cbs != NULL)
151 assert(bp->cbs == NULL);
152 bp->cbs = cbs;
153}
154
155void
Petr Machata8cce1192012-03-25 01:37:19 +0100156breakpoint_destroy(struct breakpoint *bp)
157{
158 if (bp == NULL)
159 return;
Petr Machata8cce1192012-03-25 01:37:19 +0100160 arch_breakpoint_destroy(bp);
161}
162
Petr Machatad3cc9882012-04-13 21:40:23 +0200163int
Petr Machata929bd572012-12-17 03:20:34 +0100164breakpoint_clone(struct breakpoint *retp, struct process *new_proc,
165 struct breakpoint *bp, struct process *old_proc)
Petr Machatad3cc9882012-04-13 21:40:23 +0200166{
Petr Machata165b5662012-10-27 19:23:12 +0200167 struct library_symbol *libsym = NULL;
168 if (bp->libsym != NULL) {
169 int rc = proc_find_symbol(new_proc, bp->libsym, NULL, &libsym);
170 assert(rc == 0);
Petr Machatad3cc9882012-04-13 21:40:23 +0200171 }
172
Petr Machatad3cc9882012-04-13 21:40:23 +0200173 breakpoint_init_base(retp, new_proc, bp->addr, libsym);
174 memcpy(retp->orig_value, bp->orig_value, sizeof(bp->orig_value));
175 retp->enabled = bp->enabled;
Petr Machatad3cc9882012-04-13 21:40:23 +0200176 if (arch_breakpoint_clone(retp, bp) < 0)
177 return -1;
178 breakpoint_set_callbacks(retp, bp->cbs);
179 return 0;
180}
181
Petr Machata52dbfb12012-03-29 16:38:26 +0200182int
Petr Machata929bd572012-12-17 03:20:34 +0100183breakpoint_turn_on(struct breakpoint *bp, struct process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200184{
Petr Machata52dbfb12012-03-29 16:38:26 +0200185 bp->enabled++;
186 if (bp->enabled == 1) {
Petr Machatafa0c5702012-04-13 18:43:40 +0200187 assert(proc->pid != 0);
188 enable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200189 }
190 return 0;
191}
192
193int
Petr Machata929bd572012-12-17 03:20:34 +0100194breakpoint_turn_off(struct breakpoint *bp, struct process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200195{
Petr Machata52dbfb12012-03-29 16:38:26 +0200196 bp->enabled--;
197 if (bp->enabled == 0)
Petr Machatafa0c5702012-04-13 18:43:40 +0200198 disable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200199 assert(bp->enabled >= 0);
200 return 0;
201}
202
Petr Machata9294d822012-02-07 12:35:58 +0100203struct breakpoint *
Petr Machatad7e4ca82012-11-28 03:38:47 +0100204insert_breakpoint(struct process *proc, arch_addr_t addr,
Petr Machata9df15012012-02-20 12:49:46 +0100205 struct library_symbol *libsym)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100206{
Petr Machata929bd572012-12-17 03:20:34 +0100207 struct process *leader = proc->leader;
Petr Machata9a5420c2011-07-09 11:21:23 +0200208
209 /* Only the group leader should be getting the breakpoints and
210 * thus have ->breakpoint initialized. */
211 assert(leader != NULL);
212 assert(leader->breakpoints != NULL);
213
Petr Machata050b0a62012-04-03 01:30:30 +0200214 debug(DEBUG_FUNCTION, "insert_breakpoint(pid=%d, addr=%p, symbol=%s)",
215 proc->pid, addr, libsym ? libsym->name : "NULL");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200216
Petr Machata218c5ff2012-04-15 04:22:39 +0200217 assert(addr != 0);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100218
Petr Machata52dbfb12012-03-29 16:38:26 +0200219 /* XXX what we need to do instead is have a list of
220 * breakpoints that are enabled at this address. The
221 * following works if every breakpoint is the same and there's
222 * no extra data, but that doesn't hold anymore. For now it
223 * will suffice, about the only realistic case where we need
224 * to have more than one breakpoint per address is return from
225 * a recursive library call. */
Petr Machatad7e4ca82012-11-28 03:38:47 +0100226 struct breakpoint **found
227 = DICT_FIND(leader->breakpoints, &addr, struct breakpoint *);
228 struct breakpoint *bp;
229 if (found == NULL) {
230 bp = malloc(sizeof(*bp));
231 if (bp == NULL
232 || breakpoint_init(bp, proc, addr, libsym) < 0) {
233 free(bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200234 return NULL;
235 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100236 if (proc_add_breakpoint(leader, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200237 fail:
Petr Machatad7e4ca82012-11-28 03:38:47 +0100238 breakpoint_destroy(bp);
239 free(bp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100240 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100241 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100242 } else {
243 bp = *found;
Juan Cespedescac15c32003-01-31 18:58:58 +0100244 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100245
Petr Machatad7e4ca82012-11-28 03:38:47 +0100246 if (breakpoint_turn_on(bp, proc) < 0) {
247 proc_remove_breakpoint(leader, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200248 goto fail;
Petr Machata45728772012-04-15 04:23:55 +0200249 }
Petr Machata9294d822012-02-07 12:35:58 +0100250
Petr Machatad7e4ca82012-11-28 03:38:47 +0100251 return bp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200252}
253
Juan Cespedesf1350522008-12-16 18:19:58 +0100254void
Petr Machatad7e4ca82012-11-28 03:38:47 +0100255delete_breakpoint(struct process *proc, arch_addr_t addr)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100256{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200257 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
258
Petr Machata929bd572012-12-17 03:20:34 +0100259 struct process *leader = proc->leader;
Petr Machata9a5420c2011-07-09 11:21:23 +0200260 assert(leader != NULL);
261
Petr Machatad7e4ca82012-11-28 03:38:47 +0100262 struct breakpoint **found
263 = DICT_FIND(leader->breakpoints, &addr, struct breakpoint *);
264 assert(found != NULL);
265 struct breakpoint *sbp = *found;
Petr Machataf7fee432012-04-19 17:00:53 +0200266 assert(sbp != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200267 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100268 if (sbp == NULL)
269 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200270
Petr Machatafa0c5702012-04-13 18:43:40 +0200271 if (breakpoint_turn_off(sbp, proc) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200272 fprintf(stderr, "Couldn't turn off the breakpoint %s@%p\n",
273 breakpoint_name(sbp), sbp->addr);
274 return;
275 }
Petr Machataf7fee432012-04-19 17:00:53 +0200276 if (sbp->enabled == 0) {
277 proc_remove_breakpoint(leader, sbp);
278 breakpoint_destroy(sbp);
279 free(sbp);
280 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200281}
282
Petr Machatae9aebd62012-03-25 01:38:53 +0100283const char *
284breakpoint_name(const struct breakpoint *bp)
285{
286 assert(bp != NULL);
287 return bp->libsym != NULL ? bp->libsym->name : NULL;
288}
289
Petr Machata52dbfb12012-03-29 16:38:26 +0200290struct library *
291breakpoint_library(const struct breakpoint *bp)
292{
293 assert(bp != NULL);
294 return bp->libsym != NULL ? bp->libsym->lib : NULL;
295}
296
Petr Machatad7e4ca82012-11-28 03:38:47 +0100297static enum callback_status
298enable_bp_cb(arch_addr_t *addr, struct breakpoint **bpp, void *data)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100299{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100300 struct process *proc = data;
301 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", proc->pid);
302 if ((*bpp)->enabled)
303 enable_breakpoint(proc, *bpp);
304 return CBS_CONT;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200305}
306
Juan Cespedesf1350522008-12-16 18:19:58 +0100307void
Petr Machata929bd572012-12-17 03:20:34 +0100308enable_all_breakpoints(struct process *proc)
Petr Machatabc373262012-02-07 23:31:15 +0100309{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200310 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100311
312 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100313 if (proc->breakpoints != NULL)
314 DICT_EACH(proc->breakpoints, arch_addr_t, struct breakpoint *,
315 NULL, enable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100316}
317
Petr Machatad7e4ca82012-11-28 03:38:47 +0100318static enum callback_status
319disable_bp_cb(arch_addr_t *addr, struct breakpoint **bpp, void *data)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100320{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100321 struct process *proc = data;
322 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", proc->pid);
323 if ((*bpp)->enabled)
324 disable_breakpoint(proc, *bpp);
325 return CBS_CONT;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200326}
327
Juan Cespedesf1350522008-12-16 18:19:58 +0100328void
Petr Machata929bd572012-12-17 03:20:34 +0100329disable_all_breakpoints(struct process *proc)
330{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200331 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200332 assert(proc->leader == proc);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100333 DICT_EACH(proc->breakpoints, arch_addr_t, struct breakpoint *,
334 NULL, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100335}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100336
Petr Machatad09d2402012-04-13 21:34:08 +0200337/* XXX This is not currently properly supported. On clone, this is
338 * just sliced. Hopefully at the point that clone is done, this
339 * breakpoint is not necessary anymore. If this use case ends up
340 * being important, we need to add a clone and destroy callbacks to
341 * breakpoints, and we should also probably drop arch_breakpoint_data
342 * so that we don't end up with two different customization mechanisms
343 * for one structure. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200344struct entry_breakpoint {
345 struct breakpoint super;
Petr Machatabac2da52012-05-29 00:42:59 +0200346 arch_addr_t dyn_addr;
Petr Machata52dbfb12012-03-29 16:38:26 +0200347};
348
Petr Machata02648a12012-02-07 13:44:54 +0100349static void
Petr Machata929bd572012-12-17 03:20:34 +0100350entry_breakpoint_on_hit(struct breakpoint *a, struct process *proc)
Petr Machata02648a12012-02-07 13:44:54 +0100351{
Petr Machata52dbfb12012-03-29 16:38:26 +0200352 struct entry_breakpoint *bp = (void *)a;
Petr Machata02648a12012-02-07 13:44:54 +0100353 if (proc == NULL || proc->leader == NULL)
354 return;
Petr Machatabac2da52012-05-29 00:42:59 +0200355 arch_addr_t dyn_addr = bp->dyn_addr;
Petr Machata3fd099b2012-04-03 02:25:42 +0200356 delete_breakpoint(proc, bp->super.addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200357 linkmap_init(proc, dyn_addr);
Petr Machata93d95df2012-04-17 05:16:19 +0200358 arch_dynlink_done(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200359}
360
361int
Petr Machata929bd572012-12-17 03:20:34 +0100362entry_breakpoint_init(struct process *proc,
Petr Machatabac2da52012-05-29 00:42:59 +0200363 struct entry_breakpoint *bp, arch_addr_t addr,
Petr Machata9a04d0e2012-03-29 16:50:38 +0200364 struct library *lib)
Petr Machata52dbfb12012-03-29 16:38:26 +0200365{
Petr Machata1c790252012-10-30 23:29:27 +0100366 assert(addr != 0);
367 int err = breakpoint_init(&bp->super, proc, addr, NULL);
368 if (err < 0)
Petr Machata52dbfb12012-03-29 16:38:26 +0200369 return err;
370
371 static struct bp_callbacks entry_callbacks = {
Petr Machata12affff2012-03-29 18:33:03 +0200372 .on_hit = entry_breakpoint_on_hit,
Petr Machata52dbfb12012-03-29 16:38:26 +0200373 };
374 bp->super.cbs = &entry_callbacks;
Petr Machata9a04d0e2012-03-29 16:50:38 +0200375 bp->dyn_addr = lib->dyn_addr;
Petr Machata52dbfb12012-03-29 16:38:26 +0200376 return 0;
Petr Machata02648a12012-02-07 13:44:54 +0100377}
378
Petr Machata1974dbc2011-08-19 18:58:01 +0200379int
Petr Machata929bd572012-12-17 03:20:34 +0100380breakpoints_init(struct process *proc)
Petr Machatac7585b62011-07-08 22:58:12 +0200381{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200382 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200383
Petr Machata2b46cfc2012-02-18 11:17:29 +0100384 /* XXX breakpoint dictionary should be initialized
385 * outside. Here we just put in breakpoints. */
386 assert(proc->breakpoints != NULL);
387
388 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200389 assert(proc->leader == proc);
390
Petr Machata807cdd82012-04-05 02:08:25 +0200391 /* N.B. the following used to be conditional on this, and
392 * maybe it still needs to be. */
393 assert(proc->filename != NULL);
394
395 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
396 struct entry_breakpoint *entry_bp = NULL;
397 int bp_state = 0;
398 int result = -1;
Andrey Zonov6bb42012013-02-14 12:32:06 +0100399 switch ((int)(lib != NULL)) {
Petr Machata807cdd82012-04-05 02:08:25 +0200400 fail:
Petr Machata807cdd82012-04-05 02:08:25 +0200401 switch (bp_state) {
402 case 2:
Petr Machataa2416362012-04-06 02:43:34 +0200403 proc_remove_library(proc, lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200404 proc_remove_breakpoint(proc, &entry_bp->super);
405 case 1:
406 breakpoint_destroy(&entry_bp->super);
Petr Machata1974dbc2011-08-19 18:58:01 +0200407 }
Petr Machataa2416362012-04-06 02:43:34 +0200408 library_destroy(lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200409 free(entry_bp);
410 case 0:
411 return result;
Petr Machata02648a12012-02-07 13:44:54 +0100412 }
413
Petr Machata807cdd82012-04-05 02:08:25 +0200414 entry_bp = malloc(sizeof(*entry_bp));
415 if (entry_bp == NULL
Petr Machata91c399c2012-05-15 12:17:51 +0200416 || (entry_breakpoint_init(proc, entry_bp,
417 lib->entry, lib)) < 0) {
418 fprintf(stderr,
419 "Couldn't initialize entry breakpoint for PID %d.\n"
420 "Some tracing events may be missed.\n", proc->pid);
421 free(entry_bp);
Petr Machata00928202012-04-07 01:14:24 +0200422
Petr Machata91c399c2012-05-15 12:17:51 +0200423 } else {
424 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200425
Petr Machata91c399c2012-05-15 12:17:51 +0200426 if ((result = proc_add_breakpoint(proc, &entry_bp->super)) < 0)
427 goto fail;
428 ++bp_state;
429
430 if ((result = breakpoint_turn_on(&entry_bp->super, proc)) < 0)
431 goto fail;
432 }
Petr Machataa2416362012-04-06 02:43:34 +0200433 proc_add_library(proc, lib);
434
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100435 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200436 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100437}