breakpoint.c revision c897cb796dc4a7d256cbfbf0137ef7cdff9e8ece
163184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes/* 263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * This file is part of ltrace. 363184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * 463184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * Copyright (C) 2007 by Instituto Nokia de Tecnologia (INdT) 563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * 663184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * Author: Anderson Lizardo <anderson.lizardo@indt.org.br> 763184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * 863184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * This program is free software; you can redistribute it and/or 963184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * modify it under the terms of the GNU General Public License 1063184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * version 2 as published by the Free Software Foundation. 1163184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * 1263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * This program is distributed in the hope that it will be useful, but 1363184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * WITHOUT ANY WARRANTY; without even the implied warranty of 1463184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * General Public License for more details. 1663184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * 1763184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * You should have received a copy of the GNU General Public License 1863184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * along with this program; if not, write to the Free Software 1963184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 2063184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * 02110-1301 USA 2163184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * 2263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes * Modified from sysdeps/linux-gnu/breakpoint.c and added ARM Thumb support. 2363184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes */ 2463184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 2563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes#include <sys/ptrace.h> 26c0ab28673d03bdf8f264556bde0d3806ea062322Petr Machata#include <sys/types.h> 27c0ab28673d03bdf8f264556bde0d3806ea062322Petr Machata 2863184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes#include "config.h" 29c0ab28673d03bdf8f264556bde0d3806ea062322Petr Machata#include "breakpoint.h" 30c0ab28673d03bdf8f264556bde0d3806ea062322Petr Machata#include "debug.h" 31c0ab28673d03bdf8f264556bde0d3806ea062322Petr Machata#include "proc.h" 3263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 33f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid 34bc37326ace5c70e57928c000162cffbcca9afb77Petr Machataarch_enable_breakpoint(pid_t pid, struct breakpoint *sbp) 35bc37326ace5c70e57928c000162cffbcca9afb77Petr Machata{ 3663184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes unsigned int i, j; 3763184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes const unsigned char break_insn[] = BREAKPOINT_VALUE; 3863184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes const unsigned char thumb_break_insn[] = THUMB_BREAKPOINT_VALUE; 3963184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 4063184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes debug(1, "arch_enable_breakpoint(%d,%p)", pid, sbp->addr); 4163184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 4263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) { 43c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards union _ { long l; unsigned char b[SIZEOF_LONG]; }; 44c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards union _ orig, current; 45c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards unsigned char *bytes = current.b; 46c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards for (j = 0; j < sizeof(long); j++) { 47c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards orig.b[j] = sbp->orig_value[i * sizeof(long) + j]; 48c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards } 49c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards current.l = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0); 5063184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 51c0ab28673d03bdf8f264556bde0d3806ea062322Petr Machata debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d", 52c0ab28673d03bdf8f264556bde0d3806ea062322Petr Machata current.l, orig.l, sbp->arch.thumb_mode); 5363184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) { 5463184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 5563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes sbp->orig_value[i * sizeof(long) + j] = bytes[j]; 56c0ab28673d03bdf8f264556bde0d3806ea062322Petr Machata if (!sbp->arch.thumb_mode) { 5763184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes bytes[j] = break_insn[i * sizeof(long) + j]; 5863184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 5963184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes else if (j < THUMB_BREAKPOINT_LENGTH) { 6063184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes bytes[j] = thumb_break_insn[i * sizeof(long) + j]; 6163184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 6263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 63c897cb796dc4a7d256cbfbf0137ef7cdff9e8ecePetr Machata ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), 64c897cb796dc4a7d256cbfbf0137ef7cdff9e8ecePetr Machata (void *)current.l); 6563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 6663184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes} 6763184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 68f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid 69bc37326ace5c70e57928c000162cffbcca9afb77Petr Machataarch_disable_breakpoint(pid_t pid, const struct breakpoint *sbp) 70bc37326ace5c70e57928c000162cffbcca9afb77Petr Machata{ 7163184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes unsigned int i, j; 7263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 7363184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes debug(1, "arch_disable_breakpoint(%d,%p)", pid, sbp->addr); 7463184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 7563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) { 76c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards union _ { long l; unsigned char b[SIZEOF_LONG]; }; 77c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards union _ orig, current; 78c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards unsigned char *bytes = current.b; 79c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards for (j = 0; j < sizeof(long); j++) { 80c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards orig.b[j] = sbp->orig_value[i * sizeof(long) + j]; 81c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards } 82c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards current.l = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0); 8363184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 84c0ab28673d03bdf8f264556bde0d3806ea062322Petr Machata debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d", 85c0ab28673d03bdf8f264556bde0d3806ea062322Petr Machata current.l, orig.l, sbp->arch.thumb_mode); 8663184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) { 8763184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes bytes[j] = sbp->orig_value[i * sizeof(long) + j]; 8863184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 89c897cb796dc4a7d256cbfbf0137ef7cdff9e8ecePetr Machata ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), 90c897cb796dc4a7d256cbfbf0137ef7cdff9e8ecePetr Machata (void *)current.l); 9163184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 9263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes} 932b46cfc1127d390eddd9593fe5ce5399c1f68130Petr Machata 942b46cfc1127d390eddd9593fe5ce5399c1f68130Petr Machataint 952b46cfc1127d390eddd9593fe5ce5399c1f68130Petr Machataarch_breakpoint_init(struct Process *proc, struct breakpoint *sbp) 962b46cfc1127d390eddd9593fe5ce5399c1f68130Petr Machata{ 97d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machata /* XXX That uintptr_t cast is there temporarily until 98d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machata * target_address_t becomes integral type. */ 99d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machata int thumb_mode = ((uintptr_t)sbp->addr) & 1; 1002b46cfc1127d390eddd9593fe5ce5399c1f68130Petr Machata if (thumb_mode) 101d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machata sbp->addr = (void *)((uintptr_t)sbp->addr & ~1); 1022b46cfc1127d390eddd9593fe5ce5399c1f68130Petr Machata sbp->arch.thumb_mode = thumb_mode | proc->thumb_mode; 103d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machata /* XXX This doesn't seem like it belongs here. */ 1042b46cfc1127d390eddd9593fe5ce5399c1f68130Petr Machata proc->thumb_mode = 0; 1052b46cfc1127d390eddd9593fe5ce5399c1f68130Petr Machata return 0; 1062b46cfc1127d390eddd9593fe5ce5399c1f68130Petr Machata} 1078cce1193ebd35cb5a8b288bc7325cdda1b8ffe50Petr Machata 1088cce1193ebd35cb5a8b288bc7325cdda1b8ffe50Petr Machatavoid 1098cce1193ebd35cb5a8b288bc7325cdda1b8ffe50Petr Machataarch_breakpoint_destroy(struct breakpoint *sbp) 1108cce1193ebd35cb5a8b288bc7325cdda1b8ffe50Petr Machata{ 1118cce1193ebd35cb5a8b288bc7325cdda1b8ffe50Petr Machata} 112d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machata 113d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machataint 114d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machataarch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp) 115d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machata{ 116d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machata retp->arch.thumb_mode = sbp->arch.thumb_mode; 117d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machata return 0; 118d3cc9889fdfe2e523e99ca5f664f8ae4b3936612Petr Machata} 119