blob: cc1a6a1527e171972a5e5476cdb5fe2ba9b44c71 [file] [log] [blame]
Petr Machata541cdc72012-01-09 04:28:10 +01001/*
2 * This file is part of ltrace.
Petr Machataddd96a32012-05-17 23:35:26 +02003 * Copyright (C) 2010,2011,2012 Petr Machata, Red Hat Inc.
Petr Machata541cdc72012-01-09 04:28:10 +01004 * 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 Cespedesefe85f02004-04-04 01:31:38 +020023#include "config.h"
Juan Cespedesefe85f02004-04-04 01:31:38 +020024
Juan Cespedesefe85f02004-04-04 01:31:38 +020025#include <sys/reg.h>
Petr Machataddd96a32012-05-17 23:35:26 +020026#include <sys/wait.h>
Petr Machata211f0882010-11-03 18:42:18 +010027#include <assert.h>
Petr Machataf2d2ba52011-07-09 11:03:26 +020028#include <errno.h>
Petr Machata541cdc72012-01-09 04:28:10 +010029#include <stdlib.h>
Juan Cespedesefe85f02004-04-04 01:31:38 +020030
Petr Machataba1664b2012-04-28 14:59:05 +020031#include "backend.h"
32#include "debug.h"
Petr Machataba1664b2012-04-28 14:59:05 +020033#include "proc.h"
34#include "ptrace.h"
Petr Machata000e3112012-01-03 17:03:39 +010035#include "type.h"
Juan Cespedesefe85f02004-04-04 01:31:38 +020036
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 Machataddd96a32012-05-17 23:35:26 +020045#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__
52static const int x86_64 = 1;
53#else
54static const int x86_64 = 0;
55#endif
56
Juan Cespedesf1350522008-12-16 18:19:58 +010057void
Petr Machataa7ec2d82012-05-04 17:25:13 +020058get_arch_dep(struct Process *proc)
Petr Machata541cdc72012-01-09 04:28:10 +010059{
Petr Machataddd96a32012-05-17 23:35:26 +020060 /* Unfortunately there are still remnants of mask_32bit uses
61 * around. */
Arnaud Patard7a919b92010-01-08 08:40:20 -050062
Petr Machataddd96a32012-05-17 23:35:26 +020063 if (proc->e_machine == EM_X86_64) {
64 proc->mask_32bit = 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +010065 proc->personality = 1;
Petr Machataddd96a32012-05-17 23:35:26 +020066 } else if (x86_64) { /* x86_64/i386 */
67 proc->mask_32bit = 1;
68 proc->personality = 0;
Petr Machata541cdc72012-01-09 04:28:10 +010069 } else {
70 proc->mask_32bit = 0;
71 proc->personality = 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +010072 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +020073}
74
Juan Cespedesefe85f02004-04-04 01:31:38 +020075/* Returns 1 if syscall, 2 if sysret, 0 otherwise.
76 */
Juan Cespedesf1350522008-12-16 18:19:58 +010077int
Petr Machatae577a102012-04-23 18:13:20 +020078syscall_p(struct Process *proc, int status, int *sysnum)
79{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 if (WIFSTOPPED(status)
81 && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
Petr Machatae577a102012-04-23 18:13:20 +020082 struct callstack_element *elem = NULL;
83 if (proc->callstack_depth > 0)
84 elem = proc->callstack + proc->callstack_depth - 1;
85
Petr Machataddd96a32012-05-17 23:35:26 +020086 long int ret = ptrace(PTRACE_PEEKUSER, proc->pid, ORIG_XAX, 0);
Petr Machatae577a102012-04-23 18:13:20 +020087 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 Cespedesefe85f02004-04-04 01:31:38 +020097
Petr Machataf2d2ba52011-07-09 11:03:26 +020098 *sysnum = ret;
Petr Machatae577a102012-04-23 18:13:20 +020099 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 Cespedesefe85f02004-04-04 01:31:38 +0200103 return 2;
Juan Cespedesefe85f02004-04-04 01:31:38 +0200104
Petr Machatae577a102012-04-23 18:13:20 +0200105 if (*sysnum >= 0)
Juan Cespedesefe85f02004-04-04 01:31:38 +0200106 return 1;
Juan Cespedesefe85f02004-04-04 01:31:38 +0200107 }
108 return 0;
109}
110
Petr Machata541cdc72012-01-09 04:28:10 +0100111size_t
Petr Machataa7ec2d82012-05-04 17:25:13 +0200112arch_type_sizeof(struct Process *proc, struct arg_type_info *info)
Petr Machata541cdc72012-01-09 04:28:10 +0100113{
Petr Machataddd96a32012-05-17 23:35:26 +0200114 if (proc == NULL)
Petr Machata541cdc72012-01-09 04:28:10 +0100115 return (size_t)-2;
116
117 switch (info->type) {
118 case ARGTYPE_VOID:
Petr Machataddd96a32012-05-17 23:35:26 +0200119 return 0;
Petr Machata541cdc72012-01-09 04:28:10 +0100120
121 case ARGTYPE_CHAR:
122 return 1;
123
124 case ARGTYPE_SHORT:
125 case ARGTYPE_USHORT:
126 return 2;
127
Petr Machata541cdc72012-01-09 04:28:10 +0100128 case ARGTYPE_INT:
129 case ARGTYPE_UINT:
Petr Machataddd96a32012-05-17 23:35:26 +0200130 return 4;
131
Petr Machata541cdc72012-01-09 04:28:10 +0100132 case ARGTYPE_LONG:
133 case ARGTYPE_ULONG:
134 case ARGTYPE_POINTER:
Petr Machataddd96a32012-05-17 23:35:26 +0200135 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
151size_t
152arch_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 Machata541cdc72012-01-09 04:28:10 +0100171 return 4;
172
Petr Machataddd96a32012-05-17 23:35:26 +0200173 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 Machata541cdc72012-01-09 04:28:10 +0100187 }
188 abort();
Juan Cespedesefe85f02004-04-04 01:31:38 +0200189}