tid-parse.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* TID parsing for GDB, the GNU debugger.
  2. Copyright (C) 2015-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. #ifndef TID_PARSE_H
  15. #define TID_PARSE_H
  16. #include "cli/cli-utils.h"
  17. struct thread_info;
  18. /* Issue an invalid thread ID error, pointing at STRING, the invalid
  19. ID. */
  20. extern void ATTRIBUTE_NORETURN invalid_thread_id_error (const char *string);
  21. /* Parse TIDSTR as a per-inferior thread ID, in either INF_NUM.THR_NUM
  22. or THR_NUM form. In the latter case, the missing INF_NUM is filled
  23. in from the current inferior. If ENDPTR is not NULL,
  24. parse_thread_id stores the address of the first character after the
  25. thread ID. Either a valid thread is returned, or an error is
  26. thrown. */
  27. struct thread_info *parse_thread_id (const char *tidstr, const char **end);
  28. /* Parse a thread ID or a thread range list.
  29. A range will be of the form
  30. <inferior_num>.<thread_number1>-<thread_number2>
  31. and will represent all the threads of inferior INFERIOR_NUM with
  32. number between THREAD_NUMBER1 and THREAD_NUMBER2, inclusive.
  33. <inferior_num> can also be omitted, as in
  34. <thread_number1>-<thread_number2>
  35. in which case GDB infers the inferior number from the default
  36. passed to the constructor or to the last call to the init
  37. function. */
  38. class tid_range_parser
  39. {
  40. public:
  41. /* Default construction. Must call init before calling get_*. */
  42. tid_range_parser () {}
  43. /* Calls init automatically. See init for description of
  44. parameters. */
  45. tid_range_parser (const char *tidlist, int default_inferior);
  46. /* Reinitialize a tid_range_parser. TIDLIST is the string to be
  47. parsed. DEFAULT_INFERIOR is the inferior number to assume if a
  48. non-qualified thread ID is found. */
  49. void init (const char *tidlist, int default_inferior);
  50. /* Parse a thread ID or a thread range list.
  51. This function is designed to be called iteratively. While
  52. processing a thread ID range list, at each call it will return
  53. (in the INF_NUM and THR_NUM output parameters) the next thread ID
  54. in the range (irrespective of whether the thread actually
  55. exists).
  56. At the beginning of parsing a thread range, the char pointer
  57. PARSER->m_cur_tok will be advanced past <thread_number1> and left
  58. pointing at the '-' token. Subsequent calls will not advance the
  59. pointer until the range is completed. The call that completes
  60. the range will advance the pointer past <thread_number2>.
  61. This function advances through the input string for as long you
  62. call it. Once the end of the input string is reached, a call to
  63. finished returns false (see below).
  64. E.g., with list: "1.2 3.4-6":
  65. 1st call: *INF_NUM=1; *THR_NUM=2 (finished==0)
  66. 2nd call: *INF_NUM=3; *THR_NUM=4 (finished==0)
  67. 3rd call: *INF_NUM=3; *THR_NUM=5 (finished==0)
  68. 4th call: *INF_NUM=3; *THR_NUM=6 (finished==1)
  69. Returns true if a thread/range is parsed successfully, false
  70. otherwise. */
  71. bool get_tid (int *inf_num, int *thr_num);
  72. /* Like get_tid, but return a thread ID range per call, rather then
  73. a single thread ID.
  74. If the next element in the list is a single thread ID, then
  75. *THR_START and *THR_END are set to the same value.
  76. E.g.,. with list: "1.2 3.4-6"
  77. 1st call: *INF_NUM=1; *THR_START=2; *THR_END=2 (finished==0)
  78. 2nd call: *INF_NUM=3; *THR_START=4; *THR_END=6 (finished==1)
  79. Returns true if parsed a thread/range successfully, false
  80. otherwise. */
  81. bool get_tid_range (int *inf_num, int *thr_start, int *thr_end);
  82. /* Returns true if processing a star wildcard (e.g., "1.*")
  83. range. */
  84. bool in_star_range () const;
  85. /* Returns true if processing a thread range (e.g., 1.2-3). */
  86. bool in_thread_range () const;
  87. /* Returns true if parsing has completed. */
  88. bool finished () const;
  89. /* Return the current token being parsed. When parsing has
  90. finished, this points past the last parsed token. */
  91. const char *cur_tok () const;
  92. /* When parsing a range, advance past the final token in the
  93. range. */
  94. void skip_range ();
  95. /* True if the TID last parsed was explicitly inferior-qualified.
  96. IOW, whether the spec specified an inferior number
  97. explicitly. */
  98. bool tid_is_qualified () const;
  99. private:
  100. /* No need for these. They are intentionally not defined anywhere. */
  101. tid_range_parser (const tid_range_parser &);
  102. tid_range_parser &operator= (const tid_range_parser &);
  103. bool get_tid_or_range (int *inf_num, int *thr_start, int *thr_end);
  104. /* The possible states of the tid range parser's state machine,
  105. indicating what sub-component are we expecting. */
  106. enum
  107. {
  108. /* Parsing the inferior number. */
  109. STATE_INFERIOR,
  110. /* Parsing the thread number or thread number range. */
  111. STATE_THREAD_RANGE,
  112. /* Parsing a star wildcard thread range. E.g., "1.*". */
  113. STATE_STAR_RANGE,
  114. } m_state;
  115. /* The string being parsed. When parsing has finished, this points
  116. past the last parsed token. */
  117. const char *m_cur_tok;
  118. /* The range parser state when we're parsing the thread number
  119. sub-component. */
  120. number_or_range_parser m_range_parser;
  121. /* Last inferior number returned. */
  122. int m_inf_num;
  123. /* True if the TID last parsed was explicitly inferior-qualified.
  124. IOW, whether the spec specified an inferior number
  125. explicitly. */
  126. bool m_qualified;
  127. /* The inferior number to assume if the TID is not qualified. */
  128. int m_default_inferior;
  129. };
  130. /* Accept a string-form list of thread IDs such as is accepted by
  131. tid_range_parser. Return true if the INF_NUM.THR.NUM thread is in
  132. the list. DEFAULT_INFERIOR is the inferior number to assume if a
  133. non-qualified thread ID is found in the list.
  134. By definition, an empty list includes all threads. This is to be
  135. interpreted as typing a command such as "info threads" with no
  136. arguments. */
  137. extern int tid_is_in_list (const char *list, int default_inferior,
  138. int inf_num, int thr_num);
  139. #endif /* TID_PARSE_H */