| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 3 | * Copyright (C) 2010,2011,2012 Petr Machata, Red Hat Inc. |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 4 | * Copyright (C) 2004,2008,2009 Juan Cespedes |
| 5 | * Copyright (C) 2006 Ian Wienand |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of the |
| 10 | * License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | * 02110-1301 USA |
| 21 | */ |
| 22 | |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 23 | #include "config.h" |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 24 | |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 25 | #include <sys/reg.h> |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 26 | #include <sys/wait.h> |
| Petr Machata | 211f088 | 2010-11-03 18:42:18 +0100 | [diff] [blame] | 27 | #include <assert.h> |
| Petr Machata | f2d2ba5 | 2011-07-09 11:03:26 +0200 | [diff] [blame] | 28 | #include <errno.h> |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 29 | #include <stdlib.h> |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 30 | |
| Petr Machata | ba1664b | 2012-04-28 14:59:05 +0200 | [diff] [blame] | 31 | #include "backend.h" |
| 32 | #include "debug.h" |
| Petr Machata | ba1664b | 2012-04-28 14:59:05 +0200 | [diff] [blame] | 33 | #include "proc.h" |
| 34 | #include "ptrace.h" |
| Petr Machata | 000e311 | 2012-01-03 17:03:39 +0100 | [diff] [blame] | 35 | #include "type.h" |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 36 | |
| 37 | #if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR)) |
| 38 | # define PTRACE_PEEKUSER PTRACE_PEEKUSR |
| 39 | #endif |
| 40 | |
| 41 | #if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR)) |
| 42 | # define PTRACE_POKEUSER PTRACE_POKEUSR |
| 43 | #endif |
| 44 | |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 45 | #ifdef __x86_64__ |
| 46 | # define ORIG_XAX (8 * ORIG_RAX) |
| 47 | #else |
| 48 | # define ORIG_XAX (4 * ORIG_EAX) |
| 49 | #endif |
| 50 | |
| 51 | #ifdef __x86_64__ |
| 52 | static const int x86_64 = 1; |
| 53 | #else |
| 54 | static const int x86_64 = 0; |
| 55 | #endif |
| 56 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 57 | void |
| Petr Machata | a7ec2d8 | 2012-05-04 17:25:13 +0200 | [diff] [blame] | 58 | get_arch_dep(struct Process *proc) |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 59 | { |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 60 | /* Unfortunately there are still remnants of mask_32bit uses |
| 61 | * around. */ |
| Arnaud Patard | 7a919b9 | 2010-01-08 08:40:20 -0500 | [diff] [blame] | 62 | |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 63 | if (proc->e_machine == EM_X86_64) { |
| 64 | proc->mask_32bit = 0; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 65 | proc->personality = 1; |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 66 | } else if (x86_64) { /* x86_64/i386 */ |
| 67 | proc->mask_32bit = 1; |
| 68 | proc->personality = 0; |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 69 | } else { |
| 70 | proc->mask_32bit = 0; |
| 71 | proc->personality = 0; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 72 | } |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 75 | /* Returns 1 if syscall, 2 if sysret, 0 otherwise. |
| 76 | */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 77 | int |
| Petr Machata | e577a10 | 2012-04-23 18:13:20 +0200 | [diff] [blame] | 78 | syscall_p(struct Process *proc, int status, int *sysnum) |
| 79 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 80 | if (WIFSTOPPED(status) |
| 81 | && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) { |
| Petr Machata | e577a10 | 2012-04-23 18:13:20 +0200 | [diff] [blame] | 82 | struct callstack_element *elem = NULL; |
| 83 | if (proc->callstack_depth > 0) |
| 84 | elem = proc->callstack + proc->callstack_depth - 1; |
| 85 | |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 86 | long int ret = ptrace(PTRACE_PEEKUSER, proc->pid, ORIG_XAX, 0); |
| Petr Machata | e577a10 | 2012-04-23 18:13:20 +0200 | [diff] [blame] | 87 | if (ret == -1) { |
| 88 | if (errno) |
| 89 | return -1; |
| 90 | /* Otherwise, ORIG_RAX == -1 means that the |
| 91 | * system call should not be restarted. In |
| 92 | * that case rely on what we have on |
| 93 | * stack. */ |
| 94 | if (elem != NULL && elem->is_syscall) |
| 95 | ret = elem->c_un.syscall; |
| 96 | } |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 97 | |
| Petr Machata | f2d2ba5 | 2011-07-09 11:03:26 +0200 | [diff] [blame] | 98 | *sysnum = ret; |
| Petr Machata | e577a10 | 2012-04-23 18:13:20 +0200 | [diff] [blame] | 99 | debug(DEBUG_FUNCTION, "sysnum=%ld %p %d\n", ret, |
| 100 | get_instruction_pointer(proc), errno); |
| 101 | if (elem != NULL && elem->is_syscall |
| 102 | && elem->c_un.syscall == *sysnum) |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 103 | return 2; |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 104 | |
| Petr Machata | e577a10 | 2012-04-23 18:13:20 +0200 | [diff] [blame] | 105 | if (*sysnum >= 0) |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 106 | return 1; |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 111 | size_t |
| Petr Machata | a7ec2d8 | 2012-05-04 17:25:13 +0200 | [diff] [blame] | 112 | arch_type_sizeof(struct Process *proc, struct arg_type_info *info) |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 113 | { |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 114 | if (proc == NULL) |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 115 | return (size_t)-2; |
| 116 | |
| 117 | switch (info->type) { |
| 118 | case ARGTYPE_VOID: |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 119 | return 0; |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 120 | |
| 121 | case ARGTYPE_CHAR: |
| 122 | return 1; |
| 123 | |
| 124 | case ARGTYPE_SHORT: |
| 125 | case ARGTYPE_USHORT: |
| 126 | return 2; |
| 127 | |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 128 | case ARGTYPE_INT: |
| 129 | case ARGTYPE_UINT: |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 130 | return 4; |
| 131 | |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 132 | case ARGTYPE_LONG: |
| 133 | case ARGTYPE_ULONG: |
| 134 | case ARGTYPE_POINTER: |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 135 | return proc->e_machine == EM_X86_64 ? 8 : 4; |
| 136 | |
| 137 | case ARGTYPE_FLOAT: |
| 138 | return 4; |
| 139 | case ARGTYPE_DOUBLE: |
| 140 | return 8; |
| 141 | |
| 142 | case ARGTYPE_ARRAY: |
| 143 | case ARGTYPE_STRUCT: |
| 144 | /* Use default value. */ |
| 145 | return (size_t)-2; |
| 146 | } |
| 147 | assert(info->type != info->type); |
| 148 | abort(); |
| 149 | } |
| 150 | |
| 151 | size_t |
| 152 | arch_type_alignof(struct Process *proc, struct arg_type_info *info) |
| 153 | { |
| 154 | if (proc == NULL) |
| 155 | return (size_t)-2; |
| 156 | |
| 157 | switch (info->type) { |
| 158 | case ARGTYPE_VOID: |
| 159 | assert(info->type != ARGTYPE_VOID); |
| 160 | break; |
| 161 | |
| 162 | case ARGTYPE_CHAR: |
| 163 | return 1; |
| 164 | |
| 165 | case ARGTYPE_SHORT: |
| 166 | case ARGTYPE_USHORT: |
| 167 | return 2; |
| 168 | |
| 169 | case ARGTYPE_INT: |
| 170 | case ARGTYPE_UINT: |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 171 | return 4; |
| 172 | |
| Petr Machata | ddd96a3 | 2012-05-17 23:35:26 +0200 | [diff] [blame] | 173 | case ARGTYPE_LONG: |
| 174 | case ARGTYPE_ULONG: |
| 175 | case ARGTYPE_POINTER: |
| 176 | return proc->e_machine == EM_X86_64 ? 8 : 4; |
| 177 | |
| 178 | case ARGTYPE_FLOAT: |
| 179 | return 4; |
| 180 | case ARGTYPE_DOUBLE: |
| 181 | return proc->e_machine == EM_X86_64 ? 8 : 4; |
| 182 | |
| 183 | case ARGTYPE_ARRAY: |
| 184 | case ARGTYPE_STRUCT: |
| 185 | /* Use default value. */ |
| 186 | return (size_t)-2; |
| Petr Machata | 541cdc7 | 2012-01-09 04:28:10 +0100 | [diff] [blame] | 187 | } |
| 188 | abort(); |
| Juan Cespedes | efe85f0 | 2004-04-04 01:31:38 +0200 | [diff] [blame] | 189 | } |