From 01bccc56af0f590c5ff64c24dea17fa3098572d7 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Fri, 15 Sep 2023 12:05:57 -0600 Subject: [PATCH] Use gdb::checked_static_cast for tracepoints This replaces some casts to 'tracepoint *' with checked_static_cast. Some functions are changed to accept a 'tracepoint *' now, for better type safety. Approved-By: Simon Marchi --- gdb/breakpoint.c | 34 +++++++++++++++++++--------------- gdb/mi/mi-cmd-break.c | 9 ++++++--- gdb/remote.c | 11 +++++------ gdb/target-debug.h | 2 ++ gdb/target-delegates.c | 16 ++++++++-------- gdb/target.c | 2 +- gdb/target.h | 4 ++-- gdb/tracefile-tfile.c | 4 ++-- gdb/tracepoint.c | 25 ++++++++++++++----------- gdb/tracepoint.h | 4 ++-- 10 files changed, 61 insertions(+), 50 deletions(-) diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 9a5e55df051..a22f1258a00 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -1433,7 +1433,7 @@ validate_commands_for_breakpoint (struct breakpoint *b, { if (is_tracepoint (b)) { - struct tracepoint *t = (struct tracepoint *) b; + tracepoint *t = gdb::checked_static_cast (b); struct command_line *c; struct command_line *while_stepping = 0; @@ -1471,7 +1471,7 @@ validate_commands_for_breakpoint (struct breakpoint *b, while_stepping = c; } - validate_actionline (c->line, b); + validate_actionline (c->line, t); } if (while_stepping) { @@ -1645,7 +1645,9 @@ commands_command_1 (const char *arg, int from_tty, auto do_validate = [=] (const char *line) { - validate_actionline (line, b); + tracepoint *t + = gdb::checked_static_cast (b); + validate_actionline (line, t); }; gdb::function_view validator; if (is_tracepoint (b)) @@ -6757,7 +6759,7 @@ print_one_breakpoint_location (struct breakpoint *b, if (!part_of_multiple && is_tracepoint (b)) { - struct tracepoint *tp = (struct tracepoint *) b; + tracepoint *tp = gdb::checked_static_cast (b); if (tp->traceframe_usage) { @@ -6789,7 +6791,7 @@ print_one_breakpoint_location (struct breakpoint *b, if (is_tracepoint (b)) { - struct tracepoint *t = (struct tracepoint *) b; + tracepoint *t = gdb::checked_static_cast (b); if (!part_of_multiple && t->pass_count) { @@ -8654,7 +8656,7 @@ code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_, if (type == bp_static_tracepoint || type == bp_static_marker_tracepoint) { - auto *t = gdb::checked_static_cast (this); + auto *t = gdb::checked_static_cast (this); struct static_tracepoint_marker marker; if (strace_marker_p (this)) @@ -12817,9 +12819,8 @@ ambiguous_names_p (const bp_location_range &locs) precisely because it confuses tools). */ static struct symtab_and_line -update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal) +update_static_tracepoint (tracepoint *tp, struct symtab_and_line sal) { - struct tracepoint *tp = (struct tracepoint *) b; struct static_tracepoint_marker marker; CORE_ADDR pc; @@ -12831,7 +12832,7 @@ update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal) { if (tp->static_trace_marker_id != marker.str_id) warning (_("static tracepoint %d changed probed marker from %s to %s"), - b->number, tp->static_trace_marker_id.c_str (), + tp->number, tp->static_trace_marker_id.c_str (), marker.str_id.c_str ()); tp->static_trace_marker_id = std::move (marker.str_id); @@ -12862,7 +12863,7 @@ update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal) warning (_("marker for static tracepoint %d (%s) not " "found at previous line number"), - b->number, tp->static_trace_marker_id.c_str ()); + tp->number, tp->static_trace_marker_id.c_str ()); symtab_and_line sal2 = find_pc_line (tpmarker->address, 0); sym = find_pc_sect_function (tpmarker->address, NULL); @@ -12888,17 +12889,17 @@ update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal) uiout->field_signed ("line", sal2.line); uiout->text ("\n"); - b->first_loc ().line_number = sal2.line; - b->first_loc ().symtab = sym != NULL ? sal2.symtab : NULL; + tp->first_loc ().line_number = sal2.line; + tp->first_loc ().symtab = sym != NULL ? sal2.symtab : NULL; std::unique_ptr els (new explicit_location_spec ()); els->source_filename = xstrdup (symtab_to_filename_for_display (sal2.symtab)); - els->line_offset.offset = b->first_loc ().line_number; + els->line_offset.offset = tp->first_loc ().line_number; els->line_offset.sign = LINE_OFFSET_NONE; - b->locspec = std::move (els); + tp->locspec = std::move (els); /* Might be nice to check if function changed, and warn if so. */ @@ -13159,7 +13160,10 @@ code_breakpoint::location_spec_to_sals (location_spec *locspec, } if (type == bp_static_tracepoint) - sals[0] = update_static_tracepoint (this, sals[0]); + { + tracepoint *t = gdb::checked_static_cast (this); + sals[0] = update_static_tracepoint (t, sals[0]); + } *found = 1; } diff --git a/gdb/mi/mi-cmd-break.c b/gdb/mi/mi-cmd-break.c index 975bc1c5dde..44835c7f7d0 100644 --- a/gdb/mi/mi-cmd-break.c +++ b/gdb/mi/mi-cmd-break.c @@ -585,11 +585,14 @@ mi_cmd_break_commands (const char *command, const char *const *argv, int argc) }; if (is_tracepoint (b)) - break_command = read_command_lines_1 (reader, 1, - [=] (const char *line) + { + tracepoint *t = gdb::checked_static_cast (b); + break_command = read_command_lines_1 (reader, 1, + [=] (const char *line) { - validate_actionline (line, b); + validate_actionline (line, t); }); + } else break_command = read_command_lines_1 (reader, 1, 0); diff --git a/gdb/remote.c b/gdb/remote.c index ba81c5b0b6f..9bb4f1de598 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -911,7 +911,7 @@ public: int get_trace_status (struct trace_status *ts) override; - void get_tracepoint_status (struct breakpoint *tp, struct uploaded_tp *utp) + void get_tracepoint_status (tracepoint *tp, struct uploaded_tp *utp) override; void trace_stop () override; @@ -13256,7 +13256,7 @@ remote_target::download_tracepoint (struct bp_location *loc) std::vector stepping_actions; char *pkt; struct breakpoint *b = loc->owner; - struct tracepoint *t = (struct tracepoint *) b; + tracepoint *t = gdb::checked_static_cast (b); struct remote_state *rs = get_remote_state (); int ret; const char *err_msg = _("Tracepoint packet too large for target."); @@ -13675,12 +13675,11 @@ remote_target::get_trace_status (struct trace_status *ts) } void -remote_target::get_tracepoint_status (struct breakpoint *bp, +remote_target::get_tracepoint_status (tracepoint *tp, struct uploaded_tp *utp) { struct remote_state *rs = get_remote_state (); char *reply; - struct tracepoint *tp = (struct tracepoint *) bp; size_t size = get_remote_packet_size (); if (tp) @@ -13700,7 +13699,7 @@ remote_target::get_tracepoint_status (struct breakpoint *bp, if (reply && *reply) { if (*reply == 'V') - parse_tracepoint_status (reply + 1, bp, utp); + parse_tracepoint_status (reply + 1, tp, utp); } } } @@ -13715,7 +13714,7 @@ remote_target::get_tracepoint_status (struct breakpoint *bp, if (reply && *reply) { if (*reply == 'V') - parse_tracepoint_status (reply + 1, bp, utp); + parse_tracepoint_status (reply + 1, tp, utp); } } } diff --git a/gdb/target-debug.h b/gdb/target-debug.h index a028d2ba5f4..fed91bfa875 100644 --- a/gdb/target-debug.h +++ b/gdb/target-debug.h @@ -126,6 +126,8 @@ target_debug_do_print (host_address_to_string (X)) #define target_debug_print_struct_breakpoint_p(X) \ target_debug_do_print (host_address_to_string (X)) +#define target_debug_print_tracepoint_p(X) \ + target_debug_do_print (host_address_to_string (X)) #define target_debug_print_struct_uploaded_tp_p(X) \ target_debug_do_print (host_address_to_string (X)) #define target_debug_print_struct_uploaded_tp_pp(X) \ diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c index 8b066249624..a22d191df2c 100644 --- a/gdb/target-delegates.c +++ b/gdb/target-delegates.c @@ -143,7 +143,7 @@ struct dummy_target : public target_ops void trace_set_readonly_regions () override; void trace_start () override; int get_trace_status (struct trace_status *arg0) override; - void get_tracepoint_status (struct breakpoint *arg0, struct uploaded_tp *arg1) override; + void get_tracepoint_status (tracepoint *arg0, struct uploaded_tp *arg1) override; void trace_stop () override; int trace_find (enum trace_find_type arg0, int arg1, CORE_ADDR arg2, CORE_ADDR arg3, int *arg4) override; bool get_trace_state_variable_value (int arg0, LONGEST *arg1) override; @@ -318,7 +318,7 @@ struct debug_target : public target_ops void trace_set_readonly_regions () override; void trace_start () override; int get_trace_status (struct trace_status *arg0) override; - void get_tracepoint_status (struct breakpoint *arg0, struct uploaded_tp *arg1) override; + void get_tracepoint_status (tracepoint *arg0, struct uploaded_tp *arg1) override; void trace_stop () override; int trace_find (enum trace_find_type arg0, int arg1, CORE_ADDR arg2, CORE_ADDR arg3, int *arg4) override; bool get_trace_state_variable_value (int arg0, LONGEST *arg1) override; @@ -3216,24 +3216,24 @@ debug_target::get_trace_status (struct trace_status *arg0) } void -target_ops::get_tracepoint_status (struct breakpoint *arg0, struct uploaded_tp *arg1) +target_ops::get_tracepoint_status (tracepoint *arg0, struct uploaded_tp *arg1) { this->beneath ()->get_tracepoint_status (arg0, arg1); } void -dummy_target::get_tracepoint_status (struct breakpoint *arg0, struct uploaded_tp *arg1) +dummy_target::get_tracepoint_status (tracepoint *arg0, struct uploaded_tp *arg1) { tcomplain (); } void -debug_target::get_tracepoint_status (struct breakpoint *arg0, struct uploaded_tp *arg1) +debug_target::get_tracepoint_status (tracepoint *arg0, struct uploaded_tp *arg1) { gdb_printf (gdb_stdlog, "-> %s->get_tracepoint_status (...)\n", this->beneath ()->shortname ()); this->beneath ()->get_tracepoint_status (arg0, arg1); gdb_printf (gdb_stdlog, "<- %s->get_tracepoint_status (", this->beneath ()->shortname ()); - target_debug_print_struct_breakpoint_p (arg0); + target_debug_print_tracepoint_p (arg0); gdb_puts (", ", gdb_stdlog); target_debug_print_struct_uploaded_tp_p (arg1); gdb_puts (")\n", gdb_stdlog); @@ -4553,9 +4553,9 @@ dummy_target::fetch_x86_xsave_layout () x86_xsave_layout debug_target::fetch_x86_xsave_layout () { - x86_xsave_layout result; gdb_printf (gdb_stdlog, "-> %s->fetch_x86_xsave_layout (...)\n", this->beneath ()->shortname ()); - result = this->beneath ()->fetch_x86_xsave_layout (); + x86_xsave_layout result + = this->beneath ()->fetch_x86_xsave_layout (); gdb_printf (gdb_stdlog, "<- %s->fetch_x86_xsave_layout (", this->beneath ()->shortname ()); gdb_puts (") = ", gdb_stdlog); target_debug_print_x86_xsave_layout (result); diff --git a/gdb/target.c b/gdb/target.c index 68d7fe2bb56..8cb4fa1736d 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -667,7 +667,7 @@ target_get_trace_status (trace_status *ts) } void -target_get_tracepoint_status (breakpoint *tp, uploaded_tp *utp) +target_get_tracepoint_status (tracepoint *tp, uploaded_tp *utp) { return current_inferior ()->top_target ()->get_tracepoint_status (tp, utp); } diff --git a/gdb/target.h b/gdb/target.h index aa07075f9f2..6e6aa5f8fb3 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -1043,7 +1043,7 @@ struct target_ops virtual int get_trace_status (struct trace_status *ts) TARGET_DEFAULT_RETURN (-1); - virtual void get_tracepoint_status (struct breakpoint *tp, + virtual void get_tracepoint_status (tracepoint *tp, struct uploaded_tp *utp) TARGET_DEFAULT_NORETURN (tcomplain ()); @@ -2255,7 +2255,7 @@ extern void target_trace_set_readonly_regions (); extern int target_get_trace_status (trace_status *ts); -extern void target_get_tracepoint_status (breakpoint *tp, uploaded_tp *utp); +extern void target_get_tracepoint_status (tracepoint *tp, uploaded_tp *utp); extern void target_trace_stop (); diff --git a/gdb/tracefile-tfile.c b/gdb/tracefile-tfile.c index 3440f375021..815f13f9846 100644 --- a/gdb/tracefile-tfile.c +++ b/gdb/tracefile-tfile.c @@ -67,7 +67,7 @@ class tfile_target final : public tracefile_target bool get_trace_state_variable_value (int tsv, LONGEST *val) override; traceframe_info_up traceframe_info () override; - void get_tracepoint_status (struct breakpoint *tp, + void get_tracepoint_status (tracepoint *tp, struct uploaded_tp *utp) override; }; @@ -633,7 +633,7 @@ tfile_target::files_info () } void -tfile_target::get_tracepoint_status (struct breakpoint *tp, struct uploaded_tp *utp) +tfile_target::get_tracepoint_status (tracepoint *tp, struct uploaded_tp *utp) { /* Other bits of trace status were collected as part of opening the trace files, so nothing to do here. */ diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c index 205380476b3..626fd871cf4 100644 --- a/gdb/tracepoint.c +++ b/gdb/tracepoint.c @@ -157,7 +157,7 @@ static std::string trace_stop_notes; struct collection_list; -static counted_command_line all_tracepoint_actions (struct breakpoint *); +static counted_command_line all_tracepoint_actions (tracepoint *); static struct trace_status trace_status; @@ -626,12 +626,11 @@ finalize_tracepoint_aexpr (struct agent_expr *aexpr) /* worker function */ void -validate_actionline (const char *line, struct breakpoint *b) +validate_actionline (const char *line, tracepoint *t) { struct cmd_list_element *c; const char *tmp_p; const char *p; - struct tracepoint *t = (struct tracepoint *) b; /* If EOF is typed, *line is NULL. */ if (line == NULL) @@ -1479,7 +1478,8 @@ encode_actions (struct bp_location *tloc, gdbarch_virtual_frame_pointer (tloc->gdbarch, tloc->address, &frame_reg, &frame_offset); - counted_command_line actions = all_tracepoint_actions (tloc->owner); + tracepoint *t = gdb::checked_static_cast (tloc->owner); + counted_command_line actions = all_tracepoint_actions (t); encode_actions_1 (actions.get (), tloc, frame_reg, frame_offset, tracepoint_list, stepping_list); encode_actions_1 (breakpoint_commands (tloc->owner), tloc, @@ -1884,8 +1884,11 @@ tstatus_command (const char *args, int from_tty) (long int) (ts->stop_time % 1000000)); /* Now report any per-tracepoint status available. */ - for (breakpoint &t : all_tracepoints ()) - target_get_tracepoint_status (&t, NULL); + for (breakpoint &b : all_tracepoints ()) + { + tracepoint *t = gdb::checked_static_cast (&b); + target_get_tracepoint_status (t, nullptr); + } } /* Report the trace status to uiout, in a way suitable for MI, and not @@ -2751,7 +2754,7 @@ get_traceframe_location (int *stepping_frame_p) /* Return the default collect actions of a tracepoint T. */ static counted_command_line -all_tracepoint_actions (struct breakpoint *t) +all_tracepoint_actions (tracepoint *t) { counted_command_line actions (nullptr, command_lines_deleter ()); @@ -2794,7 +2797,8 @@ tdump_command (const char *args, int from_tty) select_frame (get_current_frame ()); - counted_command_line actions = all_tracepoint_actions (loc->owner); + tracepoint *t = gdb::checked_static_cast (loc->owner); + counted_command_line actions = all_tracepoint_actions (t); trace_dump_actions (actions.get (), 0, stepping_frame, from_tty); trace_dump_actions (breakpoint_commands (loc->owner), 0, stepping_frame, @@ -3059,7 +3063,7 @@ merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps) /* Mark this location as already inserted. */ loc->inserted = 1; - t = (struct tracepoint *) loc->owner; + t = gdb::checked_static_cast (loc->owner); gdb_printf (_("Assuming tracepoint %d is same " "as target's tracepoint %d at %s.\n"), loc->owner->number, utp->number, @@ -3368,11 +3372,10 @@ Status line: '%s'\n"), p, line); } void -parse_tracepoint_status (const char *p, struct breakpoint *bp, +parse_tracepoint_status (const char *p, tracepoint *tp, struct uploaded_tp *utp) { ULONGEST uval; - struct tracepoint *tp = (struct tracepoint *) bp; p = unpack_varlen_hex (p, &uval); if (tp) diff --git a/gdb/tracepoint.h b/gdb/tracepoint.h index f78a750ea99..b9ff31fa0f6 100644 --- a/gdb/tracepoint.h +++ b/gdb/tracepoint.h @@ -352,7 +352,7 @@ extern void encode_actions_rsp (struct bp_location *tloc, std::vector *tdp_actions, std::vector *stepping_actions); -extern void validate_actionline (const char *, struct breakpoint *); +extern void validate_actionline (const char *, tracepoint *); extern void validate_trace_state_variable_name (const char *name); extern struct trace_state_variable *find_trace_state_variable (const char *name); @@ -367,7 +367,7 @@ extern int encode_source_string (int num, ULONGEST addr, extern void parse_trace_status (const char *line, struct trace_status *ts); -extern void parse_tracepoint_status (const char *p, struct breakpoint *tp, +extern void parse_tracepoint_status (const char *p, tracepoint *tp, struct uploaded_tp *utp); extern void parse_tracepoint_definition (const char *line, -- 2.30.2