1e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata/*
2e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * This file is part of ltrace.
3e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * Copyright (C) 2004,2008,2009 Juan Cespedes
4e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * Copyright (C) 2006 Ian Wienand
5e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata *
6e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * This program is free software; you can redistribute it and/or
7e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * modify it under the terms of the GNU General Public License as
8e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * published by the Free Software Foundation; either version 2 of the
9e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * License, or (at your option) any later version.
10e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata *
11e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * This program is distributed in the hope that it will be useful, but
12e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * WITHOUT ANY WARRANTY; without even the implied warranty of
13e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * General Public License for more details.
15e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata *
16e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * You should have received a copy of the GNU General Public License
17e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * along with this program; if not, write to the Free Software
18e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata * 02110-1301 USA
20e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata */
21e99af270a60891e68d465c4cd97dbe29cd1a05e4Petr Machata
225c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include "config.h"
235c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes
245c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include <sys/types.h>
255c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include <sys/wait.h>
265c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include <signal.h>
275c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include <sys/ptrace.h>
285c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include <asm/ptrace.h>
295c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes
30366c2f46d844f040458df9b7e35fc3b8527ed2d3Petr Machata#include "proc.h"
31f728123bd75a65a6a1536e198c3c30719e494e71Juan Cespedes#include "common.h"
325c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#include "debug.h"
335c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes
345c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
355c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes# define PTRACE_PEEKUSER PTRACE_PEEKUSR
365c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#endif
375c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes
385c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
395c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes# define PTRACE_POKEUSER PTRACE_POKEUSR
405c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes#endif
415c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes
42f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesvoid
43929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machataget_arch_dep(struct process *proc)
44929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata{
455c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes}
465c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes
475c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes/* Returns 1 if syscall, 2 if sysret, 0 otherwise.
485c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes */
49f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedesint
50929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machatasyscall_p(struct process *proc, int status, int *sysnum)
51929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata{
522d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand	if (WIFSTOPPED(status)
532d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
542d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand		char *ip = get_instruction_pointer(proc) - 4;
555c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes		long x = ptrace(PTRACE_PEEKTEXT, proc->pid, ip, 0);
565c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes		debug(2, "instr: %016lx", x);
572d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand		if ((x & 0xffffffff) != 0x00000083)
585c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes			return 0;
592d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand		*sysnum =
602d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand		    ptrace(PTRACE_PEEKUSER, proc->pid, 0 /* REG_R0 */ , 0);
613e94cbf0d388a5b5b49f8dbc60f4b24900de89aeJuan Cespedes		if (proc->callstack_depth > 0 &&
623e94cbf0d388a5b5b49f8dbc60f4b24900de89aeJuan Cespedes		    proc->callstack[proc->callstack_depth - 1].is_syscall &&
633e94cbf0d388a5b5b49f8dbc60f4b24900de89aeJuan Cespedes			proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
645c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes			return 2;
655c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes		}
662d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand		if (*sysnum >= 0 && *sysnum < 500) {
675c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes			return 1;
685c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes		}
695c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes	}
705c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes	return 0;
715c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes}
725c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes
73f13505251e6402460f6cc7ec84e0d8ca91607b4fJuan Cespedeslong
74929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machatagimme_arg(enum tof type, struct process *proc, int arg_num,
75929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata	  struct arg_type_info *info)
76000e31195ad4ad30a0c80c93ab57a424e7d8d918Petr Machata{
772d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand	if (arg_num == -1) {	/* return value */
782d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand		return ptrace(PTRACE_PEEKUSER, proc->pid, 0 /* REG_R0 */ , 0);
795c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes	}
805c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes
812d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand	if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
822d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand		if (arg_num <= 5)
832d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand			return ptrace(PTRACE_PEEKUSER, proc->pid,
842d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand				      arg_num + 16 /* REG_A0 */ , 0);
855c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes		else
862d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand			return ptrace(PTRACE_PEEKTEXT, proc->pid,
872d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand				      proc->stack_pointer + 8 * (arg_num - 6),
882d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand				      0);
892d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand	} else if (type == LT_TOF_SYSCALL || type == LT_TOF_SYSCALLR) {
902d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand		return ptrace(PTRACE_PEEKUSER, proc->pid,
912d45b1a8e26a36a9f85dc49e721c4390ca93dc40Ian Wienand			      arg_num + 16 /* REG_A0 */ , 0);
925c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes	} else {
935c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes		fprintf(stderr, "gimme_arg called with wrong arguments\n");
945c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes		exit(1);
955c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes	}
965c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes	return 0;
975c3fe0697b202cc7d95e90459de0fb312b297b27Juan Cespedes}
98