Add "info connections" command, "info inferiors" connection number/string
[deliverable/binutils-gdb.git] / gdb / target-connection.c
1 /* List of target connections for GDB.
2
3 Copyright (C) 2017-2020 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 "target-connection.h"
22
23 #include <map>
24
25 #include "inferior.h"
26 #include "target.h"
27
28 /* A map between connection number and representative process_stratum
29 target. */
30 static std::map<int, process_stratum_target *> process_targets;
31
32 /* The highest connection number ever given to a target. */
33 static int highest_target_connection_num;
34
35 /* See target-connection.h. */
36
37 void
38 connection_list_add (process_stratum_target *t)
39 {
40 if (t->connection_number == 0)
41 {
42 t->connection_number = ++highest_target_connection_num;
43 process_targets[t->connection_number] = t;
44 }
45 }
46
47 /* See target-connection.h. */
48
49 void
50 connection_list_remove (process_stratum_target *t)
51 {
52 process_targets.erase (t->connection_number);
53 t->connection_number = 0;
54 }
55
56 /* Make a target connection string for T. This is usually T's
57 shortname, but it includes the result of
58 process_stratum_target::connection_string() too if T supports
59 it. */
60
61 static std::string
62 make_target_connection_string (process_stratum_target *t)
63 {
64 if (t->connection_string () != NULL)
65 return string_printf ("%s %s", t->shortname (),
66 t->connection_string ());
67 else
68 return t->shortname ();
69 }
70
71 /* Prints the list of target connections and their details on UIOUT.
72
73 If REQUESTED_CONNECTIONS is not NULL, it's a list of GDB ids of the
74 target connections that should be printed. Otherwise, all target
75 connections are printed. */
76
77 static void
78 print_connection (struct ui_out *uiout, const char *requested_connections)
79 {
80 int count = 0;
81 size_t what_len = 0;
82
83 /* Compute number of lines we will print. */
84 for (const auto &it : process_targets)
85 {
86 if (!number_is_in_list (requested_connections, it.first))
87 continue;
88
89 ++count;
90
91 process_stratum_target *t = it.second;
92
93 size_t l = strlen (t->shortname ());
94 if (t->connection_string () != NULL)
95 l += 1 + strlen (t->connection_string ());
96
97 if (l > what_len)
98 what_len = l;
99 }
100
101 if (count == 0)
102 {
103 uiout->message (_("No connections.\n"));
104 return;
105 }
106
107 ui_out_emit_table table_emitter (uiout, 4, process_targets.size (),
108 "connections");
109
110 uiout->table_header (1, ui_left, "current", "");
111 uiout->table_header (4, ui_left, "number", "Num");
112 /* The text in the "what" column may include spaces. Add one extra
113 space to visually separate the What and Description columns a
114 little better. Compare:
115 "* 1 remote :9999 Remote serial target in gdb-specific protocol"
116 "* 1 remote :9999 Remote serial target in gdb-specific protocol"
117 */
118 uiout->table_header (what_len + 1, ui_left, "what", "What");
119 uiout->table_header (17, ui_left, "description", "Description");
120
121 uiout->table_body ();
122
123 for (const auto &it : process_targets)
124 {
125 process_stratum_target *t = it.second;
126
127 if (!number_is_in_list (requested_connections, t->connection_number))
128 continue;
129
130 ui_out_emit_tuple tuple_emitter (uiout, NULL);
131
132 if (current_inferior ()->process_target () == t)
133 uiout->field_string ("current", "*");
134 else
135 uiout->field_skip ("current");
136
137 uiout->field_signed ("number", t->connection_number);
138
139 uiout->field_string ("what", make_target_connection_string (t).c_str ());
140
141 uiout->field_string ("description", t->longname ());
142
143 uiout->text ("\n");
144 }
145 }
146
147 /* The "info connections" command. */
148
149 static void
150 info_connections_command (const char *args, int from_tty)
151 {
152 print_connection (current_uiout, args);
153 }
154
155 void
156 _initialize_target_connection ()
157 {
158 add_info ("connections", info_connections_command,
159 _("\
160 Target connections in use.\n\
161 Shows the list of target connections currently in use."));
162 }
This page took 0.032261 seconds and 4 git commands to generate.