breakpoint.c revision c46448f4e5a4c124fbc75ca9b14697212e676893
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> 2663184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes#include "config.h" 27a7af00db2231e99a4506e4f5587f9dd00b9d1175Juan Cespedes#include "common.h" 2863184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 29f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid 301dec217e47f998c03c642561d98753c32683985cJuan Cespedesarch_enable_breakpoint(pid_t pid, Breakpoint *sbp) { 3163184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes unsigned int i, j; 3263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes const unsigned char break_insn[] = BREAKPOINT_VALUE; 3363184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes const unsigned char thumb_break_insn[] = THUMB_BREAKPOINT_VALUE; 3463184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 3563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes debug(1, "arch_enable_breakpoint(%d,%p)", pid, sbp->addr); 3663184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 3763184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) { 38c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards union _ { long l; unsigned char b[SIZEOF_LONG]; }; 39c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards union _ orig, current; 40c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards unsigned char *bytes = current.b; 41c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards for (j = 0; j < sizeof(long); j++) { 42c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards orig.b[j] = sbp->orig_value[i * sizeof(long) + j]; 43c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards } 44c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards current.l = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0); 4563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 46c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d", current.l, orig.l, sbp->thumb_mode); 4763184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) { 4863184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 4963184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes sbp->orig_value[i * sizeof(long) + j] = bytes[j]; 5063184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes if (!sbp->thumb_mode) { 5163184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes bytes[j] = break_insn[i * sizeof(long) + j]; 5263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 5363184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes else if (j < THUMB_BREAKPOINT_LENGTH) { 5463184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes bytes[j] = thumb_break_insn[i * sizeof(long) + j]; 5563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 5663184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 57c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), current.l); 5863184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 5963184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes} 6063184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 61f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid 621dec217e47f998c03c642561d98753c32683985cJuan Cespedesarch_disable_breakpoint(pid_t pid, const Breakpoint *sbp) { 6363184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes unsigned int i, j; 6463184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 6563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes debug(1, "arch_disable_breakpoint(%d,%p)", pid, sbp->addr); 6663184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 6763184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) { 68c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards union _ { long l; unsigned char b[SIZEOF_LONG]; }; 69c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards union _ orig, current; 70c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards unsigned char *bytes = current.b; 71c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards for (j = 0; j < sizeof(long); j++) { 72c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards orig.b[j] = sbp->orig_value[i * sizeof(long) + j]; 73c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards } 74c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards current.l = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0); 7563184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes 76c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d", current.l, orig.l, sbp->thumb_mode); 7763184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) { 7863184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes bytes[j] = sbp->orig_value[i * sizeof(long) + j]; 7963184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 80c46448f4e5a4c124fbc75ca9b14697212e676893Michael K. Edwards ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), current.l); 8163184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes } 8263184be8c577f5799e44db2a4e312a8240ad7751Juan Cespedes} 83