GDB (xrefs)
Loading...
Searching...
No Matches
compile-c-support.c
Go to the documentation of this file.
1/* C/C++ language support for compilation.
2
3 Copyright (C) 2014-2023 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-c.h"
23#include "compile-cplus.h"
24#include "compile.h"
25#include "c-lang.h"
26#include "macrotab.h"
27#include "macroscope.h"
28#include "regcache.h"
29#include "gdbsupport/function-view.h"
30#include "gdbsupport/gdb-dlfcn.h"
31#include "gdbsupport/preprocessor.h"
32#include "gdbarch.h"
33
34/* See compile-internal.h. */
35
36const char *
38{
39 const char *mode = NULL;
40
41 switch (size)
42 {
43 case 1:
44 mode = "QI";
45 break;
46 case 2:
47 mode = "HI";
48 break;
49 case 4:
50 mode = "SI";
51 break;
52 case 8:
53 mode = "DI";
54 break;
55 default:
56 internal_error (_("Invalid GCC mode size %d."), size);
57 }
58
59 return mode;
60}
61
62/* See compile-internal.h. */
63
64std::string
66{
67 return string_printf ("__gdb_prop_%s", host_address_to_string (prop));
68}
69
70
71
72/* Load the plug-in library FE_LIBCC and return the initialization function
73 FE_CONTEXT. */
74
75template <typename FUNCTYPE>
76FUNCTYPE *
77load_libcompile (const char *fe_libcc, const char *fe_context)
78{
79 FUNCTYPE *func;
80
81 /* gdb_dlopen will call error () on an error, so no need to check
82 value. */
83 gdb_dlhandle_up handle = gdb_dlopen (fe_libcc);
84 func = (FUNCTYPE *) gdb_dlsym (handle, fe_context);
85
86 if (func == NULL)
87 error (_("could not find symbol %s in library %s"), fe_context, 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_libcompile
96 function. FE_LIBCC is the library to load. BASE_VERSION is the
97 base compile plug-in version we support. API_VERSION is the
98 API version supported. */
99
100template <typename INSTTYPE, typename FUNCTYPE, typename CTXTYPE,
101 typename BASE_VERSION_TYPE, typename API_VERSION_TYPE>
102std::unique_ptr<compile_instance>
103get_compile_context (const char *fe_libcc, const char *fe_context,
104 BASE_VERSION_TYPE base_version,
105 API_VERSION_TYPE api_version)
106{
107 static FUNCTYPE *func;
108 static CTXTYPE *context;
109
110 if (func == NULL)
111 {
112 func = load_libcompile<FUNCTYPE> (fe_libcc, fe_context);
113 gdb_assert (func != NULL);
114 }
115
116 context = (*func) (base_version, api_version);
117 if (context == NULL)
118 error (_("The loaded version of GCC does not support the required version "
119 "of the API."));
120
121 return gdb::make_unique<INSTTYPE> (context);
122}
123
124/* A C-language implementation of get_compile_context. */
125
126std::unique_ptr<compile_instance>
128{
130 <compile_c_instance, gcc_c_fe_context_function, gcc_c_context,
131 gcc_base_api_version, gcc_c_api_version>
132 (STRINGIFY (GCC_C_FE_LIBCC), STRINGIFY (GCC_C_FE_CONTEXT),
133 GCC_FE_VERSION_0, GCC_C_FE_VERSION_0);
134}
135
136/* A C++-language implementation of get_compile_context. */
137
138std::unique_ptr<compile_instance>
140{
142 <compile_cplus_instance, gcc_cp_fe_context_function, gcc_cp_context,
143 gcc_base_api_version, gcc_cp_api_version>
144 (STRINGIFY (GCC_CP_FE_LIBCC), STRINGIFY (GCC_CP_FE_CONTEXT),
145 GCC_FE_VERSION_0, GCC_CP_FE_VERSION_0);
146}
147
148
149
150/* Write one macro definition. */
151
152static void
153print_one_macro (const char *name, const struct macro_definition *macro,
154 struct macro_source_file *source, int line,
155 ui_file *file)
156{
157 /* Don't print command-line defines. They will be supplied another
158 way. */
159 if (line == 0)
160 return;
161
162 /* None of -Wno-builtin-macro-redefined, #undef first
163 or plain #define of the same value would avoid a warning. */
164 gdb_printf (file, "#ifndef %s\n# define %s", name, name);
165
166 if (macro->kind == macro_function_like)
167 {
168 int i;
169
170 gdb_puts ("(", file);
171 for (i = 0; i < macro->argc; i++)
172 {
173 gdb_puts (macro->argv[i], file);
174 if (i + 1 < macro->argc)
175 gdb_puts (", ", file);
176 }
177 gdb_puts (")", file);
178 }
179
180 gdb_printf (file, " %s\n#endif\n", macro->replacement);
181}
182
183/* Write macro definitions at PC to FILE. */
184
185static void
186write_macro_definitions (const struct block *block, CORE_ADDR pc,
187 struct ui_file *file)
188{
189 gdb::unique_xmalloc_ptr<struct macro_scope> scope;
190
191 if (block != NULL)
192 scope = sal_macro_scope (find_pc_line (pc, 0));
193 else
194 scope = default_macro_scope ();
195 if (scope == NULL)
196 scope = user_macro_scope ();
197
198 if (scope != NULL && scope->file != NULL && scope->file->table != NULL)
199 {
200 macro_for_each_in_scope (scope->file, scope->line,
201 [&] (const char *name,
202 const macro_definition *macro,
203 macro_source_file *source,
204 int line)
205 {
206 print_one_macro (name, macro, source, line, file);
207 });
208 }
209}
210
211/* Generate a structure holding all the registers used by the function
212 we're generating. */
213
214static void
216 const std::vector<bool> &registers_used)
217{
218 int i;
219 int seen = 0;
220
222 stream);
223
224 if (!registers_used.empty ())
225 for (i = 0; i < gdbarch_num_regs (gdbarch); ++i)
226 {
227 if (registers_used[i])
228 {
229 struct type *regtype = check_typedef (register_type (gdbarch, i));
230 std::string regname = compile_register_name_mangled (gdbarch, i);
231
232 seen = 1;
233
234 /* You might think we could use type_print here. However,
235 target descriptions often use types with names like
236 "int64_t", which may not be defined in the inferior
237 (and in any case would not be looked up due to the
238 #pragma business). So, we take a much simpler
239 approach: for pointer- or integer-typed registers, emit
240 the field in the most direct way; and for other
241 register types (typically flags or vectors), emit a
242 maximally-aligned array of the correct size. */
243
244 gdb_puts (" ", stream);
245 switch (regtype->code ())
246 {
247 case TYPE_CODE_PTR:
248 gdb_printf (stream, "__gdb_uintptr %s",
249 regname.c_str ());
250 break;
251
252 case TYPE_CODE_INT:
253 {
254 const char *mode
255 = c_get_mode_for_size (regtype->length ());
256
257 if (mode != NULL)
258 {
259 if (regtype->is_unsigned ())
260 gdb_puts ("unsigned ", stream);
261 gdb_printf (stream,
262 "int %s"
263 " __attribute__ ((__mode__(__%s__)))",
264 regname.c_str (),
265 mode);
266 break;
267 }
268 }
269
270 /* Fall through. */
271
272 default:
273 gdb_printf (stream,
274 " unsigned char %s[%s]"
275 " __attribute__((__aligned__("
276 "__BIGGEST_ALIGNMENT__)))",
277 regname.c_str (),
278 pulongest (regtype->length ()));
279 }
280 gdb_puts (";\n", stream);
281 }
282 }
283
284 if (!seen)
286 stream);
287
288 gdb_puts ("};\n\n", stream);
289}
290
291/* C-language policy to emit a push user expression pragma into BUF. */
292
294{
295 void push_user_expression (struct ui_file *buf)
296 {
297 gdb_puts ("#pragma GCC user_expression\n", buf);
298 }
299};
300
301/* C-language policy to emit a pop user expression pragma into BUF.
302 For C, this is a nop. */
303
305{
306 void pop_user_expression (struct ui_file *buf)
307 {
308 /* Nothing to do. */
309 }
310};
311
312/* C-language policy to construct a code header for a block of code.
313 Takes a scope TYPE argument which selects the correct header to
314 insert into BUF. */
315
317{
319 {
320 switch (type)
321 {
323 gdb_puts ("void "
324 GCC_FE_WRAPPER_FUNCTION
325 " (struct "
327 " *"
329 ") {\n",
330 buf);
331 break;
332
335 /* <string.h> is needed for a memcpy call below. */
336 gdb_puts ("#include <string.h>\n"
337 "void "
338 GCC_FE_WRAPPER_FUNCTION
339 " (struct "
341 " *"
343 ", "
345 " "
347 ") {\n",
348 buf);
349 break;
350
352 break;
353
354 default:
355 gdb_assert_not_reached ("Unknown compiler scope reached.");
356 }
357 }
358};
359
360/* C-language policy to construct a code footer for a block of code.
361 Takes a scope TYPE which selects the correct footer to insert into BUF. */
362
364{
366 {
367 switch (type)
368 {
372 gdb_puts ("}\n", buf);
373 break;
374
376 break;
377
378 default:
379 gdb_assert_not_reached ("Unknown compiler scope reached.");
380 }
381 }
382};
383
384/* C-language policy to emit the user code snippet INPUT into BUF based on the
385 scope TYPE. */
386
388{
389 void add_input (enum compile_i_scope_types type, const char *input,
390 struct ui_file *buf)
391 {
392 switch (type)
393 {
396 gdb_printf (buf,
397 "__auto_type " COMPILE_I_EXPR_VAL " = %s;\n"
398 "typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE ";\n"
399 "memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s"
401 "sizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n"
402 , input, input,
404 ? "&" : ""));
405 break;
406
407 default:
408 gdb_puts (input, buf);
409 break;
410 }
411 gdb_puts ("\n", buf);
412 }
413};
414
415/* C++-language policy to emit a push user expression pragma into
416 BUF. */
417
419{
420 void push_user_expression (struct ui_file *buf)
421 {
422 gdb_puts ("#pragma GCC push_user_expression\n", buf);
423 }
424};
425
426/* C++-language policy to emit a pop user expression pragma into BUF. */
427
429{
430 void pop_user_expression (struct ui_file *buf)
431 {
432 gdb_puts ("#pragma GCC pop_user_expression\n", buf);
433 }
434};
435
436/* C++-language policy to construct a code header for a block of code.
437 Takes a scope TYPE argument which selects the correct header to
438 insert into BUF. */
439
441{
443 {
444 switch (type)
445 {
447 gdb_puts ("void "
448 GCC_FE_WRAPPER_FUNCTION
449 " (struct "
451 " *"
453 ") {\n",
454 buf);
455 break;
456
459 gdb_puts (
460 "#include <cstring>\n"
461 "#include <bits/move.h>\n"
462 "void "
463 GCC_FE_WRAPPER_FUNCTION
464 " (struct "
466 " *"
468 ", "
470 " "
472 ") {\n",
473 buf);
474 break;
475
477 break;
478
479 default:
480 gdb_assert_not_reached ("Unknown compiler scope reached.");
481 }
482 }
483};
484
485/* C++-language policy to emit the user code snippet INPUT into BUF based on
486 the scope TYPE. */
487
489{
490 void add_input (enum compile_i_scope_types type, const char *input,
491 struct ui_file *buf)
492 {
493 switch (type)
494 {
498 (buf,
499 /* "auto" strips ref- and cv- qualifiers, so we need to also strip
500 those from COMPILE_I_EXPR_PTR_TYPE. */
501 "auto " COMPILE_I_EXPR_VAL " = %s;\n"
502 "typedef "
503 "std::add_pointer<std::remove_cv<decltype (%s)>::type>::type "
504 " __gdb_expr_ptr;\n"
505 "__gdb_expr_ptr " COMPILE_I_EXPR_PTR_TYPE ";\n"
506 "std::memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s ("
507 COMPILE_I_EXPR_VAL "),\n"
508 "\tsizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n"
509 ,input, input,
511 ? "__builtin_addressof" : ""));
512 break;
513
514 default:
515 gdb_puts (input, buf);
516 break;
517 }
518 gdb_puts ("\n", buf);
519 }
520};
521
522/* A host class representing a compile program.
523
524 CompileInstanceType is the type of the compile_instance for the
525 language.
526
527 PushUserExpressionPolicy and PopUserExpressionPolicy are used to
528 push and pop user expression pragmas to the compile plug-in.
529
530 AddCodeHeaderPolicy and AddCodeFooterPolicy are used to add the appropriate
531 code header and footer, respectively.
532
533 AddInputPolicy adds the actual user code. */
534
535template <class CompileInstanceType, class PushUserExpressionPolicy,
536 class PopUserExpressionPolicy, class AddCodeHeaderPolicy,
537 class AddCodeFooterPolicy, class AddInputPolicy>
539 : private PushUserExpressionPolicy, private PopUserExpressionPolicy,
540 private AddCodeHeaderPolicy, private AddCodeFooterPolicy,
541 private AddInputPolicy
542{
543public:
544
545 /* Construct a compile_program using the compiler instance INST
546 using the architecture given by GDBARCH. */
547 compile_program (CompileInstanceType *inst, struct gdbarch *gdbarch)
548 : m_instance (inst), m_arch (gdbarch)
549 {
550 }
551
552 /* Take the source code provided by the user with the 'compile'
553 command and compute the additional wrapping, macro, variable and
554 register operations needed. INPUT is the source code derived from
555 the 'compile' command, EXPR_BLOCK denotes the block relevant contextually
556 to the inferior when the expression was created, and EXPR_PC
557 indicates the value of $PC.
558
559 Returns the text of the program to compile. */
560 std::string compute (const char *input, const struct block *expr_block,
561 CORE_ADDR expr_pc)
562 {
563 string_file var_stream;
564 string_file buf;
565
566 /* Do not generate local variable information for "raw"
567 compilations. In this case we aren't emitting our own function
568 and the user's code may only refer to globals. */
569 if (m_instance->scope () != COMPILE_I_RAW_SCOPE)
570 {
571 /* Generate the code to compute variable locations, but do it
572 before generating the function header, so we can define the
573 register struct before the function body. This requires a
574 temporary stream. */
575 std::vector<bool> registers_used
577 expr_block, expr_pc);
578
579 buf.puts ("typedef unsigned int"
580 " __attribute__ ((__mode__(__pointer__)))"
581 " __gdb_uintptr;\n");
582 buf.puts ("typedef int"
583 " __attribute__ ((__mode__(__pointer__)))"
584 " __gdb_intptr;\n");
585
586 /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */
587 for (int i = 0; i < 4; ++i)
588 {
589 const char *mode = c_get_mode_for_size (1 << i);
590
591 gdb_assert (mode != NULL);
592 buf.printf ("typedef int"
593 " __attribute__ ((__mode__(__%s__)))"
594 " __gdb_int_%s;\n",
595 mode, mode);
596 }
597
598 generate_register_struct (&buf, m_arch, registers_used);
599 }
600
601 AddCodeHeaderPolicy::add_code_header (m_instance->scope (), &buf);
602
603 if (m_instance->scope () == COMPILE_I_SIMPLE_SCOPE
606 {
607 buf.write (var_stream.c_str (), var_stream.size ());
608 PushUserExpressionPolicy::push_user_expression (&buf);
609 }
610
611 write_macro_definitions (expr_block, expr_pc, &buf);
612
613 /* The user expression has to be in its own scope, so that "extern"
614 works properly. Otherwise gcc thinks that the "extern"
615 declaration is in the same scope as the declaration provided by
616 gdb. */
617 if (m_instance->scope () != COMPILE_I_RAW_SCOPE)
618 buf.puts ("{\n");
619
620 buf.puts ("#line 1 \"gdb command line\"\n");
621
622 AddInputPolicy::add_input (m_instance->scope (), input, &buf);
623
624 /* For larger user expressions the automatic semicolons may be
625 confusing. */
626 if (strchr (input, '\n') == NULL)
627 buf.puts (";\n");
628
629 if (m_instance->scope () != COMPILE_I_RAW_SCOPE)
630 buf.puts ("}\n");
631
632 if (m_instance->scope () == COMPILE_I_SIMPLE_SCOPE
635 PopUserExpressionPolicy::pop_user_expression (&buf);
636
637 AddCodeFooterPolicy::add_code_footer (m_instance->scope (), &buf);
638 return buf.release ();
639 }
640
641private:
642
643 /* The compile instance to be used for compilation and
644 type-conversion. */
645 CompileInstanceType *m_instance;
646
647 /* The architecture to be used. */
649};
650
651/* The types used for C and C++ program computations. */
652
657
662
663/* The compute_program method for C. */
664
665std::string
667 const char *input,
668 struct gdbarch *gdbarch,
669 const struct block *expr_block,
670 CORE_ADDR expr_pc)
671{
672 compile_c_instance *c_inst = static_cast<compile_c_instance *> (inst);
673 c_compile_program program (c_inst, gdbarch);
674
675 return program.compute (input, expr_block, expr_pc);
676}
677
678/* The compute_program method for C++. */
679
680std::string
682 const char *input,
683 struct gdbarch *gdbarch,
684 const struct block *expr_block,
685 CORE_ADDR expr_pc)
686{
687 compile_cplus_instance *cplus_inst
688 = static_cast<compile_cplus_instance *> (inst);
689 cplus_compile_program program (cplus_inst, gdbarch);
690
691 return program.compute (input, expr_block, expr_pc);
692}
const char *const name
CompileInstanceType * m_instance
std::string compute(const char *input, const struct block *expr_block, CORE_ADDR expr_pc)
compile_program(CompileInstanceType *inst, struct gdbarch *gdbarch)
struct gdbarch * m_arch
size_t size() const
Definition ui-file.h:223
void write(const char *buf, long length_buf) override
Definition ui-file.c:214
const char * c_str() const
Definition ui-file.h:222
std::string release()
Definition ui-file.h:204
virtual void puts(const char *str)
Definition ui-file.h:76
void printf(const char *,...) ATTRIBUTE_PRINTF(2
Definition ui-file.c:40
std::string c_get_range_decl_name(const struct dynamic_prop *prop)
static void generate_register_struct(struct ui_file *stream, struct gdbarch *gdbarch, const std::vector< bool > &registers_used)
std::string cplus_compute_program(compile_instance *inst, const char *input, struct gdbarch *gdbarch, const struct block *expr_block, CORE_ADDR expr_pc)
std::unique_ptr< compile_instance > cplus_get_compile_context()
compile_program< compile_cplus_instance, cplus_push_user_expression, cplus_pop_user_expression, cplus_add_code_header, c_add_code_footer, cplus_add_input > cplus_compile_program
FUNCTYPE * load_libcompile(const char *fe_libcc, const char *fe_context)
std::unique_ptr< compile_instance > get_compile_context(const char *fe_libcc, const char *fe_context, BASE_VERSION_TYPE base_version, API_VERSION_TYPE api_version)
std::string c_compute_program(compile_instance *inst, const char *input, struct gdbarch *gdbarch, const struct block *expr_block, CORE_ADDR expr_pc)
static void print_one_macro(const char *name, const struct macro_definition *macro, struct macro_source_file *source, int line, ui_file *file)
std::unique_ptr< compile_instance > c_get_compile_context()
static void write_macro_definitions(const struct block *block, CORE_ADDR pc, struct ui_file *file)
compile_program< compile_c_instance, c_push_user_expression, pop_user_expression_nop, c_add_code_header, c_add_code_footer, c_add_input > c_compile_program
const char * c_get_mode_for_size(int size)
std::vector< bool > generate_c_for_variable_locations(compile_instance *compiler, string_file *stream, struct gdbarch *gdbarch, const struct block *block, CORE_ADDR pc)
#define COMPILE_I_SIMPLE_REGISTER_ARG_NAME
#define COMPILE_I_SIMPLE_REGISTER_DUMMY
#define COMPILE_I_PRINT_OUT_ARG_TYPE
#define COMPILE_I_PRINT_OUT_ARG
#define COMPILE_I_EXPR_VAL
#define COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
#define COMPILE_I_EXPR_PTR_TYPE
std::string compile_register_name_mangled(struct gdbarch *gdbarch, int regnum)
Definition compile.c:847
compile_i_scope_types
Definition defs.h:70
@ COMPILE_I_PRINT_ADDRESS_SCOPE
Definition defs.h:89
@ COMPILE_I_PRINT_VALUE_SCOPE
Definition defs.h:90
@ COMPILE_I_SIMPLE_SCOPE
Definition defs.h:77
@ COMPILE_I_RAW_SCOPE
Definition defs.h:81
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition gdbarch.c:1930
struct type * check_typedef(struct type *type)
Definition gdbtypes.c:2966
size_t size
Definition go32-nat.c:239
gdb::unique_xmalloc_ptr< struct macro_scope > sal_macro_scope(struct symtab_and_line sal)
Definition macroscope.c:39
gdb::unique_xmalloc_ptr< struct macro_scope > default_macro_scope(void)
Definition macroscope.c:99
gdb::unique_xmalloc_ptr< struct macro_scope > user_macro_scope(void)
Definition macroscope.c:90
void macro_for_each_in_scope(struct macro_source_file *file, int line, gdb::function_view< macro_callback_fn > fn)
Definition macrotab.c:1005
@ macro_function_like
Definition macrotab.h:275
struct type * register_type(struct gdbarch *gdbarch, int regnum)
Definition regcache.c:158
void(* func)(remote_target *remote, char *)
Definition block.h:109
void add_code_header(enum compile_i_scope_types type, struct ui_file *buf)
void add_input(enum compile_i_scope_types type, const char *input, struct ui_file *buf)
void push_user_expression(struct ui_file *buf)
void add_code_header(enum compile_i_scope_types type, struct ui_file *buf)
void add_input(enum compile_i_scope_types type, const char *input, struct ui_file *buf)
void pop_user_expression(struct ui_file *buf)
void push_user_expression(struct ui_file *buf)
const char * replacement
Definition macrotab.h:312
const char *const * argv
Definition macrotab.h:305
__extension__ enum macro_kind kind
Definition macrotab.h:297
void pop_user_expression(struct ui_file *buf)
type_code code() const
Definition gdbtypes.h:956
ULONGEST length() const
Definition gdbtypes.h:983
bool is_unsigned() const
Definition gdbtypes.h:1100
struct symtab_and_line find_pc_line(CORE_ADDR pc, int notcurrent)
Definition symtab.c:3295
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
void gdb_puts(const char *linebuffer, struct ui_file *stream)
Definition utils.c:1809