Imported Upstream version 0.7.2
[platform/upstream/ltrace.git] / sysdeps / linux-gnu / ppc / trace.c
1 /*
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
8  *
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.
13  *
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.
18  *
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
22  * 02110-1301 USA
23  */
24
25 #include "config.h"
26
27 #include <assert.h>
28 #include <elf.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34
35 #include "backend.h"
36 #include "breakpoint.h"
37 #include "common.h"
38 #include "insn.h"
39 #include "proc.h"
40 #include "ptrace.h"
41 #include "type.h"
42
43 #if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
44 # define PTRACE_PEEKUSER PTRACE_PEEKUSR
45 #endif
46
47 #if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
48 # define PTRACE_POKEUSER PTRACE_POKEUSR
49 #endif
50
51 void
52 get_arch_dep(Process *proc) {
53 #ifdef __powerpc64__
54         proc->mask_32bit = (proc->e_machine == EM_PPC);
55 #endif
56 }
57
58 #define SYSCALL_INSN   0x44000002
59
60 /* Returns 1 if syscall, 2 if sysret, 0 otherwise. */
61 int
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);
66                 int insn =
67                     (int)ptrace(PTRACE_PEEKTEXT, proc->pid, pc - sizeof(long),
68                                 0);
69
70                 if (insn == SYSCALL_INSN) {
71                         *sysnum =
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) {
77                                 return 2;
78                         }
79                         return 1;
80                 }
81         }
82         return 0;
83 }
84
85 /* The atomic skip code is mostly taken from GDB.  */
86
87 /* In plt.h.  XXX make this official interface.  */
88 int read_target_4(struct Process *proc, arch_addr_t addr, uint32_t *lp);
89
90 int
91 arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp,
92                        int (*add_cb)(void *addr, void *data),
93                        void *add_cb_data)
94 {
95         arch_addr_t ip = get_instruction_pointer(proc);
96         struct breakpoint *other = address2bpstruct(proc->leader, ip);
97
98         debug(1, "arch_atomic_singlestep pid=%d addr=%p %s(%p)",
99               proc->pid, ip, breakpoint_name(sbp), sbp->addr);
100
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.  */
104         union {
105                 uint32_t insn;
106                 char buf[BREAKPOINT_LENGTH];
107         } u;
108         if (other != NULL) {
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.  */
113                 return 1;
114         }
115
116         if ((u.insn & LWARX_MASK) != LWARX_INSTRUCTION
117             && (u.insn & LWARX_MASK) != LDARX_INSTRUCTION)
118                 return 1;
119
120         debug(1, "singlestep over atomic block at %p", ip);
121
122         int insn_count;
123         arch_addr_t addr = ip;
124         for (insn_count = 0; ; ++insn_count) {
125                 addr += 4;
126                 unsigned long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
127                 if (l == (unsigned long)-1 && errno)
128                         return -1;
129                 uint32_t insn;
130 #ifdef __powerpc64__
131                 insn = l >> 32;
132 #else
133                 insn = l;
134 #endif
135
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)
143                                 return -1;
144                 }
145
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);
152                         break;
153                 }
154
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);
160                         return -1;
161                 }
162         }
163
164         /* Put the breakpoint to the next instruction.  */
165         addr += 4;
166         if (add_cb(addr, add_cb_data) < 0)
167                 return -1;
168
169         debug(1, "PTRACE_CONT");
170         ptrace(PTRACE_CONT, proc->pid, 0, 0);
171         return 0;
172 }
173
174 size_t
175 arch_type_sizeof(struct Process *proc, struct arg_type_info *info)
176 {
177         if (proc == NULL)
178                 return (size_t)-2;
179
180         switch (info->type) {
181         case ARGTYPE_VOID:
182                 return 0;
183
184         case ARGTYPE_CHAR:
185                 return 1;
186
187         case ARGTYPE_SHORT:
188         case ARGTYPE_USHORT:
189                 return 2;
190
191         case ARGTYPE_INT:
192         case ARGTYPE_UINT:
193                 return 4;
194
195         case ARGTYPE_LONG:
196         case ARGTYPE_ULONG:
197         case ARGTYPE_POINTER:
198                 return proc->e_machine == EM_PPC64 ? 8 : 4;
199
200         case ARGTYPE_FLOAT:
201                 return 4;
202         case ARGTYPE_DOUBLE:
203                 return 8;
204
205         case ARGTYPE_ARRAY:
206         case ARGTYPE_STRUCT:
207                 /* Use default value.  */
208                 return (size_t)-2;
209
210         default:
211                 assert(info->type != info->type);
212                 abort();
213                 break;
214         }
215 }
216
217 size_t
218 arch_type_alignof(struct Process *proc, struct arg_type_info *info)
219 {
220         if (proc == NULL)
221                 return (size_t)-2;
222
223         switch (info->type) {
224         default:
225                 assert(info->type != info->type);
226                 abort();
227                 break;
228
229         case ARGTYPE_CHAR:
230         case ARGTYPE_SHORT:
231         case ARGTYPE_USHORT:
232         case ARGTYPE_INT:
233         case ARGTYPE_UINT:
234         case ARGTYPE_LONG:
235         case ARGTYPE_ULONG:
236         case ARGTYPE_POINTER:
237         case ARGTYPE_FLOAT:
238         case ARGTYPE_DOUBLE:
239                 /* On both PPC and PPC64, fundamental types have the
240                  * same alignment as size.  */
241                 return arch_type_sizeof(proc, info);
242
243         case ARGTYPE_ARRAY:
244         case ARGTYPE_STRUCT:
245                 /* Use default value.  */
246                 return (size_t)-2;
247         }
248 }