breakpoint.c revision a7af00db2231e99a4506e4f5587f9dd00b9d1175
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"
27#include "common.h"
28
29void
30arch_enable_breakpoint(pid_t pid, Breakpoint *sbp) {
31	unsigned int i, j;
32	const unsigned char break_insn[] = BREAKPOINT_VALUE;
33	const unsigned char thumb_break_insn[] = THUMB_BREAKPOINT_VALUE;
34
35	debug(1, "arch_enable_breakpoint(%d,%p)", pid, sbp->addr);
36
37	for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) {
38		long a = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0);
39		unsigned char *bytes = (unsigned char *)&a;
40
41		debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d", a, *(long *)&sbp->orig_value, sbp->thumb_mode);
42		for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) {
43
44			sbp->orig_value[i * sizeof(long) + j] = bytes[j];
45			if (!sbp->thumb_mode) {
46				bytes[j] = break_insn[i * sizeof(long) + j];
47			}
48			else if (j < THUMB_BREAKPOINT_LENGTH) {
49				bytes[j] = thumb_break_insn[i * sizeof(long) + j];
50			}
51		}
52		ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), a);
53	}
54}
55
56void
57arch_disable_breakpoint(pid_t pid, const Breakpoint *sbp) {
58	unsigned int i, j;
59
60	debug(1, "arch_disable_breakpoint(%d,%p)", pid, sbp->addr);
61
62	for (i = 0; i < 1 + ((BREAKPOINT_LENGTH - 1) / sizeof(long)); i++) {
63		long a = ptrace(PTRACE_PEEKTEXT, pid, sbp->addr + i * sizeof(long), 0);
64		unsigned char *bytes = (unsigned char *)&a;
65
66		debug(2, "current = 0x%lx, orig_value = 0x%lx, thumb_mode = %d", a, *(long *)&sbp->orig_value, sbp->thumb_mode);
67		for (j = 0; j < sizeof(long) && i * sizeof(long) + j < BREAKPOINT_LENGTH; j++) {
68			bytes[j] = sbp->orig_value[i * sizeof(long) + j];
69		}
70		ptrace(PTRACE_POKETEXT, pid, sbp->addr + i * sizeof(long), a);
71	}
72}
73