1 /* The library used by gdb.
2 Copyright (C) 2014-2017 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 virtual char *find (std::string
&compiler
) const;
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 virtual char *find (std::string
&compiler
) const;
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 virtual void print (const char *buf
)
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 these rpc<> template functions 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
>
241 R
rpc (struct gcc_cp_context
*s
)
243 libcp1
*self
= (libcp1
*) s
;
246 if (!cc1_plugin::call (self
->connection
, NAME
, &result
))
251 template<typename R
, const char *&NAME
, typename A
>
252 R
rpc (struct gcc_cp_context
*s
, A arg
)
254 libcp1
*self
= (libcp1
*) s
;
257 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg
))
262 template<typename R
, const char *&NAME
, typename A1
, typename A2
>
263 R
rpc (struct gcc_cp_context
*s
, A1 arg1
, A2 arg2
)
265 libcp1
*self
= (libcp1
*) s
;
268 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg1
, arg2
))
273 template<typename R
, const char *&NAME
, typename A1
, typename A2
, typename A3
>
274 R
rpc (struct gcc_cp_context
*s
, A1 arg1
, A2 arg2
, A3 arg3
)
276 libcp1
*self
= (libcp1
*) s
;
279 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg1
, arg2
, arg3
))
284 template<typename R
, const char *&NAME
, typename A1
, typename A2
, typename A3
,
286 R
rpc (struct gcc_cp_context
*s
, A1 arg1
, A2 arg2
, A3 arg3
, A4 arg4
)
288 libcp1
*self
= (libcp1
*) s
;
291 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg1
, arg2
, arg3
,
297 template<typename R
, const char *&NAME
, typename A1
, typename A2
, typename A3
,
298 typename A4
, typename A5
>
299 R
rpc (struct gcc_cp_context
*s
, A1 arg1
, A2 arg2
, A3 arg3
, A4 arg4
, A5 arg5
)
301 libcp1
*self
= (libcp1
*) s
;
304 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg1
, arg2
, arg3
,
310 template<typename R
, const char *&NAME
, typename A1
, typename A2
, typename A3
,
311 typename A4
, typename A5
, typename A6
, typename A7
>
312 R
rpc (struct gcc_cp_context
*s
, A1 arg1
, A2 arg2
, A3 arg3
, A4 arg4
, A5 arg5
,
315 libcp1
*self
= (libcp1
*) s
;
318 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg1
, arg2
, arg3
,
319 arg4
, arg5
, arg6
, arg7
))
324 static const struct gcc_cp_fe_vtable cp_vtable
=
329 #define GCC_METHOD0(R, N) \
330 rpc<R, cc1_plugin::cp::N>,
331 #define GCC_METHOD1(R, N, A) \
332 rpc<R, cc1_plugin::cp::N, A>,
333 #define GCC_METHOD2(R, N, A, B) \
334 rpc<R, cc1_plugin::cp::N, A, B>,
335 #define GCC_METHOD3(R, N, A, B, C) \
336 rpc<R, cc1_plugin::cp::N, A, B, C>,
337 #define GCC_METHOD4(R, N, A, B, C, D) \
338 rpc<R, cc1_plugin::cp::N, A, B, C, D>,
339 #define GCC_METHOD5(R, N, A, B, C, D, E) \
340 rpc<R, cc1_plugin::cp::N, A, B, C, D, E>,
341 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
342 rpc<R, cc1_plugin::cp::N, A, B, C, D, E, F, G>,
344 #include "gcc-cp-fe.def"
357 // Construct an appropriate regexp to match the compiler name.
359 make_regexp (const char *triplet_regexp
, const char *compiler
)
361 std::stringstream buf
;
363 buf
<< "^" << triplet_regexp
<< "-";
365 // Quote the compiler name in case it has something funny in it.
366 for (const char *p
= compiler
; *p
; ++p
)
393 libcp1_set_verbose (struct gcc_base_context
*s
, int /* bool */ verbose
)
395 libcp1
*self
= (libcp1
*) s
;
397 self
->verbose
= verbose
!= 0;
401 libcp1::compiler::find (std::string
&compiler ATTRIBUTE_UNUSED
) const
403 return xstrdup (_("Compiler has not been specified"));
407 libcp1::compiler_triplet_regexp::find (std::string
&compiler
) const
409 std::string rx
= make_regexp (triplet_regexp_
.c_str (), CP_COMPILER_NAME
);
411 fprintf (stderr
, _("searching for compiler matching regex %s\n"),
414 int code
= regcomp (&triplet
, rx
.c_str (), REG_EXTENDED
| REG_NOSUB
);
417 size_t len
= regerror (code
, &triplet
, NULL
, 0);
420 regerror (code
, &triplet
, err
, len
);
422 return concat ("Could not compile regexp \"",
429 if (!find_compiler (triplet
, &compiler
))
432 return concat ("Could not find a compiler matching \"",
439 fprintf (stderr
, _("found compiler %s\n"), compiler
.c_str());
444 libcp1::compiler_driver_filename::find (std::string
&compiler
) const
446 // Simulate fnotice by fprintf.
448 fprintf (stderr
, _("using explicit compiler filename %s\n"),
449 driver_filename_
.c_str());
450 compiler
= driver_filename_
;
455 libcp1_set_arguments (struct gcc_base_context
*s
,
456 int argc
, char **argv
)
458 libcp1
*self
= (libcp1
*) s
;
460 std::string compiler
;
461 char *errmsg
= self
->compilerp
->find (compiler
);
465 self
->args
.push_back (compiler
);
467 for (int i
= 0; i
< argc
; ++i
)
468 self
->args
.push_back (argv
[i
]);
474 libcp1_set_triplet_regexp (struct gcc_base_context
*s
,
475 const char *triplet_regexp
)
477 libcp1
*self
= (libcp1
*) s
;
479 delete self
->compilerp
;
480 self
->compilerp
= new libcp1::compiler_triplet_regexp (self
, triplet_regexp
);
485 libcp1_set_driver_filename (struct gcc_base_context
*s
,
486 const char *driver_filename
)
488 libcp1
*self
= (libcp1
*) s
;
490 delete self
->compilerp
;
491 self
->compilerp
= new libcp1::compiler_driver_filename (self
,
497 libcp1_set_arguments_v0 (struct gcc_base_context
*s
,
498 const char *triplet_regexp
,
499 int argc
, char **argv
)
501 char *errmsg
= libcp1_set_triplet_regexp (s
, triplet_regexp
);
505 return libcp1_set_arguments (s
, argc
, argv
);
509 libcp1_set_source_file (struct gcc_base_context
*s
,
512 libcp1
*self
= (libcp1
*) s
;
514 self
->source_file
= file
;
518 libcp1_set_print_callback (struct gcc_base_context
*s
,
519 void (*print_function
) (void *datum
,
520 const char *message
),
523 libcp1
*self
= (libcp1
*) s
;
525 self
->print_function
= print_function
;
526 self
->print_datum
= datum
;
530 fork_exec (libcp1
*self
, char **argv
, int spair_fds
[2], int stderr_fds
[2])
532 pid_t child_pid
= fork ();
536 close (spair_fds
[0]);
537 close (spair_fds
[1]);
538 close (stderr_fds
[0]);
539 close (stderr_fds
[1]);
546 dup2 (stderr_fds
[1], 1);
547 dup2 (stderr_fds
[1], 2);
548 close (stderr_fds
[0]);
549 close (stderr_fds
[1]);
550 close (spair_fds
[0]);
552 execvp (argv
[0], argv
);
558 close (spair_fds
[1]);
559 close (stderr_fds
[1]);
561 cc1_plugin::status result
= cc1_plugin::FAIL
;
562 if (self
->connection
->send ('H')
563 && ::cc1_plugin::marshall (self
->connection
, GCC_CP_FE_VERSION_0
))
564 result
= self
->connection
->wait_for_query ();
566 close (spair_fds
[0]);
567 close (stderr_fds
[0]);
573 if (waitpid (child_pid
, &status
, 0) == -1)
579 if (!WIFEXITED (status
) || WEXITSTATUS (status
) != 0)
591 libcp1_compile (struct gcc_base_context
*s
,
592 const char *filename
)
594 libcp1
*self
= (libcp1
*) s
;
597 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, fds
) != 0)
599 self
->print ("could not create socketpair\n");
604 if (pipe (stderr_fds
) != 0)
606 self
->print ("could not create pipe\n");
612 self
->args
.push_back ("-fplugin=libcp1plugin");
614 if (snprintf (buf
, sizeof (buf
), "-fplugin-arg-libcp1plugin-fd=%d", fds
[1])
615 >= (long) sizeof (buf
))
617 self
->args
.push_back (buf
);
619 self
->args
.push_back (self
->source_file
);
620 self
->args
.push_back ("-c");
621 self
->args
.push_back ("-o");
622 self
->args
.push_back (filename
);
624 self
->args
.push_back ("-v");
626 self
->connection
= new libcp1_connection (fds
[0], stderr_fds
[0], self
);
628 cc1_plugin::callback_ftype
*fun
629 = cc1_plugin::callback
<int,
630 enum gcc_cp_oracle_request
,
632 cp_call_binding_oracle
>;
633 self
->connection
->add_callback ("binding_oracle", fun
);
635 fun
= cc1_plugin::callback
<gcc_address
,
637 cp_call_symbol_address
>;
638 self
->connection
->add_callback ("address_oracle", fun
);
640 fun
= cc1_plugin::callback
<int,
641 cp_call_enter_scope
>;
642 self
->connection
->add_callback ("enter_scope", fun
);
644 fun
= cc1_plugin::callback
<int,
645 cp_call_leave_scope
>;
646 self
->connection
->add_callback ("leave_scope", fun
);
648 char **argv
= new (std::nothrow
) char *[self
->args
.size () + 1];
652 for (unsigned int i
= 0; i
< self
->args
.size (); ++i
)
653 argv
[i
] = const_cast<char *> (self
->args
[i
].c_str ());
654 argv
[self
->args
.size ()] = NULL
;
656 return fork_exec (self
, argv
, fds
, stderr_fds
);
660 libcp1_compile_v0 (struct gcc_base_context
*s
, const char *filename
,
663 libcp1_set_verbose (s
, verbose
);
664 return libcp1_compile (s
, filename
);
668 libcp1_destroy (struct gcc_base_context
*s
)
670 libcp1
*self
= (libcp1
*) s
;
675 static const struct gcc_base_vtable vtable
=
678 libcp1_set_arguments_v0
,
679 libcp1_set_source_file
,
680 libcp1_set_print_callback
,
685 libcp1_set_arguments
,
686 libcp1_set_triplet_regexp
,
687 libcp1_set_driver_filename
,
690 extern "C" gcc_cp_fe_context_function gcc_cp_fe_context
;
693 #pragma GCC visibility push(default)
697 struct gcc_cp_context
*
698 gcc_cp_fe_context (enum gcc_base_api_version base_version
,
699 enum gcc_cp_api_version cp_version
)
701 if ((base_version
!= GCC_FE_VERSION_0
&& base_version
!= GCC_FE_VERSION_1
)
702 || cp_version
!= GCC_CP_FE_VERSION_0
)
705 return new libcp1 (&vtable
, &cp_vtable
);