GDB (xrefs)
compile-c-support.c
Go to the documentation of this file.
1 /* C language support for compilation.
2 
3  Copyright (C) 2014-2015 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 
29 /* See compile-internal.h. */
30 
31 const char *
33 {
34  const char *mode = NULL;
35 
36  switch (size)
37  {
38  case 1:
39  mode = "QI";
40  break;
41  case 2:
42  mode = "HI";
43  break;
44  case 4:
45  mode = "SI";
46  break;
47  case 8:
48  mode = "DI";
49  break;
50  default:
51  internal_error (__FILE__, __LINE__, _("Invalid GCC mode size %d."), size);
52  }
53 
54  return mode;
55 }
56 
57 /* See compile-internal.h. */
58 
59 char *
60 c_get_range_decl_name (const struct dynamic_prop *prop)
61 {
62  return xstrprintf ("__gdb_prop_%s", host_address_to_string (prop));
63 }
64 
65 
66 
67 #define STR(x) #x
68 #define STRINGIFY(x) STR(x)
69 
70 /* Helper function for c_get_compile_context. Open the GCC front-end
71  shared library and return the symbol specified by the current
72  GCC_C_FE_CONTEXT. */
73 
74 static gcc_c_fe_context_function *
75 load_libcc (void)
76 {
77  void *handle;
78  gcc_c_fe_context_function *func;
79 
80  /* gdb_dlopen will call error () on an error, so no need to check
81  value. */
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));
85 
86  if (func == NULL)
87  error (_("could not find symbol %s in library %s"),
88  STRINGIFY (GCC_C_FE_CONTEXT),
89  STRINGIFY (GCC_C_FE_LIBCC));
90  return func;
91 }
92 
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. */
96 
97 struct compile_instance *
99 {
100  static gcc_c_fe_context_function *func;
101 
102  struct gcc_c_context *context;
103 
104  if (func == NULL)
105  {
106  func = load_libcc ();
107  gdb_assert (func != NULL);
108  }
109 
110  context = (*func) (GCC_FE_VERSION_0, GCC_C_FE_VERSION_0);
111  if (context == NULL)
112  error (_("The loaded version of GCC does not support the required version "
113  "of the API."));
114 
115  return new_compile_instance (context);
116 }
117 
118 
119 
120 /* Write one macro definition. */
121 
122 static void
123 print_one_macro (const char *name, const struct macro_definition *macro,
124  struct macro_source_file *source, int line,
125  void *user_data)
126 {
127  struct ui_file *file = user_data;
128 
129  /* Don't print command-line defines. They will be supplied another
130  way. */
131  if (line == 0)
132  return;
133 
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);
137 
138  if (macro->kind == macro_function_like)
139  {
140  int i;
141 
142  fputs_filtered ("(", file);
143  for (i = 0; i < macro->argc; i++)
144  {
145  fputs_filtered (macro->argv[i], file);
146  if (i + 1 < macro->argc)
147  fputs_filtered (", ", file);
148  }
149  fputs_filtered (")", file);
150  }
151 
152  fprintf_filtered (file, " %s\n#endif\n", macro->replacement);
153 }
154 
155 /* Write macro definitions at PC to FILE. */
156 
157 static void
159  struct ui_file *file)
160 {
161  struct macro_scope *scope;
162 
163  if (block != NULL)
164  scope = sal_macro_scope (find_pc_line (pc, 0));
165  else
166  scope = default_macro_scope ();
167  if (scope == NULL)
168  scope = user_macro_scope ();
169 
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);
172 }
173 
174 /* Helper function to construct a header scope for a block of code.
175  Takes a scope argument which selects the correct header to
176  insert into BUF. */
177 
178 static void
180 {
181  switch (type)
182  {
184  fputs_unfiltered ("void "
185  GCC_FE_WRAPPER_FUNCTION
186  " (struct "
188  " *"
190  ") {\n",
191  buf);
192  break;
195  /* <string.h> is needed for a memcpy call below. */
196  fputs_unfiltered ("#include <string.h>\n"
197  "void "
198  GCC_FE_WRAPPER_FUNCTION
199  " (struct "
201  " *"
202  COMPILE_I_SIMPLE_REGISTER_ARG_NAME
203  ", "
205  " "
207  ") {\n",
208  buf);
209  break;
210 
211  case COMPILE_I_RAW_SCOPE:
212  break;
213  default:
214  gdb_assert_not_reached (_("Unknown compiler scope reached."));
215  }
216 }
217 
218 /* Helper function to construct a footer scope for a block of code.
219  Takes a scope argument which selects the correct footer to
220  insert into BUF. */
221 
222 static void
224 {
225  switch (type)
226  {
230  fputs_unfiltered ("}\n", buf);
231  break;
232  case COMPILE_I_RAW_SCOPE:
233  break;
234  default:
235  gdb_assert_not_reached (_("Unknown compiler scope reached."));
236  }
237 }
238 
239 /* Generate a structure holding all the registers used by the function
240  we're generating. */
241 
242 static void
244  const unsigned char *registers_used)
245 {
246  int i;
247  int seen = 0;
248 
250  stream);
251 
252  if (registers_used != NULL)
253  for (i = 0; i < gdbarch_num_regs (gdbarch); ++i)
254  {
255  if (registers_used[i])
256  {
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);
260 
261  seen = 1;
262 
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. */
272 
273  fputs_unfiltered (" ", stream);
274  switch (TYPE_CODE (regtype))
275  {
276  case TYPE_CODE_PTR:
277  fprintf_filtered (stream, "__gdb_uintptr %s", regname);
278  break;
279 
280  case TYPE_CODE_INT:
281  {
282  const char *mode
283  = c_get_mode_for_size (TYPE_LENGTH (regtype));
284 
285  if (mode != NULL)
286  {
287  if (TYPE_UNSIGNED (regtype))
288  fputs_unfiltered ("unsigned ", stream);
289  fprintf_unfiltered (stream,
290  "int %s"
291  " __attribute__ ((__mode__(__%s__)))",
292  regname,
293  mode);
294  break;
295  }
296  }
297 
298  /* Fall through. */
299 
300  default:
301  fprintf_unfiltered (stream,
302  " unsigned char %s[%d]"
303  " __attribute__((__aligned__("
304  "__BIGGEST_ALIGNMENT__)))",
305  regname,
306  TYPE_LENGTH (regtype));
307  }
308  fputs_unfiltered (";\n", stream);
309 
310  do_cleanups (cleanups);
311  }
312  }
313 
314  if (!seen)
316  stream);
317 
318  fputs_unfiltered ("};\n\n", stream);
319 }
320 
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. */
328 
329 char *
331  const char *input,
332  struct gdbarch *gdbarch,
333  const struct block *expr_block,
334  CORE_ADDR expr_pc)
335 {
336  struct ui_file *buf, *var_stream = NULL;
337  char *code;
338  struct cleanup *cleanup;
339  struct compile_c_instance *context = (struct compile_c_instance *) inst;
340 
341  buf = mem_fileopen ();
342  cleanup = make_cleanup_ui_file_delete (buf);
343 
344  write_macro_definitions (expr_block, expr_pc, buf);
345 
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)
350  {
351  unsigned char *registers_used;
352  int i;
353 
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
357  temporary stream. */
358  var_stream = mem_fileopen ();
359  make_cleanup_ui_file_delete (var_stream);
360  registers_used = generate_c_for_variable_locations (context,
361  var_stream, gdbarch,
362  expr_block, expr_pc);
363  make_cleanup (xfree, registers_used);
364 
365  fputs_unfiltered ("typedef unsigned int"
366  " __attribute__ ((__mode__(__pointer__)))"
367  " __gdb_uintptr;\n",
368  buf);
369  fputs_unfiltered ("typedef int"
370  " __attribute__ ((__mode__(__pointer__)))"
371  " __gdb_intptr;\n",
372  buf);
373 
374  /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */
375  for (i = 0; i < 4; ++i)
376  {
377  const char *mode = c_get_mode_for_size (1 << i);
378 
379  gdb_assert (mode != NULL);
380  fprintf_unfiltered (buf,
381  "typedef int"
382  " __attribute__ ((__mode__(__%s__)))"
383  " __gdb_int_%s;\n",
384  mode, mode);
385  }
386 
387  generate_register_struct (buf, gdbarch, registers_used);
388  }
389 
390  add_code_header (inst->scope, buf);
391 
392  if (inst->scope == COMPILE_I_SIMPLE_SCOPE
395  {
396  ui_file_put (var_stream, ui_file_write_for_put, buf);
397  fputs_unfiltered ("#pragma GCC user_expression\n", buf);
398  }
399 
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
403  gdb. */
404  if (inst->scope != COMPILE_I_RAW_SCOPE)
405  fputs_unfiltered ("{\n", buf);
406 
407  fputs_unfiltered ("#line 1 \"gdb command line\"\n", buf);
408 
409  switch (inst->scope)
410  {
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"
418  , input, input,
420  ? "&" : ""));
421  break;
422  default:
423  fputs_unfiltered (input, buf);
424  break;
425  }
426 
427  fputs_unfiltered ("\n", buf);
428 
429  /* For larger user expressions the automatic semicolons may be
430  confusing. */
431  if (strchr (input, '\n') == NULL)
432  fputs_unfiltered (";\n", buf);
433 
434  if (inst->scope != COMPILE_I_RAW_SCOPE)
435  fputs_unfiltered ("}\n", buf);
436 
437  add_code_footer (inst->scope, buf);
438  code = ui_file_xstrdup (buf, NULL);
439  do_cleanups (cleanup);
440  return code;
441 }
#define COMPILE_I_EXPR_PTR_TYPE
const char *const * argv
Definition: macrotab.h:300
struct macro_source_file * file
Definition: macroscope.h:34
static void generate_register_struct(struct ui_file *stream, struct gdbarch *gdbarch, const unsigned char *registers_used)
bfd_vma CORE_ADDR
Definition: common-types.h:41
__extension__ enum macro_kind kind
Definition: macrotab.h:292
void fputs_unfiltered(const char *buf, struct ui_file *file)
Definition: ui-file.c:252
void xfree(void *)
Definition: common-utils.c:97
struct macro_table * table
Definition: macrotab.h:127
void(* func)(char *)
void * gdb_dlopen(const char *filename)
Definition: gdb-dlfcn.c:68
char * ui_file_xstrdup(struct ui_file *file, long *length)
Definition: ui-file.c:345
void ui_file_put(struct ui_file *file, ui_file_put_method_ftype *write, void *dest)
Definition: ui-file.c:210
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
#define COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
compile_i_scope_types
Definition: defs.h:60
void macro_for_each_in_scope(struct macro_source_file *file, int line, macro_callback_fn fn, void *user_data)
Definition: macrotab.c:1031
#define COMPILE_I_PRINT_OUT_ARG
static gcc_c_fe_context_function * load_libcc(void)
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition: gdbarch.c:1898
#define _(String)
Definition: gdb_locale.h:40
struct macro_scope * sal_macro_scope(struct symtab_and_line sal)
Definition: macroscope.c:39
char * compile_register_name_mangled(struct gdbarch *gdbarch, int regnum)
Definition: compile.c:642
struct macro_scope * default_macro_scope(void)
Definition: macroscope.c:102
static void add_code_footer(enum compile_i_scope_types type, struct ui_file *buf)
const char *const name
Definition: aarch64-tdep.c:68
struct type * check_typedef(struct type *type)
Definition: gdbtypes.c:2217
void * gdb_dlsym(void *handle, const char *symbol)
Definition: gdb-dlfcn.c:100
struct symtab_and_line find_pc_line(CORE_ADDR pc, int notcurrent)
Definition: symtab.c:3315
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2351
struct type * register_type(struct gdbarch *gdbarch, int regnum)
Definition: regcache.c:157
void fprintf_unfiltered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2361
#define COMPILE_I_SIMPLE_REGISTER_DUMMY
void fputs_filtered(const char *linebuffer, struct ui_file *stream)
Definition: utils.c:2145
#define COMPILE_I_SIMPLE_REGISTER_ARG_NAME
#define gdb_assert_not_reached(message)
Definition: gdb_assert.h:56
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:117
#define COMPILE_I_EXPR_VAL
Definition: gdbtypes.h:749
#define gdb_assert(expr)
Definition: gdb_assert.h:33
char * xstrprintf(const char *format,...)
Definition: common-utils.c:107
const char * c_get_mode_for_size(int size)
char * c_compute_program(struct compile_instance *inst, const char *input, struct gdbarch *gdbarch, const struct block *expr_block, CORE_ADDR expr_pc)
struct ui_file * mem_fileopen(void)
Definition: ui-file.c:427
void ui_file_write_for_put(void *data, const char *buffer, long length_buffer)
Definition: ui-file.c:226
struct compile_instance * c_get_compile_context(void)
#define TYPE_UNSIGNED(t)
Definition: gdbtypes.h:233
Definition: block.h:60
unsigned char * generate_c_for_variable_locations(struct compile_c_instance *compiler, struct ui_file *stream, struct gdbarch *gdbarch, const struct block *block, CORE_ADDR pc)
static void print_one_macro(const char *name, const struct macro_definition *macro, struct macro_source_file *source, int line, void *user_data)
struct cleanup * make_cleanup_ui_file_delete(struct ui_file *arg)
Definition: utils.c:237
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1240
enum compile_i_scope_types scope
int code
Definition: ser-unix.c:684
int line
Definition: symtab.h:1570
static void add_code_header(enum compile_i_scope_types type, struct ui_file *buf)
static void write_macro_definitions(const struct block *block, CORE_ADDR pc, struct ui_file *file)
#define COMPILE_I_PRINT_OUT_ARG_TYPE
struct compile_instance * new_compile_instance(struct gcc_c_context *fe)
#define TYPE_LENGTH(thistype)
Definition: gdbtypes.h:1237
const char * replacement
Definition: macrotab.h:307
void error(const char *fmt,...)
Definition: errors.c:38
char * c_get_range_decl_name(const struct dynamic_prop *prop)
size_t size
Definition: go32-nat.c:242
#define STRINGIFY(x)
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:175
struct macro_scope * user_macro_scope(void)
Definition: macroscope.c:91