blob: e05c4d3062f886bdc240f30e88e9cd226ed39dab [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
Juan Cespedesa8909f72009-04-28 20:02:41 +020048gimme_arg(enum tof type, Process *proc, int arg_num, arg_type_info *info) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010049 proc_archdep *a = (proc_archdep *) proc->arch_ptr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020050 if (!a->valid) {
51 fprintf(stderr, "Could not get child registers\n");
52 exit(1);
53 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010054 if (arg_num == -1) /* return value */
Juan Cespedes34584562009-07-25 16:10:39 +020055 return a->regs.u_regs[UREG_G7];
Juan Cespedes5c3fe062004-06-14 18:08:37 +020056
Ian Wienand2d45b1a2006-02-20 22:48:07 +010057 if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL || arg_num >= 6) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020058 if (arg_num < 6)
Juan Cespedes34584562009-07-25 16:10:39 +020059 return ((int *)&a->regs.u_regs[UREG_G7])[arg_num];
Ian Wienand2d45b1a2006-02-20 22:48:07 +010060 return ptrace(PTRACE_PEEKTEXT, proc->pid,
61 proc->stack_pointer + 64 * (arg_num + 1));
62 } else if (type == LT_TOF_FUNCTIONR)
Juan Cespedes5c3fe062004-06-14 18:08:37 +020063 return a->func_arg[arg_num];
Ian Wienand2d45b1a2006-02-20 22:48:07 +010064 else if (type == LT_TOF_SYSCALLR)
Juan Cespedes5c3fe062004-06-14 18:08:37 +020065 return a->sysc_arg[arg_num];
66 else {
67 fprintf(stderr, "gimme_arg called with wrong arguments\n");
68 exit(1);
69 }
70 return 0;
71}
72
Juan Cespedesf1350522008-12-16 18:19:58 +010073void
Juan Cespedesa8909f72009-04-28 20:02:41 +020074save_register_args(enum tof type, Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010075 proc_archdep *a = (proc_archdep *) proc->arch_ptr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020076 if (a->valid) {
77 if (type == LT_TOF_FUNCTION)
Juan Cespedes34584562009-07-25 16:10:39 +020078 memcpy(a->func_arg, &a->regs.u_regs[UREG_G7], sizeof(a->func_arg));
Juan Cespedes5c3fe062004-06-14 18:08:37 +020079 else
Juan Cespedes34584562009-07-25 16:10:39 +020080 memcpy(a->sysc_arg, &a->regs.u_regs[UREG_G7], sizeof(a->sysc_arg));
Juan Cespedes5c3fe062004-06-14 18:08:37 +020081 }
82}