1 /* TID parsing for GDB, the GNU debugger. 2 3 Copyright (C) 2015-2019 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 parsing has completed. */ 118 bool finished () const; 119 120 /* Return the current token being parsed. When parsing has 121 finished, this points past the last parsed token. */ 122 const char *cur_tok () const; 123 124 /* When parsing a range, advance past the final token in the 125 range. */ 126 void skip_range (); 127 128 /* True if the TID last parsed was explicitly inferior-qualified. 129 IOW, whether the spec specified an inferior number 130 explicitly. */ 131 bool tid_is_qualified () const; 132 133 private: 134 /* No need for these. They are intentionally not defined anywhere. */ 135 tid_range_parser (const tid_range_parser &); 136 tid_range_parser &operator= (const tid_range_parser &); 137 138 bool get_tid_or_range (int *inf_num, int *thr_start, int *thr_end); 139 140 /* The possible states of the tid range parser's state machine, 141 indicating what sub-component are we expecting. */ 142 enum 143 { 144 /* Parsing the inferior number. */ 145 STATE_INFERIOR, 146 147 /* Parsing the thread number or thread number range. */ 148 STATE_THREAD_RANGE, 149 150 /* Parsing a star wildcard thread range. E.g., "1.*". */ 151 STATE_STAR_RANGE, 152 } m_state; 153 154 /* The string being parsed. When parsing has finished, this points 155 past the last parsed token. */ 156 const char *m_cur_tok; 157 158 /* The range parser state when we're parsing the thread number 159 sub-component. */ 160 number_or_range_parser m_range_parser; 161 162 /* Last inferior number returned. */ 163 int m_inf_num; 164 165 /* True if the TID last parsed was explicitly inferior-qualified. 166 IOW, whether the spec specified an inferior number 167 explicitly. */ 168 bool m_qualified; 169 170 /* The inferior number to assume if the TID is not qualified. */ 171 int m_default_inferior; 172 }; 173 174 175 /* Accept a string-form list of thread IDs such as is accepted by 176 tid_range_parser. Return true if the INF_NUM.THR.NUM thread is in 177 the list. DEFAULT_INFERIOR is the inferior number to assume if a 178 non-qualified thread ID is found in the list. 179 180 By definition, an empty list includes all threads. This is to be 181 interpreted as typing a command such as "info threads" with no 182 arguments. */ 183 extern int tid_is_in_list (const char *list, int default_inferior, 184 int inf_num, int thr_num); 185 186 #endif /* TID_PARSE_H */ 187