GDB (xrefs)

compile-object-run.c
Go to the documentation of this file.
1 /* Call module for 'compile' command.
2
3 Copyright (C) 2014-2018 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
33 struct do_module_cleanup
34 {
35 /* Boolean to set true upon a call of do_module_cleanup.
36 The pointer may be NULL. */
37 int *executedp;
38
39 /* .c file OBJFILE was built from. It needs to be xfree-d. */
40 char *source_file;
41
42 /* Copy from struct compile_module. */
43 enum compile_i_scope_types scope;
44 void *scope_data;
45
46 /* Copy from struct compile_module. */
47 struct type *out_value_type;
48 CORE_ADDR out_value_addr;
49
50 /* Copy from struct compile_module. */
51 struct munmap_list *munmap_list_head;
52
53 /* objfile_name of our objfile. */
54 char objfile_name_string[1];
55 };
56
57 /* Cleanup everything after the inferior function dummy frame gets
58 discarded. */
59
60 static dummy_frame_dtor_ftype do_module_cleanup;
61 static void
62 do_module_cleanup (void *arg, int registers_valid)
63 {
64 struct do_module_cleanup *data = (struct do_module_cleanup *) arg;
65 struct objfile *objfile;
66
67 if (data->executedp != NULL)
68 {
69 *data->executedp = 1;
70
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)
75 {
76 struct value *addr_value;
77 struct type *ptr_type = lookup_pointer_type (data->out_value_type);
78
79 addr_value = value_from_pointer (ptr_type, data->out_value_addr);
80
81 /* SCOPE_DATA would be stale unlesse EXECUTEDP != NULL. */
82 compile_print_value (value_ind (addr_value), data->scope_data);
83 }
84 }
85
86 ALL_OBJFILES (objfile)
87 if ((objfile->flags & OBJF_USERLOADED) == 0
88 && (strcmp (objfile_name (objfile), data->objfile_name_string) == 0))
89 {
90 delete objfile;
91
92 /* It may be a bit too pervasive in this dummy_frame dtor callback. */
93 clear_symtab_users (0);
94
95 break;
96 }
97
98 /* Delete the .c file. */
99 unlink (data->source_file);
100 xfree (data->source_file);
101
102 munmap_list_free (data->munmap_list_head);
103
104 /* Delete the .o file. */
105 unlink (data->objfile_name_string);
106 xfree (data);
107 }
108
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. */
114
115 void
116 compile_object_run (struct compile_module *module)
117 {
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;
125
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;
136
137 xfree (module->source_file);
138 xfree (module);
139 module = NULL;
140
141 TRY
142 {
143 struct type *func_type = SYMBOL_TYPE (func_sym);
144 htab_t copied_types;
145 int current_arg = 0;
146 struct value **vargs;
147
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);
152
153 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
154 func_val = value_from_pointer (lookup_pointer_type (func_type),
155 BLOCK_START (SYMBOL_BLOCK_VALUE (func_sym)));
156
157 vargs = XALLOCAVEC (struct value *, TYPE_NFIELDS (func_type));
158 if (TYPE_NFIELDS (func_type) >= 1)
159 {
160 gdb_assert (regs_addr != 0);
161 vargs[current_arg] = value_from_pointer
162 (TYPE_FIELD_TYPE (func_type, current_arg), regs_addr);
163 ++current_arg;
164 }
165 if (TYPE_NFIELDS (func_type) >= 2)
166 {
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);
170 ++current_arg;
171 }
172 gdb_assert (current_arg == TYPE_NFIELDS (func_type));
173 call_function_by_hand_dummy (func_val,
174 NULL, TYPE_NFIELDS (func_type), vargs,
175 do_module_cleanup, data);
176 }
177 CATCH (ex, RETURN_MASK_ERROR)
178 {
179 /* In the case of DTOR_FOUND or in the case of EXECUTED nothing
180 needs to be done. */
181 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
182 if (!executed)
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);
188 }
189 END_CATCH
190
191 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
192 gdb_assert (!dtor_found && executed);
193 }
dummy_frame_dtor_ftype
void() dummy_frame_dtor_ftype(void *data, int registers_valid)
Definition: dummy-frame.h:59
objfile::flags
objfile_flags flags
Definition: objfiles.h:311
call_function_by_hand_dummy
struct value * call_function_by_hand_dummy(struct value *function, type *default_return_type, int nargs, struct value **args, dummy_frame_dtor_ftype *dummy_dtor, void *dummy_dtor_data)
Definition: infcall.c:717
do_module_cleanup
Definition: compile-object-run.c:33
infcall.h
CORE_ADDR
bfd_vma CORE_ADDR
Definition: common-types.h:41
munmap_list
Definition: compile-object-load.c:38
do_module_cleanup::source_file
char * source_file
Definition: compile-object-run.c:40
xfree
void xfree(void *)
func_type
Definition: gdbtypes.h:1059
compile_module::munmap_list_head
struct munmap_list * munmap_list_head
Definition: compile-object-load.h:54
RETURN_MASK_ERROR
Definition: common-exceptions.h:43
compile_module::out_value_addr
CORE_ADDR out_value_addr
Definition: compile-object-load.h:51
value_ind
struct value * value_ind(struct value *arg1)
Definition: valops.c:1542
compile_i_scope_types
compile_i_scope_types
Definition: defs.h:61
defs.h
BLOCK_START
#define BLOCK_START(bl)
Definition: block.h:105
END_CATCH
#define END_CATCH
Definition: common-exceptions.h:259
TYPE_FIELD_TYPE
#define TYPE_FIELD_TYPE(thistype, n)
Definition: gdbtypes.h:1371
objfiles.h
TRY
#define TRY
Definition: common-exceptions.h:246
CATCH
#define CATCH(EXCEPTION, MASK)
Definition: common-exceptions.h:254
do_module_cleanup::scope
enum compile_i_scope_types scope
Definition: compile-object-run.c:43
COMPILE_I_PRINT_ADDRESS_SCOPE
Definition: defs.h:81
objfile::objfile
objfile(bfd *, const char *, objfile_flags)
Definition: objfiles.c:373
dummy-frame.h
copy_type_recursive
struct type * copy_type_recursive(struct objfile *objfile, struct type *type, htab_t copied_types)
Definition: gdbtypes.c:4763
type
Definition: gdbtypes.h:749
create_copied_types_hash
htab_t create_copied_types_hash(struct objfile *objfile)
Definition: gdbtypes.c:4724
do_module_cleanup::munmap_list_head
struct munmap_list * munmap_list_head
Definition: compile-object-run.c:51
OBJF_USERLOADED
Definition: objfile-flags.h:53
objfile_name
const char * objfile_name(const struct objfile *objfile)
Definition: objfiles.c:1557
xmalloc
void * xmalloc(YYSIZE_T)
compile_module
Definition: compile-object-load.h:24
TYPE_CODE_FUNC
Definition: gdbtypes.h:110
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
compile_module::source_file
char * source_file
Definition: compile-object-load.h:30
value
Definition: value.c:169
objfile
Definition: objfiles.h:286
compile-object-run.h
throw_exception
void throw_exception(struct gdb_exception exception)
Definition: common-exceptions.c:312
compile.h
compile_module::scope
enum compile_i_scope_types scope
Definition: compile-object-load.h:40
value_from_pointer
struct value * value_from_pointer(struct type *type, CORE_ADDR addr)
Definition: value.c:3560
COMPILE_I_PRINT_VALUE_SCOPE
Definition: defs.h:82
compile_module::func_sym
struct symbol * func_sym
Definition: compile-object-load.h:33
SYMBOL_BLOCK_VALUE
#define SYMBOL_BLOCK_VALUE(symbol)
Definition: symtab.h:467
TYPE_CODE
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1238
compile_print_value
void compile_print_value(struct value *val, void *data_voidp)
Definition: compile.c:161
compile-internal.h
munmap_list_free
void munmap_list_free(struct munmap_list *head)
Definition: compile-object-load.c:64
do_module_cleanup::objfile_name_string
char objfile_name_string[1]
Definition: compile-object-run.c:54
TYPE_NFIELDS
#define TYPE_NFIELDS(thistype)
Definition: gdbtypes.h:1239
value.h
do_module_cleanup::scope_data
void * scope_data
Definition: compile-object-run.c:44
compile_object_run
void compile_object_run(struct compile_module *module)
Definition: compile-object-run.c:116
do_module_cleanup
static dummy_frame_dtor_ftype do_module_cleanup
Definition: compile-object-run.c:60
SYMBOL_TYPE
#define SYMBOL_TYPE(symbol)
Definition: symtab.h:1161
symbol
Definition: symtab.h:1049
do_module_cleanup::executedp
int * executedp
Definition: compile-object-run.c:37
compile_module::regs_addr
CORE_ADDR regs_addr
Definition: compile-object-load.h:37
compile_module::scope_data
void * scope_data
Definition: compile-object-load.h:43
do_module_cleanup::out_value_type
struct type * out_value_type
Definition: compile-object-run.c:47
ALL_OBJFILES
#define ALL_OBJFILES(obj)
Definition: objfiles.h:582
valprint.h
compile_module::objfile
struct objfile * objfile
Definition: compile-object-load.h:27
find_dummy_frame_dtor
int find_dummy_frame_dtor(dummy_frame_dtor_ftype *dtor, void *dtor_data)
Definition: dummy-frame.c:254
clear_symtab_users
void clear_symtab_users(symfile_add_flags add_flags)
Definition: symfile.c:2885
lookup_pointer_type
struct type * lookup_pointer_type(struct type *type)
Definition: gdbtypes.c:381
compile_module::out_value_type
struct type * out_value_type
Definition: compile-object-load.h:47
block.h
do_module_cleanup::out_value_addr
CORE_ADDR out_value_addr
Definition: compile-object-run.c:48