1 /* C language support for compilation.
3 Copyright (C) 2014-2016 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"
29 /* See compile-internal.h. */
32 c_get_mode_for_size (int size
)
34 const char *mode
= NULL
;
51 internal_error (__FILE__
, __LINE__
, _("Invalid GCC mode size %d."), size
);
57 /* See compile-internal.h. */
60 c_get_range_decl_name (const struct dynamic_prop
*prop
)
62 return xstrprintf ("__gdb_prop_%s", host_address_to_string (prop
));
68 #define STRINGIFY(x) STR(x)
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
*
78 gcc_c_fe_context_function
*func
;
80 /* gdb_dlopen will call error () on an error, so no need to check
82 handle
= gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC
));
83 func
= (gcc_c_fe_context_function
*) gdb_dlsym (handle
,
84 STRINGIFY (GCC_C_FE_CONTEXT
));
87 error (_("could not find symbol %s in library %s"),
88 STRINGIFY (GCC_C_FE_CONTEXT
),
89 STRINGIFY (GCC_C_FE_LIBCC
));
93 /* Return the compile instance associated with the current context.
94 This function calls the symbol returned from the load_libcc
95 function. This will provide the gcc_c_context. */
97 struct compile_instance
*
98 c_get_compile_context (void)
100 static gcc_c_fe_context_function
*func
;
102 struct gcc_c_context
*context
;
106 func
= load_libcc ();
107 gdb_assert (func
!= NULL
);
110 context
= (*func
) (GCC_FE_VERSION_0
, GCC_C_FE_VERSION_0
);
112 error (_("The loaded version of GCC does not support the required version "
115 return new_compile_instance (context
);
120 /* Write one macro definition. */
123 print_one_macro (const char *name
, const struct macro_definition
*macro
,
124 struct macro_source_file
*source
, int line
,
127 struct ui_file
*file
= (struct ui_file
*) user_data
;
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 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
)
171 macro_for_each_in_scope (scope
->file
, scope
->line
, print_one_macro
, file
);
174 /* Helper function to construct a header scope for a block of code.
175 Takes a scope argument which selects the correct header to
179 add_code_header (enum compile_i_scope_types type
, struct ui_file
*buf
)
183 case COMPILE_I_SIMPLE_SCOPE
:
184 fputs_unfiltered ("void "
185 GCC_FE_WRAPPER_FUNCTION
187 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
189 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
193 case COMPILE_I_PRINT_ADDRESS_SCOPE
:
194 case COMPILE_I_PRINT_VALUE_SCOPE
:
195 /* <string.h> is needed for a memcpy call below. */
196 fputs_unfiltered ("#include <string.h>\n"
198 GCC_FE_WRAPPER_FUNCTION
200 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
202 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
204 COMPILE_I_PRINT_OUT_ARG_TYPE
206 COMPILE_I_PRINT_OUT_ARG
211 case COMPILE_I_RAW_SCOPE
:
214 gdb_assert_not_reached (_("Unknown compiler scope reached."));
218 /* Helper function to construct a footer scope for a block of code.
219 Takes a scope argument which selects the correct footer to
223 add_code_footer (enum compile_i_scope_types type
, struct ui_file
*buf
)
227 case COMPILE_I_SIMPLE_SCOPE
:
228 case COMPILE_I_PRINT_ADDRESS_SCOPE
:
229 case COMPILE_I_PRINT_VALUE_SCOPE
:
230 fputs_unfiltered ("}\n", buf
);
232 case COMPILE_I_RAW_SCOPE
:
235 gdb_assert_not_reached (_("Unknown compiler scope reached."));
239 /* Generate a structure holding all the registers used by the function
243 generate_register_struct (struct ui_file
*stream
, struct gdbarch
*gdbarch
,
244 const unsigned char *registers_used
)
249 fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
" {\n",
252 if (registers_used
!= NULL
)
253 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); ++i
)
255 if (registers_used
[i
])
257 struct type
*regtype
= check_typedef (register_type (gdbarch
, i
));
258 char *regname
= compile_register_name_mangled (gdbarch
, i
);
259 struct cleanup
*cleanups
= make_cleanup (xfree
, regname
);
263 /* You might think we could use type_print here. However,
264 target descriptions often use types with names like
265 "int64_t", which may not be defined in the inferior
266 (and in any case would not be looked up due to the
267 #pragma business). So, we take a much simpler
268 approach: for pointer- or integer-typed registers, emit
269 the field in the most direct way; and for other
270 register types (typically flags or vectors), emit a
271 maximally-aligned array of the correct size. */
273 fputs_unfiltered (" ", stream
);
274 switch (TYPE_CODE (regtype
))
277 fprintf_filtered (stream
, "__gdb_uintptr %s", regname
);
283 = c_get_mode_for_size (TYPE_LENGTH (regtype
));
287 if (TYPE_UNSIGNED (regtype
))
288 fputs_unfiltered ("unsigned ", stream
);
289 fprintf_unfiltered (stream
,
291 " __attribute__ ((__mode__(__%s__)))",
301 fprintf_unfiltered (stream
,
302 " unsigned char %s[%d]"
303 " __attribute__((__aligned__("
304 "__BIGGEST_ALIGNMENT__)))",
306 TYPE_LENGTH (regtype
));
308 fputs_unfiltered (";\n", stream
);
310 do_cleanups (cleanups
);
315 fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY
";\n",
318 fputs_unfiltered ("};\n\n", stream
);
321 /* Take the source code provided by the user with the 'compile'
322 command, and compute the additional wrapping, macro, variable and
323 register operations needed. INPUT is the source code derived from
324 the 'compile' command, GDBARCH is the architecture to use when
325 computing above, EXPR_BLOCK denotes the block relevant contextually
326 to the inferior when the expression was created, and EXPR_PC
327 indicates the value of $PC. */
330 c_compute_program (struct compile_instance
*inst
,
332 struct gdbarch
*gdbarch
,
333 const struct block
*expr_block
,
336 struct ui_file
*buf
, *var_stream
= NULL
;
338 struct cleanup
*cleanup
;
339 struct compile_c_instance
*context
= (struct compile_c_instance
*) inst
;
341 buf
= mem_fileopen ();
342 cleanup
= make_cleanup_ui_file_delete (buf
);
344 write_macro_definitions (expr_block
, expr_pc
, buf
);
346 /* Do not generate local variable information for "raw"
347 compilations. In this case we aren't emitting our own function
348 and the user's code may only refer to globals. */
349 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
351 unsigned char *registers_used
;
354 /* Generate the code to compute variable locations, but do it
355 before generating the function header, so we can define the
356 register struct before the function body. This requires a
358 var_stream
= mem_fileopen ();
359 make_cleanup_ui_file_delete (var_stream
);
360 registers_used
= generate_c_for_variable_locations (context
,
362 expr_block
, expr_pc
);
363 make_cleanup (xfree
, registers_used
);
365 fputs_unfiltered ("typedef unsigned int"
366 " __attribute__ ((__mode__(__pointer__)))"
369 fputs_unfiltered ("typedef int"
370 " __attribute__ ((__mode__(__pointer__)))"
374 /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */
375 for (i
= 0; i
< 4; ++i
)
377 const char *mode
= c_get_mode_for_size (1 << i
);
379 gdb_assert (mode
!= NULL
);
380 fprintf_unfiltered (buf
,
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 ui_file_put (var_stream
, ui_file_write_for_put
, buf
);
397 fputs_unfiltered ("#pragma GCC user_expression\n", buf
);
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
)
405 fputs_unfiltered ("{\n", buf
);
407 fputs_unfiltered ("#line 1 \"gdb command line\"\n", buf
);
411 case COMPILE_I_PRINT_ADDRESS_SCOPE
:
412 case COMPILE_I_PRINT_VALUE_SCOPE
:
413 fprintf_unfiltered (buf
,
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
423 fputs_unfiltered (input
, buf
);
427 fputs_unfiltered ("\n", buf
);
429 /* For larger user expressions the automatic semicolons may be
431 if (strchr (input
, '\n') == NULL
)
432 fputs_unfiltered (";\n", buf
);
434 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
435 fputs_unfiltered ("}\n", buf
);
437 add_code_footer (inst
->scope
, buf
);
438 code
= ui_file_xstrdup (buf
, NULL
);
439 do_cleanups (cleanup
);