GDB (xrefs)

compile-c-support.c
Go to the documentation of this file.
1 /* C language support for compilation.
2
3 Copyright (C) 2014-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU 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, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "compile-internal.h"
22 #include "compile.h"
23 #include "gdb-dlfcn.h"
24 #include "c-lang.h"
25 #include "macrotab.h"
26 #include "macroscope.h"
27 #include "regcache.h"
28 #include "common/function-view.h"
29 #include "common/preprocessor.h"
30
31 /* See compile-internal.h. */
32
33 const char *
34 c_get_mode_for_size (int size)
35 {
36 const char *mode = NULL;
37
38 switch (size)
39 {
40 case 1:
41 mode = "QI";
42 break;
43 case 2:
44 mode = "HI";
45 break;
46 case 4:
47 mode = "SI";
48 break;
49 case 8:
50 mode = "DI";
51 break;
52 default:
53 internal_error (__FILE__, __LINE__, _("Invalid GCC mode size %d."), size);
54 }
55
56 return mode;
57 }
58
59 /* See compile-internal.h. */
60
61 std::string
62 c_get_range_decl_name (const struct dynamic_prop *prop)
63 {
64 return string_printf ("__gdb_prop_%s", host_address_to_string (prop));
65 }
66
67
68
69 /* Helper function for c_get_compile_context. Open the GCC front-end
70 shared library and return the symbol specified by the current
71 GCC_C_FE_CONTEXT. */
72
73 static gcc_c_fe_context_function *
74 load_libcc (void)
75 {
76 gcc_c_fe_context_function *func;
77
78 /* gdb_dlopen will call error () on an error, so no need to check
79 value. */
80 gdb_dlhandle_up handle = gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC));
81 func = (gcc_c_fe_context_function *) gdb_dlsym (handle,
82 STRINGIFY (GCC_C_FE_CONTEXT));
83
84 if (func == NULL)
85 error (_("could not find symbol %s in library %s"),
86 STRINGIFY (GCC_C_FE_CONTEXT),
87 STRINGIFY (GCC_C_FE_LIBCC));
88
89 /* Leave the library open. */
90 handle.release ();
91 return func;
92 }
93
94 /* Return the compile instance associated with the current context.
95 This function calls the symbol returned from the load_libcc
96 function. This will provide the gcc_c_context. */
97
98 struct compile_instance *
99 c_get_compile_context (void)
100 {
101 static gcc_c_fe_context_function *func;
102
103 struct gcc_c_context *context;
104
105 if (func == NULL)
106 {
107 func = load_libcc ();
108 gdb_assert (func != NULL);
109 }
110
111 context = (*func) (GCC_FE_VERSION_0, GCC_C_FE_VERSION_0);
112 if (context == NULL)
113 error (_("The loaded version of GCC does not support the required version "
114 "of the API."));
115
116 return new_compile_instance (context);
117 }
118
119
120
121 /* Write one macro definition. */
122
123 static void
124 print_one_macro (const char *name, const struct macro_definition *macro,
125 struct macro_source_file *source, int line,
126 ui_file *file)
127 {
128 /* Don't print command-line defines. They will be supplied another
129 way. */
130 if (line == 0)
131 return;
132
133 /* None of -Wno-builtin-macro-redefined, #undef first
134 or plain #define of the same value would avoid a warning. */
135 fprintf_filtered (file, "#ifndef %s\n# define %s", name, name);
136
137 if (macro->kind == macro_function_like)
138 {
139 int i;
140
141 fputs_filtered ("(", file);
142 for (i = 0; i < macro->argc; i++)
143 {
144 fputs_filtered (macro->argv[i], file);
145 if (i + 1 < macro->argc)
146 fputs_filtered (", ", file);
147 }
148 fputs_filtered (")", file);
149 }
150
151 fprintf_filtered (file, " %s\n#endif\n", macro->replacement);
152 }
153
154 /* Write macro definitions at PC to FILE. */
155
156 static void
157 write_macro_definitions (const struct block *block, CORE_ADDR pc,
158 struct ui_file *file)
159 {
160 struct macro_scope *scope;
161
162 if (block != NULL)
163 scope = sal_macro_scope (find_pc_line (pc, 0));
164 else
165 scope = default_macro_scope ();
166 if (scope == NULL)
167 scope = user_macro_scope ();
168
169 if (scope != NULL && scope->file != NULL && scope->file->table != NULL)
170 {
171 macro_for_each_in_scope (scope->file, scope->line,
172 [&] (const char *name,
173 const macro_definition *macro,
174 macro_source_file *source,
175 int line)
176 {
177 print_one_macro (name, macro, source, line, file);
178 });
179 }
180 }
181
182 /* Helper function to construct a header scope for a block of code.
183 Takes a scope argument which selects the correct header to
184 insert into BUF. */
185
186 static void
187 add_code_header (enum compile_i_scope_types type, struct ui_file *buf)
188 {
189 switch (type)
190 {
191 case COMPILE_I_SIMPLE_SCOPE:
192 fputs_unfiltered ("void "
193 GCC_FE_WRAPPER_FUNCTION
194 " (struct "
195 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
196 " *"
197 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
198 ") {\n",
199 buf);
200 break;
201 case COMPILE_I_PRINT_ADDRESS_SCOPE:
202 case COMPILE_I_PRINT_VALUE_SCOPE:
203 /* <string.h> is needed for a memcpy call below. */
204 fputs_unfiltered ("#include <string.h>\n"
205 "void "
206 GCC_FE_WRAPPER_FUNCTION
207 " (struct "
208 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
209 " *"
210 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
211 ", "
212 COMPILE_I_PRINT_OUT_ARG_TYPE
213 " "
214 COMPILE_I_PRINT_OUT_ARG
215 ") {\n",
216 buf);
217 break;
218
219 case COMPILE_I_RAW_SCOPE:
220 break;
221 default:
222 gdb_assert_not_reached (_("Unknown compiler scope reached."));
223 }
224 }
225
226 /* Helper function to construct a footer scope for a block of code.
227 Takes a scope argument which selects the correct footer to
228 insert into BUF. */
229
230 static void
231 add_code_footer (enum compile_i_scope_types type, struct ui_file *buf)
232 {
233 switch (type)
234 {
235 case COMPILE_I_SIMPLE_SCOPE:
236 case COMPILE_I_PRINT_ADDRESS_SCOPE:
237 case COMPILE_I_PRINT_VALUE_SCOPE:
238 fputs_unfiltered ("}\n", buf);
239 break;
240 case COMPILE_I_RAW_SCOPE:
241 break;
242 default:
243 gdb_assert_not_reached (_("Unknown compiler scope reached."));
244 }
245 }
246
247 /* Generate a structure holding all the registers used by the function
248 we're generating. */
249
250 static void
251 generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch,
252 const unsigned char *registers_used)
253 {
254 int i;
255 int seen = 0;
256
257 fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG " {\n",
258 stream);
259
260 if (registers_used != NULL)
261 for (i = 0; i < gdbarch_num_regs (gdbarch); ++i)
262 {
263 if (registers_used[i])
264 {
265 struct type *regtype = check_typedef (register_type (gdbarch, i));
266 std::string regname = compile_register_name_mangled (gdbarch, i);
267
268 seen = 1;
269
270 /* You might think we could use type_print here. However,
271 target descriptions often use types with names like
272 "int64_t", which may not be defined in the inferior
273 (and in any case would not be looked up due to the
274 #pragma business). So, we take a much simpler
275 approach: for pointer- or integer-typed registers, emit
276 the field in the most direct way; and for other
277 register types (typically flags or vectors), emit a
278 maximally-aligned array of the correct size. */
279
280 fputs_unfiltered (" ", stream);
281 switch (TYPE_CODE (regtype))
282 {
283 case TYPE_CODE_PTR:
284 fprintf_filtered (stream, "__gdb_uintptr %s",
285 regname.c_str ());
286 break;
287
288 case TYPE_CODE_INT:
289 {
290 const char *mode
291 = c_get_mode_for_size (TYPE_LENGTH (regtype));
292
293 if (mode != NULL)
294 {
295 if (TYPE_UNSIGNED (regtype))
296 fputs_unfiltered ("unsigned ", stream);
297 fprintf_unfiltered (stream,
298 "int %s"
299 " __attribute__ ((__mode__(__%s__)))",
300 regname.c_str (),
301 mode);
302 break;
303 }
304 }
305
306 /* Fall through. */
307
308 default:
309 fprintf_unfiltered (stream,
310 " unsigned char %s[%d]"
311 " __attribute__((__aligned__("
312 "__BIGGEST_ALIGNMENT__)))",
313 regname.c_str (),
314 TYPE_LENGTH (regtype));
315 }
316 fputs_unfiltered (";\n", stream);
317 }
318 }
319
320 if (!seen)
321 fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY ";\n",
322 stream);
323
324 fputs_unfiltered ("};\n\n", stream);
325 }
326
327 /* Take the source code provided by the user with the 'compile'
328 command, and compute the additional wrapping, macro, variable and
329 register operations needed. INPUT is the source code derived from
330 the 'compile' command, GDBARCH is the architecture to use when
331 computing above, EXPR_BLOCK denotes the block relevant contextually
332 to the inferior when the expression was created, and EXPR_PC
333 indicates the value of $PC. */
334
335 std::string
336 c_compute_program (struct compile_instance *inst,
337 const char *input,
338 struct gdbarch *gdbarch,
339 const struct block *expr_block,
340 CORE_ADDR expr_pc)
341 {
342 struct compile_c_instance *context = (struct compile_c_instance *) inst;
343
344 string_file buf;
345 string_file var_stream;
346
347 write_macro_definitions (expr_block, expr_pc, &buf);
348
349 /* Do not generate local variable information for "raw"
350 compilations. In this case we aren't emitting our own function
351 and the user's code may only refer to globals. */
352 if (inst->scope != COMPILE_I_RAW_SCOPE)
353 {
354 unsigned char *registers_used;
355 int i;
356
357 /* Generate the code to compute variable locations, but do it
358 before generating the function header, so we can define the
359 register struct before the function body. This requires a
360 temporary stream. */
361 registers_used = generate_c_for_variable_locations (context,
362 var_stream, gdbarch,
363 expr_block, expr_pc);
364 make_cleanup (xfree, registers_used);
365
366 buf.puts ("typedef unsigned int"
367 " __attribute__ ((__mode__(__pointer__)))"
368 " __gdb_uintptr;\n");
369 buf.puts ("typedef int"
370 " __attribute__ ((__mode__(__pointer__)))"
371 " __gdb_intptr;\n");
372
373 /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */
374 for (i = 0; i < 4; ++i)
375 {
376 const char *mode = c_get_mode_for_size (1 << i);
377
378 gdb_assert (mode != NULL);
379 buf.printf ("typedef int"
380 " __attribute__ ((__mode__(__%s__)))"
381 " __gdb_int_%s;\n",
382 mode, mode);
383 }
384
385 generate_register_struct (&buf, gdbarch, registers_used);
386 }
387
388 add_code_header (inst->scope, &buf);
389
390 if (inst->scope == COMPILE_I_SIMPLE_SCOPE
391 || inst->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
392 || inst->scope == COMPILE_I_PRINT_VALUE_SCOPE)
393 {
394 buf.write (var_stream.c_str (), var_stream.size ());
395 buf.puts ("#pragma GCC user_expression\n");
396 }
397
398 /* The user expression has to be in its own scope, so that "extern"
399 works properly. Otherwise gcc thinks that the "extern"
400 declaration is in the same scope as the declaration provided by
401 gdb. */
402 if (inst->scope != COMPILE_I_RAW_SCOPE)
403 buf.puts ("{\n");
404
405 buf.puts ("#line 1 \"gdb command line\"\n");
406
407 switch (inst->scope)
408 {
409 case COMPILE_I_PRINT_ADDRESS_SCOPE:
410 case COMPILE_I_PRINT_VALUE_SCOPE:
411 buf.printf (
412 "__auto_type " COMPILE_I_EXPR_VAL " = %s;\n"
413 "typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE ";\n"
414 "memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s" COMPILE_I_EXPR_VAL ",\n"
415 "sizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n"
416 , input, input,
417 (inst->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
418 ? "&" : ""));
419 break;
420 default:
421 buf.puts (input);
422 break;
423 }
424
425 buf.puts ("\n");
426
427 /* For larger user expressions the automatic semicolons may be
428 confusing. */
429 if (strchr (input, '\n') == NULL)
430 buf.puts (";\n");
431
432 if (inst->scope != COMPILE_I_RAW_SCOPE)
433 buf.puts ("}\n");
434
435 add_code_footer (inst->scope, &buf);
436 return std::move (buf.string ());
437 }
c_get_range_decl_name
std::string c_get_range_decl_name(const struct dynamic_prop *prop)
Definition: compile-c-support.c:62
gdbarch
Definition: gdbarch.c:127
COMPILE_I_EXPR_PTR_TYPE
#define COMPILE_I_EXPR_PTR_TYPE
Definition: compile-internal.h:94
string
const char * string
Definition: signals.c:50
compile_c_instance
Definition: compile-internal.h:62
macro_source_file
Definition: macrotab.h:124
generate_c_for_variable_locations
unsigned char * generate_c_for_variable_locations(struct compile_c_instance *compiler, string_file &stream, struct gdbarch *gdbarch, const struct block *block, CORE_ADDR pc)
Definition: compile-c-symbols.c:712
macro_definition::argv
const char *const * argv
Definition: macrotab.h:302
macro_scope::file
struct macro_source_file * file
Definition: macroscope.h:34
generate_register_struct
static void generate_register_struct(struct ui_file *stream, struct gdbarch *gdbarch, const unsigned char *registers_used)
Definition: compile-c-support.c:251
CORE_ADDR
bfd_vma CORE_ADDR
Definition: common-types.h:41
macro_definition::kind
__extension__ enum macro_kind kind
Definition: macrotab.h:294
fputs_unfiltered
void fputs_unfiltered(const char *buf, struct ui_file *file)
Definition: ui-file.c:127
xfree
void xfree(void *)
macro_source_file::table
struct macro_table * table
Definition: macrotab.h:129
function-view.h
func
void(* func)(char *)
Definition: remote-fileio.c:1104
c_compute_program
std::string c_compute_program(struct compile_instance *inst, const char *input, struct gdbarch *gdbarch, const struct block *expr_block, CORE_ADDR expr_pc)
Definition: compile-c-support.c:336
macro_scope
Definition: macroscope.h:33
TYPE_CODE_INT
Definition: gdbtypes.h:111
internal_error
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
#define COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
Definition: compile-internal.h:88
compile_i_scope_types
compile_i_scope_types
Definition: defs.h:61
string_file::write
void write(const char *buf, long length_buf) override
Definition: ui-file.c:138
COMPILE_I_PRINT_OUT_ARG
#define COMPILE_I_PRINT_OUT_ARG
Definition: compile-internal.h:92
load_libcc
static gcc_c_fe_context_function * load_libcc(void)
Definition: compile-c-support.c:74
gdbarch_num_regs
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition: gdbarch.c:2039
dynamic_prop
Definition: gdbtypes.h:398
_
#define _(String)
Definition: gdb_locale.h:35
sal_macro_scope
struct macro_scope * sal_macro_scope(struct symtab_and_line sal)
Definition: macroscope.c:39
ui_file
Definition: ui-file.h:26
print_one_macro
static void print_one_macro(const char *name, const struct macro_definition *macro, struct macro_source_file *source, int line, ui_file *file)
Definition: compile-c-support.c:124
defs.h
default_macro_scope
struct macro_scope * default_macro_scope(void)
Definition: macroscope.c:102
macroscope.h
host_address_to_string
#define host_address_to_string(ADDR)
Definition: print-utils.h:75
add_code_footer
static void add_code_footer(enum compile_i_scope_types type, struct ui_file *buf)
Definition: compile-c-support.c:231
name
const char *const name
Definition: aarch64-tdep.c:76
check_typedef
struct type * check_typedef(struct type *type)
Definition: gdbtypes.c:2421
gdb_dlopen
gdb_dlhandle_up gdb_dlopen(const char *filename)
Definition: gdb-dlfcn.c:61
COMPILE_I_PRINT_ADDRESS_SCOPE
Definition: defs.h:81
find_pc_line
struct symtab_and_line find_pc_line(CORE_ADDR pc, int notcurrent)
Definition: symtab.c:3288
macro_definition
Definition: macrotab.h:288
fprintf_filtered
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2008
macro_function_like
Definition: macrotab.h:272
regcache.h
register_type
struct type * register_type(struct gdbarch *gdbarch, int regnum)
Definition: regcache.c:152
fprintf_unfiltered
void fprintf_unfiltered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2018
COMPILE_I_SIMPLE_REGISTER_DUMMY
#define COMPILE_I_SIMPLE_REGISTER_DUMMY
Definition: compile-internal.h:90
fputs_filtered
void fputs_filtered(const char *linebuffer, struct ui_file *stream)
Definition: utils.c:1811
COMPILE_I_SIMPLE_REGISTER_ARG_NAME
#define COMPILE_I_SIMPLE_REGISTER_ARG_NAME
Definition: compile-internal.h:89
gdb_assert_not_reached
#define gdb_assert_not_reached(message)
Definition: gdb_assert.h:55
macrotab.h
make_cleanup
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:116
COMPILE_I_EXPR_VAL
#define COMPILE_I_EXPR_VAL
Definition: compile-internal.h:93
type
Definition: gdbtypes.h:749
string_file::size
size_t size() const
Definition: ui-file.h:138
c_get_mode_for_size
const char * c_get_mode_for_size(int size)
Definition: compile-c-support.c:34
ui_file::puts
virtual void puts(const char *str)
Definition: ui-file.h:63
STRINGIFY
#define STRINGIFY(x)
Definition: preprocessor.h:29
c_get_compile_context
struct compile_instance * c_get_compile_context(void)
Definition: compile-c-support.c:99
TYPE_UNSIGNED
#define TYPE_UNSIGNED(t)
Definition: gdbtypes.h:205
compile_instance
Definition: compile-internal.h:36
string_file::string
std::string & string()
Definition: ui-file.h:132
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
block
Definition: block.h:60
gdb-dlfcn.h
TYPE_CODE_PTR
Definition: gdbtypes.h:84
ui_file::printf
void printf(const char *,...) ATTRIBUTE_PRINTF(2
Definition: ui-file.c:37
compile.h
COMPILE_I_PRINT_VALUE_SCOPE
Definition: defs.h:82
compile_register_name_mangled
std::string compile_register_name_mangled(struct gdbarch *gdbarch, int regnum)
Definition: compile.c:651
TYPE_CODE
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1238
compile_instance::scope
enum compile_i_scope_types scope
Definition: compile-internal.h:44
string_file::c_str
const char * c_str() const
Definition: ui-file.h:137
c-lang.h
preprocessor.h
compile-internal.h
macro_scope::line
int line
Definition: macroscope.h:35
string_printf
std::string string_printf(const char *fmt,...)
Definition: common-utils.c:150
add_code_header
static void add_code_header(enum compile_i_scope_types type, struct ui_file *buf)
Definition: compile-c-support.c:187
write_macro_definitions
static void write_macro_definitions(const struct block *block, CORE_ADDR pc, struct ui_file *file)
Definition: compile-c-support.c:157
macro_definition::argc
int argc
Definition: macrotab.h:301
COMPILE_I_PRINT_OUT_ARG_TYPE
#define COMPILE_I_PRINT_OUT_ARG_TYPE
Definition: compile-internal.h:91
gdb_dlhandle_up
std::unique_ptr< void, dlclose_deleter > gdb_dlhandle_up
Definition: gdb-dlfcn.h:32
macro_for_each_in_scope
void macro_for_each_in_scope(struct macro_source_file *file, int line, gdb::function_view< macro_callback_fn > fn)
Definition: macrotab.c:1030
gdb_dlsym
void * gdb_dlsym(const gdb_dlhandle_up &handle, const char *symbol)
Definition: gdb-dlfcn.c:93
new_compile_instance
struct compile_instance * new_compile_instance(struct gcc_c_context *fe)
Definition: compile-c-types.c:449
TYPE_LENGTH
#define TYPE_LENGTH(thistype)
Definition: gdbtypes.h:1235
macro_definition::replacement
const char * replacement
Definition: macrotab.h:309
COMPILE_I_SIMPLE_SCOPE
Definition: defs.h:69
COMPILE_I_RAW_SCOPE
Definition: defs.h:73
error
void error(const char *fmt,...)
Definition: errors.c:38
size
size_t size
Definition: go32-nat.c:242
user_macro_scope
struct macro_scope * user_macro_scope(void)
Definition: macroscope.c:91
string_file
Definition: ui-file.h:106