1 /* C language support for compilation.
3 Copyright (C) 2014-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "compile-internal.h"
22 #include "compile-c.h"
24 #include "gdb-dlfcn.h"
27 #include "macroscope.h"
29 #include "common/function-view.h"
30 #include "common/preprocessor.h"
32 /* See compile-internal.h. */
35 c_get_mode_for_size (int size
)
37 const char *mode
= NULL
;
54 internal_error (__FILE__
, __LINE__
, _("Invalid GCC mode size %d."), size
);
60 /* See compile-internal.h. */
63 c_get_range_decl_name (const struct dynamic_prop
*prop
)
65 return string_printf ("__gdb_prop_%s", host_address_to_string (prop
));
70 /* Helper function for c_get_compile_context. Open the GCC front-end
71 shared library and return the symbol specified by the current
74 static gcc_c_fe_context_function
*
77 gcc_c_fe_context_function
*func
;
79 /* gdb_dlopen will call error () on an error, so no need to check
81 gdb_dlhandle_up handle
= gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC
));
82 func
= (gcc_c_fe_context_function
*) gdb_dlsym (handle
,
83 STRINGIFY (GCC_C_FE_CONTEXT
));
86 error (_("could not find symbol %s in library %s"),
87 STRINGIFY (GCC_C_FE_CONTEXT
),
88 STRINGIFY (GCC_C_FE_LIBCC
));
90 /* Leave the library open. */
95 /* Return the compile instance associated with the current context.
96 This function calls the symbol returned from the load_libcc
97 function. This will provide the gcc_c_context. */
99 struct compile_instance
*
100 c_get_compile_context (void)
102 static gcc_c_fe_context_function
*func
;
104 struct gcc_c_context
*context
;
108 func
= load_libcc ();
109 gdb_assert (func
!= NULL
);
112 context
= (*func
) (GCC_FE_VERSION_0
, GCC_C_FE_VERSION_0
);
114 error (_("The loaded version of GCC does not support the required version "
117 return new_compile_instance (context
);
122 /* Write one macro definition. */
125 print_one_macro (const char *name
, const struct macro_definition
*macro
,
126 struct macro_source_file
*source
, int line
,
129 /* Don't print command-line defines. They will be supplied another
134 /* None of -Wno-builtin-macro-redefined, #undef first
135 or plain #define of the same value would avoid a warning. */
136 fprintf_filtered (file
, "#ifndef %s\n# define %s", name
, name
);
138 if (macro
->kind
== macro_function_like
)
142 fputs_filtered ("(", file
);
143 for (i
= 0; i
< macro
->argc
; i
++)
145 fputs_filtered (macro
->argv
[i
], file
);
146 if (i
+ 1 < macro
->argc
)
147 fputs_filtered (", ", file
);
149 fputs_filtered (")", file
);
152 fprintf_filtered (file
, " %s\n#endif\n", macro
->replacement
);
155 /* Write macro definitions at PC to FILE. */
158 write_macro_definitions (const struct block
*block
, CORE_ADDR pc
,
159 struct ui_file
*file
)
161 gdb::unique_xmalloc_ptr
<struct macro_scope
> scope
;
164 scope
= sal_macro_scope (find_pc_line (pc
, 0));
166 scope
= default_macro_scope ();
168 scope
= user_macro_scope ();
170 if (scope
!= NULL
&& scope
->file
!= NULL
&& scope
->file
->table
!= NULL
)
172 macro_for_each_in_scope (scope
->file
, scope
->line
,
173 [&] (const char *name
,
174 const macro_definition
*macro
,
175 macro_source_file
*source
,
178 print_one_macro (name
, macro
, source
, line
, file
);
183 /* Helper function to construct a header scope for a block of code.
184 Takes a scope argument which selects the correct header to
188 add_code_header (enum compile_i_scope_types type
, struct ui_file
*buf
)
192 case COMPILE_I_SIMPLE_SCOPE
:
193 fputs_unfiltered ("void "
194 GCC_FE_WRAPPER_FUNCTION
196 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
198 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
202 case COMPILE_I_PRINT_ADDRESS_SCOPE
:
203 case COMPILE_I_PRINT_VALUE_SCOPE
:
204 /* <string.h> is needed for a memcpy call below. */
205 fputs_unfiltered ("#include <string.h>\n"
207 GCC_FE_WRAPPER_FUNCTION
209 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
211 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
213 COMPILE_I_PRINT_OUT_ARG_TYPE
215 COMPILE_I_PRINT_OUT_ARG
220 case COMPILE_I_RAW_SCOPE
:
223 gdb_assert_not_reached (_("Unknown compiler scope reached."));
227 /* Helper function to construct a footer scope for a block of code.
228 Takes a scope argument which selects the correct footer to
232 add_code_footer (enum compile_i_scope_types type
, struct ui_file
*buf
)
236 case COMPILE_I_SIMPLE_SCOPE
:
237 case COMPILE_I_PRINT_ADDRESS_SCOPE
:
238 case COMPILE_I_PRINT_VALUE_SCOPE
:
239 fputs_unfiltered ("}\n", buf
);
241 case COMPILE_I_RAW_SCOPE
:
244 gdb_assert_not_reached (_("Unknown compiler scope reached."));
248 /* Generate a structure holding all the registers used by the function
252 generate_register_struct (struct ui_file
*stream
, struct gdbarch
*gdbarch
,
253 const unsigned char *registers_used
)
258 fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
" {\n",
261 if (registers_used
!= NULL
)
262 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); ++i
)
264 if (registers_used
[i
])
266 struct type
*regtype
= check_typedef (register_type (gdbarch
, i
));
267 std::string regname
= compile_register_name_mangled (gdbarch
, i
);
271 /* You might think we could use type_print here. However,
272 target descriptions often use types with names like
273 "int64_t", which may not be defined in the inferior
274 (and in any case would not be looked up due to the
275 #pragma business). So, we take a much simpler
276 approach: for pointer- or integer-typed registers, emit
277 the field in the most direct way; and for other
278 register types (typically flags or vectors), emit a
279 maximally-aligned array of the correct size. */
281 fputs_unfiltered (" ", stream
);
282 switch (TYPE_CODE (regtype
))
285 fprintf_filtered (stream
, "__gdb_uintptr %s",
292 = c_get_mode_for_size (TYPE_LENGTH (regtype
));
296 if (TYPE_UNSIGNED (regtype
))
297 fputs_unfiltered ("unsigned ", stream
);
298 fprintf_unfiltered (stream
,
300 " __attribute__ ((__mode__(__%s__)))",
310 fprintf_unfiltered (stream
,
311 " unsigned char %s[%d]"
312 " __attribute__((__aligned__("
313 "__BIGGEST_ALIGNMENT__)))",
315 TYPE_LENGTH (regtype
));
317 fputs_unfiltered (";\n", stream
);
322 fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY
";\n",
325 fputs_unfiltered ("};\n\n", stream
);
328 /* Take the source code provided by the user with the 'compile'
329 command, and compute the additional wrapping, macro, variable and
330 register operations needed. INPUT is the source code derived from
331 the 'compile' command, GDBARCH is the architecture to use when
332 computing above, EXPR_BLOCK denotes the block relevant contextually
333 to the inferior when the expression was created, and EXPR_PC
334 indicates the value of $PC. */
337 c_compute_program (struct compile_instance
*inst
,
339 struct gdbarch
*gdbarch
,
340 const struct block
*expr_block
,
343 struct compile_c_instance
*context
= (struct compile_c_instance
*) inst
;
346 string_file var_stream
;
348 write_macro_definitions (expr_block
, expr_pc
, &buf
);
350 /* Do not generate local variable information for "raw"
351 compilations. In this case we aren't emitting our own function
352 and the user's code may only refer to globals. */
353 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
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
361 gdb::unique_xmalloc_ptr
<unsigned char> registers_used
362 = generate_c_for_variable_locations (context
, var_stream
, gdbarch
,
363 expr_block
, expr_pc
);
365 buf
.puts ("typedef unsigned int"
366 " __attribute__ ((__mode__(__pointer__)))"
367 " __gdb_uintptr;\n");
368 buf
.puts ("typedef int"
369 " __attribute__ ((__mode__(__pointer__)))"
372 /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */
373 for (i
= 0; i
< 4; ++i
)
375 const char *mode
= c_get_mode_for_size (1 << i
);
377 gdb_assert (mode
!= NULL
);
378 buf
.printf ("typedef int"
379 " __attribute__ ((__mode__(__%s__)))"
384 generate_register_struct (&buf
, gdbarch
, registers_used
.get ());
387 add_code_header (inst
->scope
, &buf
);
389 if (inst
->scope
== COMPILE_I_SIMPLE_SCOPE
390 || inst
->scope
== COMPILE_I_PRINT_ADDRESS_SCOPE
391 || inst
->scope
== COMPILE_I_PRINT_VALUE_SCOPE
)
393 buf
.write (var_stream
.c_str (), var_stream
.size ());
394 buf
.puts ("#pragma GCC user_expression\n");
397 /* The user expression has to be in its own scope, so that "extern"
398 works properly. Otherwise gcc thinks that the "extern"
399 declaration is in the same scope as the declaration provided by
401 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
404 buf
.puts ("#line 1 \"gdb command line\"\n");
408 case COMPILE_I_PRINT_ADDRESS_SCOPE
:
409 case COMPILE_I_PRINT_VALUE_SCOPE
:
411 "__auto_type " COMPILE_I_EXPR_VAL
" = %s;\n"
412 "typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE
";\n"
413 "memcpy (" COMPILE_I_PRINT_OUT_ARG
", %s" COMPILE_I_EXPR_VAL
",\n"
414 "sizeof (*" COMPILE_I_EXPR_PTR_TYPE
"));\n"
416 (inst
->scope
== COMPILE_I_PRINT_ADDRESS_SCOPE
426 /* For larger user expressions the automatic semicolons may be
428 if (strchr (input
, '\n') == NULL
)
431 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
434 add_code_footer (inst
->scope
, &buf
);
435 return std::move (buf
.string ());