1 /* C language support for compilation.
3 Copyright (C) 2014-2017 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"
23 #include "gdb-dlfcn.h"
26 #include "macroscope.h"
28 #include "common/function-view.h"
30 /* See compile-internal.h. */
33 c_get_mode_for_size (int size
)
35 const char *mode
= NULL
;
52 internal_error (__FILE__
, __LINE__
, _("Invalid GCC mode size %d."), size
);
58 /* See compile-internal.h. */
61 c_get_range_decl_name (const struct dynamic_prop
*prop
)
63 return xstrprintf ("__gdb_prop_%s", host_address_to_string (prop
));
69 #define STRINGIFY(x) STR(x)
71 /* Helper function for c_get_compile_context. Open the GCC front-end
72 shared library and return the symbol specified by the current
75 static gcc_c_fe_context_function
*
79 gcc_c_fe_context_function
*func
;
81 /* gdb_dlopen will call error () on an error, so no need to check
83 handle
= gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC
));
84 func
= (gcc_c_fe_context_function
*) gdb_dlsym (handle
,
85 STRINGIFY (GCC_C_FE_CONTEXT
));
88 error (_("could not find symbol %s in library %s"),
89 STRINGIFY (GCC_C_FE_CONTEXT
),
90 STRINGIFY (GCC_C_FE_LIBCC
));
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. */
98 struct compile_instance
*
99 c_get_compile_context (void)
101 static gcc_c_fe_context_function
*func
;
103 struct gcc_c_context
*context
;
107 func
= load_libcc ();
108 gdb_assert (func
!= NULL
);
111 context
= (*func
) (GCC_FE_VERSION_0
, GCC_C_FE_VERSION_0
);
113 error (_("The loaded version of GCC does not support the required version "
116 return new_compile_instance (context
);
121 /* Write one macro definition. */
124 print_one_macro (const char *name
, const struct macro_definition
*macro
,
125 struct macro_source_file
*source
, int line
,
128 /* Don't print command-line defines. They will be supplied another
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
);
137 if (macro
->kind
== macro_function_like
)
141 fputs_filtered ("(", file
);
142 for (i
= 0; i
< macro
->argc
; i
++)
144 fputs_filtered (macro
->argv
[i
], file
);
145 if (i
+ 1 < macro
->argc
)
146 fputs_filtered (", ", file
);
148 fputs_filtered (")", file
);
151 fprintf_filtered (file
, " %s\n#endif\n", macro
->replacement
);
154 /* Write macro definitions at PC to FILE. */
157 write_macro_definitions (const struct block
*block
, CORE_ADDR pc
,
158 struct ui_file
*file
)
160 struct macro_scope
*scope
;
163 scope
= sal_macro_scope (find_pc_line (pc
, 0));
165 scope
= default_macro_scope ();
167 scope
= user_macro_scope ();
169 if (scope
!= NULL
&& scope
->file
!= NULL
&& scope
->file
->table
!= NULL
)
171 macro_for_each_in_scope (scope
->file
, scope
->line
,
172 [&] (const char *name
,
173 const macro_definition
*macro
,
174 macro_source_file
*source
,
177 print_one_macro (name
, macro
, source
, line
, file
);
182 /* Helper function to construct a header scope for a block of code.
183 Takes a scope argument which selects the correct header to
187 add_code_header (enum compile_i_scope_types type
, struct ui_file
*buf
)
191 case COMPILE_I_SIMPLE_SCOPE
:
192 fputs_unfiltered ("void "
193 GCC_FE_WRAPPER_FUNCTION
195 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
197 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
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"
206 GCC_FE_WRAPPER_FUNCTION
208 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
210 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
212 COMPILE_I_PRINT_OUT_ARG_TYPE
214 COMPILE_I_PRINT_OUT_ARG
219 case COMPILE_I_RAW_SCOPE
:
222 gdb_assert_not_reached (_("Unknown compiler scope reached."));
226 /* Helper function to construct a footer scope for a block of code.
227 Takes a scope argument which selects the correct footer to
231 add_code_footer (enum compile_i_scope_types type
, struct ui_file
*buf
)
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
);
240 case COMPILE_I_RAW_SCOPE
:
243 gdb_assert_not_reached (_("Unknown compiler scope reached."));
247 /* Generate a structure holding all the registers used by the function
251 generate_register_struct (struct ui_file
*stream
, struct gdbarch
*gdbarch
,
252 const unsigned char *registers_used
)
257 fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
" {\n",
260 if (registers_used
!= NULL
)
261 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); ++i
)
263 if (registers_used
[i
])
265 struct type
*regtype
= check_typedef (register_type (gdbarch
, i
));
266 char *regname
= compile_register_name_mangled (gdbarch
, i
);
267 struct cleanup
*cleanups
= make_cleanup (xfree
, regname
);
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", regname
);
291 = c_get_mode_for_size (TYPE_LENGTH (regtype
));
295 if (TYPE_UNSIGNED (regtype
))
296 fputs_unfiltered ("unsigned ", stream
);
297 fprintf_unfiltered (stream
,
299 " __attribute__ ((__mode__(__%s__)))",
309 fprintf_unfiltered (stream
,
310 " unsigned char %s[%d]"
311 " __attribute__((__aligned__("
312 "__BIGGEST_ALIGNMENT__)))",
314 TYPE_LENGTH (regtype
));
316 fputs_unfiltered (";\n", stream
);
318 do_cleanups (cleanups
);
323 fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY
";\n",
326 fputs_unfiltered ("};\n\n", stream
);
329 /* Take the source code provided by the user with the 'compile'
330 command, and compute the additional wrapping, macro, variable and
331 register operations needed. INPUT is the source code derived from
332 the 'compile' command, GDBARCH is the architecture to use when
333 computing above, EXPR_BLOCK denotes the block relevant contextually
334 to the inferior when the expression was created, and EXPR_PC
335 indicates the value of $PC. */
338 c_compute_program (struct compile_instance
*inst
,
340 struct gdbarch
*gdbarch
,
341 const struct block
*expr_block
,
344 struct compile_c_instance
*context
= (struct compile_c_instance
*) inst
;
347 string_file var_stream
;
349 write_macro_definitions (expr_block
, expr_pc
, &buf
);
351 /* Do not generate local variable information for "raw"
352 compilations. In this case we aren't emitting our own function
353 and the user's code may only refer to globals. */
354 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
356 unsigned char *registers_used
;
359 /* Generate the code to compute variable locations, but do it
360 before generating the function header, so we can define the
361 register struct before the function body. This requires a
363 registers_used
= generate_c_for_variable_locations (context
,
365 expr_block
, expr_pc
);
366 make_cleanup (xfree
, registers_used
);
368 buf
.puts ("typedef unsigned int"
369 " __attribute__ ((__mode__(__pointer__)))"
370 " __gdb_uintptr;\n");
371 buf
.puts ("typedef int"
372 " __attribute__ ((__mode__(__pointer__)))"
375 /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */
376 for (i
= 0; i
< 4; ++i
)
378 const char *mode
= c_get_mode_for_size (1 << i
);
380 gdb_assert (mode
!= NULL
);
381 buf
.printf ("typedef int"
382 " __attribute__ ((__mode__(__%s__)))"
387 generate_register_struct (&buf
, gdbarch
, registers_used
);
390 add_code_header (inst
->scope
, &buf
);
392 if (inst
->scope
== COMPILE_I_SIMPLE_SCOPE
393 || inst
->scope
== COMPILE_I_PRINT_ADDRESS_SCOPE
394 || inst
->scope
== COMPILE_I_PRINT_VALUE_SCOPE
)
396 buf
.write (var_stream
.c_str (), var_stream
.size ());
397 buf
.puts ("#pragma GCC user_expression\n");
400 /* The user expression has to be in its own scope, so that "extern"
401 works properly. Otherwise gcc thinks that the "extern"
402 declaration is in the same scope as the declaration provided by
404 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
407 buf
.puts ("#line 1 \"gdb command line\"\n");
411 case COMPILE_I_PRINT_ADDRESS_SCOPE
:
412 case COMPILE_I_PRINT_VALUE_SCOPE
:
414 "__auto_type " COMPILE_I_EXPR_VAL
" = %s;\n"
415 "typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE
";\n"
416 "memcpy (" COMPILE_I_PRINT_OUT_ARG
", %s" COMPILE_I_EXPR_VAL
",\n"
417 "sizeof (*" COMPILE_I_EXPR_PTR_TYPE
"));\n"
419 (inst
->scope
== COMPILE_I_PRINT_ADDRESS_SCOPE
429 /* For larger user expressions the automatic semicolons may be
431 if (strchr (input
, '\n') == NULL
)
434 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
437 add_code_footer (inst
->scope
, &buf
);
438 return std::move (buf
.string ());