GDBserver
|
00001 /* Memory breakpoint operations for the remote server for GDB. 00002 Copyright (C) 2002-2013 Free Software Foundation, Inc. 00003 00004 Contributed by MontaVista Software. 00005 00006 This file is part of GDB. 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00020 00021 #include "server.h" 00022 #include "regcache.h" 00023 #include "ax.h" 00024 #include <stdint.h> 00025 00026 const unsigned char *breakpoint_data; 00027 int breakpoint_len; 00028 00029 #define MAX_BREAKPOINT_LEN 8 00030 00031 /* GDB will never try to install multiple breakpoints at the same 00032 address. But, we need to keep track of internal breakpoints too, 00033 and so we do need to be able to install multiple breakpoints at the 00034 same address transparently. We keep track of two different, and 00035 closely related structures. A raw breakpoint, which manages the 00036 low level, close to the metal aspect of a breakpoint. It holds the 00037 breakpoint address, and a buffer holding a copy of the instructions 00038 that would be in memory had not been a breakpoint there (we call 00039 that the shadow memory of the breakpoint). We occasionally need to 00040 temporarilly uninsert a breakpoint without the client knowing about 00041 it (e.g., to step over an internal breakpoint), so we keep an 00042 `inserted' state associated with this low level breakpoint 00043 structure. There can only be one such object for a given address. 00044 Then, we have (a bit higher level) breakpoints. This structure 00045 holds a callback to be called whenever a breakpoint is hit, a 00046 high-level type, and a link to a low level raw breakpoint. There 00047 can be many high-level breakpoints at the same address, and all of 00048 them will point to the same raw breakpoint, which is reference 00049 counted. */ 00050 00051 /* The low level, physical, raw breakpoint. */ 00052 struct raw_breakpoint 00053 { 00054 struct raw_breakpoint *next; 00055 00056 /* A reference count. Each high level breakpoint referencing this 00057 raw breakpoint accounts for one reference. */ 00058 int refcount; 00059 00060 /* The breakpoint's insertion address. There can only be one raw 00061 breakpoint for a given PC. */ 00062 CORE_ADDR pc; 00063 00064 /* The breakpoint's shadow memory. */ 00065 unsigned char old_data[MAX_BREAKPOINT_LEN]; 00066 00067 /* Non-zero if this breakpoint is currently inserted in the 00068 inferior. */ 00069 int inserted; 00070 00071 /* Non-zero if this breakpoint is currently disabled because we no 00072 longer detect it as inserted. */ 00073 int shlib_disabled; 00074 }; 00075 00076 /* The type of a breakpoint. */ 00077 enum bkpt_type 00078 { 00079 /* A GDB breakpoint, requested with a Z0 packet. */ 00080 gdb_breakpoint, 00081 00082 /* A basic-software-single-step breakpoint. */ 00083 reinsert_breakpoint, 00084 00085 /* Any other breakpoint type that doesn't require specific 00086 treatment goes here. E.g., an event breakpoint. */ 00087 other_breakpoint, 00088 }; 00089 00090 struct point_cond_list 00091 { 00092 /* Pointer to the agent expression that is the breakpoint's 00093 conditional. */ 00094 struct agent_expr *cond; 00095 00096 /* Pointer to the next condition. */ 00097 struct point_cond_list *next; 00098 }; 00099 00100 struct point_command_list 00101 { 00102 /* Pointer to the agent expression that is the breakpoint's 00103 commands. */ 00104 struct agent_expr *cmd; 00105 00106 /* Flag that is true if this command should run even while GDB is 00107 disconnected. */ 00108 int persistence; 00109 00110 /* Pointer to the next command. */ 00111 struct point_command_list *next; 00112 }; 00113 00114 /* A high level (in gdbserver's perspective) breakpoint. */ 00115 struct breakpoint 00116 { 00117 struct breakpoint *next; 00118 00119 /* The breakpoint's type. */ 00120 enum bkpt_type type; 00121 00122 /* Pointer to the condition list that should be evaluated on 00123 the target or NULL if the breakpoint is unconditional or 00124 if GDB doesn't want us to evaluate the conditionals on the 00125 target's side. */ 00126 struct point_cond_list *cond_list; 00127 00128 /* Point to the list of commands to run when this is hit. */ 00129 struct point_command_list *command_list; 00130 00131 /* Link to this breakpoint's raw breakpoint. This is always 00132 non-NULL. */ 00133 struct raw_breakpoint *raw; 00134 00135 /* Function to call when we hit this breakpoint. If it returns 1, 00136 the breakpoint shall be deleted; 0 or if this callback is NULL, 00137 it will be left inserted. */ 00138 int (*handler) (CORE_ADDR); 00139 }; 00140 00141 int 00142 any_persistent_commands () 00143 { 00144 struct process_info *proc = current_process (); 00145 struct breakpoint *bp; 00146 struct point_command_list *cl; 00147 00148 for (bp = proc->breakpoints; bp != NULL; bp = bp->next) 00149 { 00150 for (cl = bp->command_list; cl != NULL; cl = cl->next) 00151 if (cl->persistence) 00152 return 1; 00153 } 00154 00155 return 0; 00156 } 00157 00158 static struct raw_breakpoint * 00159 find_raw_breakpoint_at (CORE_ADDR where) 00160 { 00161 struct process_info *proc = current_process (); 00162 struct raw_breakpoint *bp; 00163 00164 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next) 00165 if (bp->pc == where) 00166 return bp; 00167 00168 return NULL; 00169 } 00170 00171 static struct raw_breakpoint * 00172 set_raw_breakpoint_at (CORE_ADDR where) 00173 { 00174 struct process_info *proc = current_process (); 00175 struct raw_breakpoint *bp; 00176 int err; 00177 unsigned char buf[MAX_BREAKPOINT_LEN]; 00178 00179 if (breakpoint_data == NULL) 00180 error ("Target does not support breakpoints."); 00181 00182 bp = find_raw_breakpoint_at (where); 00183 if (bp != NULL) 00184 { 00185 bp->refcount++; 00186 return bp; 00187 } 00188 00189 bp = xcalloc (1, sizeof (*bp)); 00190 bp->pc = where; 00191 bp->refcount = 1; 00192 00193 /* Note that there can be fast tracepoint jumps installed in the 00194 same memory range, so to get at the original memory, we need to 00195 use read_inferior_memory, which masks those out. */ 00196 err = read_inferior_memory (where, buf, breakpoint_len); 00197 if (err != 0) 00198 { 00199 if (debug_threads) 00200 fprintf (stderr, 00201 "Failed to read shadow memory of" 00202 " breakpoint at 0x%s (%s).\n", 00203 paddress (where), strerror (err)); 00204 free (bp); 00205 return NULL; 00206 } 00207 memcpy (bp->old_data, buf, breakpoint_len); 00208 00209 err = (*the_target->write_memory) (where, breakpoint_data, 00210 breakpoint_len); 00211 if (err != 0) 00212 { 00213 if (debug_threads) 00214 fprintf (stderr, 00215 "Failed to insert breakpoint at 0x%s (%s).\n", 00216 paddress (where), strerror (err)); 00217 free (bp); 00218 return NULL; 00219 } 00220 00221 /* Link the breakpoint in. */ 00222 bp->inserted = 1; 00223 bp->next = proc->raw_breakpoints; 00224 proc->raw_breakpoints = bp; 00225 return bp; 00226 } 00227 00228 /* Notice that breakpoint traps are always installed on top of fast 00229 tracepoint jumps. This is even if the fast tracepoint is installed 00230 at a later time compared to when the breakpoint was installed. 00231 This means that a stopping breakpoint or tracepoint has higher 00232 "priority". In turn, this allows having fast and slow tracepoints 00233 (and breakpoints) at the same address behave correctly. */ 00234 00235 00236 /* A fast tracepoint jump. */ 00237 00238 struct fast_tracepoint_jump 00239 { 00240 struct fast_tracepoint_jump *next; 00241 00242 /* A reference count. GDB can install more than one fast tracepoint 00243 at the same address (each with its own action list, for 00244 example). */ 00245 int refcount; 00246 00247 /* The fast tracepoint's insertion address. There can only be one 00248 of these for a given PC. */ 00249 CORE_ADDR pc; 00250 00251 /* Non-zero if this fast tracepoint jump is currently inserted in 00252 the inferior. */ 00253 int inserted; 00254 00255 /* The length of the jump instruction. */ 00256 int length; 00257 00258 /* A poor-man's flexible array member, holding both the jump 00259 instruction to insert, and a copy of the instruction that would 00260 be in memory had not been a jump there (the shadow memory of the 00261 tracepoint jump). */ 00262 unsigned char insn_and_shadow[0]; 00263 }; 00264 00265 /* Fast tracepoint FP's jump instruction to insert. */ 00266 #define fast_tracepoint_jump_insn(fp) \ 00267 ((fp)->insn_and_shadow + 0) 00268 00269 /* The shadow memory of fast tracepoint jump FP. */ 00270 #define fast_tracepoint_jump_shadow(fp) \ 00271 ((fp)->insn_and_shadow + (fp)->length) 00272 00273 00274 /* Return the fast tracepoint jump set at WHERE. */ 00275 00276 static struct fast_tracepoint_jump * 00277 find_fast_tracepoint_jump_at (CORE_ADDR where) 00278 { 00279 struct process_info *proc = current_process (); 00280 struct fast_tracepoint_jump *jp; 00281 00282 for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next) 00283 if (jp->pc == where) 00284 return jp; 00285 00286 return NULL; 00287 } 00288 00289 int 00290 fast_tracepoint_jump_here (CORE_ADDR where) 00291 { 00292 struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where); 00293 00294 return (jp != NULL); 00295 } 00296 00297 int 00298 delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel) 00299 { 00300 struct fast_tracepoint_jump *bp, **bp_link; 00301 int ret; 00302 struct process_info *proc = current_process (); 00303 00304 bp = proc->fast_tracepoint_jumps; 00305 bp_link = &proc->fast_tracepoint_jumps; 00306 00307 while (bp) 00308 { 00309 if (bp == todel) 00310 { 00311 if (--bp->refcount == 0) 00312 { 00313 struct fast_tracepoint_jump *prev_bp_link = *bp_link; 00314 unsigned char *buf; 00315 00316 /* Unlink it. */ 00317 *bp_link = bp->next; 00318 00319 /* Since there can be breakpoints inserted in the same 00320 address range, we use `write_inferior_memory', which 00321 takes care of layering breakpoints on top of fast 00322 tracepoints, and on top of the buffer we pass it. 00323 This works because we've already unlinked the fast 00324 tracepoint jump above. Also note that we need to 00325 pass the current shadow contents, because 00326 write_inferior_memory updates any shadow memory with 00327 what we pass here, and we want that to be a nop. */ 00328 buf = alloca (bp->length); 00329 memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length); 00330 ret = write_inferior_memory (bp->pc, buf, bp->length); 00331 if (ret != 0) 00332 { 00333 /* Something went wrong, relink the jump. */ 00334 *bp_link = prev_bp_link; 00335 00336 if (debug_threads) 00337 fprintf (stderr, 00338 "Failed to uninsert fast tracepoint jump " 00339 "at 0x%s (%s) while deleting it.\n", 00340 paddress (bp->pc), strerror (ret)); 00341 return ret; 00342 } 00343 00344 free (bp); 00345 } 00346 00347 return 0; 00348 } 00349 else 00350 { 00351 bp_link = &bp->next; 00352 bp = *bp_link; 00353 } 00354 } 00355 00356 warning ("Could not find fast tracepoint jump in list."); 00357 return ENOENT; 00358 } 00359 00360 void 00361 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp) 00362 { 00363 jp->refcount++; 00364 } 00365 00366 struct fast_tracepoint_jump * 00367 set_fast_tracepoint_jump (CORE_ADDR where, 00368 unsigned char *insn, ULONGEST length) 00369 { 00370 struct process_info *proc = current_process (); 00371 struct fast_tracepoint_jump *jp; 00372 int err; 00373 unsigned char *buf; 00374 00375 /* We refcount fast tracepoint jumps. Check if we already know 00376 about a jump at this address. */ 00377 jp = find_fast_tracepoint_jump_at (where); 00378 if (jp != NULL) 00379 { 00380 jp->refcount++; 00381 return jp; 00382 } 00383 00384 /* We don't, so create a new object. Double the length, because the 00385 flexible array member holds both the jump insn, and the 00386 shadow. */ 00387 jp = xcalloc (1, sizeof (*jp) + (length * 2)); 00388 jp->pc = where; 00389 jp->length = length; 00390 memcpy (fast_tracepoint_jump_insn (jp), insn, length); 00391 jp->refcount = 1; 00392 buf = alloca (length); 00393 00394 /* Note that there can be trap breakpoints inserted in the same 00395 address range. To access the original memory contents, we use 00396 `read_inferior_memory', which masks out breakpoints. */ 00397 err = read_inferior_memory (where, buf, length); 00398 if (err != 0) 00399 { 00400 if (debug_threads) 00401 fprintf (stderr, 00402 "Failed to read shadow memory of" 00403 " fast tracepoint at 0x%s (%s).\n", 00404 paddress (where), strerror (err)); 00405 free (jp); 00406 return NULL; 00407 } 00408 memcpy (fast_tracepoint_jump_shadow (jp), buf, length); 00409 00410 /* Link the jump in. */ 00411 jp->inserted = 1; 00412 jp->next = proc->fast_tracepoint_jumps; 00413 proc->fast_tracepoint_jumps = jp; 00414 00415 /* Since there can be trap breakpoints inserted in the same address 00416 range, we use use `write_inferior_memory', which takes care of 00417 layering breakpoints on top of fast tracepoints, on top of the 00418 buffer we pass it. This works because we've already linked in 00419 the fast tracepoint jump above. Also note that we need to pass 00420 the current shadow contents, because write_inferior_memory 00421 updates any shadow memory with what we pass here, and we want 00422 that to be a nop. */ 00423 err = write_inferior_memory (where, buf, length); 00424 if (err != 0) 00425 { 00426 if (debug_threads) 00427 fprintf (stderr, 00428 "Failed to insert fast tracepoint jump at 0x%s (%s).\n", 00429 paddress (where), strerror (err)); 00430 00431 /* Unlink it. */ 00432 proc->fast_tracepoint_jumps = jp->next; 00433 free (jp); 00434 00435 return NULL; 00436 } 00437 00438 return jp; 00439 } 00440 00441 void 00442 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc) 00443 { 00444 struct fast_tracepoint_jump *jp; 00445 int err; 00446 00447 jp = find_fast_tracepoint_jump_at (pc); 00448 if (jp == NULL) 00449 { 00450 /* This can happen when we remove all breakpoints while handling 00451 a step-over. */ 00452 if (debug_threads) 00453 fprintf (stderr, 00454 "Could not find fast tracepoint jump at 0x%s " 00455 "in list (uninserting).\n", 00456 paddress (pc)); 00457 return; 00458 } 00459 00460 if (jp->inserted) 00461 { 00462 unsigned char *buf; 00463 00464 jp->inserted = 0; 00465 00466 /* Since there can be trap breakpoints inserted in the same 00467 address range, we use use `write_inferior_memory', which 00468 takes care of layering breakpoints on top of fast 00469 tracepoints, and on top of the buffer we pass it. This works 00470 because we've already marked the fast tracepoint fast 00471 tracepoint jump uninserted above. Also note that we need to 00472 pass the current shadow contents, because 00473 write_inferior_memory updates any shadow memory with what we 00474 pass here, and we want that to be a nop. */ 00475 buf = alloca (jp->length); 00476 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length); 00477 err = write_inferior_memory (jp->pc, buf, jp->length); 00478 if (err != 0) 00479 { 00480 jp->inserted = 1; 00481 00482 if (debug_threads) 00483 fprintf (stderr, 00484 "Failed to uninsert fast tracepoint jump at 0x%s (%s).\n", 00485 paddress (pc), strerror (err)); 00486 } 00487 } 00488 } 00489 00490 void 00491 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where) 00492 { 00493 struct fast_tracepoint_jump *jp; 00494 int err; 00495 unsigned char *buf; 00496 00497 jp = find_fast_tracepoint_jump_at (where); 00498 if (jp == NULL) 00499 { 00500 /* This can happen when we remove breakpoints when a tracepoint 00501 hit causes a tracing stop, while handling a step-over. */ 00502 if (debug_threads) 00503 fprintf (stderr, 00504 "Could not find fast tracepoint jump at 0x%s " 00505 "in list (reinserting).\n", 00506 paddress (where)); 00507 return; 00508 } 00509 00510 if (jp->inserted) 00511 error ("Jump already inserted at reinsert time."); 00512 00513 jp->inserted = 1; 00514 00515 /* Since there can be trap breakpoints inserted in the same address 00516 range, we use `write_inferior_memory', which takes care of 00517 layering breakpoints on top of fast tracepoints, and on top of 00518 the buffer we pass it. This works because we've already marked 00519 the fast tracepoint jump inserted above. Also note that we need 00520 to pass the current shadow contents, because 00521 write_inferior_memory updates any shadow memory with what we pass 00522 here, and we want that to be a nop. */ 00523 buf = alloca (jp->length); 00524 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length); 00525 err = write_inferior_memory (where, buf, jp->length); 00526 if (err != 0) 00527 { 00528 jp->inserted = 0; 00529 00530 if (debug_threads) 00531 fprintf (stderr, 00532 "Failed to reinsert fast tracepoint jump at 0x%s (%s).\n", 00533 paddress (where), strerror (err)); 00534 } 00535 } 00536 00537 struct breakpoint * 00538 set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR)) 00539 { 00540 struct process_info *proc = current_process (); 00541 struct breakpoint *bp; 00542 struct raw_breakpoint *raw; 00543 00544 raw = set_raw_breakpoint_at (where); 00545 00546 if (raw == NULL) 00547 { 00548 /* warn? */ 00549 return NULL; 00550 } 00551 00552 bp = xcalloc (1, sizeof (struct breakpoint)); 00553 bp->type = other_breakpoint; 00554 00555 bp->raw = raw; 00556 bp->handler = handler; 00557 00558 bp->next = proc->breakpoints; 00559 proc->breakpoints = bp; 00560 00561 return bp; 00562 } 00563 00564 static int 00565 delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel) 00566 { 00567 struct raw_breakpoint *bp, **bp_link; 00568 int ret; 00569 00570 bp = proc->raw_breakpoints; 00571 bp_link = &proc->raw_breakpoints; 00572 00573 while (bp) 00574 { 00575 if (bp == todel) 00576 { 00577 if (bp->inserted) 00578 { 00579 struct raw_breakpoint *prev_bp_link = *bp_link; 00580 unsigned char buf[MAX_BREAKPOINT_LEN]; 00581 00582 *bp_link = bp->next; 00583 00584 /* Since there can be trap breakpoints inserted in the 00585 same address range, we use `write_inferior_memory', 00586 which takes care of layering breakpoints on top of 00587 fast tracepoints, and on top of the buffer we pass 00588 it. This works because we've already unlinked the 00589 fast tracepoint jump above. Also note that we need 00590 to pass the current shadow contents, because 00591 write_inferior_memory updates any shadow memory with 00592 what we pass here, and we want that to be a nop. */ 00593 memcpy (buf, bp->old_data, breakpoint_len); 00594 ret = write_inferior_memory (bp->pc, buf, breakpoint_len); 00595 if (ret != 0) 00596 { 00597 /* Something went wrong, relink the breakpoint. */ 00598 *bp_link = prev_bp_link; 00599 00600 if (debug_threads) 00601 fprintf (stderr, 00602 "Failed to uninsert raw breakpoint " 00603 "at 0x%s (%s) while deleting it.\n", 00604 paddress (bp->pc), strerror (ret)); 00605 return ret; 00606 } 00607 00608 } 00609 else 00610 *bp_link = bp->next; 00611 00612 free (bp); 00613 return 0; 00614 } 00615 else 00616 { 00617 bp_link = &bp->next; 00618 bp = *bp_link; 00619 } 00620 } 00621 00622 warning ("Could not find raw breakpoint in list."); 00623 return ENOENT; 00624 } 00625 00626 static int 00627 release_breakpoint (struct process_info *proc, struct breakpoint *bp) 00628 { 00629 int newrefcount; 00630 int ret; 00631 00632 newrefcount = bp->raw->refcount - 1; 00633 if (newrefcount == 0) 00634 { 00635 ret = delete_raw_breakpoint (proc, bp->raw); 00636 if (ret != 0) 00637 return ret; 00638 } 00639 else 00640 bp->raw->refcount = newrefcount; 00641 00642 free (bp); 00643 00644 return 0; 00645 } 00646 00647 static int 00648 delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel) 00649 { 00650 struct breakpoint *bp, **bp_link; 00651 int err; 00652 00653 bp = proc->breakpoints; 00654 bp_link = &proc->breakpoints; 00655 00656 while (bp) 00657 { 00658 if (bp == todel) 00659 { 00660 *bp_link = bp->next; 00661 00662 err = release_breakpoint (proc, bp); 00663 if (err != 0) 00664 return err; 00665 00666 bp = *bp_link; 00667 return 0; 00668 } 00669 else 00670 { 00671 bp_link = &bp->next; 00672 bp = *bp_link; 00673 } 00674 } 00675 00676 warning ("Could not find breakpoint in list."); 00677 return ENOENT; 00678 } 00679 00680 int 00681 delete_breakpoint (struct breakpoint *todel) 00682 { 00683 struct process_info *proc = current_process (); 00684 return delete_breakpoint_1 (proc, todel); 00685 } 00686 00687 struct breakpoint * 00688 find_gdb_breakpoint_at (CORE_ADDR where) 00689 { 00690 struct process_info *proc = current_process (); 00691 struct breakpoint *bp; 00692 00693 for (bp = proc->breakpoints; bp != NULL; bp = bp->next) 00694 if (bp->type == gdb_breakpoint && bp->raw->pc == where) 00695 return bp; 00696 00697 return NULL; 00698 } 00699 00700 int 00701 set_gdb_breakpoint_at (CORE_ADDR where) 00702 { 00703 struct breakpoint *bp; 00704 00705 if (breakpoint_data == NULL) 00706 return 1; 00707 00708 /* If we see GDB inserting a second breakpoint at the same address, 00709 then the first breakpoint must have disappeared due to a shared 00710 library unload. On targets where the shared libraries are 00711 handled by userspace, like SVR4, for example, GDBserver can't 00712 tell if a library was loaded or unloaded. Since we refcount 00713 breakpoints, if we didn't do this, we'd just increase the 00714 refcount of the previous breakpoint at this address, but the trap 00715 was not planted in the inferior anymore, thus the breakpoint 00716 would never be hit. */ 00717 bp = find_gdb_breakpoint_at (where); 00718 if (bp != NULL) 00719 { 00720 delete_gdb_breakpoint_at (where); 00721 00722 /* Might as well validate all other breakpoints. */ 00723 validate_breakpoints (); 00724 } 00725 00726 bp = set_breakpoint_at (where, NULL); 00727 if (bp == NULL) 00728 return -1; 00729 00730 bp->type = gdb_breakpoint; 00731 return 0; 00732 } 00733 00734 int 00735 delete_gdb_breakpoint_at (CORE_ADDR addr) 00736 { 00737 struct breakpoint *bp; 00738 int err; 00739 00740 if (breakpoint_data == NULL) 00741 return 1; 00742 00743 bp = find_gdb_breakpoint_at (addr); 00744 if (bp == NULL) 00745 return -1; 00746 00747 /* Before deleting the breakpoint, make sure to free 00748 its condition list. */ 00749 clear_gdb_breakpoint_conditions (addr); 00750 err = delete_breakpoint (bp); 00751 if (err) 00752 return -1; 00753 00754 return 0; 00755 } 00756 00757 /* Clear all conditions associated with this breakpoint address. */ 00758 00759 void 00760 clear_gdb_breakpoint_conditions (CORE_ADDR addr) 00761 { 00762 struct breakpoint *bp = find_gdb_breakpoint_at (addr); 00763 struct point_cond_list *cond; 00764 00765 if (bp == NULL || bp->cond_list == NULL) 00766 return; 00767 00768 cond = bp->cond_list; 00769 00770 while (cond != NULL) 00771 { 00772 struct point_cond_list *cond_next; 00773 00774 cond_next = cond->next; 00775 free (cond->cond->bytes); 00776 free (cond->cond); 00777 free (cond); 00778 cond = cond_next; 00779 } 00780 00781 bp->cond_list = NULL; 00782 } 00783 00784 /* Add condition CONDITION to GDBserver's breakpoint BP. */ 00785 00786 void 00787 add_condition_to_breakpoint (struct breakpoint *bp, 00788 struct agent_expr *condition) 00789 { 00790 struct point_cond_list *new_cond; 00791 00792 /* Create new condition. */ 00793 new_cond = xcalloc (1, sizeof (*new_cond)); 00794 new_cond->cond = condition; 00795 00796 /* Add condition to the list. */ 00797 new_cond->next = bp->cond_list; 00798 bp->cond_list = new_cond; 00799 } 00800 00801 /* Add a target-side condition CONDITION to the breakpoint at ADDR. */ 00802 00803 int 00804 add_breakpoint_condition (CORE_ADDR addr, char **condition) 00805 { 00806 struct breakpoint *bp = find_gdb_breakpoint_at (addr); 00807 char *actparm = *condition; 00808 struct agent_expr *cond; 00809 00810 if (bp == NULL) 00811 return 1; 00812 00813 if (condition == NULL) 00814 return 1; 00815 00816 cond = gdb_parse_agent_expr (&actparm); 00817 00818 if (cond == NULL) 00819 { 00820 fprintf (stderr, "Condition evaluation failed. " 00821 "Assuming unconditional.\n"); 00822 return 0; 00823 } 00824 00825 add_condition_to_breakpoint (bp, cond); 00826 00827 *condition = actparm; 00828 00829 return 0; 00830 } 00831 00832 /* Evaluate condition (if any) at breakpoint BP. Return 1 if 00833 true and 0 otherwise. */ 00834 00835 int 00836 gdb_condition_true_at_breakpoint (CORE_ADDR where) 00837 { 00838 /* Fetch registers for the current inferior. */ 00839 struct breakpoint *bp = find_gdb_breakpoint_at (where); 00840 ULONGEST value = 0; 00841 struct point_cond_list *cl; 00842 int err = 0; 00843 struct eval_agent_expr_context ctx; 00844 00845 if (bp == NULL) 00846 return 0; 00847 00848 /* Check if the breakpoint is unconditional. If it is, 00849 the condition always evaluates to TRUE. */ 00850 if (bp->cond_list == NULL) 00851 return 1; 00852 00853 ctx.regcache = get_thread_regcache (current_inferior, 1); 00854 ctx.tframe = NULL; 00855 ctx.tpoint = NULL; 00856 00857 /* Evaluate each condition in the breakpoint's list of conditions. 00858 Return true if any of the conditions evaluates to TRUE. 00859 00860 If we failed to evaluate the expression, TRUE is returned. This 00861 forces GDB to reevaluate the conditions. */ 00862 for (cl = bp->cond_list; 00863 cl && !value && !err; cl = cl->next) 00864 { 00865 /* Evaluate the condition. */ 00866 err = gdb_eval_agent_expr (&ctx, cl->cond, &value); 00867 } 00868 00869 if (err) 00870 return 1; 00871 00872 return (value != 0); 00873 } 00874 00875 /* Add commands COMMANDS to GDBserver's breakpoint BP. */ 00876 00877 void 00878 add_commands_to_breakpoint (struct breakpoint *bp, 00879 struct agent_expr *commands, int persist) 00880 { 00881 struct point_command_list *new_cmd; 00882 00883 /* Create new command. */ 00884 new_cmd = xcalloc (1, sizeof (*new_cmd)); 00885 new_cmd->cmd = commands; 00886 new_cmd->persistence = persist; 00887 00888 /* Add commands to the list. */ 00889 new_cmd->next = bp->command_list; 00890 bp->command_list = new_cmd; 00891 } 00892 00893 /* Add a target-side command COMMAND to the breakpoint at ADDR. */ 00894 00895 int 00896 add_breakpoint_commands (CORE_ADDR addr, char **command, int persist) 00897 { 00898 struct breakpoint *bp = find_gdb_breakpoint_at (addr); 00899 char *actparm = *command; 00900 struct agent_expr *cmd; 00901 00902 if (bp == NULL) 00903 return 1; 00904 00905 if (command == NULL) 00906 return 1; 00907 00908 cmd = gdb_parse_agent_expr (&actparm); 00909 00910 if (cmd == NULL) 00911 { 00912 fprintf (stderr, "Command evaluation failed. " 00913 "Disabling.\n"); 00914 return 0; 00915 } 00916 00917 add_commands_to_breakpoint (bp, cmd, persist); 00918 00919 *command = actparm; 00920 00921 return 0; 00922 } 00923 00924 /* Return true if there are no commands to run at this location, 00925 which likely means we want to report back to GDB. */ 00926 int 00927 gdb_no_commands_at_breakpoint (CORE_ADDR where) 00928 { 00929 struct breakpoint *bp = find_gdb_breakpoint_at (where); 00930 00931 if (bp == NULL) 00932 return 0; 00933 00934 if (debug_threads) 00935 fprintf (stderr, "at 0x%s, bp command_list is 0x%s\n", 00936 paddress (where), 00937 phex_nz ((uintptr_t) bp->command_list, 0)); 00938 return (bp->command_list == NULL); 00939 } 00940 00941 void 00942 run_breakpoint_commands (CORE_ADDR where) 00943 { 00944 /* Fetch registers for the current inferior. */ 00945 struct breakpoint *bp = find_gdb_breakpoint_at (where); 00946 ULONGEST value = 0; 00947 struct point_command_list *cl; 00948 int err = 0; 00949 struct eval_agent_expr_context ctx; 00950 00951 if (bp == NULL) 00952 return; 00953 00954 ctx.regcache = get_thread_regcache (current_inferior, 1); 00955 ctx.tframe = NULL; 00956 ctx.tpoint = NULL; 00957 00958 for (cl = bp->command_list; 00959 cl && !value && !err; cl = cl->next) 00960 { 00961 /* Run the command. */ 00962 err = gdb_eval_agent_expr (&ctx, cl->cmd, &value); 00963 00964 /* If one command has a problem, stop digging the hole deeper. */ 00965 if (err) 00966 break; 00967 } 00968 } 00969 00970 /* Return 1 if there is a breakpoint inserted in address WHERE 00971 and if its condition, if it exists, is true. */ 00972 00973 int 00974 gdb_breakpoint_here (CORE_ADDR where) 00975 { 00976 return (find_gdb_breakpoint_at (where) != NULL); 00977 } 00978 00979 void 00980 set_reinsert_breakpoint (CORE_ADDR stop_at) 00981 { 00982 struct breakpoint *bp; 00983 00984 bp = set_breakpoint_at (stop_at, NULL); 00985 bp->type = reinsert_breakpoint; 00986 } 00987 00988 void 00989 delete_reinsert_breakpoints (void) 00990 { 00991 struct process_info *proc = current_process (); 00992 struct breakpoint *bp, **bp_link; 00993 00994 bp = proc->breakpoints; 00995 bp_link = &proc->breakpoints; 00996 00997 while (bp) 00998 { 00999 if (bp->type == reinsert_breakpoint) 01000 { 01001 *bp_link = bp->next; 01002 release_breakpoint (proc, bp); 01003 bp = *bp_link; 01004 } 01005 else 01006 { 01007 bp_link = &bp->next; 01008 bp = *bp_link; 01009 } 01010 } 01011 } 01012 01013 static void 01014 uninsert_raw_breakpoint (struct raw_breakpoint *bp) 01015 { 01016 if (bp->inserted) 01017 { 01018 int err; 01019 unsigned char buf[MAX_BREAKPOINT_LEN]; 01020 01021 bp->inserted = 0; 01022 /* Since there can be fast tracepoint jumps inserted in the same 01023 address range, we use `write_inferior_memory', which takes 01024 care of layering breakpoints on top of fast tracepoints, and 01025 on top of the buffer we pass it. This works because we've 01026 already unlinked the fast tracepoint jump above. Also note 01027 that we need to pass the current shadow contents, because 01028 write_inferior_memory updates any shadow memory with what we 01029 pass here, and we want that to be a nop. */ 01030 memcpy (buf, bp->old_data, breakpoint_len); 01031 err = write_inferior_memory (bp->pc, buf, breakpoint_len); 01032 if (err != 0) 01033 { 01034 bp->inserted = 1; 01035 01036 if (debug_threads) 01037 fprintf (stderr, 01038 "Failed to uninsert raw breakpoint at 0x%s (%s).\n", 01039 paddress (bp->pc), strerror (err)); 01040 } 01041 } 01042 } 01043 01044 void 01045 uninsert_breakpoints_at (CORE_ADDR pc) 01046 { 01047 struct raw_breakpoint *bp; 01048 01049 bp = find_raw_breakpoint_at (pc); 01050 if (bp == NULL) 01051 { 01052 /* This can happen when we remove all breakpoints while handling 01053 a step-over. */ 01054 if (debug_threads) 01055 fprintf (stderr, 01056 "Could not find breakpoint at 0x%s " 01057 "in list (uninserting).\n", 01058 paddress (pc)); 01059 return; 01060 } 01061 01062 if (bp->inserted) 01063 uninsert_raw_breakpoint (bp); 01064 } 01065 01066 void 01067 uninsert_all_breakpoints (void) 01068 { 01069 struct process_info *proc = current_process (); 01070 struct raw_breakpoint *bp; 01071 01072 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next) 01073 if (bp->inserted) 01074 uninsert_raw_breakpoint (bp); 01075 } 01076 01077 static void 01078 reinsert_raw_breakpoint (struct raw_breakpoint *bp) 01079 { 01080 int err; 01081 01082 if (bp->inserted) 01083 error ("Breakpoint already inserted at reinsert time."); 01084 01085 err = (*the_target->write_memory) (bp->pc, breakpoint_data, 01086 breakpoint_len); 01087 if (err == 0) 01088 bp->inserted = 1; 01089 else if (debug_threads) 01090 fprintf (stderr, 01091 "Failed to reinsert breakpoint at 0x%s (%s).\n", 01092 paddress (bp->pc), strerror (err)); 01093 } 01094 01095 void 01096 reinsert_breakpoints_at (CORE_ADDR pc) 01097 { 01098 struct raw_breakpoint *bp; 01099 01100 bp = find_raw_breakpoint_at (pc); 01101 if (bp == NULL) 01102 { 01103 /* This can happen when we remove all breakpoints while handling 01104 a step-over. */ 01105 if (debug_threads) 01106 fprintf (stderr, 01107 "Could not find raw breakpoint at 0x%s " 01108 "in list (reinserting).\n", 01109 paddress (pc)); 01110 return; 01111 } 01112 01113 reinsert_raw_breakpoint (bp); 01114 } 01115 01116 void 01117 reinsert_all_breakpoints (void) 01118 { 01119 struct process_info *proc = current_process (); 01120 struct raw_breakpoint *bp; 01121 01122 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next) 01123 if (!bp->inserted) 01124 reinsert_raw_breakpoint (bp); 01125 } 01126 01127 void 01128 check_breakpoints (CORE_ADDR stop_pc) 01129 { 01130 struct process_info *proc = current_process (); 01131 struct breakpoint *bp, **bp_link; 01132 01133 bp = proc->breakpoints; 01134 bp_link = &proc->breakpoints; 01135 01136 while (bp) 01137 { 01138 if (bp->raw->pc == stop_pc) 01139 { 01140 if (!bp->raw->inserted) 01141 { 01142 warning ("Hit a removed breakpoint?"); 01143 return; 01144 } 01145 01146 if (bp->handler != NULL && (*bp->handler) (stop_pc)) 01147 { 01148 *bp_link = bp->next; 01149 01150 release_breakpoint (proc, bp); 01151 01152 bp = *bp_link; 01153 continue; 01154 } 01155 } 01156 01157 bp_link = &bp->next; 01158 bp = *bp_link; 01159 } 01160 } 01161 01162 void 01163 set_breakpoint_data (const unsigned char *bp_data, int bp_len) 01164 { 01165 breakpoint_data = bp_data; 01166 breakpoint_len = bp_len; 01167 } 01168 01169 int 01170 breakpoint_here (CORE_ADDR addr) 01171 { 01172 return (find_raw_breakpoint_at (addr) != NULL); 01173 } 01174 01175 int 01176 breakpoint_inserted_here (CORE_ADDR addr) 01177 { 01178 struct raw_breakpoint *bp; 01179 01180 bp = find_raw_breakpoint_at (addr); 01181 01182 return (bp != NULL && bp->inserted); 01183 } 01184 01185 static int 01186 validate_inserted_breakpoint (struct raw_breakpoint *bp) 01187 { 01188 unsigned char *buf; 01189 int err; 01190 01191 gdb_assert (bp->inserted); 01192 01193 buf = alloca (breakpoint_len); 01194 err = (*the_target->read_memory) (bp->pc, buf, breakpoint_len); 01195 if (err || memcmp (buf, breakpoint_data, breakpoint_len) != 0) 01196 { 01197 /* Tag it as gone. */ 01198 bp->inserted = 0; 01199 bp->shlib_disabled = 1; 01200 return 0; 01201 } 01202 01203 return 1; 01204 } 01205 01206 static void 01207 delete_disabled_breakpoints (void) 01208 { 01209 struct process_info *proc = current_process (); 01210 struct breakpoint *bp, *next; 01211 01212 for (bp = proc->breakpoints; bp != NULL; bp = next) 01213 { 01214 next = bp->next; 01215 if (bp->raw->shlib_disabled) 01216 delete_breakpoint_1 (proc, bp); 01217 } 01218 } 01219 01220 /* Check if breakpoints we inserted still appear to be inserted. They 01221 may disappear due to a shared library unload, and worse, a new 01222 shared library may be reloaded at the same address as the 01223 previously unloaded one. If that happens, we should make sure that 01224 the shadow memory of the old breakpoints isn't used when reading or 01225 writing memory. */ 01226 01227 void 01228 validate_breakpoints (void) 01229 { 01230 struct process_info *proc = current_process (); 01231 struct breakpoint *bp; 01232 01233 for (bp = proc->breakpoints; bp != NULL; bp = bp->next) 01234 { 01235 if (bp->raw->inserted) 01236 validate_inserted_breakpoint (bp->raw); 01237 } 01238 01239 delete_disabled_breakpoints (); 01240 } 01241 01242 void 01243 check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len) 01244 { 01245 struct process_info *proc = current_process (); 01246 struct raw_breakpoint *bp = proc->raw_breakpoints; 01247 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps; 01248 CORE_ADDR mem_end = mem_addr + mem_len; 01249 int disabled_one = 0; 01250 01251 for (; jp != NULL; jp = jp->next) 01252 { 01253 CORE_ADDR bp_end = jp->pc + jp->length; 01254 CORE_ADDR start, end; 01255 int copy_offset, copy_len, buf_offset; 01256 01257 gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len 01258 || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length); 01259 01260 if (mem_addr >= bp_end) 01261 continue; 01262 if (jp->pc >= mem_end) 01263 continue; 01264 01265 start = jp->pc; 01266 if (mem_addr > start) 01267 start = mem_addr; 01268 01269 end = bp_end; 01270 if (end > mem_end) 01271 end = mem_end; 01272 01273 copy_len = end - start; 01274 copy_offset = start - jp->pc; 01275 buf_offset = start - mem_addr; 01276 01277 if (jp->inserted) 01278 memcpy (buf + buf_offset, 01279 fast_tracepoint_jump_shadow (jp) + copy_offset, 01280 copy_len); 01281 } 01282 01283 for (; bp != NULL; bp = bp->next) 01284 { 01285 CORE_ADDR bp_end = bp->pc + breakpoint_len; 01286 CORE_ADDR start, end; 01287 int copy_offset, copy_len, buf_offset; 01288 01289 gdb_assert (bp->old_data >= buf + mem_len 01290 || buf >= &bp->old_data[sizeof (bp->old_data)]); 01291 01292 if (mem_addr >= bp_end) 01293 continue; 01294 if (bp->pc >= mem_end) 01295 continue; 01296 01297 start = bp->pc; 01298 if (mem_addr > start) 01299 start = mem_addr; 01300 01301 end = bp_end; 01302 if (end > mem_end) 01303 end = mem_end; 01304 01305 copy_len = end - start; 01306 copy_offset = start - bp->pc; 01307 buf_offset = start - mem_addr; 01308 01309 if (bp->inserted) 01310 { 01311 if (validate_inserted_breakpoint (bp)) 01312 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len); 01313 else 01314 disabled_one = 1; 01315 } 01316 } 01317 01318 if (disabled_one) 01319 delete_disabled_breakpoints (); 01320 } 01321 01322 void 01323 check_mem_write (CORE_ADDR mem_addr, unsigned char *buf, 01324 const unsigned char *myaddr, int mem_len) 01325 { 01326 struct process_info *proc = current_process (); 01327 struct raw_breakpoint *bp = proc->raw_breakpoints; 01328 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps; 01329 CORE_ADDR mem_end = mem_addr + mem_len; 01330 int disabled_one = 0; 01331 01332 /* First fast tracepoint jumps, then breakpoint traps on top. */ 01333 01334 for (; jp != NULL; jp = jp->next) 01335 { 01336 CORE_ADDR jp_end = jp->pc + jp->length; 01337 CORE_ADDR start, end; 01338 int copy_offset, copy_len, buf_offset; 01339 01340 gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len 01341 || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length); 01342 gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len 01343 || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length); 01344 01345 if (mem_addr >= jp_end) 01346 continue; 01347 if (jp->pc >= mem_end) 01348 continue; 01349 01350 start = jp->pc; 01351 if (mem_addr > start) 01352 start = mem_addr; 01353 01354 end = jp_end; 01355 if (end > mem_end) 01356 end = mem_end; 01357 01358 copy_len = end - start; 01359 copy_offset = start - jp->pc; 01360 buf_offset = start - mem_addr; 01361 01362 memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset, 01363 myaddr + buf_offset, copy_len); 01364 if (jp->inserted) 01365 memcpy (buf + buf_offset, 01366 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len); 01367 } 01368 01369 for (; bp != NULL; bp = bp->next) 01370 { 01371 CORE_ADDR bp_end = bp->pc + breakpoint_len; 01372 CORE_ADDR start, end; 01373 int copy_offset, copy_len, buf_offset; 01374 01375 gdb_assert (bp->old_data >= myaddr + mem_len 01376 || myaddr >= &bp->old_data[sizeof (bp->old_data)]); 01377 01378 if (mem_addr >= bp_end) 01379 continue; 01380 if (bp->pc >= mem_end) 01381 continue; 01382 01383 start = bp->pc; 01384 if (mem_addr > start) 01385 start = mem_addr; 01386 01387 end = bp_end; 01388 if (end > mem_end) 01389 end = mem_end; 01390 01391 copy_len = end - start; 01392 copy_offset = start - bp->pc; 01393 buf_offset = start - mem_addr; 01394 01395 memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len); 01396 if (bp->inserted) 01397 { 01398 if (validate_inserted_breakpoint (bp)) 01399 memcpy (buf + buf_offset, breakpoint_data + copy_offset, copy_len); 01400 else 01401 disabled_one = 1; 01402 } 01403 } 01404 01405 if (disabled_one) 01406 delete_disabled_breakpoints (); 01407 } 01408 01409 /* Delete all breakpoints, and un-insert them from the inferior. */ 01410 01411 void 01412 delete_all_breakpoints (void) 01413 { 01414 struct process_info *proc = current_process (); 01415 01416 while (proc->breakpoints) 01417 delete_breakpoint_1 (proc, proc->breakpoints); 01418 } 01419 01420 /* Clear the "inserted" flag in all breakpoints. */ 01421 01422 void 01423 mark_breakpoints_out (struct process_info *proc) 01424 { 01425 struct raw_breakpoint *raw_bp; 01426 01427 for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next) 01428 raw_bp->inserted = 0; 01429 } 01430 01431 /* Release all breakpoints, but do not try to un-insert them from the 01432 inferior. */ 01433 01434 void 01435 free_all_breakpoints (struct process_info *proc) 01436 { 01437 mark_breakpoints_out (proc); 01438 01439 /* Note: use PROC explicitly instead of deferring to 01440 delete_all_breakpoints --- CURRENT_INFERIOR may already have been 01441 released when we get here. There should be no call to 01442 current_process from here on. */ 01443 while (proc->breakpoints) 01444 delete_breakpoint_1 (proc, proc->breakpoints); 01445 }