blob: 8e8db814e8778664aefc6d5fbf2018597e25a843 [file] [log] [blame]
Juan Cespedes5c3fe062004-06-14 18:08:37 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include <stdlib.h>
6#include <sys/types.h>
7#include <sys/wait.h>
8#include <signal.h>
9#include <string.h>
10#include "ptrace.h"
11#include "ltrace.h"
12
Juan Cespedesf1350522008-12-16 18:19:58 +010013void
Juan Cespedesa8909f72009-04-28 20:02:41 +020014get_arch_dep(Process *proc) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020015 proc_archdep *a;
16 if (!proc->arch_ptr)
17 proc->arch_ptr = (void *)malloc(sizeof(proc_archdep));
Ian Wienand2d45b1a2006-02-20 22:48:07 +010018 a = (proc_archdep *) (proc->arch_ptr);
19 a->valid = (ptrace(PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020020}
21
22/* Returns syscall number if `pid' stopped because of a syscall.
23 * Returns -1 otherwise
24 */
Juan Cespedesf1350522008-12-16 18:19:58 +010025int
Juan Cespedesa8909f72009-04-28 20:02:41 +020026syscall_p(Process *proc, int status, int *sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010027 if (WIFSTOPPED(status)
28 && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020029 void *ip = get_instruction_pointer(proc);
30 unsigned int insn;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010031 if (ip == (void *)-1)
32 return 0;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020033 insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
34 if ((insn & 0xc1f8007f) == 0x81d00010) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010035 *sysnum = ((proc_archdep *) proc->arch_ptr)->regs.r_g1;
Juan Cespedes3e94cbf2009-05-22 19:12:07 +020036 if (proc->callstack_depth > 0 &&
37 proc->callstack[proc->callstack_depth - 1].is_syscall &&
38 proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020039 return 2;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010040 } else if (*sysnum >= 0) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020041 return 1;
42 }
43 }
44 }
45 return 0;
46}
47
Juan Cespedesf1350522008-12-16 18:19:58 +010048long
Juan Cespedesa8909f72009-04-28 20:02:41 +020049gimme_arg(enum tof type, Process *proc, int arg_num, arg_type_info *info) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010050 proc_archdep *a = (proc_archdep *) proc->arch_ptr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020051 if (!a->valid) {
52 fprintf(stderr, "Could not get child registers\n");
53 exit(1);
54 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010055 if (arg_num == -1) /* return value */
Juan Cespedes5c3fe062004-06-14 18:08:37 +020056 return a->regs.r_o0;
57
Ian Wienand2d45b1a2006-02-20 22:48:07 +010058 if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL || arg_num >= 6) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020059 if (arg_num < 6)
60 return ((int *)&a->regs.r_o0)[arg_num];
Ian Wienand2d45b1a2006-02-20 22:48:07 +010061 return ptrace(PTRACE_PEEKTEXT, proc->pid,
62 proc->stack_pointer + 64 * (arg_num + 1));
63 } else if (type == LT_TOF_FUNCTIONR)
Juan Cespedes5c3fe062004-06-14 18:08:37 +020064 return a->func_arg[arg_num];
Ian Wienand2d45b1a2006-02-20 22:48:07 +010065 else if (type == LT_TOF_SYSCALLR)
Juan Cespedes5c3fe062004-06-14 18:08:37 +020066 return a->sysc_arg[arg_num];
67 else {
68 fprintf(stderr, "gimme_arg called with wrong arguments\n");
69 exit(1);
70 }
71 return 0;
72}
73
Juan Cespedesf1350522008-12-16 18:19:58 +010074void
Juan Cespedesa8909f72009-04-28 20:02:41 +020075save_register_args(enum tof type, Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010076 proc_archdep *a = (proc_archdep *) proc->arch_ptr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020077 if (a->valid) {
78 if (type == LT_TOF_FUNCTION)
79 memcpy(a->func_arg, &a->regs.r_o0, sizeof(a->func_arg));
80 else
81 memcpy(a->sysc_arg, &a->regs.r_o0, sizeof(a->sysc_arg));
82 }
83}