gdbctx.hh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* Generic GDB-side plugin
  2. Copyright (C) 2020-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. #ifndef CC1_PLUGIN_GDBCTX_HH
  16. #define CC1_PLUGIN_GDBCTX_HH
  17. namespace cc1_plugin
  18. {
  19. // The compiler context that we hand back to our caller.
  20. // Due to this, the entire implementation is in this header.
  21. template<typename T>
  22. struct base_gdb_plugin : public T
  23. {
  24. base_gdb_plugin (const char *plugin_name_, const char *base_name,
  25. int version)
  26. : verbose (false),
  27. plugin_name (plugin_name_),
  28. fe_version (version),
  29. compiler_name (base_name),
  30. compilerp (new compiler (verbose))
  31. {
  32. vtable =
  33. {
  34. GCC_FE_VERSION_1,
  35. do_set_arguments_v0,
  36. do_set_source_file,
  37. do_set_print_callback,
  38. do_compile_v0,
  39. do_destroy,
  40. do_set_verbose,
  41. do_compile,
  42. do_set_arguments,
  43. do_set_triplet_regexp,
  44. do_set_driver_filename,
  45. };
  46. this->base.ops = &vtable;
  47. }
  48. virtual ~base_gdb_plugin () = default;
  49. // A convenience function to print something.
  50. void print (const char *str)
  51. {
  52. this->print_function (this->print_datum, str);
  53. }
  54. // Set the verbose flag.
  55. void set_verbose (bool v)
  56. {
  57. verbose = v;
  58. if (compilerp != nullptr)
  59. compilerp->set_verbose (v);
  60. }
  61. // Make a new connection.
  62. void set_connection (int fd, int aux_fd)
  63. {
  64. connection.reset (new local_connection (fd, aux_fd, this));
  65. }
  66. // This is called just before compilation begins. It should set
  67. // any needed callbacks on the connection.
  68. virtual void add_callbacks () = 0;
  69. // A local subclass of connection that holds a back-pointer to the
  70. // context object that we provide to our caller.
  71. class local_connection : public cc1_plugin::connection
  72. {
  73. public:
  74. local_connection (int fd, int aux_fd, base_gdb_plugin<T> *b)
  75. : connection (fd, aux_fd),
  76. back_ptr (b)
  77. {
  78. }
  79. void print (const char *buf) override
  80. {
  81. back_ptr->print (buf);
  82. }
  83. base_gdb_plugin<T> *back_ptr;
  84. };
  85. std::unique_ptr<local_connection> connection;
  86. void (*print_function) (void *datum, const char *message) = nullptr;
  87. void *print_datum = nullptr;
  88. std::vector<std::string> args;
  89. std::string source_file;
  90. /* Non-zero as an equivalent to gcc driver option "-v". */
  91. bool verbose;
  92. const char *plugin_name;
  93. int fe_version;
  94. const char *compiler_name;
  95. std::unique_ptr<cc1_plugin::compiler> compilerp;
  96. private:
  97. struct gcc_base_vtable vtable;
  98. static inline base_gdb_plugin<T> *
  99. get_self (gcc_base_context *s)
  100. {
  101. T *sub = (T *) s;
  102. return static_cast<base_gdb_plugin<T> *> (sub);
  103. }
  104. static void
  105. do_set_verbose (struct gcc_base_context *s, int /* bool */ verbose)
  106. {
  107. base_gdb_plugin<T> *self = get_self (s);
  108. self->set_verbose (verbose != 0);
  109. }
  110. static char *
  111. do_set_arguments (struct gcc_base_context *s,
  112. int argc, char **argv)
  113. {
  114. base_gdb_plugin<T> *self = get_self (s);
  115. std::string compiler;
  116. char *errmsg = self->compilerp->find (self->compiler_name, compiler);
  117. if (errmsg != NULL)
  118. return errmsg;
  119. self->args.push_back (compiler);
  120. for (int i = 0; i < argc; ++i)
  121. self->args.push_back (argv[i]);
  122. return NULL;
  123. }
  124. static char *
  125. do_set_triplet_regexp (struct gcc_base_context *s,
  126. const char *triplet_regexp)
  127. {
  128. base_gdb_plugin<T> *self = get_self (s);
  129. self->compilerp.reset
  130. (new cc1_plugin::compiler_triplet_regexp (self->verbose,
  131. triplet_regexp));
  132. return NULL;
  133. }
  134. static char *
  135. do_set_driver_filename (struct gcc_base_context *s,
  136. const char *driver_filename)
  137. {
  138. base_gdb_plugin<T> *self = get_self (s);
  139. self->compilerp.reset
  140. (new cc1_plugin::compiler_driver_filename (self->verbose,
  141. driver_filename));
  142. return NULL;
  143. }
  144. static char *
  145. do_set_arguments_v0 (struct gcc_base_context *s,
  146. const char *triplet_regexp,
  147. int argc, char **argv)
  148. {
  149. char *errmsg = do_set_triplet_regexp (s, triplet_regexp);
  150. if (errmsg != NULL)
  151. return errmsg;
  152. return do_set_arguments (s, argc, argv);
  153. }
  154. static void
  155. do_set_source_file (struct gcc_base_context *s,
  156. const char *file)
  157. {
  158. base_gdb_plugin<T> *self = get_self (s);
  159. self->source_file = file;
  160. }
  161. static void
  162. do_set_print_callback (struct gcc_base_context *s,
  163. void (*print_function) (void *datum,
  164. const char *message),
  165. void *datum)
  166. {
  167. base_gdb_plugin<T> *self = get_self (s);
  168. self->print_function = print_function;
  169. self->print_datum = datum;
  170. }
  171. int fork_exec (char **argv, int spair_fds[2], int stderr_fds[2])
  172. {
  173. pid_t child_pid = fork ();
  174. if (child_pid == -1)
  175. {
  176. close (spair_fds[0]);
  177. close (spair_fds[1]);
  178. close (stderr_fds[0]);
  179. close (stderr_fds[1]);
  180. return 0;
  181. }
  182. if (child_pid == 0)
  183. {
  184. // Child.
  185. dup2 (stderr_fds[1], 1);
  186. dup2 (stderr_fds[1], 2);
  187. close (stderr_fds[0]);
  188. close (stderr_fds[1]);
  189. close (spair_fds[0]);
  190. execvp (argv[0], argv);
  191. _exit (127);
  192. }
  193. else
  194. {
  195. // Parent.
  196. close (spair_fds[1]);
  197. close (stderr_fds[1]);
  198. cc1_plugin::status result = cc1_plugin::FAIL;
  199. if (connection->send ('H')
  200. && ::cc1_plugin::marshall (connection.get (), fe_version))
  201. result = connection->wait_for_query ();
  202. close (spair_fds[0]);
  203. close (stderr_fds[0]);
  204. while (true)
  205. {
  206. int status;
  207. if (waitpid (child_pid, &status, 0) == -1)
  208. {
  209. if (errno != EINTR)
  210. return 0;
  211. }
  212. if (!WIFEXITED (status) || WEXITSTATUS (status) != 0)
  213. return 0;
  214. break;
  215. }
  216. if (!result)
  217. return 0;
  218. return 1;
  219. }
  220. }
  221. static int
  222. do_compile (struct gcc_base_context *s,
  223. const char *filename)
  224. {
  225. base_gdb_plugin<T> *self = get_self (s);
  226. int fds[2];
  227. if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) != 0)
  228. {
  229. self->print ("could not create socketpair\n");
  230. return 0;
  231. }
  232. int stderr_fds[2];
  233. if (pipe (stderr_fds) != 0)
  234. {
  235. self->print ("could not create pipe\n");
  236. close (fds[0]);
  237. close (fds[1]);
  238. return 0;
  239. }
  240. self->args.push_back (std::string ("-fplugin=") + self->plugin_name);
  241. self->args.push_back (std::string ("-fplugin-arg-") + self->plugin_name
  242. + "-fd=" + std::to_string (fds[1]));
  243. self->args.push_back (self->source_file);
  244. self->args.push_back ("-c");
  245. self->args.push_back ("-o");
  246. self->args.push_back (filename);
  247. if (self->verbose)
  248. self->args.push_back ("-v");
  249. self->set_connection (fds[0], stderr_fds[0]);
  250. self->add_callbacks ();
  251. std::vector<char *> argv (self->args.size () + 1);
  252. for (unsigned int i = 0; i < self->args.size (); ++i)
  253. argv[i] = const_cast<char *> (self->args[i].c_str ());
  254. return self->fork_exec (argv.data (), fds, stderr_fds);
  255. }
  256. static int
  257. do_compile_v0 (struct gcc_base_context *s, const char *filename,
  258. int verbose)
  259. {
  260. do_set_verbose (s, verbose);
  261. return do_compile (s, filename);
  262. }
  263. static void
  264. do_destroy (struct gcc_base_context *s)
  265. {
  266. base_gdb_plugin<T> *self = get_self (s);
  267. delete self;
  268. }
  269. };
  270. // Instances of this rpc<> template function are installed into the
  271. // "vtable"s. These functions are parameterized by type and method
  272. // name and forward the call via the connection.
  273. template<typename CTX, typename R, const char *&NAME, typename... Arg>
  274. R rpc (CTX *s, Arg... rest)
  275. {
  276. base_gdb_plugin<CTX> *self = (base_gdb_plugin<CTX> *) s;
  277. R result;
  278. if (!cc1_plugin::call (self->connection.get (), NAME, &result, rest...))
  279. return 0;
  280. return result;
  281. }
  282. }
  283. #endif // CC1_PLUGIN_GDBCTX_HH