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