GDB (xrefs)
Loading...
Searching...
No Matches
compile-object-run.c
Go to the documentation of this file.
1/* Call module for 'compile' command.
2
3 Copyright (C) 2014-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "compile-object-run.h"
22#include "value.h"
23#include "infcall.h"
24#include "objfiles.h"
25#include "compile-internal.h"
26#include "dummy-frame.h"
27#include "block.h"
28#include "valprint.h"
29#include "compile.h"
30
31/* Helper for do_module_cleanup. */
32
34{
36 : executedp (ptr),
37 module (std::move (mod))
38 {
39 }
40
42
43 /* Boolean to set true upon a call of do_module_cleanup.
44 The pointer may be NULL. */
46
47 /* The compile module. */
49};
50
51/* Cleanup everything after the inferior function dummy frame gets
52 discarded. */
53
55static void
56do_module_cleanup (void *arg, int registers_valid)
57{
58 struct do_module_cleanup *data = (struct do_module_cleanup *) arg;
59
60 if (data->executedp != NULL)
61 {
62 *data->executedp = 1;
63
64 /* This code cannot be in compile_object_run as OUT_VALUE_TYPE
65 no longer exists there. */
66 if (data->module->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
67 || data->module->scope == COMPILE_I_PRINT_VALUE_SCOPE)
68 {
69 struct value *addr_value;
70 struct type *ptr_type
71 = lookup_pointer_type (data->module->out_value_type);
72
73 addr_value = value_from_pointer (ptr_type,
74 data->module->out_value_addr);
75
76 /* SCOPE_DATA would be stale unless EXECUTEDP != NULL. */
77 compile_print_value (value_ind (addr_value),
78 data->module->scope_data);
79 }
80 }
81
82 objfile *objfile = data->module->objfile;
83 gdb_assert (objfile != nullptr);
84
85 /* We have to make a copy of the name so that we can unlink the
86 underlying file -- removing the objfile will cause the name to be
87 freed, so we can't simply keep a reference to it. */
88 std::string objfile_name_s = objfile_name (objfile);
89
90 objfile->unlink ();
91
92 /* It may be a bit too pervasive in this dummy_frame dtor callback. */
94
95 /* Delete the .c file. */
96 unlink (data->module->source_file.c_str ());
97
98 /* Delete the .o file. */
99 unlink (objfile_name_s.c_str ());
100
101 delete data;
102}
103
104/* Create a copy of FUNC_TYPE that is independent of OBJFILE. */
105
106static type *
108{
109 htab_up copied_types = create_copied_types_hash ();
110 func_type = copy_type_recursive (func_type, copied_types.get ());
111 return func_type;
112}
113
114/* Perform inferior call of MODULE. This function may throw an error.
115 This function may leave files referenced by MODULE on disk until
116 the inferior call dummy frame is discarded. This function may throw errors.
117 Thrown errors and left MODULE files are unrelated events. Caller must no
118 longer touch MODULE's memory after this function has been called. */
119
120void
122{
123 struct value *func_val;
124 struct do_module_cleanup *data;
125 int dtor_found, executed = 0;
126 struct symbol *func_sym = module->func_sym;
127 CORE_ADDR regs_addr = module->regs_addr;
128 struct objfile *objfile = module->objfile;
129
130 data = new struct do_module_cleanup (&executed, std::move (module));
131
132 try
133 {
134 struct type *func_type = func_sym->type ();
135 int current_arg = 0;
136 struct value **vargs;
137
138 /* OBJFILE may disappear while FUNC_TYPE is still in use as a
139 result of the call to DO_MODULE_CLEANUP below, so we need a copy
140 that does not depend on the objfile in anyway. */
142
143 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
145 func_sym->value_block ()->entry_pc ());
146
147 vargs = XALLOCAVEC (struct value *, func_type->num_fields ());
148 if (func_type->num_fields () >= 1)
149 {
150 gdb_assert (regs_addr != 0);
151 vargs[current_arg] = value_from_pointer
152 (func_type->field (current_arg).type (), regs_addr);
153 ++current_arg;
154 }
155 if (func_type->num_fields () >= 2)
156 {
157 gdb_assert (data->module->out_value_addr != 0);
158 vargs[current_arg] = value_from_pointer
159 (func_type->field (current_arg).type (),
160 data->module->out_value_addr);
161 ++current_arg;
162 }
163 gdb_assert (current_arg == func_type->num_fields ());
164 auto args = gdb::make_array_view (vargs, func_type->num_fields ());
165 call_function_by_hand_dummy (func_val, NULL, args,
166 do_module_cleanup, data);
167 }
168 catch (const gdb_exception_error &ex)
169 {
170 /* In the case of DTOR_FOUND or in the case of EXECUTED nothing
171 needs to be done. */
172 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
173 if (!executed)
174 data->executedp = NULL;
175 gdb_assert (!(dtor_found && executed));
176 if (!dtor_found && !executed)
177 do_module_cleanup (data, 0);
178 throw;
179 }
180
181 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
182 gdb_assert (!dtor_found && executed);
183}
std::unique_ptr< compile_module > compile_module_up
static type * create_copied_type_recursive(objfile *objfile, type *func_type)
void compile_object_run(compile_module_up &&module)
void compile_print_value(struct value *val, void *data_voidp)
Definition: compile.c:373
@ COMPILE_I_PRINT_ADDRESS_SCOPE
Definition: defs.h:91
@ COMPILE_I_PRINT_VALUE_SCOPE
Definition: defs.h:92
int find_dummy_frame_dtor(dummy_frame_dtor_ftype *dtor, void *dtor_data)
Definition: dummy-frame.c:255
void() dummy_frame_dtor_ftype(void *data, int registers_valid)
Definition: dummy-frame.h:61
struct type * copy_type_recursive(struct type *type, htab_t copied_types)
Definition: gdbtypes.c:5634
struct type * lookup_pointer_type(struct type *type)
Definition: gdbtypes.c:402
htab_up create_copied_types_hash()
Definition: gdbtypes.c:5596
struct value * call_function_by_hand_dummy(struct value *function, type *default_return_type, gdb::array_view< value * > args, dummy_frame_dtor_ftype *dummy_dtor, void *dummy_dtor_data)
Definition: infcall.c:808
Definition: aarch64.h:50
const char * objfile_name(const struct objfile *objfile)
Definition: objfiles.c:1308
CORE_ADDR entry_pc() const
Definition: block.h:199
compile_module_up module
DISABLE_COPY_AND_ASSIGN(do_module_cleanup)
do_module_cleanup(int *ptr, compile_module_up &&mod)
objfile(gdb_bfd_ref_ptr, const char *, objfile_flags)
Definition: objfiles.c:315
void unlink()
Definition: objfiles.c:470
const block * value_block() const
Definition: symtab.h:1348
struct type * type() const
Definition: symtab.h:1285
Definition: gdbtypes.h:922
Definition: value.c:181
void clear_symtab_users(symfile_add_flags add_flags)
Definition: symfile.c:2862
struct value * value_ind(struct value *arg1)
Definition: valops.c:1623
struct value * value_from_pointer(struct type *type, CORE_ADDR addr)
Definition: value.c:3651