1 /* TID parsing for GDB, the GNU debugger.
3 Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "tid-parse.h"
23 #include "gdbthread.h"
26 /* See tid-parse.h. */
28 void ATTRIBUTE_NORETURN
29 invalid_thread_id_error (const char *string)
31 error (_("Invalid thread ID: %s"), string);
34 /* See tid-parse.h. */
37 parse_thread_id (const char *tidstr, const char **end)
39 const char *number = tidstr;
41 struct thread_info *tp;
44 int explicit_inf_id = 0;
46 dot = strchr (number, '.');
50 /* Parse number to the left of the dot. */
54 inf_num = get_number_trailer (&p1, '.');
56 invalid_thread_id_error (number);
58 inf = find_inferior_id (inf_num);
60 error (_("No inferior number '%d'"), inf_num);
67 inf = current_inferior ();
72 thr_num = get_number_const (&p1);
74 invalid_thread_id_error (number);
78 if (ptid_get_pid (tp->ptid) == inf->pid
79 && tp->per_inf_num == thr_num)
85 if (show_inferior_qualified_tids () || explicit_inf_id)
86 error (_("Unknown thread %d.%d."), inf->num, thr_num);
88 error (_("Unknown thread %d."), thr_num);
97 /* See tid-parse.h. */
100 tid_range_parser_init (struct tid_range_parser *parser, const char *tidlist,
101 int default_inferior)
103 parser->state = TID_RANGE_STATE_INFERIOR;
104 parser->string = tidlist;
106 parser->qualified = 0;
107 parser->default_inferior = default_inferior;
110 /* See tid-parse.h. */
113 tid_range_parser_finished (struct tid_range_parser *parser)
115 switch (parser->state)
117 case TID_RANGE_STATE_INFERIOR:
118 return *parser->string == '\0';
119 case TID_RANGE_STATE_THREAD_RANGE:
120 return parser->range_parser.finished;
123 gdb_assert_not_reached (_("unhandled state"));
126 /* See tid-parse.h. */
129 tid_range_parser_string (struct tid_range_parser *parser)
131 switch (parser->state)
133 case TID_RANGE_STATE_INFERIOR:
134 return parser->string;
135 case TID_RANGE_STATE_THREAD_RANGE:
136 return parser->range_parser.string;
139 gdb_assert_not_reached (_("unhandled state"));
142 /* See tid-parse.h. */
145 tid_range_parser_skip (struct tid_range_parser *parser)
147 gdb_assert ((parser->state == TID_RANGE_STATE_THREAD_RANGE)
148 && parser->range_parser.in_range);
150 tid_range_parser_init (parser, parser->range_parser.end_ptr,
151 parser->default_inferior);
154 /* See tid-parse.h. */
157 tid_range_parser_qualified (struct tid_range_parser *parser)
159 return parser->qualified;
162 /* Helper for tid_range_parser_get_tid and
163 tid_range_parser_get_tid_range. Return the next range if THR_END
164 is non-NULL, return a single thread ID otherwise. */
167 get_tid_or_range (struct tid_range_parser *parser, int *inf_num,
168 int *thr_start, int *thr_end)
170 if (parser->state == TID_RANGE_STATE_INFERIOR)
175 space = skip_to_space (parser->string);
178 while (p < space && *p != '.')
184 /* Parse number to the left of the dot. */
186 parser->inf_num = get_number_trailer (&p, '.');
187 if (parser->inf_num == 0)
188 invalid_thread_id_error (parser->string);
190 parser->qualified = 1;
194 invalid_thread_id_error (parser->string);
198 parser->inf_num = parser->default_inferior;
199 parser->qualified = 0;
203 init_number_or_range (&parser->range_parser, p);
204 parser->state = TID_RANGE_STATE_THREAD_RANGE;
207 *inf_num = parser->inf_num;
208 *thr_start = get_number_or_range (&parser->range_parser);
210 invalid_thread_id_error (parser->string);
212 /* If we successfully parsed a thread number or finished parsing a
213 thread range, switch back to assuming the next TID is
214 inferior-qualified. */
215 if (parser->range_parser.end_ptr == NULL
216 || parser->range_parser.string == parser->range_parser.end_ptr)
218 parser->state = TID_RANGE_STATE_INFERIOR;
219 parser->string = parser->range_parser.string;
222 *thr_end = *thr_start;
225 /* If we're midway through a range, and the caller wants the end
226 value, return it and skip to the end of the range. */
227 if (thr_end != NULL && parser->state == TID_RANGE_STATE_THREAD_RANGE)
229 *thr_end = parser->range_parser.end_value;
230 tid_range_parser_skip (parser);
233 return (*inf_num != 0 && *thr_start != 0);
236 /* See tid-parse.h. */
239 tid_range_parser_get_tid_range (struct tid_range_parser *parser, int *inf_num,
240 int *thr_start, int *thr_end)
242 gdb_assert (inf_num != NULL && thr_start != NULL && thr_end != NULL);
244 return get_tid_or_range (parser, inf_num, thr_start, thr_end);
247 /* See tid-parse.h. */
250 tid_range_parser_get_tid (struct tid_range_parser *parser,
251 int *inf_num, int *thr_num)
253 gdb_assert (inf_num != NULL && thr_num != NULL);
255 return get_tid_or_range (parser, inf_num, thr_num, NULL);
258 /* See tid-parse.h. */
261 tid_is_in_list (const char *list, int default_inferior,
262 int inf_num, int thr_num)
264 struct tid_range_parser parser;
266 if (list == NULL || *list == '\0')
269 tid_range_parser_init (&parser, list, default_inferior);
270 while (!tid_range_parser_finished (&parser))
272 int tmp_inf, tmp_thr_start, tmp_thr_end;
274 if (!tid_range_parser_get_tid_range (&parser, &tmp_inf,
275 &tmp_thr_start, &tmp_thr_end))
276 invalid_thread_id_error (parser.string);
277 if (tmp_inf == inf_num
278 && tmp_thr_start <= thr_num && thr_num <= tmp_thr_end)