1 /* Call module for 'compile' command.
3 Copyright (C) 2014-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "compile-object-run.h"
25 #include "compile-internal.h"
26 #include "dummy-frame.h"
31 /* Helper for do_module_cleanup. */
33 struct do_module_cleanup
35 /* Boolean to set true upon a call of do_module_cleanup.
36 The pointer may be NULL. */
39 /* .c file OBJFILE was built from. It needs to be xfree-d. */
42 /* Copy from struct compile_module. */
43 enum compile_i_scope_types scope;
46 /* Copy from struct compile_module. */
47 struct type *out_value_type;
48 CORE_ADDR out_value_addr;
50 /* Copy from struct compile_module. */
51 struct munmap_list *munmap_list_head;
53 /* objfile_name of our objfile. */
54 char objfile_name_string[1];
57 /* Cleanup everything after the inferior function dummy frame gets
60 static dummy_frame_dtor_ftype do_module_cleanup;
62 do_module_cleanup (void *arg, int registers_valid)
64 struct do_module_cleanup *data = (struct do_module_cleanup *) arg;
65 struct objfile *objfile;
67 if (data->executedp != NULL)
71 /* This code cannot be in compile_object_run as OUT_VALUE_TYPE
72 no longer exists there. */
73 if (data->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
74 || data->scope == COMPILE_I_PRINT_VALUE_SCOPE)
76 struct value *addr_value;
77 struct type *ptr_type = lookup_pointer_type (data->out_value_type);
79 addr_value = value_from_pointer (ptr_type, data->out_value_addr);
81 /* SCOPE_DATA would be stale unlesse EXECUTEDP != NULL. */
82 compile_print_value (value_ind (addr_value), data->scope_data);
86 ALL_OBJFILES (objfile)
87 if ((objfile->flags & OBJF_USERLOADED) == 0
88 && (strcmp (objfile_name (objfile), data->objfile_name_string) == 0))
92 /* It may be a bit too pervasive in this dummy_frame dtor callback. */
93 clear_symtab_users (0);
98 /* Delete the .c file. */
99 unlink (data->source_file);
100 xfree (data->source_file);
102 delete data->munmap_list_head;
104 /* Delete the .o file. */
105 unlink (data->objfile_name_string);
109 /* Perform inferior call of MODULE. This function may throw an error.
110 This function may leave files referenced by MODULE on disk until
111 the inferior call dummy frame is discarded. This function may throw errors.
112 Thrown errors and left MODULE files are unrelated events. Caller must no
113 longer touch MODULE's memory after this function has been called. */
116 compile_object_run (struct compile_module *module)
118 struct value *func_val;
119 struct do_module_cleanup *data;
120 const char *objfile_name_s = objfile_name (module->objfile);
121 int dtor_found, executed = 0;
122 struct symbol *func_sym = module->func_sym;
123 CORE_ADDR regs_addr = module->regs_addr;
124 struct objfile *objfile = module->objfile;
126 data = (struct do_module_cleanup *) xmalloc (sizeof (*data)
127 + strlen (objfile_name_s));
128 data->executedp = &executed;
129 data->source_file = xstrdup (module->source_file);
130 strcpy (data->objfile_name_string, objfile_name_s);
131 data->scope = module->scope;
132 data->scope_data = module->scope_data;
133 data->out_value_type = module->out_value_type;
134 data->out_value_addr = module->out_value_addr;
135 data->munmap_list_head = module->munmap_list_head;
137 xfree (module->source_file);
143 struct type *func_type = SYMBOL_TYPE (func_sym);
146 struct value **vargs;
148 /* OBJFILE may disappear while FUNC_TYPE still will be in use. */
149 copied_types = create_copied_types_hash (objfile);
150 func_type = copy_type_recursive (objfile, func_type, copied_types);
151 htab_delete (copied_types);
153 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
154 func_val = value_from_pointer (lookup_pointer_type (func_type),
155 BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func_sym)));
157 vargs = XALLOCAVEC (struct value *, TYPE_NFIELDS (func_type));
158 if (TYPE_NFIELDS (func_type) >= 1)
160 gdb_assert (regs_addr != 0);
161 vargs[current_arg] = value_from_pointer
162 (TYPE_FIELD_TYPE (func_type, current_arg), regs_addr);
165 if (TYPE_NFIELDS (func_type) >= 2)
167 gdb_assert (data->out_value_addr != 0);
168 vargs[current_arg] = value_from_pointer
169 (TYPE_FIELD_TYPE (func_type, current_arg), data->out_value_addr);
172 gdb_assert (current_arg == TYPE_NFIELDS (func_type));
173 auto args = gdb::make_array_view (vargs, TYPE_NFIELDS (func_type));
174 call_function_by_hand_dummy (func_val, NULL, args,
175 do_module_cleanup, data);
177 CATCH (ex, RETURN_MASK_ERROR)
179 /* In the case of DTOR_FOUND or in the case of EXECUTED nothing
181 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
183 data->executedp = NULL;
184 gdb_assert (!(dtor_found && executed));
185 if (!dtor_found && !executed)
186 do_module_cleanup (data, 0);
187 throw_exception (ex);
191 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
192 gdb_assert (!dtor_found && executed);