blob: 04efdf2185077be09687cf75c5175f3a5679a5ba [file] [log] [blame]
Juan Cespedes5c3fe062004-06-14 18:08:37 +02001#include "config.h"
Juan Cespedes5c3fe062004-06-14 18:08:37 +02002
3#include <stdlib.h>
4#include <sys/types.h>
5#include <sys/wait.h>
6#include <signal.h>
7#include <string.h>
8#include "ptrace.h"
Petr Machata366c2f42012-02-09 19:34:36 +01009#include "proc.h"
Juan Cespedesf7281232009-06-25 16:11:21 +020010#include "common.h"
Juan Cespedes5c3fe062004-06-14 18:08:37 +020011
Juan Cespedesf1350522008-12-16 18:19:58 +010012void
Juan Cespedesa8909f72009-04-28 20:02:41 +020013get_arch_dep(Process *proc) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020014 proc_archdep *a;
15 if (!proc->arch_ptr)
16 proc->arch_ptr = (void *)malloc(sizeof(proc_archdep));
Ian Wienand2d45b1a2006-02-20 22:48:07 +010017 a = (proc_archdep *) (proc->arch_ptr);
18 a->valid = (ptrace(PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020019}
20
21/* Returns syscall number if `pid' stopped because of a syscall.
22 * Returns -1 otherwise
23 */
Juan Cespedesf1350522008-12-16 18:19:58 +010024int
Juan Cespedesa8909f72009-04-28 20:02:41 +020025syscall_p(Process *proc, int status, int *sysnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010026 if (WIFSTOPPED(status)
27 && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020028 void *ip = get_instruction_pointer(proc);
29 unsigned int insn;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010030 if (ip == (void *)-1)
31 return 0;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020032 insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
33 if ((insn & 0xc1f8007f) == 0x81d00010) {
Juan Cespedes34584562009-07-25 16:10:39 +020034 *sysnum = ((proc_archdep *) proc->arch_ptr)->regs.u_regs[UREG_G0];
Juan Cespedes3e94cbf2009-05-22 19:12:07 +020035 if (proc->callstack_depth > 0 &&
36 proc->callstack[proc->callstack_depth - 1].is_syscall &&
37 proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020038 return 2;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010039 } else if (*sysnum >= 0) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020040 return 1;
41 }
42 }
43 }
44 return 0;
45}
46
Juan Cespedesf1350522008-12-16 18:19:58 +010047long
Petr Machata000e3112012-01-03 17:03:39 +010048gimme_arg(enum tof type, Process *proc, int arg_num, struct arg_type_info *info)
49{
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 Cespedes34584562009-07-25 16:10:39 +020056 return a->regs.u_regs[UREG_G7];
Juan Cespedes5c3fe062004-06-14 18:08:37 +020057
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)
Juan Cespedes34584562009-07-25 16:10:39 +020060 return ((int *)&a->regs.u_regs[UREG_G7])[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)
Juan Cespedes34584562009-07-25 16:10:39 +020079 memcpy(a->func_arg, &a->regs.u_regs[UREG_G7], sizeof(a->func_arg));
Juan Cespedes5c3fe062004-06-14 18:08:37 +020080 else
Juan Cespedes34584562009-07-25 16:10:39 +020081 memcpy(a->sysc_arg, &a->regs.u_regs[UREG_G7], sizeof(a->sysc_arg));
Juan Cespedes5c3fe062004-06-14 18:08:37 +020082 }
83}