blob: 5f60bec339fdf54b8a6ec14f3028ee52c23768b2 [file] [log] [blame]
Juan Cespedesa4850832002-03-03 02:37:50 +01001#include "config.h"
Juan Cespedesa4850832002-03-03 02:37:50 +01002
3#include <sys/ptrace.h>
Petr Machata81c65272012-03-21 04:57:25 +01004#include <errno.h>
5#include <error.h>
Petr Machataf0f90ed2012-04-13 18:44:12 +02006#include <string.h>
Juan Cespedes61da3372009-07-03 11:55:44 +02007
Juan Cespedes8d1b92b2009-07-03 10:39:34 +02008#include "common.h"
Petr Machatad3dc17b2012-03-21 03:23:25 +01009#include "sysdep.h"
Petr Machata9294d822012-02-07 12:35:58 +010010#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010011#include "proc.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010012#include "library.h"
Juan Cespedesa4850832002-03-03 02:37:50 +010013
Petr Machataf789c9c2011-07-09 10:54:27 +020014#ifdef ARCH_HAVE_ENABLE_BREAKPOINT
Petr Machatabc373262012-02-07 23:31:15 +010015extern void arch_enable_breakpoint(pid_t, struct breakpoint *);
Petr Machataf789c9c2011-07-09 10:54:27 +020016#else /* ARCH_HAVE_ENABLE_BREAKPOINT */
Juan Cespedesf1350522008-12-16 18:19:58 +010017void
Petr Machatabc373262012-02-07 23:31:15 +010018arch_enable_breakpoint(pid_t pid, struct breakpoint *sbp)
Petr Machataf789c9c2011-07-09 10:54:27 +020019{
Petr Machata28e80f62011-08-09 23:33:52 +020020 static unsigned char break_insn[] = BREAKPOINT_VALUE;
Paul Gilliam3f1219f2006-04-24 18:25:38 +020021 unsigned int i, j;
Juan Cespedesa4850832002-03-03 02:37:50 +010022
Petr Machataf0f90ed2012-04-13 18:44:12 +020023 debug(DEBUG_PROCESS,
24 "arch_enable_breakpoint: pid=%d, addr=%p, symbol=%s",
Petr Machata050b0a62012-04-03 01:30:30 +020025 pid, sbp->addr, breakpoint_name(sbp));
Juan Cespedesa4850832002-03-03 02:37:50 +010026
Ian Wienand2d45b1a2006-02-20 22:48:07 +010027 for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) {
Petr Machata26627682011-07-08 18:15:32 +020028 long a = ptrace(PTRACE_PEEKTEXT, pid,
29 sbp->addr + i * sizeof(long), 0);
Petr Machata81c65272012-03-21 04:57:25 +010030 if (a == -1 && errno) {
Petr Machataf0f90ed2012-04-13 18:44:12 +020031 error(0, errno,
32 "enable_breakpoint pid=%d, addr=%p, symbol=%s",
33 pid, sbp->addr, breakpoint_name(sbp));
Petr Machata81c65272012-03-21 04:57:25 +010034 return;
35 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010036 for (j = 0;
37 j < sizeof(long)
38 && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) {
39 unsigned char *bytes = (unsigned char *)&a;
Juan Cespedesa4850832002-03-03 02:37:50 +010040
Paul Gilliam76c61f12006-06-14 06:55:21 +020041 sbp->orig_value[i * sizeof(long) + j] = bytes[j];
Ian Wienand2d45b1a2006-02-20 22:48:07 +010042 bytes[j] = break_insn[i * sizeof(long) + j];
Juan Cespedesa4850832002-03-03 02:37:50 +010043 }
Petr Machata949a56a2012-04-03 00:32:03 +020044 a = ptrace(PTRACE_POKETEXT, pid,
45 sbp->addr + i * sizeof(long), a);
Petr Machata81c65272012-03-21 04:57:25 +010046 if (a == -1) {
Petr Machataf0f90ed2012-04-13 18:44:12 +020047 error(0, errno,
48 "enable_breakpoint pid=%d, addr=%p, symbol=%s",
49 pid, sbp->addr, breakpoint_name(sbp));
Petr Machata81c65272012-03-21 04:57:25 +010050 return;
51 }
Juan Cespedesa4850832002-03-03 02:37:50 +010052 }
53}
Ian Wienand2d45b1a2006-02-20 22:48:07 +010054#endif /* ARCH_HAVE_ENABLE_BREAKPOINT */
Juan Cespedesa4850832002-03-03 02:37:50 +010055
Petr Machataf789c9c2011-07-09 10:54:27 +020056void
Petr Machatabc373262012-02-07 23:31:15 +010057enable_breakpoint(Process *proc, struct breakpoint *sbp)
58{
Petr Machata050b0a62012-04-03 01:30:30 +020059 debug(DEBUG_PROCESS, "enable_breakpoint: pid=%d, addr=%p, symbol=%s",
60 proc->pid, sbp->addr, breakpoint_name(sbp));
Petr Machataf789c9c2011-07-09 10:54:27 +020061 arch_enable_breakpoint(proc->pid, sbp);
62}
63
Ian Wienand5570a772006-02-17 02:00:00 +010064#ifdef ARCH_HAVE_DISABLE_BREAKPOINT
Petr Machatabc373262012-02-07 23:31:15 +010065extern void arch_disable_breakpoint(pid_t, const struct breakpoint *sbp);
Petr Machataf789c9c2011-07-09 10:54:27 +020066#else /* ARCH_HAVE_DISABLE_BREAKPOINT */
Juan Cespedesf1350522008-12-16 18:19:58 +010067void
Petr Machatabc373262012-02-07 23:31:15 +010068arch_disable_breakpoint(pid_t pid, const struct breakpoint *sbp)
Petr Machataf789c9c2011-07-09 10:54:27 +020069{
Paul Gilliam3f1219f2006-04-24 18:25:38 +020070 unsigned int i, j;
Juan Cespedesa4850832002-03-03 02:37:50 +010071
Petr Machataf0f90ed2012-04-13 18:44:12 +020072 debug(DEBUG_PROCESS,
73 "arch_disable_breakpoint: pid=%d, addr=%p, symbol=%s",
Petr Machata050b0a62012-04-03 01:30:30 +020074 pid, sbp->addr, breakpoint_name(sbp));
Juan Cespedesa4850832002-03-03 02:37:50 +010075
Ian Wienand2d45b1a2006-02-20 22:48:07 +010076 for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) {
Petr Machata949a56a2012-04-03 00:32:03 +020077 long a = ptrace(PTRACE_PEEKTEXT, pid,
78 sbp->addr + i * sizeof(long), 0);
79 if (a == -1 && errno) {
80 error(0, errno, "disable_breakpoint pid=%d, addr=%p",
81 pid, sbp->addr);
82 return;
83 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 for (j = 0;
85 j < sizeof(long)
86 && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) {
87 unsigned char *bytes = (unsigned char *)&a;
Juan Cespedesa4850832002-03-03 02:37:50 +010088
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 bytes[j] = sbp->orig_value[i * sizeof(long) + j];
Juan Cespedesa4850832002-03-03 02:37:50 +010090 }
Petr Machata949a56a2012-04-03 00:32:03 +020091 a = ptrace(PTRACE_POKETEXT, pid,
92 sbp->addr + i * sizeof(long), a);
93 if (a == -1 && errno) {
94 error(0, errno, "disable_breakpoint pid=%d, addr=%p",
95 pid, sbp->addr);
96 return;
97 }
Juan Cespedesa4850832002-03-03 02:37:50 +010098 }
99}
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100100#endif /* ARCH_HAVE_DISABLE_BREAKPOINT */
Petr Machataf789c9c2011-07-09 10:54:27 +0200101
102void
Petr Machatabc373262012-02-07 23:31:15 +0100103disable_breakpoint(Process *proc, struct breakpoint *sbp)
104{
Petr Machata050b0a62012-04-03 01:30:30 +0200105 debug(DEBUG_PROCESS, "disable_breakpoint: pid=%d, addr=%p, symbol=%s",
106 proc->pid, sbp->addr, breakpoint_name(sbp));
Petr Machataf789c9c2011-07-09 10:54:27 +0200107 arch_disable_breakpoint(proc->pid, sbp);
108}