1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/* TID parsing for GDB, the GNU debugger.
Copyright (C) 2015-2017 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "tid-parse.h"
#include "inferior.h"
#include "gdbthread.h"
#include <ctype.h>
/* See tid-parse.h. */
void ATTRIBUTE_NORETURN
invalid_thread_id_error (const char *string)
{
error (_("Invalid thread ID: %s"), string);
}
/* Wrapper for get_number_trailer that throws an error if we get back
a negative number. We'll see a negative value if the number is
stored in a negative convenience variable (e.g., $minus_one = -1).
STRING is the parser string to be used in the error message if we
do get back a negative number. */
static int
get_positive_number_trailer (const char **pp, int trailer, const char *string)
{
int num;
num = get_number_trailer (pp, trailer);
if (num < 0)
error (_("negative value: %s"), string);
return num;
}
/* See tid-parse.h. */
struct thread_info *
parse_thread_id (const char *tidstr, const char **end)
{
const char *number = tidstr;
const char *dot, *p1;
struct thread_info *tp;
struct inferior *inf;
int thr_num;
int explicit_inf_id = 0;
dot = strchr (number, '.');
if (dot != NULL)
{
/* Parse number to the left of the dot. */
int inf_num;
p1 = number;
inf_num = get_positive_number_trailer (&p1, '.', number);
if (inf_num == 0)
invalid_thread_id_error (number);
inf = find_inferior_id (inf_num);
if (inf == NULL)
error (_("No inferior number '%d'"), inf_num);
explicit_inf_id = 1;
p1 = dot + 1;
}
else
{
inf = current_inferior ();
p1 = number;
}
thr_num = get_positive_number_trailer (&p1, 0, number);
if (thr_num == 0)
invalid_thread_id_error (number);
ALL_THREADS (tp)
{
if (ptid_get_pid (tp->ptid) == inf->pid
&& tp->per_inf_num == thr_num)
break;
}
if (tp == NULL)
{
if (show_inferior_qualified_tids () || explicit_inf_id)
error (_("Unknown thread %d.%d."), inf->num, thr_num);
else
error (_("Unknown thread %d."), thr_num);
}
if (end != NULL)
*end = p1;
return tp;
}
/* See tid-parse.h. */
void
tid_range_parser_init (struct tid_range_parser *parser, const char *tidlist,
int default_inferior)
{
parser->state = TID_RANGE_STATE_INFERIOR;
parser->string = tidlist;
parser->inf_num = 0;
parser->qualified = 0;
parser->default_inferior = default_inferior;
}
/* See tid-parse.h. */
int
tid_range_parser_finished (struct tid_range_parser *parser)
{
switch (parser->state)
{
case TID_RANGE_STATE_INFERIOR:
return *parser->string == '\0';
case TID_RANGE_STATE_THREAD_RANGE:
case TID_RANGE_STATE_STAR_RANGE:
return parser->range_parser.finished;
}
gdb_assert_not_reached (_("unhandled state"));
}
/* See tid-parse.h. */
const char *
tid_range_parser_string (struct tid_range_parser *parser)
{
switch (parser->state)
{
case TID_RANGE_STATE_INFERIOR:
return parser->string;
case TID_RANGE_STATE_THREAD_RANGE:
case TID_RANGE_STATE_STAR_RANGE:
return parser->range_parser.string;
}
gdb_assert_not_reached (_("unhandled state"));
}
/* See tid-parse.h. */
void
tid_range_parser_skip (struct tid_range_parser *parser)
{
gdb_assert ((parser->state == TID_RANGE_STATE_THREAD_RANGE
|| parser->state == TID_RANGE_STATE_STAR_RANGE)
&& parser->range_parser.in_range);
tid_range_parser_init (parser, parser->range_parser.end_ptr,
parser->default_inferior);
}
/* See tid-parse.h. */
int
tid_range_parser_qualified (struct tid_range_parser *parser)
{
return parser->qualified;
}
/* Helper for tid_range_parser_get_tid and
tid_range_parser_get_tid_range. Return the next range if THR_END
is non-NULL, return a single thread ID otherwise. */
static int
get_tid_or_range (struct tid_range_parser *parser, int *inf_num,
int *thr_start, int *thr_end)
{
if (parser->state == TID_RANGE_STATE_INFERIOR)
{
const char *p;
const char *space;
space = skip_to_space (parser->string);
p = parser->string;
while (p < space && *p != '.')
p++;
if (p < space)
{
const char *dot = p;
/* Parse number to the left of the dot. */
p = parser->string;
parser->inf_num
= get_positive_number_trailer (&p, '.', parser->string);
if (parser->inf_num == 0)
return 0;
parser->qualified = 1;
p = dot + 1;
if (isspace (*p))
return 0;
}
else
{
parser->inf_num = parser->default_inferior;
parser->qualified = 0;
p = parser->string;
}
init_number_or_range (&parser->range_parser, p);
if (p[0] == '*' && (p[1] == '\0' || isspace (p[1])))
{
/* Setup the number range parser to return numbers in the
whole [1,INT_MAX] range. */
number_range_setup_range (&parser->range_parser, 1, INT_MAX,
skip_spaces_const (p + 1));
parser->state = TID_RANGE_STATE_STAR_RANGE;
}
else
parser->state = TID_RANGE_STATE_THREAD_RANGE;
}
*inf_num = parser->inf_num;
*thr_start = get_number_or_range (&parser->range_parser);
if (*thr_start < 0)
error (_("negative value: %s"), parser->string);
if (*thr_start == 0)
{
parser->state = TID_RANGE_STATE_INFERIOR;
return 0;
}
/* If we successfully parsed a thread number or finished parsing a
thread range, switch back to assuming the next TID is
inferior-qualified. */
if (parser->range_parser.end_ptr == NULL
|| parser->range_parser.string == parser->range_parser.end_ptr)
{
parser->state = TID_RANGE_STATE_INFERIOR;
parser->string = parser->range_parser.string;
if (thr_end != NULL)
*thr_end = *thr_start;
}
/* If we're midway through a range, and the caller wants the end
value, return it and skip to the end of the range. */
if (thr_end != NULL
&& (parser->state == TID_RANGE_STATE_THREAD_RANGE
|| parser->state == TID_RANGE_STATE_STAR_RANGE))
{
*thr_end = parser->range_parser.end_value;
tid_range_parser_skip (parser);
}
return (*inf_num != 0 && *thr_start != 0);
}
/* See tid-parse.h. */
int
tid_range_parser_get_tid_range (struct tid_range_parser *parser, int *inf_num,
int *thr_start, int *thr_end)
{
gdb_assert (inf_num != NULL && thr_start != NULL && thr_end != NULL);
return get_tid_or_range (parser, inf_num, thr_start, thr_end);
}
/* See tid-parse.h. */
int
tid_range_parser_get_tid (struct tid_range_parser *parser,
int *inf_num, int *thr_num)
{
gdb_assert (inf_num != NULL && thr_num != NULL);
return get_tid_or_range (parser, inf_num, thr_num, NULL);
}
/* See tid-parse.h. */
int
tid_range_parser_star_range (struct tid_range_parser *parser)
{
return parser->state == TID_RANGE_STATE_STAR_RANGE;
}
/* See gdbthread.h. */
int
tid_is_in_list (const char *list, int default_inferior,
int inf_num, int thr_num)
{
struct tid_range_parser parser;
if (list == NULL || *list == '\0')
return 1;
tid_range_parser_init (&parser, list, default_inferior);
while (!tid_range_parser_finished (&parser))
{
int tmp_inf, tmp_thr_start, tmp_thr_end;
if (!tid_range_parser_get_tid_range (&parser, &tmp_inf,
&tmp_thr_start, &tmp_thr_end))
invalid_thread_id_error (parser.string);
if (tmp_inf == inf_num
&& tmp_thr_start <= thr_num && thr_num <= tmp_thr_end)
return 1;
}
return 0;
}
|