blob: c026aa7bb737afbef5bd2f2a0badd8ef244b8480 [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>
Ian Wienand5570a772006-02-17 02:00:00 +01004#include <string.h>
Petr Machata81c65272012-03-21 04:57:25 +01005#include <errno.h>
6#include <error.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 Machata050b0a62012-04-03 01:30:30 +020023 debug(DEBUG_PROCESS, "enable_breakpoint: pid=%d, addr=%p, symbol=%s",
24 pid, sbp->addr, breakpoint_name(sbp));
Juan Cespedesa4850832002-03-03 02:37:50 +010025
Ian Wienand2d45b1a2006-02-20 22:48:07 +010026 for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) {
Petr Machata26627682011-07-08 18:15:32 +020027 long a = ptrace(PTRACE_PEEKTEXT, pid,
28 sbp->addr + i * sizeof(long), 0);
Petr Machata81c65272012-03-21 04:57:25 +010029 if (a == -1 && errno) {
Petr Machata949a56a2012-04-03 00:32:03 +020030 error(0, errno, "enable_breakpoint pid=%d, addr=%p",
31 pid, sbp->addr);
Petr Machata81c65272012-03-21 04:57:25 +010032 return;
33 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010034 for (j = 0;
35 j < sizeof(long)
36 && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) {
37 unsigned char *bytes = (unsigned char *)&a;
Juan Cespedesa4850832002-03-03 02:37:50 +010038
Paul Gilliam76c61f12006-06-14 06:55:21 +020039 sbp->orig_value[i * sizeof(long) + j] = bytes[j];
Ian Wienand2d45b1a2006-02-20 22:48:07 +010040 bytes[j] = break_insn[i * sizeof(long) + j];
Juan Cespedesa4850832002-03-03 02:37:50 +010041 }
Petr Machata949a56a2012-04-03 00:32:03 +020042 a = ptrace(PTRACE_POKETEXT, pid,
43 sbp->addr + i * sizeof(long), a);
Petr Machata81c65272012-03-21 04:57:25 +010044 if (a == -1) {
Petr Machata949a56a2012-04-03 00:32:03 +020045 error(0, errno, "enable_breakpoint pid=%d, addr=%p",
46 pid, sbp->addr);
Petr Machata81c65272012-03-21 04:57:25 +010047 return;
48 }
Juan Cespedesa4850832002-03-03 02:37:50 +010049 }
50}
Ian Wienand2d45b1a2006-02-20 22:48:07 +010051#endif /* ARCH_HAVE_ENABLE_BREAKPOINT */
Juan Cespedesa4850832002-03-03 02:37:50 +010052
Petr Machataf789c9c2011-07-09 10:54:27 +020053void
Petr Machatabc373262012-02-07 23:31:15 +010054enable_breakpoint(Process *proc, struct breakpoint *sbp)
55{
Petr Machata050b0a62012-04-03 01:30:30 +020056 debug(DEBUG_PROCESS, "enable_breakpoint: pid=%d, addr=%p, symbol=%s",
57 proc->pid, sbp->addr, breakpoint_name(sbp));
Petr Machataf789c9c2011-07-09 10:54:27 +020058 arch_enable_breakpoint(proc->pid, sbp);
59}
60
Ian Wienand5570a772006-02-17 02:00:00 +010061#ifdef ARCH_HAVE_DISABLE_BREAKPOINT
Petr Machatabc373262012-02-07 23:31:15 +010062extern void arch_disable_breakpoint(pid_t, const struct breakpoint *sbp);
Petr Machataf789c9c2011-07-09 10:54:27 +020063#else /* ARCH_HAVE_DISABLE_BREAKPOINT */
Juan Cespedesf1350522008-12-16 18:19:58 +010064void
Petr Machatabc373262012-02-07 23:31:15 +010065arch_disable_breakpoint(pid_t pid, const struct breakpoint *sbp)
Petr Machataf789c9c2011-07-09 10:54:27 +020066{
Paul Gilliam3f1219f2006-04-24 18:25:38 +020067 unsigned int i, j;
Juan Cespedesa4850832002-03-03 02:37:50 +010068
Petr Machata050b0a62012-04-03 01:30:30 +020069 debug(DEBUG_PROCESS, "disable_breakpoint: pid=%d, addr=%p, symbol=%s",
70 pid, sbp->addr, breakpoint_name(sbp));
Juan Cespedesa4850832002-03-03 02:37:50 +010071
Ian Wienand2d45b1a2006-02-20 22:48:07 +010072 for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) {
Petr Machata949a56a2012-04-03 00:32:03 +020073 long a = ptrace(PTRACE_PEEKTEXT, pid,
74 sbp->addr + i * sizeof(long), 0);
75 if (a == -1 && errno) {
76 error(0, errno, "disable_breakpoint pid=%d, addr=%p",
77 pid, sbp->addr);
78 return;
79 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 for (j = 0;
81 j < sizeof(long)
82 && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) {
83 unsigned char *bytes = (unsigned char *)&a;
Juan Cespedesa4850832002-03-03 02:37:50 +010084
Ian Wienand2d45b1a2006-02-20 22:48:07 +010085 bytes[j] = sbp->orig_value[i * sizeof(long) + j];
Juan Cespedesa4850832002-03-03 02:37:50 +010086 }
Petr Machata949a56a2012-04-03 00:32:03 +020087 a = ptrace(PTRACE_POKETEXT, pid,
88 sbp->addr + i * sizeof(long), a);
89 if (a == -1 && errno) {
90 error(0, errno, "disable_breakpoint pid=%d, addr=%p",
91 pid, sbp->addr);
92 return;
93 }
Juan Cespedesa4850832002-03-03 02:37:50 +010094 }
95}
Ian Wienand2d45b1a2006-02-20 22:48:07 +010096#endif /* ARCH_HAVE_DISABLE_BREAKPOINT */
Petr Machataf789c9c2011-07-09 10:54:27 +020097
98void
Petr Machatabc373262012-02-07 23:31:15 +010099disable_breakpoint(Process *proc, struct breakpoint *sbp)
100{
Petr Machata050b0a62012-04-03 01:30:30 +0200101 debug(DEBUG_PROCESS, "disable_breakpoint: pid=%d, addr=%p, symbol=%s",
102 proc->pid, sbp->addr, breakpoint_name(sbp));
Petr Machataf789c9c2011-07-09 10:54:27 +0200103 arch_disable_breakpoint(proc->pid, sbp);
104}