blob: 3eee38b99861e6ec07be895b9fca736db33891ce [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02002
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02003#include <assert.h>
Petr Machata2b46cfc2012-02-18 11:17:29 +01004#include <errno.h>
Petr Machataba1664b2012-04-28 14:59:05 +02005#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +02008
Juan Cespedesf1bfe202002-03-27 00:22:23 +01009#ifdef __powerpc__
10#include <sys/ptrace.h>
11#endif
12
Petr Machata64262602012-01-07 03:41:36 +010013#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020014#include "breakpoint.h"
15#include "debug.h"
16#include "library.h"
17#include "ltrace-elf.h"
18#include "proc.h"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020019
Petr Machatac67a6e62012-03-28 02:39:49 +020020#ifndef ARCH_HAVE_TRANSLATE_ADDRESS
21int
Petr Machatab1492df2012-04-30 21:01:40 +020022arch_translate_address_dyn(struct Process *proc,
Petr Machatabac2da52012-05-29 00:42:59 +020023 arch_addr_t addr, arch_addr_t *ret)
Petr Machatab1492df2012-04-30 21:01:40 +020024{
25 *ret = addr;
26 return 0;
27}
28
29struct ltelf;
30int
31arch_translate_address(struct ltelf *lte,
Petr Machatabac2da52012-05-29 00:42:59 +020032 arch_addr_t addr, arch_addr_t *ret)
Petr Machatac67a6e62012-03-28 02:39:49 +020033{
34 *ret = addr;
35 return 0;
36}
37#endif
38
Petr Machataa9fd8f42012-02-07 13:25:56 +010039void
40breakpoint_on_hit(struct breakpoint *bp, struct Process *proc)
41{
42 assert(bp != NULL);
43 if (bp->cbs != NULL && bp->cbs->on_hit != NULL)
Petr Machata55ac9322012-03-27 03:07:35 +020044 (bp->cbs->on_hit)(bp, proc);
45}
46
47void
48breakpoint_on_continue(struct breakpoint *bp, struct Process *proc)
49{
50 assert(bp != NULL);
51 if (bp->cbs != NULL && bp->cbs->on_continue != NULL)
52 (bp->cbs->on_continue)(bp, proc);
53 else
54 continue_after_breakpoint(proc, bp);
Petr Machataa9fd8f42012-02-07 13:25:56 +010055}
56
Petr Machata86d38282012-04-24 18:09:01 +020057void
58breakpoint_on_retract(struct breakpoint *bp, struct Process *proc)
59{
60 assert(bp != NULL);
61 if (bp->cbs != NULL && bp->cbs->on_retract != NULL)
62 (bp->cbs->on_retract)(bp, proc);
63}
64
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020065/*****************************************************************************/
66
Petr Machata9294d822012-02-07 12:35:58 +010067struct breakpoint *
Petr Machatafed1e8d2012-02-07 02:06:29 +010068address2bpstruct(Process *proc, void *addr)
69{
Petr Machata26627682011-07-08 18:15:32 +020070 assert(proc != NULL);
71 assert(proc->breakpoints != NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +020072 assert(proc->leader == proc);
Juan Cespedescd8976d2009-05-14 13:47:58 +020073 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
Juan Cespedescac15c32003-01-31 18:58:58 +010074 return dict_find_entry(proc->breakpoints, addr);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020075}
76
Petr Machata8cce1192012-03-25 01:37:19 +010077#ifndef ARCH_HAVE_BREAKPOINT_DATA
Petr Machata2b46cfc2012-02-18 11:17:29 +010078int
79arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp)
80{
81 return 0;
82}
Petr Machata8cce1192012-03-25 01:37:19 +010083
84void
85arch_breakpoint_destroy(struct breakpoint *sbp)
86{
87}
Petr Machatad3cc9882012-04-13 21:40:23 +020088
89int
90arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp)
91{
92 return 0;
93}
Petr Machata2b46cfc2012-02-18 11:17:29 +010094#endif
95
Petr Machatad3cc9882012-04-13 21:40:23 +020096static void
97breakpoint_init_base(struct breakpoint *bp, struct Process *proc,
Petr Machatabac2da52012-05-29 00:42:59 +020098 arch_addr_t addr, struct library_symbol *libsym)
Petr Machatad3cc9882012-04-13 21:40:23 +020099{
100 bp->cbs = NULL;
101 bp->addr = addr;
102 memset(bp->orig_value, 0, sizeof(bp->orig_value));
103 bp->enabled = 0;
104 bp->libsym = libsym;
105}
106
Petr Machata52dbfb12012-03-29 16:38:26 +0200107/* On second thought, I don't think we need PROC. All the translation
108 * (arch_translate_address in particular) should be doable using
109 * static lookups of various sections in the ELF file. We shouldn't
110 * need process for anything. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100111int
112breakpoint_init(struct breakpoint *bp, struct Process *proc,
Petr Machatabac2da52012-05-29 00:42:59 +0200113 arch_addr_t addr, struct library_symbol *libsym)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100114{
Petr Machatad3cc9882012-04-13 21:40:23 +0200115 breakpoint_init_base(bp, proc, addr, libsym);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100116 return arch_breakpoint_init(proc, bp);
117}
118
Petr Machata8cce1192012-03-25 01:37:19 +0100119void
Petr Machata55ac9322012-03-27 03:07:35 +0200120breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs)
121{
122 if (bp->cbs != NULL)
123 assert(bp->cbs == NULL);
124 bp->cbs = cbs;
125}
126
127void
Petr Machata8cce1192012-03-25 01:37:19 +0100128breakpoint_destroy(struct breakpoint *bp)
129{
130 if (bp == NULL)
131 return;
Petr Machata8cce1192012-03-25 01:37:19 +0100132 arch_breakpoint_destroy(bp);
133}
134
Petr Machatad3cc9882012-04-13 21:40:23 +0200135struct find_symbol_data {
136 struct library_symbol *old_libsym;
137 struct library_symbol *found_libsym;
138};
139
140static enum callback_status
141find_sym_in_lib(struct Process *proc, struct library *lib, void *u)
142{
143 struct find_symbol_data *fs = u;
144 fs->found_libsym
145 = library_each_symbol(lib, NULL, library_symbol_equal_cb,
146 fs->old_libsym);
147 return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT;
148}
149
150int
151breakpoint_clone(struct breakpoint *retp, struct Process *new_proc,
152 struct breakpoint *bp, struct Process *old_proc)
153{
154 /* Find library and symbol that this breakpoint was linked to. */
155 struct library_symbol *libsym = bp->libsym;
156 struct library *lib = NULL;
157 if (libsym != NULL) {
158 struct find_symbol_data f_data = {
159 .old_libsym = libsym,
160 };
161 lib = proc_each_library(old_proc, NULL,
162 find_sym_in_lib, &f_data);
163 assert(lib != NULL);
164 libsym = f_data.found_libsym;
165 }
166
167 /* LIB and LIBSYM now hold the new library and symbol that
168 * correspond to the original breakpoint. Now we can do the
169 * clone itself. */
170 breakpoint_init_base(retp, new_proc, bp->addr, libsym);
171 memcpy(retp->orig_value, bp->orig_value, sizeof(bp->orig_value));
172 retp->enabled = bp->enabled;
Petr Machatad3cc9882012-04-13 21:40:23 +0200173 if (arch_breakpoint_clone(retp, bp) < 0)
174 return -1;
175 breakpoint_set_callbacks(retp, bp->cbs);
176 return 0;
177}
178
Petr Machata52dbfb12012-03-29 16:38:26 +0200179int
Petr Machatafa0c5702012-04-13 18:43:40 +0200180breakpoint_turn_on(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200181{
Petr Machata52dbfb12012-03-29 16:38:26 +0200182 bp->enabled++;
183 if (bp->enabled == 1) {
Petr Machatafa0c5702012-04-13 18:43:40 +0200184 assert(proc->pid != 0);
185 enable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200186 }
187 return 0;
188}
189
190int
Petr Machatafa0c5702012-04-13 18:43:40 +0200191breakpoint_turn_off(struct breakpoint *bp, struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200192{
Petr Machata52dbfb12012-03-29 16:38:26 +0200193 bp->enabled--;
194 if (bp->enabled == 0)
Petr Machatafa0c5702012-04-13 18:43:40 +0200195 disable_breakpoint(proc, bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200196 assert(bp->enabled >= 0);
197 return 0;
198}
199
Petr Machata9294d822012-02-07 12:35:58 +0100200struct breakpoint *
Petr Machata9df15012012-02-20 12:49:46 +0100201insert_breakpoint(struct Process *proc, void *addr,
202 struct library_symbol *libsym)
Petr Machatafed1e8d2012-02-07 02:06:29 +0100203{
Petr Machata9df15012012-02-20 12:49:46 +0100204 Process *leader = proc->leader;
Petr Machata9a5420c2011-07-09 11:21:23 +0200205
206 /* Only the group leader should be getting the breakpoints and
207 * thus have ->breakpoint initialized. */
208 assert(leader != NULL);
209 assert(leader->breakpoints != NULL);
210
Petr Machata050b0a62012-04-03 01:30:30 +0200211 debug(DEBUG_FUNCTION, "insert_breakpoint(pid=%d, addr=%p, symbol=%s)",
212 proc->pid, addr, libsym ? libsym->name : "NULL");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200213
Petr Machata218c5ff2012-04-15 04:22:39 +0200214 assert(addr != 0);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100215
Petr Machata52dbfb12012-03-29 16:38:26 +0200216 /* XXX what we need to do instead is have a list of
217 * breakpoints that are enabled at this address. The
218 * following works if every breakpoint is the same and there's
219 * no extra data, but that doesn't hold anymore. For now it
220 * will suffice, about the only realistic case where we need
221 * to have more than one breakpoint per address is return from
222 * a recursive library call. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100223 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
Petr Machatafed1e8d2012-02-07 02:06:29 +0100224 if (sbp == NULL) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100225 sbp = malloc(sizeof(*sbp));
226 if (sbp == NULL
Petr Machata52dbfb12012-03-29 16:38:26 +0200227 || breakpoint_init(sbp, proc, addr, libsym) < 0) {
228 free(sbp);
229 return NULL;
230 }
Petr Machatafa0c5702012-04-13 18:43:40 +0200231 if (proc_add_breakpoint(leader, sbp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200232 fail:
233 breakpoint_destroy(sbp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234 free(sbp);
235 return NULL;
Juan Cespedescac15c32003-01-31 18:58:58 +0100236 }
Juan Cespedescac15c32003-01-31 18:58:58 +0100237 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100238
Petr Machata45728772012-04-15 04:23:55 +0200239 if (breakpoint_turn_on(sbp, proc) < 0) {
240 proc_remove_breakpoint(leader, sbp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200241 goto fail;
Petr Machata45728772012-04-15 04:23:55 +0200242 }
Petr Machata9294d822012-02-07 12:35:58 +0100243
244 return sbp;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200245}
246
Juan Cespedesf1350522008-12-16 18:19:58 +0100247void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100248delete_breakpoint(Process *proc, void *addr)
249{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200250 debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);
251
Petr Machata9a5420c2011-07-09 11:21:23 +0200252 Process * leader = proc->leader;
253 assert(leader != NULL);
254
Petr Machataf7fee432012-04-19 17:00:53 +0200255 struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
256 assert(sbp != NULL);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200257 /* This should only happen on out-of-memory conditions. */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100258 if (sbp == NULL)
259 return;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200260
Petr Machatafa0c5702012-04-13 18:43:40 +0200261 if (breakpoint_turn_off(sbp, proc) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200262 fprintf(stderr, "Couldn't turn off the breakpoint %s@%p\n",
263 breakpoint_name(sbp), sbp->addr);
264 return;
265 }
Petr Machataf7fee432012-04-19 17:00:53 +0200266 if (sbp->enabled == 0) {
267 proc_remove_breakpoint(leader, sbp);
268 breakpoint_destroy(sbp);
269 free(sbp);
270 }
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200271}
272
Petr Machatae9aebd62012-03-25 01:38:53 +0100273const char *
274breakpoint_name(const struct breakpoint *bp)
275{
276 assert(bp != NULL);
277 return bp->libsym != NULL ? bp->libsym->name : NULL;
278}
279
Petr Machata52dbfb12012-03-29 16:38:26 +0200280struct library *
281breakpoint_library(const struct breakpoint *bp)
282{
283 assert(bp != NULL);
284 return bp->libsym != NULL ? bp->libsym->lib : NULL;
285}
286
Juan Cespedesf1350522008-12-16 18:19:58 +0100287static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100288enable_bp_cb(void *addr, void *sbp, void *proc)
289{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200290 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100291 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200292 enable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200293}
294
Juan Cespedesf1350522008-12-16 18:19:58 +0100295void
Petr Machatabc373262012-02-07 23:31:15 +0100296enable_all_breakpoints(Process *proc)
297{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200298 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata61196a42012-02-07 16:41:03 +0100299
300 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
301 if (proc->breakpoints) {
302 dict_apply_to_all(proc->breakpoints, enable_bp_cb,
303 proc);
304 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100305}
306
Juan Cespedesf1350522008-12-16 18:19:58 +0100307static void
Petr Machatafed1e8d2012-02-07 02:06:29 +0100308disable_bp_cb(void *addr, void *sbp, void *proc)
309{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200310 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
Petr Machatabc373262012-02-07 23:31:15 +0100311 if (((struct breakpoint *)sbp)->enabled)
Petr Machataf789c9c2011-07-09 10:54:27 +0200312 disable_breakpoint(proc, sbp);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200313}
314
Juan Cespedesf1350522008-12-16 18:19:58 +0100315void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200316disable_all_breakpoints(Process *proc) {
Juan Cespedescd8976d2009-05-14 13:47:58 +0200317 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200318 assert(proc->leader == proc);
Petr Machata61196a42012-02-07 16:41:03 +0100319 dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100320}
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100321
Petr Machatad09d2402012-04-13 21:34:08 +0200322/* XXX This is not currently properly supported. On clone, this is
323 * just sliced. Hopefully at the point that clone is done, this
324 * breakpoint is not necessary anymore. If this use case ends up
325 * being important, we need to add a clone and destroy callbacks to
326 * breakpoints, and we should also probably drop arch_breakpoint_data
327 * so that we don't end up with two different customization mechanisms
328 * for one structure. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200329struct entry_breakpoint {
330 struct breakpoint super;
Petr Machatabac2da52012-05-29 00:42:59 +0200331 arch_addr_t dyn_addr;
Petr Machata52dbfb12012-03-29 16:38:26 +0200332};
333
Petr Machata02648a12012-02-07 13:44:54 +0100334static void
Petr Machata12affff2012-03-29 18:33:03 +0200335entry_breakpoint_on_hit(struct breakpoint *a, struct Process *proc)
Petr Machata02648a12012-02-07 13:44:54 +0100336{
Petr Machata52dbfb12012-03-29 16:38:26 +0200337 struct entry_breakpoint *bp = (void *)a;
Petr Machata02648a12012-02-07 13:44:54 +0100338 if (proc == NULL || proc->leader == NULL)
339 return;
Petr Machatabac2da52012-05-29 00:42:59 +0200340 arch_addr_t dyn_addr = bp->dyn_addr;
Petr Machata3fd099b2012-04-03 02:25:42 +0200341 delete_breakpoint(proc, bp->super.addr);
Petr Machata5ee36822012-04-19 17:01:51 +0200342 linkmap_init(proc, dyn_addr);
Petr Machata93d95df2012-04-17 05:16:19 +0200343 arch_dynlink_done(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200344}
345
346int
347entry_breakpoint_init(struct Process *proc,
Petr Machatabac2da52012-05-29 00:42:59 +0200348 struct entry_breakpoint *bp, arch_addr_t addr,
Petr Machata9a04d0e2012-03-29 16:50:38 +0200349 struct library *lib)
Petr Machata52dbfb12012-03-29 16:38:26 +0200350{
351 int err;
352 if ((err = breakpoint_init(&bp->super, proc, addr, NULL)) < 0)
353 return err;
354
355 static struct bp_callbacks entry_callbacks = {
Petr Machata12affff2012-03-29 18:33:03 +0200356 .on_hit = entry_breakpoint_on_hit,
Petr Machata52dbfb12012-03-29 16:38:26 +0200357 };
358 bp->super.cbs = &entry_callbacks;
Petr Machata9a04d0e2012-03-29 16:50:38 +0200359 bp->dyn_addr = lib->dyn_addr;
Petr Machata52dbfb12012-03-29 16:38:26 +0200360 return 0;
Petr Machata02648a12012-02-07 13:44:54 +0100361}
362
Petr Machata1974dbc2011-08-19 18:58:01 +0200363int
Petr Machata75934ad2012-04-14 02:28:03 +0200364breakpoints_init(Process *proc)
Petr Machatac7585b62011-07-08 22:58:12 +0200365{
Juan Cespedescd8976d2009-05-14 13:47:58 +0200366 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
Petr Machata26627682011-07-08 18:15:32 +0200367
Petr Machata2b46cfc2012-02-18 11:17:29 +0100368 /* XXX breakpoint dictionary should be initialized
369 * outside. Here we just put in breakpoints. */
370 assert(proc->breakpoints != NULL);
371
372 /* Only the thread group leader should hold the breakpoints. */
Petr Machata9a5420c2011-07-09 11:21:23 +0200373 assert(proc->leader == proc);
374
Petr Machata807cdd82012-04-05 02:08:25 +0200375 /* N.B. the following used to be conditional on this, and
376 * maybe it still needs to be. */
377 assert(proc->filename != NULL);
378
379 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
380 struct entry_breakpoint *entry_bp = NULL;
381 int bp_state = 0;
382 int result = -1;
383 switch (lib != NULL) {
384 fail:
Petr Machata807cdd82012-04-05 02:08:25 +0200385 switch (bp_state) {
386 case 2:
Petr Machataa2416362012-04-06 02:43:34 +0200387 proc_remove_library(proc, lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200388 proc_remove_breakpoint(proc, &entry_bp->super);
389 case 1:
390 breakpoint_destroy(&entry_bp->super);
Petr Machata1974dbc2011-08-19 18:58:01 +0200391 }
Petr Machataa2416362012-04-06 02:43:34 +0200392 library_destroy(lib);
Petr Machata807cdd82012-04-05 02:08:25 +0200393 free(entry_bp);
394 case 0:
395 return result;
Petr Machata02648a12012-02-07 13:44:54 +0100396 }
397
Petr Machata807cdd82012-04-05 02:08:25 +0200398 entry_bp = malloc(sizeof(*entry_bp));
399 if (entry_bp == NULL
Petr Machata91c399c2012-05-15 12:17:51 +0200400 || (entry_breakpoint_init(proc, entry_bp,
401 lib->entry, lib)) < 0) {
402 fprintf(stderr,
403 "Couldn't initialize entry breakpoint for PID %d.\n"
404 "Some tracing events may be missed.\n", proc->pid);
405 free(entry_bp);
Petr Machata00928202012-04-07 01:14:24 +0200406
Petr Machata91c399c2012-05-15 12:17:51 +0200407 } else {
408 ++bp_state;
Petr Machata00928202012-04-07 01:14:24 +0200409
Petr Machata91c399c2012-05-15 12:17:51 +0200410 if ((result = proc_add_breakpoint(proc, &entry_bp->super)) < 0)
411 goto fail;
412 ++bp_state;
413
414 if ((result = breakpoint_turn_on(&entry_bp->super, proc)) < 0)
415 goto fail;
416 }
Petr Machataa2416362012-04-06 02:43:34 +0200417 proc_add_library(proc, lib);
418
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100419 proc->callstack_depth = 0;
Petr Machata1974dbc2011-08-19 18:58:01 +0200420 return 0;
Juan Cespedes7186e2a2003-01-31 19:56:34 +0100421}