| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 1 | /* |
| 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 Machata | c0ab286 | 2012-05-05 00:06:17 +0200 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 28 | #include "config.h" |
| Petr Machata | c0ab286 | 2012-05-05 00:06:17 +0200 | [diff] [blame] | 29 | #include "breakpoint.h" |
| 30 | #include "debug.h" |
| 31 | #include "proc.h" |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 32 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 33 | void |
| Petr Machata | bc37326 | 2012-02-07 23:31:15 +0100 | [diff] [blame] | 34 | arch_enable_breakpoint(pid_t pid, struct breakpoint *sbp) |
| 35 | { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 36 | 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. Edwards | c46448f | 2011-03-07 16:15:48 +0000 | [diff] [blame] | 43 | 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 Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 50 | |
| Petr Machata | c0ab286 | 2012-05-05 00:06:17 +0200 | [diff] [blame] | 51 | debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d", |
| 52 | current.l, orig.l, sbp->arch.thumb_mode); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 53 | 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 Machata | c0ab286 | 2012-05-05 00:06:17 +0200 | [diff] [blame] | 56 | if (!sbp->arch.thumb_mode) { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 57 | 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 | } |
| Petr Machata | c897cb7 | 2012-05-05 01:26:58 +0200 | [diff] [blame^] | 63 | ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), |
| 64 | (void *)current.l); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 68 | void |
| Petr Machata | bc37326 | 2012-02-07 23:31:15 +0100 | [diff] [blame] | 69 | arch_disable_breakpoint(pid_t pid, const struct breakpoint *sbp) |
| 70 | { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 71 | unsigned int i, j; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 72 | |
| 73 | debug(1, "arch_disable_breakpoint(%d,%p)", pid, sbp->addr); |
| 74 | |
| 75 | for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) { |
| Michael K. Edwards | c46448f | 2011-03-07 16:15:48 +0000 | [diff] [blame] | 76 | union _ { long l; unsigned char b[SIZEOF_LONG]; }; |
| 77 | union _ orig, current; |
| 78 | unsigned char *bytes = current.b; |
| 79 | for (j = 0; j < sizeof(long); j++) { |
| 80 | orig.b[j] = sbp->orig_value[i * sizeof(long) + j]; |
| 81 | } |
| 82 | current.l = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 83 | |
| Petr Machata | c0ab286 | 2012-05-05 00:06:17 +0200 | [diff] [blame] | 84 | debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d", |
| 85 | current.l, orig.l, sbp->arch.thumb_mode); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 86 | for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) { |
| 87 | bytes[j] = sbp->orig_value[i * sizeof(long) + j]; |
| 88 | } |
| Petr Machata | c897cb7 | 2012-05-05 01:26:58 +0200 | [diff] [blame^] | 89 | ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), |
| 90 | (void *)current.l); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 93 | |
| 94 | int |
| 95 | arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp) |
| 96 | { |
| Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 97 | /* XXX That uintptr_t cast is there temporarily until |
| 98 | * target_address_t becomes integral type. */ |
| 99 | int thumb_mode = ((uintptr_t)sbp->addr) & 1; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 100 | if (thumb_mode) |
| Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 101 | sbp->addr = (void *)((uintptr_t)sbp->addr & ~1); |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 102 | sbp->arch.thumb_mode = thumb_mode | proc->thumb_mode; |
| Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 103 | /* XXX This doesn't seem like it belongs here. */ |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 104 | proc->thumb_mode = 0; |
| 105 | return 0; |
| 106 | } |
| Petr Machata | 8cce119 | 2012-03-25 01:37:19 +0100 | [diff] [blame] | 107 | |
| 108 | void |
| 109 | arch_breakpoint_destroy(struct breakpoint *sbp) |
| 110 | { |
| 111 | } |
| Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 112 | |
| 113 | int |
| 114 | arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp) |
| 115 | { |
| 116 | retp->arch.thumb_mode = sbp->arch.thumb_mode; |
| 117 | return 0; |
| 118 | } |