2 * This file is part of ltrace.
3 * Copyright (C) 2010,2012,2013 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(struct process *proc)
55 proc->mask_32bit = (proc->e_machine == EM_PPC);
59 #define SYSCALL_INSN 0x44000002
61 /* Returns 1 if syscall, 2 if sysret, 0 otherwise. */
63 syscall_p(struct process *proc, int status, int *sysnum)
65 if (WIFSTOPPED(status)
66 && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
67 long pc = (long)get_instruction_pointer(proc);
69 (int)ptrace(PTRACE_PEEKTEXT, proc->pid, pc - sizeof(long),
72 if (insn == SYSCALL_INSN) {
74 (int)ptrace(PTRACE_PEEKUSER, proc->pid,
75 sizeof(long) * PT_R0, 0);
76 if (proc->callstack_depth > 0 &&
77 proc->callstack[proc->callstack_depth - 1].is_syscall &&
78 proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
87 /* The atomic skip code is mostly taken from GDB. */
89 enum sw_singlestep_status
90 arch_sw_singlestep(struct process *proc, struct breakpoint *sbp,
91 int (*add_cb)(arch_addr_t, struct sw_singlestep_data *),
92 struct sw_singlestep_data *add_cb_data)
94 arch_addr_t ip = get_instruction_pointer(proc);
95 struct breakpoint *other = address2bpstruct(proc->leader, ip);
97 debug(1, "arch_sw_singlestep pid=%d addr=%p %s(%p)",
98 proc->pid, ip, breakpoint_name(sbp), sbp->addr);
100 /* If the original instruction was lwarx/ldarx, we can't
101 * single-step over it, instead we have to execute the whole
102 * atomic block at once. */
105 char buf[BREAKPOINT_LENGTH];
108 memcpy(u.buf, sbp->orig_value, BREAKPOINT_LENGTH);
109 } else if (proc_read_32(proc, ip, &u.insn) < 0) {
110 fprintf(stderr, "couldn't read instruction at IP %p\n", ip);
111 /* Do the normal singlestep. */
115 if ((u.insn & LWARX_MASK) != LWARX_INSTRUCTION
116 && (u.insn & LWARX_MASK) != LDARX_INSTRUCTION)
119 debug(1, "singlestep over atomic block at %p", ip);
122 arch_addr_t addr = ip;
123 for (insn_count = 0; ; ++insn_count) {
125 unsigned long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
126 if (l == (unsigned long)-1 && errno)
135 /* If a conditional branch is found, put a breakpoint
136 * in its destination address. */
137 if ((insn & BRANCH_MASK) == BC_INSN) {
138 arch_addr_t branch_addr = ppc_branch_dest(addr, insn);
139 debug(1, "pid=%d, branch in atomic block from %p to %p",
140 proc->pid, addr, branch_addr);
141 if (add_cb(branch_addr, add_cb_data) < 0)
145 /* Assume that the atomic sequence ends with a
146 * stwcx/stdcx instruction. */
147 if ((insn & STWCX_MASK) == STWCX_INSTRUCTION
148 || (insn & STWCX_MASK) == STDCX_INSTRUCTION) {
149 debug(1, "pid=%d, found end of atomic block %p at %p",
150 proc->pid, ip, addr);
154 /* Arbitrary cut-off. If we didn't find the
155 * terminating instruction by now, just give up. */
156 if (insn_count > 16) {
157 fprintf(stderr, "[%d] couldn't find end of atomic block"
158 " at %p\n", proc->pid, ip);
163 /* Put the breakpoint to the next instruction. */
165 if (add_cb(addr, add_cb_data) < 0)
168 debug(1, "PTRACE_CONT");
169 ptrace(PTRACE_CONT, proc->pid, 0, 0);
174 arch_type_sizeof(struct process *proc, struct arg_type_info *info)
179 switch (info->type) {
196 case ARGTYPE_POINTER:
197 return proc->e_machine == EM_PPC64 ? 8 : 4;
206 /* Use default value. */
210 assert(info->type != info->type);
217 arch_type_alignof(struct process *proc, struct arg_type_info *info)
222 switch (info->type) {
224 assert(info->type != info->type);
235 case ARGTYPE_POINTER:
238 /* On both PPC and PPC64, fundamental types have the
239 * same alignment as size. */
240 return arch_type_sizeof(proc, info);
244 /* Use default value. */