2 * This file is part of ltrace.
3 * Copyright (C) 2010,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2011 Andreas Schwab
5 * Copyright (C) 2002,2004,2008,2009 Juan Cespedes
6 * Copyright (C) 2008 Luis Machado, IBM Corporation
7 * Copyright (C) 2006 Ian Wienand
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
32 #include <sys/types.h>
36 #include "breakpoint.h"
43 #if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
44 # define PTRACE_PEEKUSER PTRACE_PEEKUSR
47 #if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
48 # define PTRACE_POKEUSER PTRACE_POKEUSR
52 get_arch_dep(Process *proc) {
54 proc->mask_32bit = (proc->e_machine == EM_PPC);
58 #define SYSCALL_INSN 0x44000002
60 /* Returns 1 if syscall, 2 if sysret, 0 otherwise. */
62 syscall_p(Process *proc, int status, int *sysnum) {
63 if (WIFSTOPPED(status)
64 && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
65 long pc = (long)get_instruction_pointer(proc);
67 (int)ptrace(PTRACE_PEEKTEXT, proc->pid, pc - sizeof(long),
70 if (insn == SYSCALL_INSN) {
72 (int)ptrace(PTRACE_PEEKUSER, proc->pid,
73 sizeof(long) * PT_R0, 0);
74 if (proc->callstack_depth > 0 &&
75 proc->callstack[proc->callstack_depth - 1].is_syscall &&
76 proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
85 /* The atomic skip code is mostly taken from GDB. */
87 /* In plt.h. XXX make this official interface. */
88 int read_target_4(struct Process *proc, arch_addr_t addr, uint32_t *lp);
91 arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
92 int (*add_cb)(void *addr, void *data),
95 arch_addr_t ip = get_instruction_pointer(proc);
96 struct breakpoint *other = address2bpstruct(proc->leader, ip);
98 debug(1, "arch_atomic_singlestep pid=%d addr=%p %s(%p)",
99 proc->pid, ip, breakpoint_name(sbp), sbp->addr);
101 /* If the original instruction was lwarx/ldarx, we can't
102 * single-step over it, instead we have to execute the whole
103 * atomic block at once. */
106 char buf[BREAKPOINT_LENGTH];
109 memcpy(u.buf, sbp->orig_value, BREAKPOINT_LENGTH);
110 } else if (read_target_4(proc, ip, &u.insn) < 0) {
111 fprintf(stderr, "couldn't read instruction at IP %p\n", ip);
112 /* Do the normal singlestep. */
116 if ((u.insn & LWARX_MASK) != LWARX_INSTRUCTION
117 && (u.insn & LWARX_MASK) != LDARX_INSTRUCTION)
120 debug(1, "singlestep over atomic block at %p", ip);
123 arch_addr_t addr = ip;
124 for (insn_count = 0; ; ++insn_count) {
126 unsigned long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
127 if (l == (unsigned long)-1 && errno)
136 /* If a conditional branch is found, put a breakpoint
137 * in its destination address. */
138 if ((insn & BRANCH_MASK) == BC_INSN) {
139 arch_addr_t branch_addr = ppc_branch_dest(addr, insn);
140 debug(1, "pid=%d, branch in atomic block from %p to %p",
141 proc->pid, addr, branch_addr);
142 if (add_cb(branch_addr, add_cb_data) < 0)
146 /* Assume that the atomic sequence ends with a
147 * stwcx/stdcx instruction. */
148 if ((insn & STWCX_MASK) == STWCX_INSTRUCTION
149 || (insn & STWCX_MASK) == STDCX_INSTRUCTION) {
150 debug(1, "pid=%d, found end of atomic block %p at %p",
151 proc->pid, ip, addr);
155 /* Arbitrary cut-off. If we didn't find the
156 * terminating instruction by now, just give up. */
157 if (insn_count > 16) {
158 fprintf(stderr, "[%d] couldn't find end of atomic block"
159 " at %p\n", proc->pid, ip);
164 /* Put the breakpoint to the next instruction. */
166 if (add_cb(addr, add_cb_data) < 0)
169 debug(1, "PTRACE_CONT");
170 ptrace(PTRACE_CONT, proc->pid, 0, 0);
175 arch_type_sizeof(struct Process *proc, struct arg_type_info *info)
180 switch (info->type) {
197 case ARGTYPE_POINTER:
198 return proc->e_machine == EM_PPC64 ? 8 : 4;
207 /* Use default value. */
211 assert(info->type != info->type);
218 arch_type_alignof(struct Process *proc, struct arg_type_info *info)
223 switch (info->type) {
225 assert(info->type != info->type);
236 case ARGTYPE_POINTER:
239 /* On both PPC and PPC64, fundamental types have the
240 * same alignment as size. */
241 return arch_type_sizeof(proc, info);
245 /* Use default value. */