| 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> |
| 26 | #include "config.h" |
| Juan Cespedes | a7af00d | 2009-07-26 13:23:18 +0200 | [diff] [blame] | 27 | #include "common.h" |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 28 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 29 | void |
| Petr Machata | bc37326 | 2012-02-07 23:31:15 +0100 | [diff] [blame^] | 30 | arch_enable_breakpoint(pid_t pid, struct breakpoint *sbp) |
| 31 | { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 32 | unsigned int i, j; |
| 33 | const unsigned char break_insn[] = BREAKPOINT_VALUE; |
| 34 | const unsigned char thumb_break_insn[] = THUMB_BREAKPOINT_VALUE; |
| 35 | |
| 36 | debug(1, "arch_enable_breakpoint(%d,%p)", pid, sbp->addr); |
| 37 | |
| 38 | for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) { |
| Michael K. Edwards | c46448f | 2011-03-07 16:15:48 +0000 | [diff] [blame] | 39 | union _ { long l; unsigned char b[SIZEOF_LONG]; }; |
| 40 | union _ orig, current; |
| 41 | unsigned char *bytes = current.b; |
| 42 | for (j = 0; j < sizeof(long); j++) { |
| 43 | orig.b[j] = sbp->orig_value[i * sizeof(long) + j]; |
| 44 | } |
| 45 | current.l = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 46 | |
| Michael K. Edwards | c46448f | 2011-03-07 16:15:48 +0000 | [diff] [blame] | 47 | debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d", current.l, orig.l, sbp->thumb_mode); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 48 | for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) { |
| 49 | |
| 50 | sbp->orig_value[i * sizeof(long) + j] = bytes[j]; |
| 51 | if (!sbp->thumb_mode) { |
| 52 | bytes[j] = break_insn[i * sizeof(long) + j]; |
| 53 | } |
| 54 | else if (j < THUMB_BREAKPOINT_LENGTH) { |
| 55 | bytes[j] = thumb_break_insn[i * sizeof(long) + j]; |
| 56 | } |
| 57 | } |
| Michael K. Edwards | c46448f | 2011-03-07 16:15:48 +0000 | [diff] [blame] | 58 | ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), current.l); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 62 | void |
| Petr Machata | bc37326 | 2012-02-07 23:31:15 +0100 | [diff] [blame^] | 63 | arch_disable_breakpoint(pid_t pid, const struct breakpoint *sbp) |
| 64 | { |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 65 | unsigned int i, j; |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 66 | |
| 67 | debug(1, "arch_disable_breakpoint(%d,%p)", pid, sbp->addr); |
| 68 | |
| 69 | for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) { |
| Michael K. Edwards | c46448f | 2011-03-07 16:15:48 +0000 | [diff] [blame] | 70 | union _ { long l; unsigned char b[SIZEOF_LONG]; }; |
| 71 | union _ orig, current; |
| 72 | unsigned char *bytes = current.b; |
| 73 | for (j = 0; j < sizeof(long); j++) { |
| 74 | orig.b[j] = sbp->orig_value[i * sizeof(long) + j]; |
| 75 | } |
| 76 | current.l = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 77 | |
| Michael K. Edwards | c46448f | 2011-03-07 16:15:48 +0000 | [diff] [blame] | 78 | debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d", current.l, orig.l, sbp->thumb_mode); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 79 | for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) { |
| 80 | bytes[j] = sbp->orig_value[i * sizeof(long) + j]; |
| 81 | } |
| Michael K. Edwards | c46448f | 2011-03-07 16:15:48 +0000 | [diff] [blame] | 82 | ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), current.l); |
| Juan Cespedes | 63184be | 2008-12-10 13:30:12 +0100 | [diff] [blame] | 83 | } |
| 84 | } |