libcp1.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* The library used by gdb.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <cc1plugin-config.h>
  16. #include <vector>
  17. #include <string>
  18. #include <sys/socket.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <sys/wait.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <sys/stat.h>
  25. #include <stdlib.h>
  26. #include "marshall-cp.hh"
  27. #include "rpc.hh"
  28. #include "connection.hh"
  29. #include "names.hh"
  30. #include "callbacks.hh"
  31. #include "libiberty.h"
  32. #include "compiler-name.hh"
  33. #include "compiler.hh"
  34. #include "gdbctx.hh"
  35. // The C compiler context that we hand back to our caller.
  36. struct libcp1 : public cc1_plugin::base_gdb_plugin<gcc_cp_context>
  37. {
  38. explicit libcp1 (const gcc_cp_fe_vtable *);
  39. void add_callbacks () override;
  40. gcc_cp_oracle_function *binding_oracle = nullptr;
  41. gcc_cp_symbol_address_function *address_oracle = nullptr;
  42. gcc_cp_enter_leave_user_expr_scope_function *enter_scope = nullptr;
  43. gcc_cp_enter_leave_user_expr_scope_function *leave_scope = nullptr;
  44. void *oracle_datum = nullptr;
  45. };
  46. libcp1::libcp1 (const gcc_cp_fe_vtable *cv)
  47. : cc1_plugin::base_gdb_plugin<gcc_cp_context> ("libcp1plugin",
  48. CP_COMPILER_NAME,
  49. GCC_CP_FE_VERSION_0)
  50. {
  51. cp_ops = cv;
  52. }
  53. // Enclose these functions in an anonymous namespace because they
  54. // shouldn't be exported, but they can't be static because they're
  55. // used as template arguments.
  56. namespace {
  57. // This is a wrapper function that is called by the RPC system and
  58. // that then forwards the call to the library user. Note that the
  59. // return value is not used; the type cannot be 'void' due to
  60. // limitations in our simple RPC.
  61. int
  62. cp_call_binding_oracle (cc1_plugin::connection *conn,
  63. enum gcc_cp_oracle_request request,
  64. const char *identifier)
  65. {
  66. libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
  67. self->binding_oracle (self->oracle_datum, self, request, identifier);
  68. return 1;
  69. }
  70. // This is a wrapper function that is called by the RPC system and
  71. // that then forwards the call to the library user.
  72. gcc_address
  73. cp_call_symbol_address (cc1_plugin::connection *conn, const char *identifier)
  74. {
  75. libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
  76. return self->address_oracle (self->oracle_datum, self, identifier);
  77. }
  78. int
  79. cp_call_enter_scope (cc1_plugin::connection *conn)
  80. {
  81. libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
  82. self->enter_scope (self->oracle_datum, self);
  83. return 1;
  84. }
  85. int
  86. cp_call_leave_scope (cc1_plugin::connection *conn)
  87. {
  88. libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
  89. self->leave_scope (self->oracle_datum, self);
  90. return 1;
  91. }
  92. } /* anonymous namespace */
  93. static void
  94. set_callbacks (struct gcc_cp_context *s,
  95. gcc_cp_oracle_function *binding_oracle,
  96. gcc_cp_symbol_address_function *address_oracle,
  97. gcc_cp_enter_leave_user_expr_scope_function *enter_scope,
  98. gcc_cp_enter_leave_user_expr_scope_function *leave_scope,
  99. void *datum)
  100. {
  101. libcp1 *self = (libcp1 *) s;
  102. self->binding_oracle = binding_oracle;
  103. self->address_oracle = address_oracle;
  104. self->enter_scope = enter_scope;
  105. self->leave_scope = leave_scope;
  106. self->oracle_datum = datum;
  107. }
  108. static const struct gcc_cp_fe_vtable cp_vtable =
  109. {
  110. GCC_CP_FE_VERSION_0,
  111. set_callbacks,
  112. #define GCC_METHOD0(R, N) \
  113. cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N>,
  114. #define GCC_METHOD1(R, N, A) \
  115. cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A>,
  116. #define GCC_METHOD2(R, N, A, B) \
  117. cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B>,
  118. #define GCC_METHOD3(R, N, A, B, C) \
  119. cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C>,
  120. #define GCC_METHOD4(R, N, A, B, C, D) \
  121. cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D>,
  122. #define GCC_METHOD5(R, N, A, B, C, D, E) \
  123. cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D, E>,
  124. #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
  125. cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D, E, F, G>,
  126. #include "gcc-cp-fe.def"
  127. #undef GCC_METHOD0
  128. #undef GCC_METHOD1
  129. #undef GCC_METHOD2
  130. #undef GCC_METHOD3
  131. #undef GCC_METHOD4
  132. #undef GCC_METHOD5
  133. #undef GCC_METHOD7
  134. };
  135. void
  136. libcp1::add_callbacks ()
  137. {
  138. cc1_plugin::callback_ftype *fun
  139. = cc1_plugin::invoker<int, enum gcc_cp_oracle_request,
  140. const char *>::invoke<cp_call_binding_oracle>;
  141. connection->add_callback ("binding_oracle", fun);
  142. fun = cc1_plugin::invoker<gcc_address,
  143. const char *>::invoke<cp_call_symbol_address>;
  144. connection->add_callback ("address_oracle", fun);
  145. fun = cc1_plugin::invoker<int>::invoke<cp_call_enter_scope>;
  146. connection->add_callback ("enter_scope", fun);
  147. fun = cc1_plugin::invoker<int>::invoke<cp_call_leave_scope>;
  148. connection->add_callback ("leave_scope", fun);
  149. }
  150. extern "C" gcc_cp_fe_context_function gcc_cp_fe_context;
  151. #ifdef __GNUC__
  152. #pragma GCC visibility push(default)
  153. #endif
  154. extern "C"
  155. struct gcc_cp_context *
  156. gcc_cp_fe_context (enum gcc_base_api_version base_version,
  157. enum gcc_cp_api_version cp_version)
  158. {
  159. if ((base_version != GCC_FE_VERSION_0 && base_version != GCC_FE_VERSION_1)
  160. || cp_version != GCC_CP_FE_VERSION_0)
  161. return NULL;
  162. return new libcp1 (&cp_vtable);
  163. }