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>
32 #include "marshall-cp.hh"
34 #include "connection.hh"
36 #include "callbacks.hh"
37 #include "libiberty.h"
39 #include "findcomp.hh"
40 #include "compiler-name.hh"
45 class libcp1_connection
;
47 // The C compiler context that we hand back to our caller.
48 struct libcp1
: public gcc_cp_context
50 libcp1 (const gcc_base_vtable
*, const gcc_cp_fe_vtable
*);
53 // A convenience function to print something.
54 void print (const char *str
)
56 this->print_function (this->print_datum
, str
);
59 libcp1_connection
*connection
;
61 gcc_cp_oracle_function
*binding_oracle
;
62 gcc_cp_symbol_address_function
*address_oracle
;
63 gcc_cp_enter_leave_user_expr_scope_function
*enter_scope
;
64 gcc_cp_enter_leave_user_expr_scope_function
*leave_scope
;
67 void (*print_function
) (void *datum
, const char *message
);
70 std::vector
<std::string
> args
;
71 std::string source_file
;
73 /* Non-zero as an equivalent to gcc driver option "-v". */
76 /* Compiler to set by set_triplet_regexp or set_driver_filename. */
82 compiler (libcp1
*self
) : self_ (self
)
85 virtual char *find (std::string
&compiler
) const;
91 /* Compiler to set by set_triplet_regexp. */
92 class compiler_triplet_regexp
: public compiler
95 std::string triplet_regexp_
;
97 char *find (std::string
&compiler
) const override
;
98 compiler_triplet_regexp (libcp1
*self
, std::string triplet_regexp
)
99 : compiler (self
), triplet_regexp_ (triplet_regexp
)
102 virtual ~compiler_triplet_regexp ()
107 /* Compiler to set by set_driver_filename. */
108 class compiler_driver_filename
: public compiler
111 std::string driver_filename_
;
113 char *find (std::string
&compiler
) const override
;
114 compiler_driver_filename (libcp1
*self
, std::string driver_filename
)
115 : compiler (self
), driver_filename_ (driver_filename
)
118 virtual ~compiler_driver_filename ()
124 // A local subclass of connection that holds a back-pointer to the
125 // gcc_c_context object that we provide to our caller.
126 class libcp1_connection
: public cc1_plugin::connection
130 libcp1_connection (int fd
, int aux_fd
, libcp1
*b
)
131 : connection (fd
, aux_fd
),
136 void print (const char *buf
) override
138 back_ptr
->print (buf
);
144 libcp1::libcp1 (const gcc_base_vtable
*v
,
145 const gcc_cp_fe_vtable
*cv
)
147 binding_oracle (NULL
),
148 address_oracle (NULL
),
150 print_function (NULL
),
155 compilerp (new libcp1::compiler (this))
169 // Enclose these functions in an anonymous namespace because they
170 // shouldn't be exported, but they can't be static because they're
171 // used as template arguments.
173 // This is a wrapper function that is called by the RPC system and
174 // that then forwards the call to the library user. Note that the
175 // return value is not used; the type cannot be 'void' due to
176 // limitations in our simple RPC.
178 cp_call_binding_oracle (cc1_plugin::connection
*conn
,
179 enum gcc_cp_oracle_request request
,
180 const char *identifier
)
182 libcp1
*self
= ((libcp1_connection
*) conn
)->back_ptr
;
184 self
->binding_oracle (self
->oracle_datum
, self
, request
, identifier
);
188 // This is a wrapper function that is called by the RPC system and
189 // that then forwards the call to the library user.
191 cp_call_symbol_address (cc1_plugin::connection
*conn
, const char *identifier
)
193 libcp1
*self
= ((libcp1_connection
*) conn
)->back_ptr
;
195 return self
->address_oracle (self
->oracle_datum
, self
, identifier
);
199 cp_call_enter_scope (cc1_plugin::connection
*conn
)
201 libcp1
*self
= ((libcp1_connection
*) conn
)->back_ptr
;
203 self
->enter_scope (self
->oracle_datum
, self
);
208 cp_call_leave_scope (cc1_plugin::connection
*conn
)
210 libcp1
*self
= ((libcp1_connection
*) conn
)->back_ptr
;
212 self
->leave_scope (self
->oracle_datum
, self
);
215 } /* anonymous namespace */
220 set_callbacks (struct gcc_cp_context
*s
,
221 gcc_cp_oracle_function
*binding_oracle
,
222 gcc_cp_symbol_address_function
*address_oracle
,
223 gcc_cp_enter_leave_user_expr_scope_function
*enter_scope
,
224 gcc_cp_enter_leave_user_expr_scope_function
*leave_scope
,
227 libcp1
*self
= (libcp1
*) s
;
229 self
->binding_oracle
= binding_oracle
;
230 self
->address_oracle
= address_oracle
;
231 self
->enter_scope
= enter_scope
;
232 self
->leave_scope
= leave_scope
;
233 self
->oracle_datum
= datum
;
236 // Instances of this rpc<> template function are installed into the
237 // "cp_vtable". These functions are parameterized by type and method
238 // name and forward the call via the connection.
240 template<typename R
, const char *&NAME
, typename
... Arg
>
241 R
rpc (struct gcc_cp_context
*s
, Arg
... rest
)
243 libcp1
*self
= (libcp1
*) s
;
246 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, rest
...))
251 static const struct gcc_cp_fe_vtable cp_vtable
=
256 #define GCC_METHOD0(R, N) \
257 rpc<R, cc1_plugin::cp::N>,
258 #define GCC_METHOD1(R, N, A) \
259 rpc<R, cc1_plugin::cp::N, A>,
260 #define GCC_METHOD2(R, N, A, B) \
261 rpc<R, cc1_plugin::cp::N, A, B>,
262 #define GCC_METHOD3(R, N, A, B, C) \
263 rpc<R, cc1_plugin::cp::N, A, B, C>,
264 #define GCC_METHOD4(R, N, A, B, C, D) \
265 rpc<R, cc1_plugin::cp::N, A, B, C, D>,
266 #define GCC_METHOD5(R, N, A, B, C, D, E) \
267 rpc<R, cc1_plugin::cp::N, A, B, C, D, E>,
268 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
269 rpc<R, cc1_plugin::cp::N, A, B, C, D, E, F, G>,
271 #include "gcc-cp-fe.def"
284 // Construct an appropriate regexp to match the compiler name.
286 make_regexp (const char *triplet_regexp
, const char *compiler
)
288 std::stringstream buf
;
290 buf
<< "^" << triplet_regexp
<< "-";
292 // Quote the compiler name in case it has something funny in it.
293 for (const char *p
= compiler
; *p
; ++p
)
320 libcp1_set_verbose (struct gcc_base_context
*s
, int /* bool */ verbose
)
322 libcp1
*self
= (libcp1
*) s
;
324 self
->verbose
= verbose
!= 0;
328 libcp1::compiler::find (std::string
&compiler ATTRIBUTE_UNUSED
) const
330 return xstrdup (_("Compiler has not been specified"));
334 libcp1::compiler_triplet_regexp::find (std::string
&compiler
) const
336 std::string rx
= make_regexp (triplet_regexp_
.c_str (), CP_COMPILER_NAME
);
338 fprintf (stderr
, _("searching for compiler matching regex %s\n"),
341 int code
= regcomp (&triplet
, rx
.c_str (), REG_EXTENDED
| REG_NOSUB
);
344 size_t len
= regerror (code
, &triplet
, NULL
, 0);
347 regerror (code
, &triplet
, err
, len
);
349 return concat ("Could not compile regexp \"",
356 if (!find_compiler (triplet
, &compiler
))
359 return concat ("Could not find a compiler matching \"",
366 fprintf (stderr
, _("found compiler %s\n"), compiler
.c_str());
371 libcp1::compiler_driver_filename::find (std::string
&compiler
) const
373 // Simulate fnotice by fprintf.
375 fprintf (stderr
, _("using explicit compiler filename %s\n"),
376 driver_filename_
.c_str());
377 compiler
= driver_filename_
;
382 libcp1_set_arguments (struct gcc_base_context
*s
,
383 int argc
, char **argv
)
385 libcp1
*self
= (libcp1
*) s
;
387 std::string compiler
;
388 char *errmsg
= self
->compilerp
->find (compiler
);
392 self
->args
.push_back (compiler
);
394 for (int i
= 0; i
< argc
; ++i
)
395 self
->args
.push_back (argv
[i
]);
401 libcp1_set_triplet_regexp (struct gcc_base_context
*s
,
402 const char *triplet_regexp
)
404 libcp1
*self
= (libcp1
*) s
;
406 delete self
->compilerp
;
407 self
->compilerp
= new libcp1::compiler_triplet_regexp (self
, triplet_regexp
);
412 libcp1_set_driver_filename (struct gcc_base_context
*s
,
413 const char *driver_filename
)
415 libcp1
*self
= (libcp1
*) s
;
417 delete self
->compilerp
;
418 self
->compilerp
= new libcp1::compiler_driver_filename (self
,
424 libcp1_set_arguments_v0 (struct gcc_base_context
*s
,
425 const char *triplet_regexp
,
426 int argc
, char **argv
)
428 char *errmsg
= libcp1_set_triplet_regexp (s
, triplet_regexp
);
432 return libcp1_set_arguments (s
, argc
, argv
);
436 libcp1_set_source_file (struct gcc_base_context
*s
,
439 libcp1
*self
= (libcp1
*) s
;
441 self
->source_file
= file
;
445 libcp1_set_print_callback (struct gcc_base_context
*s
,
446 void (*print_function
) (void *datum
,
447 const char *message
),
450 libcp1
*self
= (libcp1
*) s
;
452 self
->print_function
= print_function
;
453 self
->print_datum
= datum
;
457 fork_exec (libcp1
*self
, char **argv
, int spair_fds
[2], int stderr_fds
[2])
459 pid_t child_pid
= fork ();
463 close (spair_fds
[0]);
464 close (spair_fds
[1]);
465 close (stderr_fds
[0]);
466 close (stderr_fds
[1]);
473 dup2 (stderr_fds
[1], 1);
474 dup2 (stderr_fds
[1], 2);
475 close (stderr_fds
[0]);
476 close (stderr_fds
[1]);
477 close (spair_fds
[0]);
479 execvp (argv
[0], argv
);
485 close (spair_fds
[1]);
486 close (stderr_fds
[1]);
488 cc1_plugin::status result
= cc1_plugin::FAIL
;
489 if (self
->connection
->send ('H')
490 && ::cc1_plugin::marshall (self
->connection
, GCC_CP_FE_VERSION_0
))
491 result
= self
->connection
->wait_for_query ();
493 close (spair_fds
[0]);
494 close (stderr_fds
[0]);
500 if (waitpid (child_pid
, &status
, 0) == -1)
506 if (!WIFEXITED (status
) || WEXITSTATUS (status
) != 0)
518 libcp1_compile (struct gcc_base_context
*s
,
519 const char *filename
)
521 libcp1
*self
= (libcp1
*) s
;
524 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, fds
) != 0)
526 self
->print ("could not create socketpair\n");
531 if (pipe (stderr_fds
) != 0)
533 self
->print ("could not create pipe\n");
539 self
->args
.push_back ("-fplugin=libcp1plugin");
541 if (snprintf (buf
, sizeof (buf
), "-fplugin-arg-libcp1plugin-fd=%d", fds
[1])
542 >= (long) sizeof (buf
))
544 self
->args
.push_back (buf
);
546 self
->args
.push_back (self
->source_file
);
547 self
->args
.push_back ("-c");
548 self
->args
.push_back ("-o");
549 self
->args
.push_back (filename
);
551 self
->args
.push_back ("-v");
553 self
->connection
= new libcp1_connection (fds
[0], stderr_fds
[0], self
);
555 cc1_plugin::callback_ftype
*fun
556 = cc1_plugin::callback
<int,
557 enum gcc_cp_oracle_request
,
559 cp_call_binding_oracle
>;
560 self
->connection
->add_callback ("binding_oracle", fun
);
562 fun
= cc1_plugin::callback
<gcc_address
,
564 cp_call_symbol_address
>;
565 self
->connection
->add_callback ("address_oracle", fun
);
567 fun
= cc1_plugin::callback
<int,
568 cp_call_enter_scope
>;
569 self
->connection
->add_callback ("enter_scope", fun
);
571 fun
= cc1_plugin::callback
<int,
572 cp_call_leave_scope
>;
573 self
->connection
->add_callback ("leave_scope", fun
);
575 char **argv
= new (std::nothrow
) char *[self
->args
.size () + 1];
579 for (unsigned int i
= 0; i
< self
->args
.size (); ++i
)
580 argv
[i
] = const_cast<char *> (self
->args
[i
].c_str ());
581 argv
[self
->args
.size ()] = NULL
;
583 return fork_exec (self
, argv
, fds
, stderr_fds
);
587 libcp1_compile_v0 (struct gcc_base_context
*s
, const char *filename
,
590 libcp1_set_verbose (s
, verbose
);
591 return libcp1_compile (s
, filename
);
595 libcp1_destroy (struct gcc_base_context
*s
)
597 libcp1
*self
= (libcp1
*) s
;
602 static const struct gcc_base_vtable vtable
=
605 libcp1_set_arguments_v0
,
606 libcp1_set_source_file
,
607 libcp1_set_print_callback
,
612 libcp1_set_arguments
,
613 libcp1_set_triplet_regexp
,
614 libcp1_set_driver_filename
,
617 extern "C" gcc_cp_fe_context_function gcc_cp_fe_context
;
620 #pragma GCC visibility push(default)
624 struct gcc_cp_context
*
625 gcc_cp_fe_context (enum gcc_base_api_version base_version
,
626 enum gcc_cp_api_version cp_version
)
628 if ((base_version
!= GCC_FE_VERSION_0
&& base_version
!= GCC_FE_VERSION_1
)
629 || cp_version
!= GCC_CP_FE_VERSION_0
)
632 return new libcp1 (&vtable
, &cp_vtable
);