1 /* The library used by gdb.
2 Copyright (C) 2014-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include <cc1plugin-config.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
31 #include "marshall-cp.hh"
33 #include "connection.hh"
35 #include "callbacks.hh"
36 #include "libiberty.h"
37 #include "compiler-name.hh"
38 #include "compiler.hh"
41 // The C compiler context that we hand back to our caller.
42 struct libcp1 : public cc1_plugin::base_gdb_plugin<gcc_cp_context>
44 libcp1 (const gcc_base_vtable *, const gcc_cp_fe_vtable *);
46 gcc_cp_oracle_function *binding_oracle = nullptr;
47 gcc_cp_symbol_address_function *address_oracle = nullptr;
48 gcc_cp_enter_leave_user_expr_scope_function *enter_scope = nullptr;
49 gcc_cp_enter_leave_user_expr_scope_function *leave_scope = nullptr;
50 void *oracle_datum = nullptr;
53 libcp1::libcp1 (const gcc_base_vtable *v, const gcc_cp_fe_vtable *cv)
54 : cc1_plugin::base_gdb_plugin<gcc_cp_context> (v)
61 // Enclose these functions in an anonymous namespace because they
62 // shouldn't be exported, but they can't be static because they're
63 // used as template arguments.
65 // This is a wrapper function that is called by the RPC system and
66 // that then forwards the call to the library user. Note that the
67 // return value is not used; the type cannot be 'void' due to
68 // limitations in our simple RPC.
70 cp_call_binding_oracle (cc1_plugin::connection *conn,
71 enum gcc_cp_oracle_request request,
72 const char *identifier)
74 libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
76 self->binding_oracle (self->oracle_datum, self, request, identifier);
80 // This is a wrapper function that is called by the RPC system and
81 // that then forwards the call to the library user.
83 cp_call_symbol_address (cc1_plugin::connection *conn, const char *identifier)
85 libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
87 return self->address_oracle (self->oracle_datum, self, identifier);
91 cp_call_enter_scope (cc1_plugin::connection *conn)
93 libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
95 self->enter_scope (self->oracle_datum, self);
100 cp_call_leave_scope (cc1_plugin::connection *conn)
102 libcp1 *self = (libcp1 *) (((libcp1::local_connection *) conn)->back_ptr);
104 self->leave_scope (self->oracle_datum, self);
107 } /* anonymous namespace */
112 set_callbacks (struct gcc_cp_context *s,
113 gcc_cp_oracle_function *binding_oracle,
114 gcc_cp_symbol_address_function *address_oracle,
115 gcc_cp_enter_leave_user_expr_scope_function *enter_scope,
116 gcc_cp_enter_leave_user_expr_scope_function *leave_scope,
119 libcp1 *self = (libcp1 *) s;
121 self->binding_oracle = binding_oracle;
122 self->address_oracle = address_oracle;
123 self->enter_scope = enter_scope;
124 self->leave_scope = leave_scope;
125 self->oracle_datum = datum;
128 static const struct gcc_cp_fe_vtable cp_vtable =
133 #define GCC_METHOD0(R, N) \
134 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N>,
135 #define GCC_METHOD1(R, N, A) \
136 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A>,
137 #define GCC_METHOD2(R, N, A, B) \
138 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B>,
139 #define GCC_METHOD3(R, N, A, B, C) \
140 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C>,
141 #define GCC_METHOD4(R, N, A, B, C, D) \
142 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D>,
143 #define GCC_METHOD5(R, N, A, B, C, D, E) \
144 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D, E>,
145 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
146 cc1_plugin::rpc<gcc_cp_context, R, cc1_plugin::cp::N, A, B, C, D, E, F, G>,
148 #include "gcc-cp-fe.def"
162 libcp1_set_verbose (struct gcc_base_context *s, int /* bool */ verbose)
164 libcp1 *self = (libcp1 *) s;
166 self->set_verbose (verbose != 0);
170 libcp1_set_arguments (struct gcc_base_context *s,
171 int argc, char **argv)
173 libcp1 *self = (libcp1 *) s;
175 std::string compiler;
176 char *errmsg = self->compilerp->find (CP_COMPILER_NAME, compiler);
180 self->args.push_back (compiler);
182 for (int i = 0; i < argc; ++i)
183 self->args.push_back (argv[i]);
189 libcp1_set_triplet_regexp (struct gcc_base_context *s,
190 const char *triplet_regexp)
192 libcp1 *self = (libcp1 *) s;
194 self->compilerp.reset
195 (new cc1_plugin::compiler_triplet_regexp (self->verbose,
201 libcp1_set_driver_filename (struct gcc_base_context *s,
202 const char *driver_filename)
204 libcp1 *self = (libcp1 *) s;
206 self->compilerp.reset
207 (new cc1_plugin::compiler_driver_filename (self->verbose,
213 libcp1_set_arguments_v0 (struct gcc_base_context *s,
214 const char *triplet_regexp,
215 int argc, char **argv)
217 char *errmsg = libcp1_set_triplet_regexp (s, triplet_regexp);
221 return libcp1_set_arguments (s, argc, argv);
225 libcp1_set_source_file (struct gcc_base_context *s,
228 libcp1 *self = (libcp1 *) s;
230 self->source_file = file;
234 libcp1_set_print_callback (struct gcc_base_context *s,
235 void (*print_function) (void *datum,
236 const char *message),
239 libcp1 *self = (libcp1 *) s;
241 self->print_function = print_function;
242 self->print_datum = datum;
246 fork_exec (libcp1 *self, char **argv, int spair_fds[2], int stderr_fds[2])
248 pid_t child_pid = fork ();
252 close (spair_fds[0]);
253 close (spair_fds[1]);
254 close (stderr_fds[0]);
255 close (stderr_fds[1]);
262 dup2 (stderr_fds[1], 1);
263 dup2 (stderr_fds[1], 2);
264 close (stderr_fds[0]);
265 close (stderr_fds[1]);
266 close (spair_fds[0]);
268 execvp (argv[0], argv);
274 close (spair_fds[1]);
275 close (stderr_fds[1]);
277 cc1_plugin::status result = cc1_plugin::FAIL;
278 if (self->connection->send ('H')
279 && ::cc1_plugin::marshall (self->connection.get (),
280 GCC_CP_FE_VERSION_0))
281 result = self->connection->wait_for_query ();
283 close (spair_fds[0]);
284 close (stderr_fds[0]);
290 if (waitpid (child_pid, &status, 0) == -1)
296 if (!WIFEXITED (status) || WEXITSTATUS (status) != 0)
308 libcp1_compile (struct gcc_base_context *s,
309 const char *filename)
311 libcp1 *self = (libcp1 *) s;
314 if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) != 0)
316 self->print ("could not create socketpair\n");
321 if (pipe (stderr_fds) != 0)
323 self->print ("could not create pipe\n");
329 self->args.push_back ("-fplugin=libcp1plugin");
331 if (snprintf (buf, sizeof (buf), "-fplugin-arg-libcp1plugin-fd=%d", fds[1])
332 >= (long) sizeof (buf))
334 self->args.push_back (buf);
336 self->args.push_back (self->source_file);
337 self->args.push_back ("-c");
338 self->args.push_back ("-o");
339 self->args.push_back (filename);
341 self->args.push_back ("-v");
343 self->set_connection (fds[0], stderr_fds[0]);
345 cc1_plugin::callback_ftype *fun
346 = cc1_plugin::callback<int,
347 enum gcc_cp_oracle_request,
349 cp_call_binding_oracle>;
350 self->connection->add_callback ("binding_oracle", fun);
352 fun = cc1_plugin::callback<gcc_address,
354 cp_call_symbol_address>;
355 self->connection->add_callback ("address_oracle", fun);
357 fun = cc1_plugin::callback<int,
358 cp_call_enter_scope>;
359 self->connection->add_callback ("enter_scope", fun);
361 fun = cc1_plugin::callback<int,
362 cp_call_leave_scope>;
363 self->connection->add_callback ("leave_scope", fun);
365 char **argv = new (std::nothrow) char *[self->args.size () + 1];
369 for (unsigned int i = 0; i < self->args.size (); ++i)
370 argv[i] = const_cast<char *> (self->args[i].c_str ());
371 argv[self->args.size ()] = NULL;
373 return fork_exec (self, argv, fds, stderr_fds);
377 libcp1_compile_v0 (struct gcc_base_context *s, const char *filename,
380 libcp1_set_verbose (s, verbose);
381 return libcp1_compile (s, filename);
385 libcp1_destroy (struct gcc_base_context *s)
387 libcp1 *self = (libcp1 *) s;
392 static const struct gcc_base_vtable vtable =
395 libcp1_set_arguments_v0,
396 libcp1_set_source_file,
397 libcp1_set_print_callback,
402 libcp1_set_arguments,
403 libcp1_set_triplet_regexp,
404 libcp1_set_driver_filename,
407 extern "C" gcc_cp_fe_context_function gcc_cp_fe_context;
410 #pragma GCC visibility push(default)
414 struct gcc_cp_context *
415 gcc_cp_fe_context (enum gcc_base_api_version base_version,
416 enum gcc_cp_api_version cp_version)
418 if ((base_version != GCC_FE_VERSION_0 && base_version != GCC_FE_VERSION_1)
419 || cp_version != GCC_CP_FE_VERSION_0)
422 return new libcp1 (&vtable, &cp_vtable);