Automatic date update in version.in
[binutils-gdb.git] / gdb / target-connection.c
blobd1da6a2d7b9f318ac1bdac145efa569560a8af97
1 /* List of target connections for GDB.
3 Copyright (C) 2017-2022 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/>. */
20 #include "defs.h"
21 #include "target-connection.h"
23 #include <map>
25 #include "inferior.h"
26 #include "target.h"
27 #include "observable.h"
29 /* A map between connection number and representative process_stratum
30 target. */
31 static std::map<int, process_stratum_target *> process_targets;
33 /* The highest connection number ever given to a target. */
34 static int highest_target_connection_num;
36 /* See target-connection.h. */
38 void
39 connection_list_add (process_stratum_target *t)
41 if (t->connection_number == 0)
43 t->connection_number = ++highest_target_connection_num;
44 process_targets[t->connection_number] = t;
48 /* See target-connection.h. */
50 void
51 connection_list_remove (process_stratum_target *t)
53 /* Notify about the connection being removed before we reset the
54 connection number to zero. */
55 gdb::observers::connection_removed.notify (t);
56 process_targets.erase (t->connection_number);
57 t->connection_number = 0;
60 /* See target-connection.h. */
62 std::string
63 make_target_connection_string (process_stratum_target *t)
65 if (t->connection_string () != NULL)
66 return string_printf ("%s %s", t->shortname (),
67 t->connection_string ());
68 else
69 return t->shortname ();
72 /* Prints the list of target connections and their details on UIOUT.
74 If REQUESTED_CONNECTIONS is not NULL, it's a list of GDB ids of the
75 target connections that should be printed. Otherwise, all target
76 connections are printed. */
78 static void
79 print_connection (struct ui_out *uiout, const char *requested_connections)
81 int count = 0;
82 size_t what_len = 0;
84 /* Compute number of lines we will print. */
85 for (const auto &it : process_targets)
87 if (!number_is_in_list (requested_connections, it.first))
88 continue;
90 ++count;
92 process_stratum_target *t = it.second;
94 size_t l = strlen (t->shortname ());
95 if (t->connection_string () != NULL)
96 l += 1 + strlen (t->connection_string ());
98 if (l > what_len)
99 what_len = l;
102 if (count == 0)
104 uiout->message (_("No connections.\n"));
105 return;
108 ui_out_emit_table table_emitter (uiout, 4, process_targets.size (),
109 "connections");
111 uiout->table_header (1, ui_left, "current", "");
112 uiout->table_header (4, ui_left, "number", "Num");
113 /* The text in the "what" column may include spaces. Add one extra
114 space to visually separate the What and Description columns a
115 little better. Compare:
116 "* 1 remote :9999 Remote serial target in gdb-specific protocol"
117 "* 1 remote :9999 Remote serial target in gdb-specific protocol"
119 uiout->table_header (what_len + 1, ui_left, "what", "What");
120 uiout->table_header (17, ui_left, "description", "Description");
122 uiout->table_body ();
124 for (const auto &it : process_targets)
126 process_stratum_target *t = it.second;
128 if (!number_is_in_list (requested_connections, t->connection_number))
129 continue;
131 ui_out_emit_tuple tuple_emitter (uiout, NULL);
133 if (current_inferior ()->process_target () == t)
134 uiout->field_string ("current", "*");
135 else
136 uiout->field_skip ("current");
138 uiout->field_signed ("number", t->connection_number);
140 uiout->field_string ("what", make_target_connection_string (t));
142 uiout->field_string ("description", t->longname ());
144 uiout->text ("\n");
148 /* The "info connections" command. */
150 static void
151 info_connections_command (const char *args, int from_tty)
153 print_connection (current_uiout, args);
156 void _initialize_target_connection ();
158 void
159 _initialize_target_connection ()
161 add_info ("connections", info_connections_command,
162 _("\
163 Target connections in use.\n\
164 Shows the list of target connections currently in use."));