gdb/compile/compile-c-support.c - gdb

Functions defined

Macros defined

Source code

  1. /* C language support for compilation.

  2.    Copyright (C) 2014-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "compile-internal.h"
  16. #include "compile.h"
  17. #include "gdb-dlfcn.h"
  18. #include "c-lang.h"
  19. #include "macrotab.h"
  20. #include "macroscope.h"
  21. #include "regcache.h"

  22. /* See compile-internal.h.  */

  23. const char *
  24. c_get_mode_for_size (int size)
  25. {
  26.   const char *mode = NULL;

  27.   switch (size)
  28.     {
  29.     case 1:
  30.       mode = "QI";
  31.       break;
  32.     case 2:
  33.       mode = "HI";
  34.       break;
  35.     case 4:
  36.       mode = "SI";
  37.       break;
  38.     case 8:
  39.       mode = "DI";
  40.       break;
  41.     default:
  42.       internal_error (__FILE__, __LINE__, _("Invalid GCC mode size %d."), size);
  43.     }

  44.   return mode;
  45. }

  46. /* See compile-internal.h.  */

  47. char *
  48. c_get_range_decl_name (const struct dynamic_prop *prop)
  49. {
  50.   return xstrprintf ("__gdb_prop_%s", host_address_to_string (prop));
  51. }



  52. #define STR(x) #x
  53. #define STRINGIFY(x) STR(x)

  54. /* Helper function for c_get_compile_context.  Open the GCC front-end
  55.    shared library and return the symbol specified by the current
  56.    GCC_C_FE_CONTEXT.  */

  57. static gcc_c_fe_context_function *
  58. load_libcc (void)
  59. {
  60.   void *handle;
  61.   gcc_c_fe_context_function *func;

  62.    /* gdb_dlopen will call error () on an error, so no need to check
  63.       value.  */
  64.   handle = gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC));
  65.   func = (gcc_c_fe_context_function *) gdb_dlsym (handle,
  66.                                                   STRINGIFY (GCC_C_FE_CONTEXT));

  67.   if (func == NULL)
  68.     error (_("could not find symbol %s in library %s"),
  69.            STRINGIFY (GCC_C_FE_CONTEXT),
  70.            STRINGIFY (GCC_C_FE_LIBCC));
  71.   return func;
  72. }

  73. /* Return the compile instance associated with the current context.
  74.    This function calls the symbol returned from the load_libcc
  75.    function.  This will provide the gcc_c_context.  */

  76. struct compile_instance *
  77. c_get_compile_context (void)
  78. {
  79.   static gcc_c_fe_context_function *func;

  80.   struct gcc_c_context *context;

  81.   if (func == NULL)
  82.     {
  83.       func = load_libcc ();
  84.       gdb_assert (func != NULL);
  85.     }

  86.   context = (*func) (GCC_FE_VERSION_0, GCC_C_FE_VERSION_0);
  87.   if (context == NULL)
  88.     error (_("The loaded version of GCC does not support the required version "
  89.              "of the API."));

  90.   return new_compile_instance (context);
  91. }



  92. /* Write one macro definition.  */

  93. static void
  94. print_one_macro (const char *name, const struct macro_definition *macro,
  95.                  struct macro_source_file *source, int line,
  96.                  void *user_data)
  97. {
  98.   struct ui_file *file = user_data;

  99.   /* Don't print command-line defines.  They will be supplied another
  100.      way.  */
  101.   if (line == 0)
  102.     return;

  103.   fprintf_filtered (file, "#define %s", name);

  104.   if (macro->kind == macro_function_like)
  105.     {
  106.       int i;

  107.       fputs_filtered ("(", file);
  108.       for (i = 0; i < macro->argc; i++)
  109.         {
  110.           fputs_filtered (macro->argv[i], file);
  111.           if (i + 1 < macro->argc)
  112.             fputs_filtered (", ", file);
  113.         }
  114.       fputs_filtered (")", file);
  115.     }

  116.   fprintf_filtered (file, " %s\n", macro->replacement);
  117. }

  118. /* Write macro definitions at PC to FILE.  */

  119. static void
  120. write_macro_definitions (const struct block *block, CORE_ADDR pc,
  121.                          struct ui_file *file)
  122. {
  123.   struct macro_scope *scope;

  124.   if (block != NULL)
  125.     scope = sal_macro_scope (find_pc_line (pc, 0));
  126.   else
  127.     scope = default_macro_scope ();
  128.   if (scope == NULL)
  129.     scope = user_macro_scope ();

  130.   if (scope != NULL && scope->file != NULL && scope->file->table != NULL)
  131.     macro_for_each_in_scope (scope->file, scope->line, print_one_macro, file);
  132. }

  133. /* Helper function to construct a header scope for a block of code.
  134.    Takes a scope argument which selects the correct header to
  135.    insert into BUF.  */

  136. static void
  137. add_code_header (enum compile_i_scope_types type, struct ui_file *buf)
  138. {
  139.   switch (type)
  140.     {
  141.     case COMPILE_I_SIMPLE_SCOPE:
  142.       fputs_unfiltered ("void "
  143.                         GCC_FE_WRAPPER_FUNCTION
  144.                         " (struct "
  145.                         COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
  146.                         " *"
  147.                         COMPILE_I_SIMPLE_REGISTER_ARG_NAME
  148.                         ") {\n",
  149.                         buf);
  150.       break;
  151.     case COMPILE_I_RAW_SCOPE:
  152.       break;
  153.     default:
  154.       gdb_assert_not_reached (_("Unknown compiler scope reached."));
  155.     }
  156. }

  157. /* Helper function to construct a footer scope for a block of code.
  158.    Takes a scope argument which selects the correct footer to
  159.    insert into BUF.  */

  160. static void
  161. add_code_footer (enum compile_i_scope_types type, struct ui_file *buf)
  162. {
  163.   switch (type)
  164.     {
  165.     case COMPILE_I_SIMPLE_SCOPE:
  166.       fputs_unfiltered ("}\n", buf);
  167.       break;
  168.     case COMPILE_I_RAW_SCOPE:
  169.       break;
  170.     default:
  171.       gdb_assert_not_reached (_("Unknown compiler scope reached."));
  172.     }
  173. }

  174. /* Generate a structure holding all the registers used by the function
  175.    we're generating.  */

  176. static void
  177. generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch,
  178.                           const unsigned char *registers_used)
  179. {
  180.   int i;
  181.   int seen = 0;

  182.   fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG " {\n",
  183.                     stream);

  184.   if (registers_used != NULL)
  185.     for (i = 0; i < gdbarch_num_regs (gdbarch); ++i)
  186.       {
  187.         if (registers_used[i])
  188.           {
  189.             struct type *regtype = check_typedef (register_type (gdbarch, i));
  190.             char *regname = compile_register_name_mangled (gdbarch, i);
  191.             struct cleanup *cleanups = make_cleanup (xfree, regname);

  192.             seen = 1;

  193.             /* You might think we could use type_print here.  However,
  194.                target descriptions often use types with names like
  195.                "int64_t", which may not be defined in the inferior
  196.                (and in any case would not be looked up due to the
  197.                #pragma business).  So, we take a much simpler
  198.                approach: for pointer- or integer-typed registers, emit
  199.                the field in the most direct way; and for other
  200.                register types (typically flags or vectors), emit a
  201.                maximally-aligned array of the correct size.  */

  202.             fputs_unfiltered ("  ", stream);
  203.             switch (TYPE_CODE (regtype))
  204.               {
  205.               case TYPE_CODE_PTR:
  206.                 fprintf_filtered (stream, "void *%s", regname);
  207.                 break;

  208.               case TYPE_CODE_INT:
  209.                 {
  210.                   const char *mode
  211.                     = c_get_mode_for_size (TYPE_LENGTH (regtype));

  212.                   if (mode != NULL)
  213.                     {
  214.                       if (TYPE_UNSIGNED (regtype))
  215.                         fputs_unfiltered ("unsigned ", stream);
  216.                       fprintf_unfiltered (stream,
  217.                                           "int %s"
  218.                                           " __attribute__ ((__mode__(__%s__)))",
  219.                                           regname,
  220.                                           mode);
  221.                       break;
  222.                     }
  223.                 }

  224.                 /* Fall through.  */

  225.               default:
  226.                 fprintf_unfiltered (stream,
  227.                                     "  unsigned char %s[%d]"
  228.                                     " __attribute__((__aligned__("
  229.                                     "__BIGGEST_ALIGNMENT__)))",
  230.                                     regname,
  231.                                     TYPE_LENGTH (regtype));
  232.               }
  233.             fputs_unfiltered (";\n", stream);

  234.             do_cleanups (cleanups);
  235.           }
  236.       }

  237.   if (!seen)
  238.     fputs_unfiltered ("  char " COMPILE_I_SIMPLE_REGISTER_DUMMY ";\n",
  239.                       stream);

  240.   fputs_unfiltered ("};\n\n", stream);
  241. }

  242. /* Take the source code provided by the user with the 'compile'
  243.    command, and compute the additional wrapping, macro, variable and
  244.    register operations needed.  INPUT is the source code derived from
  245.    the 'compile' command, GDBARCH is the architecture to use when
  246.    computing above, EXPR_BLOCK denotes the block relevant contextually
  247.    to the inferior when the expression was created, and EXPR_PC
  248.    indicates the value of $PC.  */

  249. char *
  250. c_compute_program (struct compile_instance *inst,
  251.                    const char *input,
  252.                    struct gdbarch *gdbarch,
  253.                    const struct block *expr_block,
  254.                    CORE_ADDR expr_pc)
  255. {
  256.   struct ui_file *buf, *var_stream = NULL;
  257.   char *code;
  258.   struct cleanup *cleanup;
  259.   struct compile_c_instance *context = (struct compile_c_instance *) inst;

  260.   buf = mem_fileopen ();
  261.   cleanup = make_cleanup_ui_file_delete (buf);

  262.   write_macro_definitions (expr_block, expr_pc, buf);

  263.   /* Do not generate local variable information for "raw"
  264.      compilations.  In this case we aren't emitting our own function
  265.      and the user's code may only refer to globals.  */
  266.   if (inst->scope != COMPILE_I_RAW_SCOPE)
  267.     {
  268.       unsigned char *registers_used;
  269.       int i;

  270.       /* Generate the code to compute variable locations, but do it
  271.          before generating the function header, so we can define the
  272.          register struct before the function body.  This requires a
  273.          temporary stream.  */
  274.       var_stream = mem_fileopen ();
  275.       make_cleanup_ui_file_delete (var_stream);
  276.       registers_used = generate_c_for_variable_locations (context,
  277.                                                           var_stream, gdbarch,
  278.                                                           expr_block, expr_pc);
  279.       make_cleanup (xfree, registers_used);

  280.       generate_register_struct (buf, gdbarch, registers_used);

  281.       fputs_unfiltered ("typedef unsigned int"
  282.                         " __attribute__ ((__mode__(__pointer__)))"
  283.                         " __gdb_uintptr;\n",
  284.                         buf);
  285.       fputs_unfiltered ("typedef int"
  286.                         " __attribute__ ((__mode__(__pointer__)))"
  287.                         " __gdb_intptr;\n",
  288.                         buf);

  289.       // Iterate all log2 sizes in bytes supported by c_get_mode_for_size.
  290.       for (i = 0; i < 4; ++i)
  291.         {
  292.           const char *mode = c_get_mode_for_size (1 << i);

  293.           gdb_assert (mode != NULL);
  294.           fprintf_unfiltered (buf,
  295.                               "typedef int"
  296.                               " __attribute__ ((__mode__(__%s__)))"
  297.                               " __gdb_int_%s;\n",
  298.                               mode, mode);
  299.         }
  300.     }

  301.   add_code_header (inst->scope, buf);

  302.   if (inst->scope == COMPILE_I_SIMPLE_SCOPE)
  303.     {
  304.       ui_file_put (var_stream, ui_file_write_for_put, buf);
  305.       fputs_unfiltered ("#pragma GCC user_expression\n", buf);
  306.     }

  307.   /* The user expression has to be in its own scope, so that "extern"
  308.      works properly.  Otherwise gcc thinks that the "extern"
  309.      declaration is in the same scope as the declaration provided by
  310.      gdb.  */
  311.   if (inst->scope != COMPILE_I_RAW_SCOPE)
  312.     fputs_unfiltered ("{\n", buf);

  313.   fputs_unfiltered ("#line 1 \"gdb command line\"\n", buf);
  314.   fputs_unfiltered (input, buf);
  315.   fputs_unfiltered ("\n", buf);

  316.   /* For larger user expressions the automatic semicolons may be
  317.      confusing.  */
  318.   if (strchr (input, '\n') == NULL)
  319.     fputs_unfiltered (";\n", buf);

  320.   if (inst->scope != COMPILE_I_RAW_SCOPE)
  321.     fputs_unfiltered ("}\n", buf);

  322.   add_code_footer (inst->scope, buf);
  323.   code = ui_file_xstrdup (buf, NULL);
  324.   do_cleanups (cleanup);
  325.   return code;
  326. }