summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/gdb/gdbserver/fbsd-amd64-low.c
blob: 82a8c93f3dfb313503619ceda7fa48ea5fc5597d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* GNU/FreeBSD/amd64 specific low level interface, for the remote server for GDB.
   Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002
   Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "server.h"
#include "fbsd-low.h"
#include "i387-fp.h"

#include <sys/stddef.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <machine/reg.h>

/* Mapping between the general-purpose registers in `struct user'
   format and GDB's register array layout.  */
static int amd64_regmap[] = {
	offsetof(struct reg, r_rax),
	offsetof(struct reg, r_rbx),
	offsetof(struct reg, r_rcx),
	offsetof(struct reg, r_rdx),
	offsetof(struct reg, r_rsi),
	offsetof(struct reg, r_rdi),
	offsetof(struct reg, r_rbp),
	offsetof(struct reg, r_rsp),
	offsetof(struct reg, r_r8),
	offsetof(struct reg, r_r9),
	offsetof(struct reg, r_r10),
	offsetof(struct reg, r_r11),
	offsetof(struct reg, r_r12),
	offsetof(struct reg, r_r13),
	offsetof(struct reg, r_r14),
	offsetof(struct reg, r_r15),
	offsetof(struct reg, r_rip),
	offsetof(struct reg, r_rflags),	/* XXX 64-bit */
	offsetof(struct reg, r_cs),
	offsetof(struct reg, r_ss),
	offsetof(struct reg, r_ds),
	offsetof(struct reg, r_es),
	offsetof(struct reg, r_fs),
	offsetof(struct reg, r_gs),
};
#define	AMD64_NUM_REGS	(sizeof(amd64_regmap) / sizeof(amd64_regmap[0]))

static const char amd64_breakpoint[] = { 0xCC };
#define	AMD64_BP_LEN	1

extern int debug_threads;

static int
amd64_cannot_store_register(int regno)
{

	return (regno >= AMD64_NUM_REGS);
}

static int
amd64_cannot_fetch_register(int regno)
{

	return (regno >= AMD64_NUM_REGS);
}

static void
amd64_fill_gregset(void *buf)
{
	int i;

	for (i = 0; i < AMD64_NUM_REGS; i++)
		collect_register(i, ((char *)buf) + amd64_regmap[i]);
}

static void
amd64_store_gregset(const void *buf)
{
	int i;

	for (i = 0; i < AMD64_NUM_REGS; i++)
		supply_register(i, ((char *)buf) + amd64_regmap[i]);
}

static void
amd64_fill_fpregset(void *buf)
{

	i387_cache_to_fsave(buf);
}

static void
amd64_store_fpregset(const void *buf)
{

	i387_fsave_to_cache(buf);
}

static void
amd64_fill_fpxregset(void *buf)
{  

	i387_cache_to_fxsave(buf);
}

static void
amd64_store_fpxregset(const void *buf)
{

	i387_fxsave_to_cache(buf);
}


struct regset_info target_regsets[] = {
	{
		PT_GETREGS,
		PT_SETREGS,
		sizeof(struct reg),
		GENERAL_REGS,
		amd64_fill_gregset,
		amd64_store_gregset,
	},
#ifdef HAVE_PTRACE_GETFPXREGS
	{
		PTRACE_GETFPXREGS,
		PTRACE_SETFPXREGS,
		sizeof(elf_fpxregset_t),
		EXTENDED_REGS,
		amd64_fill_fpxregset,
		amd64_store_fpxregset,
	},
#endif
	{
		PT_GETFPREGS,
		PT_SETFPREGS,
		sizeof(struct fpreg),
		FP_REGS,
		amd64_fill_fpregset,
		amd64_store_fpregset,
	},
	{
		0,
		0,
		-1,
		-1,
		NULL,
		NULL,
	}
};

static CORE_ADDR
amd64_get_pc(void)
{
	unsigned long pc;

	collect_register_by_name("rip", &pc);

	if (debug_threads)
		fprintf(stderr, "stop pc (before any decrement) is %016lx\n", pc);

	return (pc);
}

static void
amd64_set_pc(CORE_ADDR newpc)
{

	if (debug_threads)
		fprintf(stderr, "set pc to %016lx\n", (long)newpc);
	supply_register_by_name("rip", &newpc);
}

static int
amd64_breakpoint_at(CORE_ADDR pc)
{
	unsigned char c;

	read_inferior_memory(pc, &c, 1);
	if (c == 0xCC)
		return (1);

	return (0);
}

struct fbsd_target_ops the_low_target = {
	AMD64_NUM_REGS,
	amd64_regmap,
	amd64_cannot_fetch_register,
	amd64_cannot_store_register,
	amd64_get_pc,
	amd64_set_pc,
	amd64_breakpoint,
	AMD64_BP_LEN,
	NULL,
	1,
	amd64_breakpoint_at,
};