blob: 1f407e56e4ac8483c0666b3081c859c98f147de2 [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
13extern FILE *output;
14extern int opt_d;
15
16void get_arch_dep(struct process *proc)
17{
18 proc_archdep *a;
19 if (!proc->arch_ptr)
20 proc->arch_ptr = (void *)malloc(sizeof(proc_archdep));
Ian Wienand3219f322006-02-16 06:00:00 +010021 a = (proc_archdep *) (proc->arch_ptr);
22 a->valid = (ptrace(PTRACE_GETREGS, proc->pid, &a->regs, 0) >= 0);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020023}
24
25/* Returns syscall number if `pid' stopped because of a syscall.
26 * Returns -1 otherwise
27 */
28int syscall_p(struct process *proc, int status, int *sysnum)
29{
Ian Wienand3219f322006-02-16 06:00:00 +010030 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020031 void *ip = get_instruction_pointer(proc);
32 unsigned int insn;
Ian Wienand3219f322006-02-16 06:00:00 +010033 if (ip == (void *)-1)
34 return 0;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020035 insn = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
36 if ((insn & 0xc1f8007f) == 0x81d00010) {
Ian Wienand3219f322006-02-16 06:00:00 +010037 *sysnum = ((proc_archdep *) proc->arch_ptr)->regs.r_g1;
38 if ((proc->callstack_depth > 0)
39 && proc->callstack[proc->callstack_depth -
40 1].is_syscall) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020041 return 2;
Ian Wienand3219f322006-02-16 06:00:00 +010042 } else if (*sysnum >= 0) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020043 return 1;
44 }
45 }
46 }
47 return 0;
48}
49
Ian Wienand3219f322006-02-16 06:00:00 +010050long gimme_arg(enum tof type, struct process *proc, int arg_num)
Juan Cespedes5c3fe062004-06-14 18:08:37 +020051{
Ian Wienand3219f322006-02-16 06:00:00 +010052 proc_archdep *a = (proc_archdep *) proc->arch_ptr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020053 if (!a->valid) {
54 fprintf(stderr, "Could not get child registers\n");
55 exit(1);
56 }
Ian Wienand3219f322006-02-16 06:00:00 +010057 if (arg_num == -1) /* return value */
Juan Cespedes5c3fe062004-06-14 18:08:37 +020058 return a->regs.r_o0;
59
Ian Wienand3219f322006-02-16 06:00:00 +010060 if (type == LT_TOF_FUNCTION || type == LT_TOF_SYSCALL || arg_num >= 6) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020061 if (arg_num < 6)
62 return ((int *)&a->regs.r_o0)[arg_num];
Ian Wienand3219f322006-02-16 06:00:00 +010063 return ptrace(PTRACE_PEEKTEXT, proc->pid,
64 proc->stack_pointer + 64 * (arg_num + 1));
65 } else if (type == LT_TOF_FUNCTIONR)
Juan Cespedes5c3fe062004-06-14 18:08:37 +020066 return a->func_arg[arg_num];
Ian Wienand3219f322006-02-16 06:00:00 +010067 else if (type == LT_TOF_SYSCALLR)
Juan Cespedes5c3fe062004-06-14 18:08:37 +020068 return a->sysc_arg[arg_num];
69 else {
70 fprintf(stderr, "gimme_arg called with wrong arguments\n");
71 exit(1);
72 }
73 return 0;
74}
75
Ian Wienand3219f322006-02-16 06:00:00 +010076void save_register_args(enum tof type, struct process *proc)
Juan Cespedes5c3fe062004-06-14 18:08:37 +020077{
Ian Wienand3219f322006-02-16 06:00:00 +010078 proc_archdep *a = (proc_archdep *) proc->arch_ptr;
Juan Cespedes5c3fe062004-06-14 18:08:37 +020079 if (a->valid) {
80 if (type == LT_TOF_FUNCTION)
81 memcpy(a->func_arg, &a->regs.r_o0, sizeof(a->func_arg));
82 else
83 memcpy(a->sysc_arg, &a->regs.r_o0, sizeof(a->sysc_arg));
84 }
85}