[PR gdb/13808] gdb.trace: Pass tdesc selected in gdbserver to IPA.
[external/binutils.git] / gdb / gdbserver / linux-amd64-ipa.c
1 /* GNU/Linux/x86-64 specific low level interface, for the in-process
2    agent library for GDB.
3
4    Copyright (C) 2010-2016 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "server.h"
22 #include "tracepoint.h"
23 #include "linux-x86-tdesc.h"
24
25 /* Defined in auto-generated file amd64-linux.c.  */
26 void init_registers_amd64_linux (void);
27 extern const struct target_desc *tdesc_amd64_linux;
28
29 /* fast tracepoints collect registers.  */
30
31 #define FT_CR_RIP 0
32 #define FT_CR_EFLAGS 1
33 #define FT_CR_R8 2
34 #define FT_CR_R9 3
35 #define FT_CR_R10 4
36 #define FT_CR_R11 5
37 #define FT_CR_R12 6
38 #define FT_CR_R13 7
39 #define FT_CR_R14 8
40 #define FT_CR_R15 9
41 #define FT_CR_RAX 10
42 #define FT_CR_RBX 11
43 #define FT_CR_RCX 12
44 #define FT_CR_RDX 13
45 #define FT_CR_RSI 14
46 #define FT_CR_RDI 15
47 #define FT_CR_RBP 16
48 #define FT_CR_RSP 17
49
50 static const int x86_64_ft_collect_regmap[] = {
51   FT_CR_RAX * 8, FT_CR_RBX * 8, FT_CR_RCX * 8, FT_CR_RDX * 8,
52   FT_CR_RSI * 8, FT_CR_RDI * 8, FT_CR_RBP * 8, FT_CR_RSP * 8,
53   FT_CR_R8 * 8,  FT_CR_R9 * 8,  FT_CR_R10 * 8, FT_CR_R11 * 8,
54   FT_CR_R12 * 8, FT_CR_R13 * 8, FT_CR_R14 * 8, FT_CR_R15 * 8,
55   FT_CR_RIP * 8, FT_CR_EFLAGS * 8
56 };
57
58 #define X86_64_NUM_FT_COLLECT_GREGS \
59   (sizeof (x86_64_ft_collect_regmap) / sizeof(x86_64_ft_collect_regmap[0]))
60
61 void
62 supply_fast_tracepoint_registers (struct regcache *regcache,
63                                   const unsigned char *buf)
64 {
65   int i;
66
67   for (i = 0; i < X86_64_NUM_FT_COLLECT_GREGS; i++)
68     supply_register (regcache, i,
69                      ((char *) buf) + x86_64_ft_collect_regmap[i]);
70 }
71
72 IP_AGENT_EXPORT_FUNC ULONGEST
73 gdb_agent_get_raw_reg (const unsigned char *raw_regs, int regnum)
74 {
75   if (regnum >= X86_64_NUM_FT_COLLECT_GREGS)
76     return 0;
77
78   return *(ULONGEST *) (raw_regs + x86_64_ft_collect_regmap[regnum]);
79 }
80
81 #ifdef HAVE_UST
82
83 #include <ust/processor.h>
84
85 /* "struct registers" is the UST object type holding the registers at
86    the time of the static tracepoint marker call.  This doesn't
87    contain RIP, but we know what it must have been (the marker
88    address).  */
89
90 #define ST_REGENTRY(REG)                        \
91   {                                             \
92     offsetof (struct registers, REG),           \
93     sizeof (((struct registers *) NULL)->REG)   \
94   }
95
96 static struct
97 {
98   int offset;
99   int size;
100 } x86_64_st_collect_regmap[] =
101   {
102     ST_REGENTRY(rax),
103     ST_REGENTRY(rbx),
104     ST_REGENTRY(rcx),
105     ST_REGENTRY(rdx),
106     ST_REGENTRY(rsi),
107     ST_REGENTRY(rdi),
108     ST_REGENTRY(rbp),
109     ST_REGENTRY(rsp),
110     ST_REGENTRY(r8),
111     ST_REGENTRY(r9),
112     ST_REGENTRY(r10),
113     ST_REGENTRY(r11),
114     ST_REGENTRY(r12),
115     ST_REGENTRY(r13),
116     ST_REGENTRY(r14),
117     ST_REGENTRY(r15),
118     { -1, 0 },
119     ST_REGENTRY(rflags),
120     ST_REGENTRY(cs),
121     ST_REGENTRY(ss),
122   };
123
124 #define X86_64_NUM_ST_COLLECT_GREGS \
125   (sizeof (x86_64_st_collect_regmap) / sizeof (x86_64_st_collect_regmap[0]))
126
127 /* GDB's RIP register number.  */
128 #define AMD64_RIP_REGNUM 16
129
130 void
131 supply_static_tracepoint_registers (struct regcache *regcache,
132                                     const unsigned char *buf,
133                                     CORE_ADDR pc)
134 {
135   int i;
136   unsigned long newpc = pc;
137
138   supply_register (regcache, AMD64_RIP_REGNUM, &newpc);
139
140   for (i = 0; i < X86_64_NUM_ST_COLLECT_GREGS; i++)
141     if (x86_64_st_collect_regmap[i].offset != -1)
142       {
143         switch (x86_64_st_collect_regmap[i].size)
144           {
145           case 8:
146             supply_register (regcache, i,
147                              ((char *) buf)
148                              + x86_64_st_collect_regmap[i].offset);
149             break;
150           case 2:
151             {
152               unsigned long reg
153                 = * (short *) (((char *) buf)
154                                + x86_64_st_collect_regmap[i].offset);
155               reg &= 0xffff;
156               supply_register (regcache, i, &reg);
157             }
158             break;
159           default:
160             internal_error (__FILE__, __LINE__,
161                             "unhandled register size: %d",
162                             x86_64_st_collect_regmap[i].size);
163             break;
164           }
165       }
166 }
167
168 #endif /* HAVE_UST */
169
170 /* Return target_desc to use for IPA, given the tdesc index passed by
171    gdbserver.  */
172
173 const struct target_desc *
174 get_ipa_tdesc (int idx)
175 {
176   switch (idx)
177     {
178     case X86_TDESC_SSE:
179       return tdesc_amd64_linux;
180     case X86_TDESC_AVX:
181       return tdesc_amd64_avx_linux;
182     case X86_TDESC_MPX:
183       return tdesc_amd64_mpx_linux;
184     case X86_TDESC_AVX512:
185       return tdesc_amd64_avx512_linux;
186     default:
187       internal_error (__FILE__, __LINE__,
188                       "unknown ipa tdesc index: %d", idx);
189       return tdesc_amd64_linux;
190     }
191 }
192
193 void
194 initialize_low_tracepoint (void)
195 {
196   init_registers_amd64_linux ();
197   init_registers_amd64_avx_linux ();
198   init_registers_amd64_mpx_linux ();
199   init_registers_amd64_avx512_linux ();
200 }