blob: ff28fa6da1aeac745eeb03558385c0b3eff03211 [file] [log] [blame]
Juan Cespedes63184be2008-12-10 13:30:12 +01001/*
2 * This file is part of ltrace.
3 *
4 * Copyright (C) 2007 by Instituto Nokia de Tecnologia (INdT)
5 *
6 * Author: Anderson Lizardo <anderson.lizardo@indt.org.br>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 * Modified from sysdeps/linux-gnu/breakpoint.c and added ARM Thumb support.
23 */
24
25#include <sys/ptrace.h>
Petr Machatac0ab2862012-05-05 00:06:17 +020026#include <sys/types.h>
27
Juan Cespedes63184be2008-12-10 13:30:12 +010028#include "config.h"
Petr Machatac0ab2862012-05-05 00:06:17 +020029#include "breakpoint.h"
30#include "debug.h"
31#include "proc.h"
Juan Cespedes63184be2008-12-10 13:30:12 +010032
Juan Cespedesf1350522008-12-16 18:19:58 +010033void
Petr Machatabc373262012-02-07 23:31:15 +010034arch_enable_breakpoint(pid_t pid, struct breakpoint *sbp)
35{
Juan Cespedes63184be2008-12-10 13:30:12 +010036 unsigned int i, j;
37 const unsigned char break_insn[] = BREAKPOINT_VALUE;
38 const unsigned char thumb_break_insn[] = THUMB_BREAKPOINT_VALUE;
39
40 debug(1, "arch_enable_breakpoint(%d,%p)", pid, sbp->addr);
41
42 for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) {
Michael K. Edwardsc46448f2011-03-07 16:15:48 +000043 union _ { long l; unsigned char b[SIZEOF_LONG]; };
44 union _ orig, current;
45 unsigned char *bytes = current.b;
46 for (j = 0; j < sizeof(long); j++) {
47 orig.b[j] = sbp->orig_value[i * sizeof(long) + j];
48 }
49 current.l = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0);
Juan Cespedes63184be2008-12-10 13:30:12 +010050
Petr Machatac0ab2862012-05-05 00:06:17 +020051 debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d",
52 current.l, orig.l, sbp->arch.thumb_mode);
Juan Cespedes63184be2008-12-10 13:30:12 +010053 for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) {
54
55 sbp->orig_value[i * sizeof(long) + j] = bytes[j];
Petr Machatac0ab2862012-05-05 00:06:17 +020056 if (!sbp->arch.thumb_mode) {
Juan Cespedes63184be2008-12-10 13:30:12 +010057 bytes[j] = break_insn[i * sizeof(long) + j];
58 }
59 else if (j < THUMB_BREAKPOINT_LENGTH) {
60 bytes[j] = thumb_break_insn[i * sizeof(long) + j];
61 }
62 }
Michael K. Edwardsc46448f2011-03-07 16:15:48 +000063 ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), current.l);
Juan Cespedes63184be2008-12-10 13:30:12 +010064 }
65}
66
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)
69{
Juan Cespedes63184be2008-12-10 13:30:12 +010070 unsigned int i, j;
Juan Cespedes63184be2008-12-10 13:30:12 +010071
72 debug(1, "arch_disable_breakpoint(%d,%p)", pid, sbp->addr);
73
74 for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) {
Michael K. Edwardsc46448f2011-03-07 16:15:48 +000075 union _ { long l; unsigned char b[SIZEOF_LONG]; };
76 union _ orig, current;
77 unsigned char *bytes = current.b;
78 for (j = 0; j < sizeof(long); j++) {
79 orig.b[j] = sbp->orig_value[i * sizeof(long) + j];
80 }
81 current.l = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0);
Juan Cespedes63184be2008-12-10 13:30:12 +010082
Petr Machatac0ab2862012-05-05 00:06:17 +020083 debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d",
84 current.l, orig.l, sbp->arch.thumb_mode);
Juan Cespedes63184be2008-12-10 13:30:12 +010085 for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) {
86 bytes[j] = sbp->orig_value[i * sizeof(long) + j];
87 }
Michael K. Edwardsc46448f2011-03-07 16:15:48 +000088 ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), current.l);
Juan Cespedes63184be2008-12-10 13:30:12 +010089 }
90}
Petr Machata2b46cfc2012-02-18 11:17:29 +010091
92int
93arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp)
94{
Petr Machatad3cc9882012-04-13 21:40:23 +020095 /* XXX That uintptr_t cast is there temporarily until
96 * target_address_t becomes integral type. */
97 int thumb_mode = ((uintptr_t)sbp->addr) & 1;
Petr Machata2b46cfc2012-02-18 11:17:29 +010098 if (thumb_mode)
Petr Machatad3cc9882012-04-13 21:40:23 +020099 sbp->addr = (void *)((uintptr_t)sbp->addr & ~1);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100100 sbp->arch.thumb_mode = thumb_mode | proc->thumb_mode;
Petr Machatad3cc9882012-04-13 21:40:23 +0200101 /* XXX This doesn't seem like it belongs here. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100102 proc->thumb_mode = 0;
103 return 0;
104}
Petr Machata8cce1192012-03-25 01:37:19 +0100105
106void
107arch_breakpoint_destroy(struct breakpoint *sbp)
108{
109}
Petr Machatad3cc9882012-04-13 21:40:23 +0200110
111int
112arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp)
113{
114 retp->arch.thumb_mode = sbp->arch.thumb_mode;
115 return 0;
116}