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