From 8dd8c8d4ab262bc4e469b9914cc9b83de3b5bcfb Mon Sep 17 00:00:00 2001 From: Marco Barisione Date: Wed, 19 May 2021 13:58:41 +0100 Subject: [PATCH] gdb: Pass std::strings to ui_out::field_string () where convenient While adding a ui_out::text () overload accepting a std::string, I noticed that several callers of ui_out::field_string () were converting std::string instances to char pointers even if not necessary. gdb/ChangeLog: * ui-out.c (ui_out::field_string): Add missing style_argument to the overload accepting a std::string, to make it equivalent to the char pointer version. * ui-out.h (class ui_out): Ditto. * break-catch-sig.c (signal_catchpoint_print_one): Do not convert std::strings to char pointers before passing them to ui_out::field_string (). * break-catch-throw.c (print_one_detail_exception_catchpoint): Ditto. * cli/cli-setshow.c (do_show_command): Ditto. * disasm.c (gdb_pretty_print_disassembler::pretty_print_insn): Ditto. * infcmd.c (print_return_value_1): Ditto. * inferior.c (print_inferior): Ditto. * linux-thread-db.c (info_auto_load_libthread_db): Ditto. * mi/mi-cmd-var.c (print_varobj): Ditto. (mi_cmd_var_set_format): Ditto. (mi_cmd_var_info_type): Ditto. (mi_cmd_var_info_expression): Ditto. (mi_cmd_var_evaluate_expression): Ditto. (mi_cmd_var_assign): Ditto. (varobj_update_one): Ditto. * mi/mi-main.c (list_available_thread_groups): Ditto. (mi_cmd_data_read_memory_bytes): Ditto. (mi_cmd_trace_frame_collected): Ditto. * osdata.c (info_osdata): Ditto. * probe.c (info_probes_for_spops): Ditto. * target-connection.c (print_connection): Ditto. * thread.c (print_thread_info_1): Ditto. * tracepoint.c (print_one_static_tracepoint_marker): Ditto. --- gdb/break-catch-sig.c | 2 +- gdb/break-catch-throw.c | 2 +- gdb/cli/cli-setshow.c | 2 +- gdb/disasm.c | 2 +- gdb/infcmd.c | 2 +- gdb/inferior.c | 2 +- gdb/linux-thread-db.c | 2 +- gdb/mi/mi-cmd-var.c | 22 +++++++++++----------- gdb/mi/mi-main.c | 14 +++++++------- gdb/osdata.c | 3 +-- gdb/probe.c | 5 ++--- gdb/target-connection.c | 2 +- gdb/thread.c | 3 +-- gdb/tracepoint.c | 4 ++-- gdb/ui-out.c | 5 +++-- gdb/ui-out.h | 3 ++- 16 files changed, 37 insertions(+), 38 deletions(-) diff --git a/gdb/break-catch-sig.c b/gdb/break-catch-sig.c index 31a622c83e5..9530dea86ba 100644 --- a/gdb/break-catch-sig.c +++ b/gdb/break-catch-sig.c @@ -237,7 +237,7 @@ signal_catchpoint_print_one (struct breakpoint *b, text += name; } - uiout->field_string ("what", text.c_str ()); + uiout->field_string ("what", text); } else uiout->field_string ("what", diff --git a/gdb/break-catch-throw.c b/gdb/break-catch-throw.c index c8d5ccd5152..7fc6953b90c 100644 --- a/gdb/break-catch-throw.c +++ b/gdb/break-catch-throw.c @@ -300,7 +300,7 @@ print_one_detail_exception_catchpoint (const struct breakpoint *b, if (!cp->exception_rx.empty ()) { uiout->text (_("\tmatching: ")); - uiout->field_string ("regexp", cp->exception_rx.c_str ()); + uiout->field_string ("regexp", cp->exception_rx); uiout->text ("\n"); } } diff --git a/gdb/cli/cli-setshow.c b/gdb/cli/cli-setshow.c index f6a594d69a2..5fd5fd15c6a 100644 --- a/gdb/cli/cli-setshow.c +++ b/gdb/cli/cli-setshow.c @@ -714,7 +714,7 @@ do_show_command (const char *arg, int from_tty, struct cmd_list_element *c) versions of code to print the value out. */ if (uiout->is_mi_like_p ()) - uiout->field_string ("value", val.c_str ()); + uiout->field_string ("value", val); else { if (c->show_value_func != NULL) diff --git a/gdb/disasm.c b/gdb/disasm.c index eb69e89017b..70c54220a29 100644 --- a/gdb/disasm.c +++ b/gdb/disasm.c @@ -244,7 +244,7 @@ gdb_pretty_print_disassembler::pretty_print_insn (const struct disasm_insn *insn the future. */ m_uiout->text (" <"); if (!omit_fname) - m_uiout->field_string ("func-name", name.c_str (), + m_uiout->field_string ("func-name", name, function_name_style.style ()); /* For negative offsets, avoid displaying them as +-N; the sign of the offset takes the place of the "+" here. */ diff --git a/gdb/infcmd.c b/gdb/infcmd.c index a22d815f230..4351409af50 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -1539,7 +1539,7 @@ print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv) { std::string type_name = type_to_string (rv->type); uiout->text ("Value returned has type: "); - uiout->field_string ("return-type", type_name.c_str ()); + uiout->field_string ("return-type", type_name); uiout->text ("."); uiout->text (" Cannot determine contents\n"); } diff --git a/gdb/inferior.c b/gdb/inferior.c index a8779c354b5..059839ec962 100644 --- a/gdb/inferior.c +++ b/gdb/inferior.c @@ -529,7 +529,7 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors) uiout->field_string ("target-id", inferior_pid_to_str (inf->pid)); std::string conn = uiout_field_connection (inf->process_target ()); - uiout->field_string ("connection-id", conn.c_str ()); + uiout->field_string ("connection-id", conn); if (inf->pspace->exec_filename != nullptr) uiout->field_string ("exec", inf->pspace->exec_filename.get ()); diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c index 2c75cd60794..9925b02e778 100644 --- a/gdb/linux-thread-db.c +++ b/gdb/linux-thread-db.c @@ -1935,7 +1935,7 @@ info_auto_load_libthread_db (const char *args, int from_tty) i++; } - uiout->field_string ("pids", pids.c_str ()); + uiout->field_string ("pids", pids); uiout->text ("\n"); } diff --git a/gdb/mi/mi-cmd-var.c b/gdb/mi/mi-cmd-var.c index e54a5e04970..61bc169304b 100644 --- a/gdb/mi/mi-cmd-var.c +++ b/gdb/mi/mi-cmd-var.c @@ -56,7 +56,7 @@ print_varobj (struct varobj *var, enum print_values print_values, { std::string exp = varobj_get_expression (var); - uiout->field_string ("exp", exp.c_str ()); + uiout->field_string ("exp", exp); } uiout->field_signed ("numchild", varobj_get_num_children (var)); @@ -64,12 +64,12 @@ print_varobj (struct varobj *var, enum print_values print_values, { std::string val = varobj_get_value (var); - uiout->field_string ("value", val.c_str ()); + uiout->field_string ("value", val); } std::string type = varobj_get_type (var); if (!type.empty ()) - uiout->field_string ("type", type.c_str ()); + uiout->field_string ("type", type); thread_id = varobj_get_thread_id (var); if (thread_id > 0) @@ -236,7 +236,7 @@ mi_cmd_var_set_format (const char *command, char **argv, int argc) /* Report the value in the new format. */ std::string val = varobj_get_value (var); - uiout->field_string ("value", val.c_str ()); + uiout->field_string ("value", val); } void @@ -423,7 +423,7 @@ mi_cmd_var_info_type (const char *command, char **argv, int argc) var = varobj_get_handle (argv[0]); std::string type_name = varobj_get_type (var); - uiout->field_string ("type", type_name.c_str ()); + uiout->field_string ("type", type_name); } void @@ -461,7 +461,7 @@ mi_cmd_var_info_expression (const char *command, char **argv, int argc) uiout->field_string ("lang", lang->natural_name ()); std::string exp = varobj_get_expression (var); - uiout->field_string ("exp", exp.c_str ()); + uiout->field_string ("exp", exp); } void @@ -545,13 +545,13 @@ mi_cmd_var_evaluate_expression (const char *command, char **argv, int argc) { std::string val = varobj_get_formatted_value (var, format); - uiout->field_string ("value", val.c_str ()); + uiout->field_string ("value", val); } else { std::string val = varobj_get_value (var); - uiout->field_string ("value", val.c_str ()); + uiout->field_string ("value", val); } } @@ -582,7 +582,7 @@ mi_cmd_var_assign (const char *command, char **argv, int argc) "expression to variable object")); std::string val = varobj_get_value (var); - uiout->field_string ("value", val.c_str ()); + uiout->field_string ("value", val); } /* Helper for mi_cmd_var_update - update each VAR. */ @@ -692,7 +692,7 @@ varobj_update_one (struct varobj *var, enum print_values print_values, { std::string val = varobj_get_value (r.varobj); - uiout->field_string ("value", val.c_str ()); + uiout->field_string ("value", val); } uiout->field_string ("in_scope", "true"); break; @@ -716,7 +716,7 @@ varobj_update_one (struct varobj *var, enum print_values print_values, { std::string type_name = varobj_get_type (r.varobj); - uiout->field_string ("new_type", type_name.c_str ()); + uiout->field_string ("new_type", type_name); } if (r.type_changed || r.children_changed) diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index 582a55490d1..9d205f0208b 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -738,12 +738,12 @@ list_available_thread_groups (const std::set &ids, int recurse) ui_out_emit_tuple tuple_emitter (uiout, NULL); - uiout->field_string ("id", pid->c_str ()); + uiout->field_string ("id", *pid); uiout->field_string ("type", "process"); if (cmd) - uiout->field_string ("description", cmd->c_str ()); + uiout->field_string ("description", *cmd); if (user) - uiout->field_string ("user", user->c_str ()); + uiout->field_string ("user", *user); if (cores) output_cores (uiout, "cores", cores->c_str ()); @@ -762,9 +762,9 @@ list_available_thread_groups (const std::set &ids, int recurse) const std::string *tid = get_osdata_column (child, "tid"); const std::string *tcore = get_osdata_column (child, "core"); - uiout->field_string ("id", tid->c_str ()); + uiout->field_string ("id", *tid); if (tcore) - uiout->field_string ("core", tcore->c_str ()); + uiout->field_string ("core", *tcore); } } } @@ -1470,7 +1470,7 @@ mi_cmd_data_read_memory_bytes (const char *command, char **argv, int argc) std::string data = bin2hex (read_result.data.get (), (read_result.end - read_result.begin) * unit_size); - uiout->field_string ("contents", data.c_str ()); + uiout->field_string ("contents", data); } } @@ -2670,7 +2670,7 @@ mi_cmd_trace_frame_collected (const char *command, char **argv, int argc) if (target_read_memory (r.start, data.data (), r.length) == 0) { std::string data_str = bin2hex (data.data (), r.length); - uiout->field_string ("contents", data_str.c_str ()); + uiout->field_string ("contents", data_str); } else uiout->field_skip ("contents"); diff --git a/gdb/osdata.c b/gdb/osdata.c index 2ff0f520b0b..bc621cb2332 100644 --- a/gdb/osdata.c +++ b/gdb/osdata.c @@ -273,8 +273,7 @@ info_osdata (const char *type) continue; snprintf (col_name, 32, "col%d", ix_cols); - uiout->field_string (col_name, - item.columns[ix_cols].value.c_str ()); + uiout->field_string (col_name, item.columns[ix_cols].value); } } diff --git a/gdb/probe.c b/gdb/probe.c index 9eccf82f25d..d9a06e53416 100644 --- a/gdb/probe.c +++ b/gdb/probe.c @@ -571,9 +571,8 @@ info_probes_for_spops (const char *arg, int from_tty, ui_out_emit_tuple tuple_emitter (current_uiout, "probe"); current_uiout->field_string ("type", probe_type); - current_uiout->field_string ("provider", - probe.prob->get_provider ().c_str ()); - current_uiout->field_string ("name", probe.prob->get_name ().c_str ()); + current_uiout->field_string ("provider", probe.prob->get_provider ()); + current_uiout->field_string ("name", probe.prob->get_name ()); current_uiout->field_core_addr ("addr", probe.prob->get_gdbarch (), probe.prob->get_relocated_address (probe.objfile)); diff --git a/gdb/target-connection.c b/gdb/target-connection.c index 2f8e01d77e5..a649423e07e 100644 --- a/gdb/target-connection.c +++ b/gdb/target-connection.c @@ -133,7 +133,7 @@ print_connection (struct ui_out *uiout, const char *requested_connections) uiout->field_signed ("number", t->connection_number); - uiout->field_string ("what", make_target_connection_string (t).c_str ()); + uiout->field_string ("what", make_target_connection_string (t)); uiout->field_string ("description", t->longname ()); diff --git a/gdb/thread.c b/gdb/thread.c index 87b6cbf74fd..40051013c0a 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -1184,8 +1184,7 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads, } else { - uiout->field_string ("target-id", - thread_target_id_str (tp).c_str ()); + uiout->field_string ("target-id", thread_target_id_str (tp)); } if (tp->state == THREAD_RUNNING) diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c index 7f6d3e4a16b..da0e976c869 100644 --- a/gdb/tracepoint.c +++ b/gdb/tracepoint.c @@ -3678,7 +3678,7 @@ print_one_static_tracepoint_marker (int count, identifier! */ uiout->field_signed ("count", count); - uiout->field_string ("marker-id", marker.str_id.c_str ()); + uiout->field_string ("marker-id", marker.str_id); uiout->field_fmt ("enabled", "%c", !tracepoints.empty () ? 'y' : 'n'); @@ -3735,7 +3735,7 @@ print_one_static_tracepoint_marker (int count, uiout->text ("\n"); uiout->text (extra_field_indent); uiout->text (_("Data: \"")); - uiout->field_string ("extra-data", marker.extra.c_str ()); + uiout->field_string ("extra-data", marker.extra); uiout->text ("\"\n"); if (!tracepoints.empty ()) diff --git a/gdb/ui-out.c b/gdb/ui-out.c index 56251c9445a..c091af6cad3 100644 --- a/gdb/ui-out.c +++ b/gdb/ui-out.c @@ -528,9 +528,10 @@ ui_out::field_string (const char *fldname, const char *string, } void -ui_out::field_string (const char *fldname, const std::string &string) +ui_out::field_string (const char *fldname, const std::string &string, + const ui_file_style &style) { - field_string (fldname, string.c_str ()); + field_string (fldname, string.c_str (), style); } /* VARARGS */ diff --git a/gdb/ui-out.h b/gdb/ui-out.h index a06477df533..46dfe28c1e1 100644 --- a/gdb/ui-out.h +++ b/gdb/ui-out.h @@ -191,7 +191,8 @@ class ui_out CORE_ADDR address); void field_string (const char *fldname, const char *string, const ui_file_style &style = ui_file_style ()); - void field_string (const char *fldname, const std::string &string); + void field_string (const char *fldname, const std::string &string, + const ui_file_style &style = ui_file_style ()); void field_stream (const char *fldname, string_file &stream, const ui_file_style &style = ui_file_style ()); void field_skip (const char *fldname); -- 2.39.2