target-connection.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* List of target connections for GDB.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "target-connection.h"
  16. #include <map>
  17. #include "inferior.h"
  18. #include "target.h"
  19. #include "observable.h"
  20. /* A map between connection number and representative process_stratum
  21. target. */
  22. static std::map<int, process_stratum_target *> process_targets;
  23. /* The highest connection number ever given to a target. */
  24. static int highest_target_connection_num;
  25. /* See target-connection.h. */
  26. void
  27. connection_list_add (process_stratum_target *t)
  28. {
  29. if (t->connection_number == 0)
  30. {
  31. t->connection_number = ++highest_target_connection_num;
  32. process_targets[t->connection_number] = t;
  33. }
  34. }
  35. /* See target-connection.h. */
  36. void
  37. connection_list_remove (process_stratum_target *t)
  38. {
  39. /* Notify about the connection being removed before we reset the
  40. connection number to zero. */
  41. gdb::observers::connection_removed.notify (t);
  42. process_targets.erase (t->connection_number);
  43. t->connection_number = 0;
  44. }
  45. /* See target-connection.h. */
  46. std::string
  47. make_target_connection_string (process_stratum_target *t)
  48. {
  49. if (t->connection_string () != NULL)
  50. return string_printf ("%s %s", t->shortname (),
  51. t->connection_string ());
  52. else
  53. return t->shortname ();
  54. }
  55. /* Prints the list of target connections and their details on UIOUT.
  56. If REQUESTED_CONNECTIONS is not NULL, it's a list of GDB ids of the
  57. target connections that should be printed. Otherwise, all target
  58. connections are printed. */
  59. static void
  60. print_connection (struct ui_out *uiout, const char *requested_connections)
  61. {
  62. int count = 0;
  63. size_t what_len = 0;
  64. /* Compute number of lines we will print. */
  65. for (const auto &it : process_targets)
  66. {
  67. if (!number_is_in_list (requested_connections, it.first))
  68. continue;
  69. ++count;
  70. process_stratum_target *t = it.second;
  71. size_t l = strlen (t->shortname ());
  72. if (t->connection_string () != NULL)
  73. l += 1 + strlen (t->connection_string ());
  74. if (l > what_len)
  75. what_len = l;
  76. }
  77. if (count == 0)
  78. {
  79. uiout->message (_("No connections.\n"));
  80. return;
  81. }
  82. ui_out_emit_table table_emitter (uiout, 4, process_targets.size (),
  83. "connections");
  84. uiout->table_header (1, ui_left, "current", "");
  85. uiout->table_header (4, ui_left, "number", "Num");
  86. /* The text in the "what" column may include spaces. Add one extra
  87. space to visually separate the What and Description columns a
  88. little better. Compare:
  89. "* 1 remote :9999 Remote serial target in gdb-specific protocol"
  90. "* 1 remote :9999 Remote serial target in gdb-specific protocol"
  91. */
  92. uiout->table_header (what_len + 1, ui_left, "what", "What");
  93. uiout->table_header (17, ui_left, "description", "Description");
  94. uiout->table_body ();
  95. for (const auto &it : process_targets)
  96. {
  97. process_stratum_target *t = it.second;
  98. if (!number_is_in_list (requested_connections, t->connection_number))
  99. continue;
  100. ui_out_emit_tuple tuple_emitter (uiout, NULL);
  101. if (current_inferior ()->process_target () == t)
  102. uiout->field_string ("current", "*");
  103. else
  104. uiout->field_skip ("current");
  105. uiout->field_signed ("number", t->connection_number);
  106. uiout->field_string ("what", make_target_connection_string (t));
  107. uiout->field_string ("description", t->longname ());
  108. uiout->text ("\n");
  109. }
  110. }
  111. /* The "info connections" command. */
  112. static void
  113. info_connections_command (const char *args, int from_tty)
  114. {
  115. print_connection (current_uiout, args);
  116. }
  117. void _initialize_target_connection ();
  118. void
  119. _initialize_target_connection ()
  120. {
  121. add_info ("connections", info_connections_command,
  122. _("\
  123. Target connections in use.\n\
  124. Shows the list of target connections currently in use."));
  125. }