blob: 38a68cc4a6886f496bd8e1b0dccd6c8b6d79ce5f [file] [log] [blame]
Juan Cespedesa4850832002-03-03 02:37:50 +01001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include <sys/ptrace.h>
6#include "arch.h"
7#include "options.h"
8#include "output.h"
9
10static unsigned char break_insn[] = BREAKPOINT_VALUE;
11
12void
13enable_breakpoint(pid_t pid, struct breakpoint * sbp) {
14 int i,j;
15
16 if (opt_d>1) {
Juan Cespedesefe85f02004-04-04 01:31:38 +020017 output_line(0, "enable_breakpoint(%d,%p)", pid, sbp->addr);
Juan Cespedesa4850832002-03-03 02:37:50 +010018 }
19
20 for(i=0; i < 1+((BREAKPOINT_LENGTH-1)/sizeof(long)); i++) {
21 long a = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i*sizeof(long), 0);
22 for(j=0; j<sizeof(long) && i*sizeof(long)+j < BREAKPOINT_LENGTH; j++) {
23 unsigned char * bytes = (unsigned char *)&a;
24
Juan Cespedes5c3fe062004-06-14 18:08:37 +020025 sbp->orig_value[i*sizeof(long)+j] = bytes[+j];
26 bytes[j] = break_insn[i*sizeof(long)+j];
Juan Cespedesa4850832002-03-03 02:37:50 +010027 }
28 ptrace(PTRACE_POKETEXT, pid, sbp->addr + i*sizeof(long), a);
29 }
30}
31
32void
33disable_breakpoint(pid_t pid, const struct breakpoint * sbp) {
34 int i,j;
35
36 if (opt_d>1) {
Juan Cespedesefe85f02004-04-04 01:31:38 +020037 output_line(0, "disable_breakpoint(%d,%p)", pid, sbp->addr);
Juan Cespedesa4850832002-03-03 02:37:50 +010038 }
39
40 for(i=0; i < 1+((BREAKPOINT_LENGTH-1)/sizeof(long)); i++) {
41 long a = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i*sizeof(long), 0);
42 for(j=0; j<sizeof(long) && i*sizeof(long)+j < BREAKPOINT_LENGTH; j++) {
43 unsigned char * bytes = (unsigned char *)&a;
44
Juan Cespedes5c3fe062004-06-14 18:08:37 +020045 bytes[j] = sbp->orig_value[i*sizeof(long)+j];
Juan Cespedesa4850832002-03-03 02:37:50 +010046 }
47 ptrace(PTRACE_POKETEXT, pid, sbp->addr + i*sizeof(long), a);
48 }
49}