Commit | Line | Data |
---|---|---|
02d27625 MM |
1 | /* Branch trace support for GDB, the GNU debugger. |
2 | ||
61baf725 | 3 | Copyright (C) 2013-2017 Free Software Foundation, Inc. |
02d27625 MM |
4 | |
5 | Contributed by Intel Corp. <markus.t.metzger@intel.com> | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
21 | ||
d41f6d8e | 22 | #include "defs.h" |
02d27625 MM |
23 | #include "btrace.h" |
24 | #include "gdbthread.h" | |
02d27625 MM |
25 | #include "inferior.h" |
26 | #include "target.h" | |
27 | #include "record.h" | |
28 | #include "symtab.h" | |
29 | #include "disasm.h" | |
30 | #include "source.h" | |
31 | #include "filenames.h" | |
c12a2917 | 32 | #include "xml-support.h" |
6e07b1d2 | 33 | #include "regcache.h" |
b20a6524 | 34 | #include "rsp-low.h" |
b0627500 MM |
35 | #include "gdbcmd.h" |
36 | #include "cli/cli-utils.h" | |
b20a6524 MM |
37 | |
38 | #include <inttypes.h> | |
b0627500 | 39 | #include <ctype.h> |
325fac50 | 40 | #include <algorithm> |
b0627500 MM |
41 | |
42 | /* Command lists for btrace maintenance commands. */ | |
43 | static struct cmd_list_element *maint_btrace_cmdlist; | |
44 | static struct cmd_list_element *maint_btrace_set_cmdlist; | |
45 | static struct cmd_list_element *maint_btrace_show_cmdlist; | |
46 | static struct cmd_list_element *maint_btrace_pt_set_cmdlist; | |
47 | static struct cmd_list_element *maint_btrace_pt_show_cmdlist; | |
48 | ||
49 | /* Control whether to skip PAD packets when computing the packet history. */ | |
50 | static int maint_btrace_pt_skip_pad = 1; | |
b20a6524 | 51 | |
d87fdac3 MM |
52 | /* A vector of function segments. */ |
53 | typedef struct btrace_function * bfun_s; | |
54 | DEF_VEC_P (bfun_s); | |
55 | ||
b20a6524 | 56 | static void btrace_add_pc (struct thread_info *tp); |
02d27625 MM |
57 | |
58 | /* Print a record debug message. Use do ... while (0) to avoid ambiguities | |
59 | when used in if statements. */ | |
60 | ||
61 | #define DEBUG(msg, args...) \ | |
62 | do \ | |
63 | { \ | |
64 | if (record_debug != 0) \ | |
65 | fprintf_unfiltered (gdb_stdlog, \ | |
66 | "[btrace] " msg "\n", ##args); \ | |
67 | } \ | |
68 | while (0) | |
69 | ||
70 | #define DEBUG_FTRACE(msg, args...) DEBUG ("[ftrace] " msg, ##args) | |
71 | ||
02d27625 MM |
72 | /* Return the function name of a recorded function segment for printing. |
73 | This function never returns NULL. */ | |
74 | ||
75 | static const char * | |
23a7fe75 | 76 | ftrace_print_function_name (const struct btrace_function *bfun) |
02d27625 MM |
77 | { |
78 | struct minimal_symbol *msym; | |
79 | struct symbol *sym; | |
80 | ||
81 | msym = bfun->msym; | |
82 | sym = bfun->sym; | |
83 | ||
84 | if (sym != NULL) | |
85 | return SYMBOL_PRINT_NAME (sym); | |
86 | ||
87 | if (msym != NULL) | |
efd66ac6 | 88 | return MSYMBOL_PRINT_NAME (msym); |
02d27625 MM |
89 | |
90 | return "<unknown>"; | |
91 | } | |
92 | ||
93 | /* Return the file name of a recorded function segment for printing. | |
94 | This function never returns NULL. */ | |
95 | ||
96 | static const char * | |
23a7fe75 | 97 | ftrace_print_filename (const struct btrace_function *bfun) |
02d27625 MM |
98 | { |
99 | struct symbol *sym; | |
100 | const char *filename; | |
101 | ||
102 | sym = bfun->sym; | |
103 | ||
104 | if (sym != NULL) | |
08be3fe3 | 105 | filename = symtab_to_filename_for_display (symbol_symtab (sym)); |
02d27625 MM |
106 | else |
107 | filename = "<unknown>"; | |
108 | ||
109 | return filename; | |
110 | } | |
111 | ||
23a7fe75 MM |
112 | /* Return a string representation of the address of an instruction. |
113 | This function never returns NULL. */ | |
02d27625 | 114 | |
23a7fe75 MM |
115 | static const char * |
116 | ftrace_print_insn_addr (const struct btrace_insn *insn) | |
02d27625 | 117 | { |
23a7fe75 MM |
118 | if (insn == NULL) |
119 | return "<nil>"; | |
120 | ||
121 | return core_addr_to_string_nz (insn->pc); | |
02d27625 MM |
122 | } |
123 | ||
23a7fe75 | 124 | /* Print an ftrace debug status message. */ |
02d27625 MM |
125 | |
126 | static void | |
23a7fe75 | 127 | ftrace_debug (const struct btrace_function *bfun, const char *prefix) |
02d27625 | 128 | { |
23a7fe75 MM |
129 | const char *fun, *file; |
130 | unsigned int ibegin, iend; | |
ce0dfbea | 131 | int level; |
23a7fe75 MM |
132 | |
133 | fun = ftrace_print_function_name (bfun); | |
134 | file = ftrace_print_filename (bfun); | |
135 | level = bfun->level; | |
136 | ||
23a7fe75 MM |
137 | ibegin = bfun->insn_offset; |
138 | iend = ibegin + VEC_length (btrace_insn_s, bfun->insn); | |
139 | ||
ce0dfbea MM |
140 | DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, insn = [%u; %u)", |
141 | prefix, fun, file, level, ibegin, iend); | |
02d27625 MM |
142 | } |
143 | ||
69090cee TW |
144 | /* Return the number of instructions in a given function call segment. */ |
145 | ||
146 | static unsigned int | |
147 | ftrace_call_num_insn (const struct btrace_function* bfun) | |
148 | { | |
149 | if (bfun == NULL) | |
150 | return 0; | |
151 | ||
152 | /* A gap is always counted as one instruction. */ | |
153 | if (bfun->errcode != 0) | |
154 | return 1; | |
155 | ||
156 | return VEC_length (btrace_insn_s, bfun->insn); | |
157 | } | |
158 | ||
42bfe59e TW |
159 | /* Return the function segment with the given NUMBER or NULL if no such segment |
160 | exists. BTINFO is the branch trace information for the current thread. */ | |
161 | ||
162 | static struct btrace_function * | |
163 | ftrace_find_call_by_number (const struct btrace_thread_info *btinfo, | |
164 | unsigned int number) | |
165 | { | |
166 | if (number == 0 || number > btinfo->functions.size ()) | |
167 | return NULL; | |
168 | ||
169 | return btinfo->functions[number - 1]; | |
170 | } | |
171 | ||
23a7fe75 MM |
172 | /* Return non-zero if BFUN does not match MFUN and FUN, |
173 | return zero otherwise. */ | |
02d27625 MM |
174 | |
175 | static int | |
23a7fe75 MM |
176 | ftrace_function_switched (const struct btrace_function *bfun, |
177 | const struct minimal_symbol *mfun, | |
178 | const struct symbol *fun) | |
02d27625 MM |
179 | { |
180 | struct minimal_symbol *msym; | |
181 | struct symbol *sym; | |
182 | ||
02d27625 MM |
183 | msym = bfun->msym; |
184 | sym = bfun->sym; | |
185 | ||
186 | /* If the minimal symbol changed, we certainly switched functions. */ | |
187 | if (mfun != NULL && msym != NULL | |
efd66ac6 | 188 | && strcmp (MSYMBOL_LINKAGE_NAME (mfun), MSYMBOL_LINKAGE_NAME (msym)) != 0) |
02d27625 MM |
189 | return 1; |
190 | ||
191 | /* If the symbol changed, we certainly switched functions. */ | |
192 | if (fun != NULL && sym != NULL) | |
193 | { | |
194 | const char *bfname, *fname; | |
195 | ||
196 | /* Check the function name. */ | |
197 | if (strcmp (SYMBOL_LINKAGE_NAME (fun), SYMBOL_LINKAGE_NAME (sym)) != 0) | |
198 | return 1; | |
199 | ||
200 | /* Check the location of those functions, as well. */ | |
08be3fe3 DE |
201 | bfname = symtab_to_fullname (symbol_symtab (sym)); |
202 | fname = symtab_to_fullname (symbol_symtab (fun)); | |
02d27625 MM |
203 | if (filename_cmp (fname, bfname) != 0) |
204 | return 1; | |
205 | } | |
206 | ||
23a7fe75 MM |
207 | /* If we lost symbol information, we switched functions. */ |
208 | if (!(msym == NULL && sym == NULL) && mfun == NULL && fun == NULL) | |
209 | return 1; | |
210 | ||
211 | /* If we gained symbol information, we switched functions. */ | |
212 | if (msym == NULL && sym == NULL && !(mfun == NULL && fun == NULL)) | |
213 | return 1; | |
214 | ||
02d27625 MM |
215 | return 0; |
216 | } | |
217 | ||
8286623c TW |
218 | /* Allocate and initialize a new branch trace function segment at the end of |
219 | the trace. | |
17b89b34 | 220 | BTINFO is the branch trace information for the current thread. |
23a7fe75 MM |
221 | MFUN and FUN are the symbol information we have for this function. */ |
222 | ||
223 | static struct btrace_function * | |
17b89b34 | 224 | ftrace_new_function (struct btrace_thread_info *btinfo, |
23a7fe75 MM |
225 | struct minimal_symbol *mfun, |
226 | struct symbol *fun) | |
227 | { | |
b54b03bd | 228 | struct btrace_function *bfun; |
23a7fe75 | 229 | |
8d749320 | 230 | bfun = XCNEW (struct btrace_function); |
23a7fe75 MM |
231 | |
232 | bfun->msym = mfun; | |
233 | bfun->sym = fun; | |
23a7fe75 | 234 | |
b54b03bd | 235 | if (btinfo->functions.empty ()) |
5de9129b MM |
236 | { |
237 | /* Start counting at one. */ | |
238 | bfun->number = 1; | |
239 | bfun->insn_offset = 1; | |
240 | } | |
241 | else | |
23a7fe75 | 242 | { |
b54b03bd TW |
243 | struct btrace_function *prev = btinfo->functions.back (); |
244 | ||
23a7fe75 MM |
245 | gdb_assert (prev->flow.next == NULL); |
246 | prev->flow.next = bfun; | |
b54b03bd | 247 | bfun->flow.prev = prev; |
02d27625 | 248 | |
23a7fe75 | 249 | bfun->number = prev->number + 1; |
69090cee | 250 | bfun->insn_offset = prev->insn_offset + ftrace_call_num_insn (prev); |
31fd9caa | 251 | bfun->level = prev->level; |
23a7fe75 MM |
252 | } |
253 | ||
17b89b34 | 254 | btinfo->functions.push_back (bfun); |
23a7fe75 | 255 | return bfun; |
02d27625 MM |
256 | } |
257 | ||
23a7fe75 | 258 | /* Update the UP field of a function segment. */ |
02d27625 | 259 | |
23a7fe75 MM |
260 | static void |
261 | ftrace_update_caller (struct btrace_function *bfun, | |
262 | struct btrace_function *caller, | |
263 | enum btrace_function_flag flags) | |
02d27625 | 264 | { |
42bfe59e | 265 | if (bfun->up != 0) |
23a7fe75 | 266 | ftrace_debug (bfun, "updating caller"); |
02d27625 | 267 | |
42bfe59e | 268 | bfun->up = caller->number; |
23a7fe75 MM |
269 | bfun->flags = flags; |
270 | ||
271 | ftrace_debug (bfun, "set caller"); | |
d87fdac3 | 272 | ftrace_debug (caller, "..to"); |
23a7fe75 MM |
273 | } |
274 | ||
275 | /* Fix up the caller for all segments of a function. */ | |
276 | ||
277 | static void | |
278 | ftrace_fixup_caller (struct btrace_function *bfun, | |
279 | struct btrace_function *caller, | |
280 | enum btrace_function_flag flags) | |
281 | { | |
282 | struct btrace_function *prev, *next; | |
283 | ||
284 | ftrace_update_caller (bfun, caller, flags); | |
285 | ||
286 | /* Update all function segments belonging to the same function. */ | |
287 | for (prev = bfun->segment.prev; prev != NULL; prev = prev->segment.prev) | |
288 | ftrace_update_caller (prev, caller, flags); | |
289 | ||
290 | for (next = bfun->segment.next; next != NULL; next = next->segment.next) | |
291 | ftrace_update_caller (next, caller, flags); | |
292 | } | |
293 | ||
8286623c | 294 | /* Add a new function segment for a call at the end of the trace. |
17b89b34 | 295 | BTINFO is the branch trace information for the current thread. |
23a7fe75 MM |
296 | MFUN and FUN are the symbol information we have for this function. */ |
297 | ||
298 | static struct btrace_function * | |
17b89b34 | 299 | ftrace_new_call (struct btrace_thread_info *btinfo, |
23a7fe75 MM |
300 | struct minimal_symbol *mfun, |
301 | struct symbol *fun) | |
302 | { | |
b54b03bd | 303 | const unsigned int length = btinfo->functions.size (); |
8286623c | 304 | struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun); |
23a7fe75 | 305 | |
42bfe59e | 306 | bfun->up = length; |
31fd9caa | 307 | bfun->level += 1; |
23a7fe75 MM |
308 | |
309 | ftrace_debug (bfun, "new call"); | |
310 | ||
311 | return bfun; | |
312 | } | |
313 | ||
8286623c | 314 | /* Add a new function segment for a tail call at the end of the trace. |
17b89b34 | 315 | BTINFO is the branch trace information for the current thread. |
23a7fe75 MM |
316 | MFUN and FUN are the symbol information we have for this function. */ |
317 | ||
318 | static struct btrace_function * | |
17b89b34 | 319 | ftrace_new_tailcall (struct btrace_thread_info *btinfo, |
23a7fe75 MM |
320 | struct minimal_symbol *mfun, |
321 | struct symbol *fun) | |
322 | { | |
b54b03bd | 323 | const unsigned int length = btinfo->functions.size (); |
8286623c | 324 | struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun); |
02d27625 | 325 | |
42bfe59e | 326 | bfun->up = length; |
31fd9caa | 327 | bfun->level += 1; |
23a7fe75 | 328 | bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL; |
02d27625 | 329 | |
23a7fe75 MM |
330 | ftrace_debug (bfun, "new tail call"); |
331 | ||
332 | return bfun; | |
333 | } | |
334 | ||
d87fdac3 | 335 | /* Return the caller of BFUN or NULL if there is none. This function skips |
42bfe59e TW |
336 | tail calls in the call chain. BTINFO is the branch trace information for |
337 | the current thread. */ | |
d87fdac3 | 338 | static struct btrace_function * |
42bfe59e TW |
339 | ftrace_get_caller (struct btrace_thread_info *btinfo, |
340 | struct btrace_function *bfun) | |
d87fdac3 | 341 | { |
42bfe59e | 342 | for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up)) |
d87fdac3 | 343 | if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0) |
42bfe59e | 344 | return ftrace_find_call_by_number (btinfo, bfun->up); |
d87fdac3 MM |
345 | |
346 | return NULL; | |
347 | } | |
348 | ||
23a7fe75 | 349 | /* Find the innermost caller in the back trace of BFUN with MFUN/FUN |
42bfe59e TW |
350 | symbol information. BTINFO is the branch trace information for the current |
351 | thread. */ | |
23a7fe75 MM |
352 | |
353 | static struct btrace_function * | |
42bfe59e TW |
354 | ftrace_find_caller (struct btrace_thread_info *btinfo, |
355 | struct btrace_function *bfun, | |
23a7fe75 MM |
356 | struct minimal_symbol *mfun, |
357 | struct symbol *fun) | |
358 | { | |
42bfe59e | 359 | for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up)) |
23a7fe75 MM |
360 | { |
361 | /* Skip functions with incompatible symbol information. */ | |
362 | if (ftrace_function_switched (bfun, mfun, fun)) | |
363 | continue; | |
364 | ||
365 | /* This is the function segment we're looking for. */ | |
366 | break; | |
367 | } | |
368 | ||
369 | return bfun; | |
370 | } | |
371 | ||
372 | /* Find the innermost caller in the back trace of BFUN, skipping all | |
373 | function segments that do not end with a call instruction (e.g. | |
42bfe59e TW |
374 | tail calls ending with a jump). BTINFO is the branch trace information for |
375 | the current thread. */ | |
23a7fe75 MM |
376 | |
377 | static struct btrace_function * | |
42bfe59e TW |
378 | ftrace_find_call (struct btrace_thread_info *btinfo, |
379 | struct btrace_function *bfun) | |
23a7fe75 | 380 | { |
42bfe59e | 381 | for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up)) |
02d27625 | 382 | { |
23a7fe75 | 383 | struct btrace_insn *last; |
02d27625 | 384 | |
31fd9caa MM |
385 | /* Skip gaps. */ |
386 | if (bfun->errcode != 0) | |
387 | continue; | |
23a7fe75 MM |
388 | |
389 | last = VEC_last (btrace_insn_s, bfun->insn); | |
02d27625 | 390 | |
7d5c24b3 | 391 | if (last->iclass == BTRACE_INSN_CALL) |
23a7fe75 MM |
392 | break; |
393 | } | |
394 | ||
395 | return bfun; | |
396 | } | |
397 | ||
8286623c TW |
398 | /* Add a continuation segment for a function into which we return at the end of |
399 | the trace. | |
17b89b34 | 400 | BTINFO is the branch trace information for the current thread. |
23a7fe75 MM |
401 | MFUN and FUN are the symbol information we have for this function. */ |
402 | ||
403 | static struct btrace_function * | |
17b89b34 | 404 | ftrace_new_return (struct btrace_thread_info *btinfo, |
23a7fe75 MM |
405 | struct minimal_symbol *mfun, |
406 | struct symbol *fun) | |
407 | { | |
b54b03bd | 408 | struct btrace_function *prev = btinfo->functions.back (); |
23a7fe75 MM |
409 | struct btrace_function *bfun, *caller; |
410 | ||
8286623c | 411 | bfun = ftrace_new_function (btinfo, mfun, fun); |
23a7fe75 MM |
412 | |
413 | /* It is important to start at PREV's caller. Otherwise, we might find | |
414 | PREV itself, if PREV is a recursive function. */ | |
42bfe59e TW |
415 | caller = ftrace_find_call_by_number (btinfo, prev->up); |
416 | caller = ftrace_find_caller (btinfo, caller, mfun, fun); | |
23a7fe75 MM |
417 | if (caller != NULL) |
418 | { | |
419 | /* The caller of PREV is the preceding btrace function segment in this | |
420 | function instance. */ | |
421 | gdb_assert (caller->segment.next == NULL); | |
422 | ||
423 | caller->segment.next = bfun; | |
424 | bfun->segment.prev = caller; | |
425 | ||
426 | /* Maintain the function level. */ | |
427 | bfun->level = caller->level; | |
428 | ||
429 | /* Maintain the call stack. */ | |
430 | bfun->up = caller->up; | |
431 | bfun->flags = caller->flags; | |
432 | ||
433 | ftrace_debug (bfun, "new return"); | |
434 | } | |
435 | else | |
436 | { | |
437 | /* We did not find a caller. This could mean that something went | |
438 | wrong or that the call is simply not included in the trace. */ | |
02d27625 | 439 | |
23a7fe75 | 440 | /* Let's search for some actual call. */ |
42bfe59e TW |
441 | caller = ftrace_find_call_by_number (btinfo, prev->up); |
442 | caller = ftrace_find_call (btinfo, caller); | |
23a7fe75 | 443 | if (caller == NULL) |
02d27625 | 444 | { |
23a7fe75 MM |
445 | /* There is no call in PREV's back trace. We assume that the |
446 | branch trace did not include it. */ | |
447 | ||
259ba1e8 MM |
448 | /* Let's find the topmost function and add a new caller for it. |
449 | This should handle a series of initial tail calls. */ | |
42bfe59e TW |
450 | while (prev->up != 0) |
451 | prev = ftrace_find_call_by_number (btinfo, prev->up); | |
02d27625 | 452 | |
259ba1e8 | 453 | bfun->level = prev->level - 1; |
23a7fe75 MM |
454 | |
455 | /* Fix up the call stack for PREV. */ | |
456 | ftrace_fixup_caller (prev, bfun, BFUN_UP_LINKS_TO_RET); | |
457 | ||
458 | ftrace_debug (bfun, "new return - no caller"); | |
459 | } | |
460 | else | |
02d27625 | 461 | { |
23a7fe75 | 462 | /* There is a call in PREV's back trace to which we should have |
259ba1e8 MM |
463 | returned but didn't. Let's start a new, separate back trace |
464 | from PREV's level. */ | |
465 | bfun->level = prev->level - 1; | |
466 | ||
467 | /* We fix up the back trace for PREV but leave other function segments | |
468 | on the same level as they are. | |
469 | This should handle things like schedule () correctly where we're | |
470 | switching contexts. */ | |
42bfe59e | 471 | prev->up = bfun->number; |
259ba1e8 | 472 | prev->flags = BFUN_UP_LINKS_TO_RET; |
02d27625 | 473 | |
23a7fe75 | 474 | ftrace_debug (bfun, "new return - unknown caller"); |
02d27625 | 475 | } |
23a7fe75 MM |
476 | } |
477 | ||
478 | return bfun; | |
479 | } | |
480 | ||
8286623c | 481 | /* Add a new function segment for a function switch at the end of the trace. |
17b89b34 | 482 | BTINFO is the branch trace information for the current thread. |
23a7fe75 MM |
483 | MFUN and FUN are the symbol information we have for this function. */ |
484 | ||
485 | static struct btrace_function * | |
17b89b34 | 486 | ftrace_new_switch (struct btrace_thread_info *btinfo, |
23a7fe75 MM |
487 | struct minimal_symbol *mfun, |
488 | struct symbol *fun) | |
489 | { | |
b54b03bd | 490 | struct btrace_function *prev = btinfo->functions.back (); |
23a7fe75 MM |
491 | struct btrace_function *bfun; |
492 | ||
4c2c7ac6 MM |
493 | /* This is an unexplained function switch. We can't really be sure about the |
494 | call stack, yet the best I can think of right now is to preserve it. */ | |
8286623c | 495 | bfun = ftrace_new_function (btinfo, mfun, fun); |
4c2c7ac6 MM |
496 | bfun->up = prev->up; |
497 | bfun->flags = prev->flags; | |
02d27625 | 498 | |
23a7fe75 MM |
499 | ftrace_debug (bfun, "new switch"); |
500 | ||
501 | return bfun; | |
502 | } | |
503 | ||
8286623c TW |
504 | /* Add a new function segment for a gap in the trace due to a decode error at |
505 | the end of the trace. | |
17b89b34 | 506 | BTINFO is the branch trace information for the current thread. |
31fd9caa MM |
507 | ERRCODE is the format-specific error code. */ |
508 | ||
509 | static struct btrace_function * | |
8286623c | 510 | ftrace_new_gap (struct btrace_thread_info *btinfo, int errcode) |
31fd9caa MM |
511 | { |
512 | struct btrace_function *bfun; | |
513 | ||
b54b03bd | 514 | if (btinfo->functions.empty ()) |
8286623c | 515 | bfun = ftrace_new_function (btinfo, NULL, NULL); |
b54b03bd TW |
516 | else |
517 | { | |
518 | /* We hijack the previous function segment if it was empty. */ | |
519 | bfun = btinfo->functions.back (); | |
520 | if (bfun->errcode != 0 || !VEC_empty (btrace_insn_s, bfun->insn)) | |
521 | bfun = ftrace_new_function (btinfo, NULL, NULL); | |
522 | } | |
31fd9caa MM |
523 | |
524 | bfun->errcode = errcode; | |
525 | ||
526 | ftrace_debug (bfun, "new gap"); | |
527 | ||
528 | return bfun; | |
529 | } | |
530 | ||
8286623c TW |
531 | /* Update the current function segment at the end of the trace in BTINFO with |
532 | respect to the instruction at PC. This may create new function segments. | |
23a7fe75 MM |
533 | Return the chronologically latest function segment, never NULL. */ |
534 | ||
535 | static struct btrace_function * | |
8286623c | 536 | ftrace_update_function (struct btrace_thread_info *btinfo, CORE_ADDR pc) |
23a7fe75 MM |
537 | { |
538 | struct bound_minimal_symbol bmfun; | |
539 | struct minimal_symbol *mfun; | |
540 | struct symbol *fun; | |
541 | struct btrace_insn *last; | |
b54b03bd | 542 | struct btrace_function *bfun; |
23a7fe75 MM |
543 | |
544 | /* Try to determine the function we're in. We use both types of symbols | |
545 | to avoid surprises when we sometimes get a full symbol and sometimes | |
546 | only a minimal symbol. */ | |
547 | fun = find_pc_function (pc); | |
548 | bmfun = lookup_minimal_symbol_by_pc (pc); | |
549 | mfun = bmfun.minsym; | |
550 | ||
551 | if (fun == NULL && mfun == NULL) | |
552 | DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc)); | |
553 | ||
b54b03bd TW |
554 | /* If we didn't have a function, we create one. */ |
555 | if (btinfo->functions.empty ()) | |
556 | return ftrace_new_function (btinfo, mfun, fun); | |
557 | ||
558 | /* If we had a gap before, we create a function. */ | |
559 | bfun = btinfo->functions.back (); | |
560 | if (bfun->errcode != 0) | |
8286623c | 561 | return ftrace_new_function (btinfo, mfun, fun); |
23a7fe75 MM |
562 | |
563 | /* Check the last instruction, if we have one. | |
564 | We do this check first, since it allows us to fill in the call stack | |
565 | links in addition to the normal flow links. */ | |
566 | last = NULL; | |
567 | if (!VEC_empty (btrace_insn_s, bfun->insn)) | |
568 | last = VEC_last (btrace_insn_s, bfun->insn); | |
569 | ||
570 | if (last != NULL) | |
571 | { | |
7d5c24b3 MM |
572 | switch (last->iclass) |
573 | { | |
574 | case BTRACE_INSN_RETURN: | |
986b6601 MM |
575 | { |
576 | const char *fname; | |
577 | ||
578 | /* On some systems, _dl_runtime_resolve returns to the resolved | |
579 | function instead of jumping to it. From our perspective, | |
580 | however, this is a tailcall. | |
581 | If we treated it as return, we wouldn't be able to find the | |
582 | resolved function in our stack back trace. Hence, we would | |
583 | lose the current stack back trace and start anew with an empty | |
584 | back trace. When the resolved function returns, we would then | |
585 | create a stack back trace with the same function names but | |
586 | different frame id's. This will confuse stepping. */ | |
587 | fname = ftrace_print_function_name (bfun); | |
588 | if (strcmp (fname, "_dl_runtime_resolve") == 0) | |
8286623c | 589 | return ftrace_new_tailcall (btinfo, mfun, fun); |
986b6601 | 590 | |
8286623c | 591 | return ftrace_new_return (btinfo, mfun, fun); |
986b6601 | 592 | } |
23a7fe75 | 593 | |
7d5c24b3 MM |
594 | case BTRACE_INSN_CALL: |
595 | /* Ignore calls to the next instruction. They are used for PIC. */ | |
596 | if (last->pc + last->size == pc) | |
597 | break; | |
23a7fe75 | 598 | |
8286623c | 599 | return ftrace_new_call (btinfo, mfun, fun); |
23a7fe75 | 600 | |
7d5c24b3 MM |
601 | case BTRACE_INSN_JUMP: |
602 | { | |
603 | CORE_ADDR start; | |
23a7fe75 | 604 | |
7d5c24b3 | 605 | start = get_pc_function_start (pc); |
23a7fe75 | 606 | |
2dfdb47a MM |
607 | /* A jump to the start of a function is (typically) a tail call. */ |
608 | if (start == pc) | |
8286623c | 609 | return ftrace_new_tailcall (btinfo, mfun, fun); |
2dfdb47a | 610 | |
7d5c24b3 | 611 | /* If we can't determine the function for PC, we treat a jump at |
2dfdb47a MM |
612 | the end of the block as tail call if we're switching functions |
613 | and as an intra-function branch if we don't. */ | |
614 | if (start == 0 && ftrace_function_switched (bfun, mfun, fun)) | |
8286623c | 615 | return ftrace_new_tailcall (btinfo, mfun, fun); |
2dfdb47a MM |
616 | |
617 | break; | |
7d5c24b3 | 618 | } |
02d27625 | 619 | } |
23a7fe75 MM |
620 | } |
621 | ||
622 | /* Check if we're switching functions for some other reason. */ | |
623 | if (ftrace_function_switched (bfun, mfun, fun)) | |
624 | { | |
625 | DEBUG_FTRACE ("switching from %s in %s at %s", | |
626 | ftrace_print_insn_addr (last), | |
627 | ftrace_print_function_name (bfun), | |
628 | ftrace_print_filename (bfun)); | |
02d27625 | 629 | |
8286623c | 630 | return ftrace_new_switch (btinfo, mfun, fun); |
23a7fe75 MM |
631 | } |
632 | ||
633 | return bfun; | |
634 | } | |
635 | ||
23a7fe75 MM |
636 | /* Add the instruction at PC to BFUN's instructions. */ |
637 | ||
638 | static void | |
7d5c24b3 MM |
639 | ftrace_update_insns (struct btrace_function *bfun, |
640 | const struct btrace_insn *insn) | |
23a7fe75 | 641 | { |
7d5c24b3 | 642 | VEC_safe_push (btrace_insn_s, bfun->insn, insn); |
23a7fe75 MM |
643 | |
644 | if (record_debug > 1) | |
645 | ftrace_debug (bfun, "update insn"); | |
646 | } | |
647 | ||
7d5c24b3 MM |
648 | /* Classify the instruction at PC. */ |
649 | ||
650 | static enum btrace_insn_class | |
651 | ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc) | |
652 | { | |
7d5c24b3 MM |
653 | enum btrace_insn_class iclass; |
654 | ||
655 | iclass = BTRACE_INSN_OTHER; | |
492d29ea | 656 | TRY |
7d5c24b3 MM |
657 | { |
658 | if (gdbarch_insn_is_call (gdbarch, pc)) | |
659 | iclass = BTRACE_INSN_CALL; | |
660 | else if (gdbarch_insn_is_ret (gdbarch, pc)) | |
661 | iclass = BTRACE_INSN_RETURN; | |
662 | else if (gdbarch_insn_is_jump (gdbarch, pc)) | |
663 | iclass = BTRACE_INSN_JUMP; | |
664 | } | |
492d29ea PA |
665 | CATCH (error, RETURN_MASK_ERROR) |
666 | { | |
667 | } | |
668 | END_CATCH | |
7d5c24b3 MM |
669 | |
670 | return iclass; | |
671 | } | |
672 | ||
d87fdac3 MM |
673 | /* Try to match the back trace at LHS to the back trace at RHS. Returns the |
674 | number of matching function segments or zero if the back traces do not | |
42bfe59e | 675 | match. BTINFO is the branch trace information for the current thread. */ |
d87fdac3 MM |
676 | |
677 | static int | |
42bfe59e TW |
678 | ftrace_match_backtrace (struct btrace_thread_info *btinfo, |
679 | struct btrace_function *lhs, | |
d87fdac3 MM |
680 | struct btrace_function *rhs) |
681 | { | |
682 | int matches; | |
683 | ||
684 | for (matches = 0; lhs != NULL && rhs != NULL; ++matches) | |
685 | { | |
686 | if (ftrace_function_switched (lhs, rhs->msym, rhs->sym)) | |
687 | return 0; | |
688 | ||
42bfe59e TW |
689 | lhs = ftrace_get_caller (btinfo, lhs); |
690 | rhs = ftrace_get_caller (btinfo, rhs); | |
d87fdac3 MM |
691 | } |
692 | ||
693 | return matches; | |
694 | } | |
695 | ||
696 | /* Add ADJUSTMENT to the level of BFUN and succeeding function segments. */ | |
697 | ||
698 | static void | |
699 | ftrace_fixup_level (struct btrace_function *bfun, int adjustment) | |
700 | { | |
701 | if (adjustment == 0) | |
702 | return; | |
703 | ||
704 | DEBUG_FTRACE ("fixup level (%+d)", adjustment); | |
705 | ftrace_debug (bfun, "..bfun"); | |
706 | ||
707 | for (; bfun != NULL; bfun = bfun->flow.next) | |
708 | bfun->level += adjustment; | |
709 | } | |
710 | ||
711 | /* Recompute the global level offset. Traverse the function trace and compute | |
712 | the global level offset as the negative of the minimal function level. */ | |
713 | ||
714 | static void | |
715 | ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo) | |
716 | { | |
b54b03bd | 717 | int level = INT_MAX; |
d87fdac3 MM |
718 | |
719 | if (btinfo == NULL) | |
720 | return; | |
721 | ||
b54b03bd | 722 | if (btinfo->functions.empty ()) |
d87fdac3 MM |
723 | return; |
724 | ||
b54b03bd TW |
725 | unsigned int length = btinfo->functions.size() - 1; |
726 | for (unsigned int i = 0; i < length; ++i) | |
727 | level = std::min (level, btinfo->functions[i]->level); | |
728 | ||
d87fdac3 MM |
729 | /* The last function segment contains the current instruction, which is not |
730 | really part of the trace. If it contains just this one instruction, we | |
b54b03bd TW |
731 | ignore the segment. */ |
732 | struct btrace_function *last = btinfo->functions.back(); | |
733 | if (VEC_length (btrace_insn_s, last->insn) != 1) | |
734 | level = std::min (level, last->level); | |
d87fdac3 MM |
735 | |
736 | DEBUG_FTRACE ("setting global level offset: %d", -level); | |
737 | btinfo->level = -level; | |
738 | } | |
739 | ||
740 | /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in | |
42bfe59e TW |
741 | ftrace_connect_backtrace. BTINFO is the branch trace information for the |
742 | current thread. */ | |
d87fdac3 MM |
743 | |
744 | static void | |
42bfe59e TW |
745 | ftrace_connect_bfun (struct btrace_thread_info *btinfo, |
746 | struct btrace_function *prev, | |
d87fdac3 MM |
747 | struct btrace_function *next) |
748 | { | |
749 | DEBUG_FTRACE ("connecting..."); | |
750 | ftrace_debug (prev, "..prev"); | |
751 | ftrace_debug (next, "..next"); | |
752 | ||
753 | /* The function segments are not yet connected. */ | |
754 | gdb_assert (prev->segment.next == NULL); | |
755 | gdb_assert (next->segment.prev == NULL); | |
756 | ||
757 | prev->segment.next = next; | |
758 | next->segment.prev = prev; | |
759 | ||
760 | /* We may have moved NEXT to a different function level. */ | |
761 | ftrace_fixup_level (next, prev->level - next->level); | |
762 | ||
763 | /* If we run out of back trace for one, let's use the other's. */ | |
42bfe59e | 764 | if (prev->up == 0) |
d87fdac3 | 765 | { |
42bfe59e TW |
766 | const btrace_function_flags flags = next->flags; |
767 | ||
768 | next = ftrace_find_call_by_number (btinfo, next->up); | |
769 | if (next != NULL) | |
d87fdac3 MM |
770 | { |
771 | DEBUG_FTRACE ("using next's callers"); | |
42bfe59e | 772 | ftrace_fixup_caller (prev, next, flags); |
d87fdac3 MM |
773 | } |
774 | } | |
42bfe59e | 775 | else if (next->up == 0) |
d87fdac3 | 776 | { |
42bfe59e TW |
777 | const btrace_function_flags flags = prev->flags; |
778 | ||
779 | prev = ftrace_find_call_by_number (btinfo, prev->up); | |
780 | if (prev != NULL) | |
d87fdac3 MM |
781 | { |
782 | DEBUG_FTRACE ("using prev's callers"); | |
42bfe59e | 783 | ftrace_fixup_caller (next, prev, flags); |
d87fdac3 MM |
784 | } |
785 | } | |
786 | else | |
787 | { | |
788 | /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up | |
789 | link to add the tail callers to NEXT's back trace. | |
790 | ||
791 | This removes NEXT->UP from NEXT's back trace. It will be added back | |
792 | when connecting NEXT and PREV's callers - provided they exist. | |
793 | ||
794 | If PREV's back trace consists of a series of tail calls without an | |
795 | actual call, there will be no further connection and NEXT's caller will | |
796 | be removed for good. To catch this case, we handle it here and connect | |
797 | the top of PREV's back trace to NEXT's caller. */ | |
798 | if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0) | |
799 | { | |
800 | struct btrace_function *caller; | |
42bfe59e | 801 | btrace_function_flags next_flags, prev_flags; |
d87fdac3 MM |
802 | |
803 | /* We checked NEXT->UP above so CALLER can't be NULL. */ | |
42bfe59e TW |
804 | caller = ftrace_find_call_by_number (btinfo, next->up); |
805 | next_flags = next->flags; | |
806 | prev_flags = prev->flags; | |
d87fdac3 MM |
807 | |
808 | DEBUG_FTRACE ("adding prev's tail calls to next"); | |
809 | ||
42bfe59e TW |
810 | prev = ftrace_find_call_by_number (btinfo, prev->up); |
811 | ftrace_fixup_caller (next, prev, prev_flags); | |
d87fdac3 | 812 | |
42bfe59e TW |
813 | for (; prev != NULL; prev = ftrace_find_call_by_number (btinfo, |
814 | prev->up)) | |
d87fdac3 MM |
815 | { |
816 | /* At the end of PREV's back trace, continue with CALLER. */ | |
42bfe59e | 817 | if (prev->up == 0) |
d87fdac3 MM |
818 | { |
819 | DEBUG_FTRACE ("fixing up link for tailcall chain"); | |
820 | ftrace_debug (prev, "..top"); | |
821 | ftrace_debug (caller, "..up"); | |
822 | ||
42bfe59e | 823 | ftrace_fixup_caller (prev, caller, next_flags); |
d87fdac3 MM |
824 | |
825 | /* If we skipped any tail calls, this may move CALLER to a | |
826 | different function level. | |
827 | ||
828 | Note that changing CALLER's level is only OK because we | |
829 | know that this is the last iteration of the bottom-to-top | |
830 | walk in ftrace_connect_backtrace. | |
831 | ||
832 | Otherwise we will fix up CALLER's level when we connect it | |
833 | to PREV's caller in the next iteration. */ | |
834 | ftrace_fixup_level (caller, prev->level - caller->level - 1); | |
835 | break; | |
836 | } | |
837 | ||
838 | /* There's nothing to do if we find a real call. */ | |
839 | if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0) | |
840 | { | |
841 | DEBUG_FTRACE ("will fix up link in next iteration"); | |
842 | break; | |
843 | } | |
844 | } | |
845 | } | |
846 | } | |
847 | } | |
848 | ||
849 | /* Connect function segments on the same level in the back trace at LHS and RHS. | |
850 | The back traces at LHS and RHS are expected to match according to | |
42bfe59e TW |
851 | ftrace_match_backtrace. BTINFO is the branch trace information for the |
852 | current thread. */ | |
d87fdac3 MM |
853 | |
854 | static void | |
42bfe59e TW |
855 | ftrace_connect_backtrace (struct btrace_thread_info *btinfo, |
856 | struct btrace_function *lhs, | |
d87fdac3 MM |
857 | struct btrace_function *rhs) |
858 | { | |
859 | while (lhs != NULL && rhs != NULL) | |
860 | { | |
861 | struct btrace_function *prev, *next; | |
862 | ||
863 | gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym)); | |
864 | ||
865 | /* Connecting LHS and RHS may change the up link. */ | |
866 | prev = lhs; | |
867 | next = rhs; | |
868 | ||
42bfe59e TW |
869 | lhs = ftrace_get_caller (btinfo, lhs); |
870 | rhs = ftrace_get_caller (btinfo, rhs); | |
d87fdac3 | 871 | |
42bfe59e | 872 | ftrace_connect_bfun (btinfo, prev, next); |
d87fdac3 MM |
873 | } |
874 | } | |
875 | ||
876 | /* Bridge the gap between two function segments left and right of a gap if their | |
42bfe59e TW |
877 | respective back traces match in at least MIN_MATCHES functions. BTINFO is |
878 | the branch trace information for the current thread. | |
d87fdac3 MM |
879 | |
880 | Returns non-zero if the gap could be bridged, zero otherwise. */ | |
881 | ||
882 | static int | |
42bfe59e TW |
883 | ftrace_bridge_gap (struct btrace_thread_info *btinfo, |
884 | struct btrace_function *lhs, struct btrace_function *rhs, | |
d87fdac3 MM |
885 | int min_matches) |
886 | { | |
887 | struct btrace_function *best_l, *best_r, *cand_l, *cand_r; | |
888 | int best_matches; | |
889 | ||
890 | DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)", | |
891 | rhs->insn_offset - 1, min_matches); | |
892 | ||
893 | best_matches = 0; | |
894 | best_l = NULL; | |
895 | best_r = NULL; | |
896 | ||
897 | /* We search the back traces of LHS and RHS for valid connections and connect | |
898 | the two functon segments that give the longest combined back trace. */ | |
899 | ||
42bfe59e TW |
900 | for (cand_l = lhs; cand_l != NULL; |
901 | cand_l = ftrace_get_caller (btinfo, cand_l)) | |
902 | for (cand_r = rhs; cand_r != NULL; | |
903 | cand_r = ftrace_get_caller (btinfo, cand_r)) | |
d87fdac3 MM |
904 | { |
905 | int matches; | |
906 | ||
42bfe59e | 907 | matches = ftrace_match_backtrace (btinfo, cand_l, cand_r); |
d87fdac3 MM |
908 | if (best_matches < matches) |
909 | { | |
910 | best_matches = matches; | |
911 | best_l = cand_l; | |
912 | best_r = cand_r; | |
913 | } | |
914 | } | |
915 | ||
916 | /* We need at least MIN_MATCHES matches. */ | |
917 | gdb_assert (min_matches > 0); | |
918 | if (best_matches < min_matches) | |
919 | return 0; | |
920 | ||
921 | DEBUG_FTRACE ("..matches: %d", best_matches); | |
922 | ||
923 | /* We will fix up the level of BEST_R and succeeding function segments such | |
924 | that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R. | |
925 | ||
926 | This will ignore the level of RHS and following if BEST_R != RHS. I.e. if | |
927 | BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3). | |
928 | ||
929 | To catch this, we already fix up the level here where we can start at RHS | |
930 | instead of at BEST_R. We will ignore the level fixup when connecting | |
931 | BEST_L to BEST_R as they will already be on the same level. */ | |
932 | ftrace_fixup_level (rhs, best_l->level - best_r->level); | |
933 | ||
42bfe59e | 934 | ftrace_connect_backtrace (btinfo, best_l, best_r); |
d87fdac3 MM |
935 | |
936 | return best_matches; | |
937 | } | |
938 | ||
939 | /* Try to bridge gaps due to overflow or decode errors by connecting the | |
940 | function segments that are separated by the gap. */ | |
941 | ||
942 | static void | |
943 | btrace_bridge_gaps (struct thread_info *tp, VEC (bfun_s) **gaps) | |
944 | { | |
945 | VEC (bfun_s) *remaining; | |
946 | struct cleanup *old_chain; | |
947 | int min_matches; | |
948 | ||
949 | DEBUG ("bridge gaps"); | |
950 | ||
951 | remaining = NULL; | |
952 | old_chain = make_cleanup (VEC_cleanup (bfun_s), &remaining); | |
953 | ||
954 | /* We require a minimum amount of matches for bridging a gap. The number of | |
955 | required matches will be lowered with each iteration. | |
956 | ||
957 | The more matches the higher our confidence that the bridging is correct. | |
958 | For big gaps or small traces, however, it may not be feasible to require a | |
959 | high number of matches. */ | |
960 | for (min_matches = 5; min_matches > 0; --min_matches) | |
961 | { | |
962 | /* Let's try to bridge as many gaps as we can. In some cases, we need to | |
963 | skip a gap and revisit it again after we closed later gaps. */ | |
964 | while (!VEC_empty (bfun_s, *gaps)) | |
965 | { | |
966 | struct btrace_function *gap; | |
967 | unsigned int idx; | |
968 | ||
969 | for (idx = 0; VEC_iterate (bfun_s, *gaps, idx, gap); ++idx) | |
970 | { | |
971 | struct btrace_function *lhs, *rhs; | |
972 | int bridged; | |
973 | ||
974 | /* We may have a sequence of gaps if we run from one error into | |
975 | the next as we try to re-sync onto the trace stream. Ignore | |
976 | all but the leftmost gap in such a sequence. | |
977 | ||
978 | Also ignore gaps at the beginning of the trace. */ | |
979 | lhs = gap->flow.prev; | |
980 | if (lhs == NULL || lhs->errcode != 0) | |
981 | continue; | |
982 | ||
983 | /* Skip gaps to the right. */ | |
984 | for (rhs = gap->flow.next; rhs != NULL; rhs = rhs->flow.next) | |
985 | if (rhs->errcode == 0) | |
986 | break; | |
987 | ||
988 | /* Ignore gaps at the end of the trace. */ | |
989 | if (rhs == NULL) | |
990 | continue; | |
991 | ||
42bfe59e | 992 | bridged = ftrace_bridge_gap (&tp->btrace, lhs, rhs, min_matches); |
d87fdac3 MM |
993 | |
994 | /* Keep track of gaps we were not able to bridge and try again. | |
995 | If we just pushed them to the end of GAPS we would risk an | |
996 | infinite loop in case we simply cannot bridge a gap. */ | |
997 | if (bridged == 0) | |
998 | VEC_safe_push (bfun_s, remaining, gap); | |
999 | } | |
1000 | ||
1001 | /* Let's see if we made any progress. */ | |
1002 | if (VEC_length (bfun_s, remaining) == VEC_length (bfun_s, *gaps)) | |
1003 | break; | |
1004 | ||
1005 | VEC_free (bfun_s, *gaps); | |
1006 | ||
1007 | *gaps = remaining; | |
1008 | remaining = NULL; | |
1009 | } | |
1010 | ||
1011 | /* We get here if either GAPS is empty or if GAPS equals REMAINING. */ | |
1012 | if (VEC_empty (bfun_s, *gaps)) | |
1013 | break; | |
1014 | ||
1015 | VEC_free (bfun_s, remaining); | |
1016 | } | |
1017 | ||
1018 | do_cleanups (old_chain); | |
1019 | ||
1020 | /* We may omit this in some cases. Not sure it is worth the extra | |
1021 | complication, though. */ | |
1022 | ftrace_compute_global_level_offset (&tp->btrace); | |
1023 | } | |
1024 | ||
734b0e4b | 1025 | /* Compute the function branch trace from BTS trace. */ |
23a7fe75 MM |
1026 | |
1027 | static void | |
76235df1 | 1028 | btrace_compute_ftrace_bts (struct thread_info *tp, |
d87fdac3 MM |
1029 | const struct btrace_data_bts *btrace, |
1030 | VEC (bfun_s) **gaps) | |
23a7fe75 | 1031 | { |
76235df1 | 1032 | struct btrace_thread_info *btinfo; |
23a7fe75 | 1033 | struct gdbarch *gdbarch; |
d87fdac3 | 1034 | unsigned int blk; |
23a7fe75 MM |
1035 | int level; |
1036 | ||
23a7fe75 | 1037 | gdbarch = target_gdbarch (); |
76235df1 | 1038 | btinfo = &tp->btrace; |
734b0e4b | 1039 | blk = VEC_length (btrace_block_s, btrace->blocks); |
23a7fe75 | 1040 | |
b54b03bd TW |
1041 | if (btinfo->functions.empty ()) |
1042 | level = INT_MAX; | |
1043 | else | |
1044 | level = -btinfo->level; | |
1045 | ||
23a7fe75 MM |
1046 | while (blk != 0) |
1047 | { | |
1048 | btrace_block_s *block; | |
1049 | CORE_ADDR pc; | |
1050 | ||
1051 | blk -= 1; | |
1052 | ||
734b0e4b | 1053 | block = VEC_index (btrace_block_s, btrace->blocks, blk); |
23a7fe75 MM |
1054 | pc = block->begin; |
1055 | ||
1056 | for (;;) | |
1057 | { | |
b54b03bd | 1058 | struct btrace_function *bfun; |
7d5c24b3 | 1059 | struct btrace_insn insn; |
23a7fe75 MM |
1060 | int size; |
1061 | ||
1062 | /* We should hit the end of the block. Warn if we went too far. */ | |
1063 | if (block->end < pc) | |
1064 | { | |
b61ce85c | 1065 | /* Indicate the gap in the trace. */ |
b54b03bd | 1066 | bfun = ftrace_new_gap (btinfo, BDE_BTS_OVERFLOW); |
b61ce85c | 1067 | |
b54b03bd | 1068 | VEC_safe_push (bfun_s, *gaps, bfun); |
b61ce85c MM |
1069 | |
1070 | warning (_("Recorded trace may be corrupted at instruction " | |
b54b03bd | 1071 | "%u (pc = %s)."), bfun->insn_offset - 1, |
b61ce85c | 1072 | core_addr_to_string_nz (pc)); |
63ab433e | 1073 | |
23a7fe75 MM |
1074 | break; |
1075 | } | |
1076 | ||
b54b03bd | 1077 | bfun = ftrace_update_function (btinfo, pc); |
23a7fe75 | 1078 | |
8710b709 MM |
1079 | /* Maintain the function level offset. |
1080 | For all but the last block, we do it here. */ | |
1081 | if (blk != 0) | |
b54b03bd | 1082 | level = std::min (level, bfun->level); |
23a7fe75 | 1083 | |
7d5c24b3 | 1084 | size = 0; |
492d29ea PA |
1085 | TRY |
1086 | { | |
1087 | size = gdb_insn_length (gdbarch, pc); | |
1088 | } | |
1089 | CATCH (error, RETURN_MASK_ERROR) | |
1090 | { | |
1091 | } | |
1092 | END_CATCH | |
7d5c24b3 MM |
1093 | |
1094 | insn.pc = pc; | |
1095 | insn.size = size; | |
1096 | insn.iclass = ftrace_classify_insn (gdbarch, pc); | |
da8c46d2 | 1097 | insn.flags = 0; |
7d5c24b3 | 1098 | |
b54b03bd | 1099 | ftrace_update_insns (bfun, &insn); |
23a7fe75 MM |
1100 | |
1101 | /* We're done once we pushed the instruction at the end. */ | |
1102 | if (block->end == pc) | |
1103 | break; | |
1104 | ||
7d5c24b3 | 1105 | /* We can't continue if we fail to compute the size. */ |
23a7fe75 MM |
1106 | if (size <= 0) |
1107 | { | |
31fd9caa MM |
1108 | /* Indicate the gap in the trace. We just added INSN so we're |
1109 | not at the beginning. */ | |
b54b03bd | 1110 | bfun = ftrace_new_gap (btinfo, BDE_BTS_INSN_SIZE); |
d87fdac3 | 1111 | |
b54b03bd | 1112 | VEC_safe_push (bfun_s, *gaps, bfun); |
31fd9caa | 1113 | |
63ab433e | 1114 | warning (_("Recorded trace may be incomplete at instruction %u " |
b54b03bd | 1115 | "(pc = %s)."), bfun->insn_offset - 1, |
63ab433e MM |
1116 | core_addr_to_string_nz (pc)); |
1117 | ||
23a7fe75 MM |
1118 | break; |
1119 | } | |
1120 | ||
1121 | pc += size; | |
8710b709 MM |
1122 | |
1123 | /* Maintain the function level offset. | |
1124 | For the last block, we do it here to not consider the last | |
1125 | instruction. | |
1126 | Since the last instruction corresponds to the current instruction | |
1127 | and is not really part of the execution history, it shouldn't | |
1128 | affect the level. */ | |
1129 | if (blk == 0) | |
b54b03bd | 1130 | level = std::min (level, bfun->level); |
23a7fe75 | 1131 | } |
02d27625 MM |
1132 | } |
1133 | ||
23a7fe75 MM |
1134 | /* LEVEL is the minimal function level of all btrace function segments. |
1135 | Define the global level offset to -LEVEL so all function levels are | |
1136 | normalized to start at zero. */ | |
1137 | btinfo->level = -level; | |
02d27625 MM |
1138 | } |
1139 | ||
b20a6524 MM |
1140 | #if defined (HAVE_LIBIPT) |
1141 | ||
1142 | static enum btrace_insn_class | |
1143 | pt_reclassify_insn (enum pt_insn_class iclass) | |
1144 | { | |
1145 | switch (iclass) | |
1146 | { | |
1147 | case ptic_call: | |
1148 | return BTRACE_INSN_CALL; | |
1149 | ||
1150 | case ptic_return: | |
1151 | return BTRACE_INSN_RETURN; | |
1152 | ||
1153 | case ptic_jump: | |
1154 | return BTRACE_INSN_JUMP; | |
1155 | ||
1156 | default: | |
1157 | return BTRACE_INSN_OTHER; | |
1158 | } | |
1159 | } | |
1160 | ||
da8c46d2 MM |
1161 | /* Return the btrace instruction flags for INSN. */ |
1162 | ||
d7abe101 | 1163 | static btrace_insn_flags |
b5c36682 | 1164 | pt_btrace_insn_flags (const struct pt_insn &insn) |
da8c46d2 | 1165 | { |
d7abe101 | 1166 | btrace_insn_flags flags = 0; |
da8c46d2 | 1167 | |
b5c36682 | 1168 | if (insn.speculative) |
da8c46d2 MM |
1169 | flags |= BTRACE_INSN_FLAG_SPECULATIVE; |
1170 | ||
1171 | return flags; | |
1172 | } | |
1173 | ||
b5c36682 PA |
1174 | /* Return the btrace instruction for INSN. */ |
1175 | ||
1176 | static btrace_insn | |
1177 | pt_btrace_insn (const struct pt_insn &insn) | |
1178 | { | |
1179 | return {(CORE_ADDR) insn.ip, (gdb_byte) insn.size, | |
1180 | pt_reclassify_insn (insn.iclass), | |
1181 | pt_btrace_insn_flags (insn)}; | |
1182 | } | |
1183 | ||
1184 | ||
17b89b34 | 1185 | /* Add function branch trace to BTINFO using DECODER. */ |
b20a6524 MM |
1186 | |
1187 | static void | |
17b89b34 TW |
1188 | ftrace_add_pt (struct btrace_thread_info *btinfo, |
1189 | struct pt_insn_decoder *decoder, | |
b54b03bd | 1190 | int *plevel, |
d87fdac3 | 1191 | VEC (bfun_s) **gaps) |
b20a6524 | 1192 | { |
b54b03bd | 1193 | struct btrace_function *bfun; |
b20a6524 | 1194 | uint64_t offset; |
63ab433e | 1195 | int errcode; |
b20a6524 | 1196 | |
b20a6524 MM |
1197 | for (;;) |
1198 | { | |
b20a6524 MM |
1199 | struct pt_insn insn; |
1200 | ||
1201 | errcode = pt_insn_sync_forward (decoder); | |
1202 | if (errcode < 0) | |
1203 | { | |
1204 | if (errcode != -pte_eos) | |
bc504a31 | 1205 | warning (_("Failed to synchronize onto the Intel Processor " |
b20a6524 MM |
1206 | "Trace stream: %s."), pt_errstr (pt_errcode (errcode))); |
1207 | break; | |
1208 | } | |
1209 | ||
b20a6524 MM |
1210 | for (;;) |
1211 | { | |
1212 | errcode = pt_insn_next (decoder, &insn, sizeof(insn)); | |
1213 | if (errcode < 0) | |
1214 | break; | |
1215 | ||
1216 | /* Look for gaps in the trace - unless we're at the beginning. */ | |
b54b03bd | 1217 | if (!btinfo->functions.empty ()) |
b20a6524 MM |
1218 | { |
1219 | /* Tracing is disabled and re-enabled each time we enter the | |
1220 | kernel. Most times, we continue from the same instruction we | |
1221 | stopped before. This is indicated via the RESUMED instruction | |
1222 | flag. The ENABLED instruction flag means that we continued | |
1223 | from some other instruction. Indicate this as a trace gap. */ | |
1224 | if (insn.enabled) | |
63ab433e | 1225 | { |
b54b03bd | 1226 | bfun = ftrace_new_gap (btinfo, BDE_PT_DISABLED); |
d87fdac3 | 1227 | |
b54b03bd | 1228 | VEC_safe_push (bfun_s, *gaps, bfun); |
63ab433e MM |
1229 | |
1230 | pt_insn_get_offset (decoder, &offset); | |
1231 | ||
1232 | warning (_("Non-contiguous trace at instruction %u (offset " | |
1233 | "= 0x%" PRIx64 ", pc = 0x%" PRIx64 ")."), | |
b54b03bd | 1234 | bfun->insn_offset - 1, offset, insn.ip); |
63ab433e | 1235 | } |
b61ce85c | 1236 | } |
b20a6524 | 1237 | |
b61ce85c MM |
1238 | /* Indicate trace overflows. */ |
1239 | if (insn.resynced) | |
1240 | { | |
b54b03bd | 1241 | bfun = ftrace_new_gap (btinfo, BDE_PT_OVERFLOW); |
63ab433e | 1242 | |
b54b03bd | 1243 | VEC_safe_push (bfun_s, *gaps, bfun); |
63ab433e | 1244 | |
b61ce85c MM |
1245 | pt_insn_get_offset (decoder, &offset); |
1246 | ||
1247 | warning (_("Overflow at instruction %u (offset = 0x%" PRIx64 | |
b54b03bd | 1248 | ", pc = 0x%" PRIx64 ")."), bfun->insn_offset - 1, |
b61ce85c | 1249 | offset, insn.ip); |
b20a6524 MM |
1250 | } |
1251 | ||
b54b03bd | 1252 | bfun = ftrace_update_function (btinfo, insn.ip); |
b20a6524 MM |
1253 | |
1254 | /* Maintain the function level offset. */ | |
b54b03bd | 1255 | *plevel = std::min (*plevel, bfun->level); |
b20a6524 | 1256 | |
b5c36682 | 1257 | btrace_insn btinsn = pt_btrace_insn (insn); |
b54b03bd | 1258 | ftrace_update_insns (bfun, &btinsn); |
b20a6524 MM |
1259 | } |
1260 | ||
1261 | if (errcode == -pte_eos) | |
1262 | break; | |
1263 | ||
b20a6524 | 1264 | /* Indicate the gap in the trace. */ |
b54b03bd | 1265 | bfun = ftrace_new_gap (btinfo, errcode); |
d87fdac3 | 1266 | |
b54b03bd | 1267 | VEC_safe_push (bfun_s, *gaps, bfun); |
b20a6524 | 1268 | |
63ab433e MM |
1269 | pt_insn_get_offset (decoder, &offset); |
1270 | ||
1271 | warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64 | |
b54b03bd | 1272 | ", pc = 0x%" PRIx64 "): %s."), errcode, bfun->insn_offset - 1, |
63ab433e MM |
1273 | offset, insn.ip, pt_errstr (pt_errcode (errcode))); |
1274 | } | |
b20a6524 MM |
1275 | } |
1276 | ||
1277 | /* A callback function to allow the trace decoder to read the inferior's | |
1278 | memory. */ | |
1279 | ||
1280 | static int | |
1281 | btrace_pt_readmem_callback (gdb_byte *buffer, size_t size, | |
80a2b330 | 1282 | const struct pt_asid *asid, uint64_t pc, |
b20a6524 MM |
1283 | void *context) |
1284 | { | |
43368e1d | 1285 | int result, errcode; |
b20a6524 | 1286 | |
43368e1d | 1287 | result = (int) size; |
b20a6524 MM |
1288 | TRY |
1289 | { | |
80a2b330 | 1290 | errcode = target_read_code ((CORE_ADDR) pc, buffer, size); |
b20a6524 | 1291 | if (errcode != 0) |
43368e1d | 1292 | result = -pte_nomap; |
b20a6524 MM |
1293 | } |
1294 | CATCH (error, RETURN_MASK_ERROR) | |
1295 | { | |
43368e1d | 1296 | result = -pte_nomap; |
b20a6524 MM |
1297 | } |
1298 | END_CATCH | |
1299 | ||
43368e1d | 1300 | return result; |
b20a6524 MM |
1301 | } |
1302 | ||
1303 | /* Translate the vendor from one enum to another. */ | |
1304 | ||
1305 | static enum pt_cpu_vendor | |
1306 | pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor) | |
1307 | { | |
1308 | switch (vendor) | |
1309 | { | |
1310 | default: | |
1311 | return pcv_unknown; | |
1312 | ||
1313 | case CV_INTEL: | |
1314 | return pcv_intel; | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | /* Finalize the function branch trace after decode. */ | |
1319 | ||
1320 | static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder, | |
1321 | struct thread_info *tp, int level) | |
1322 | { | |
1323 | pt_insn_free_decoder (decoder); | |
1324 | ||
1325 | /* LEVEL is the minimal function level of all btrace function segments. | |
1326 | Define the global level offset to -LEVEL so all function levels are | |
1327 | normalized to start at zero. */ | |
1328 | tp->btrace.level = -level; | |
1329 | ||
1330 | /* Add a single last instruction entry for the current PC. | |
1331 | This allows us to compute the backtrace at the current PC using both | |
1332 | standard unwind and btrace unwind. | |
1333 | This extra entry is ignored by all record commands. */ | |
1334 | btrace_add_pc (tp); | |
1335 | } | |
1336 | ||
bc504a31 PA |
1337 | /* Compute the function branch trace from Intel Processor Trace |
1338 | format. */ | |
b20a6524 MM |
1339 | |
1340 | static void | |
1341 | btrace_compute_ftrace_pt (struct thread_info *tp, | |
d87fdac3 MM |
1342 | const struct btrace_data_pt *btrace, |
1343 | VEC (bfun_s) **gaps) | |
b20a6524 MM |
1344 | { |
1345 | struct btrace_thread_info *btinfo; | |
1346 | struct pt_insn_decoder *decoder; | |
1347 | struct pt_config config; | |
1348 | int level, errcode; | |
1349 | ||
1350 | if (btrace->size == 0) | |
1351 | return; | |
1352 | ||
1353 | btinfo = &tp->btrace; | |
b54b03bd TW |
1354 | if (btinfo->functions.empty ()) |
1355 | level = INT_MAX; | |
1356 | else | |
1357 | level = -btinfo->level; | |
b20a6524 MM |
1358 | |
1359 | pt_config_init(&config); | |
1360 | config.begin = btrace->data; | |
1361 | config.end = btrace->data + btrace->size; | |
1362 | ||
1363 | config.cpu.vendor = pt_translate_cpu_vendor (btrace->config.cpu.vendor); | |
1364 | config.cpu.family = btrace->config.cpu.family; | |
1365 | config.cpu.model = btrace->config.cpu.model; | |
1366 | config.cpu.stepping = btrace->config.cpu.stepping; | |
1367 | ||
1368 | errcode = pt_cpu_errata (&config.errata, &config.cpu); | |
1369 | if (errcode < 0) | |
bc504a31 | 1370 | error (_("Failed to configure the Intel Processor Trace decoder: %s."), |
b20a6524 MM |
1371 | pt_errstr (pt_errcode (errcode))); |
1372 | ||
1373 | decoder = pt_insn_alloc_decoder (&config); | |
1374 | if (decoder == NULL) | |
bc504a31 | 1375 | error (_("Failed to allocate the Intel Processor Trace decoder.")); |
b20a6524 MM |
1376 | |
1377 | TRY | |
1378 | { | |
1379 | struct pt_image *image; | |
1380 | ||
1381 | image = pt_insn_get_image(decoder); | |
1382 | if (image == NULL) | |
bc504a31 | 1383 | error (_("Failed to configure the Intel Processor Trace decoder.")); |
b20a6524 MM |
1384 | |
1385 | errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL); | |
1386 | if (errcode < 0) | |
bc504a31 | 1387 | error (_("Failed to configure the Intel Processor Trace decoder: " |
b20a6524 MM |
1388 | "%s."), pt_errstr (pt_errcode (errcode))); |
1389 | ||
b54b03bd | 1390 | ftrace_add_pt (btinfo, decoder, &level, gaps); |
b20a6524 MM |
1391 | } |
1392 | CATCH (error, RETURN_MASK_ALL) | |
1393 | { | |
1394 | /* Indicate a gap in the trace if we quit trace processing. */ | |
b54b03bd | 1395 | if (error.reason == RETURN_QUIT && !btinfo->functions.empty ()) |
b20a6524 | 1396 | { |
b54b03bd | 1397 | struct btrace_function *bfun; |
d87fdac3 | 1398 | |
b54b03bd TW |
1399 | bfun = ftrace_new_gap (btinfo, BDE_PT_USER_QUIT); |
1400 | ||
1401 | VEC_safe_push (bfun_s, *gaps, bfun); | |
b20a6524 MM |
1402 | } |
1403 | ||
1404 | btrace_finalize_ftrace_pt (decoder, tp, level); | |
1405 | ||
1406 | throw_exception (error); | |
1407 | } | |
1408 | END_CATCH | |
1409 | ||
1410 | btrace_finalize_ftrace_pt (decoder, tp, level); | |
1411 | } | |
1412 | ||
1413 | #else /* defined (HAVE_LIBIPT) */ | |
1414 | ||
1415 | static void | |
1416 | btrace_compute_ftrace_pt (struct thread_info *tp, | |
d87fdac3 MM |
1417 | const struct btrace_data_pt *btrace, |
1418 | VEC (bfun_s) **gaps) | |
b20a6524 MM |
1419 | { |
1420 | internal_error (__FILE__, __LINE__, _("Unexpected branch trace format.")); | |
1421 | } | |
1422 | ||
1423 | #endif /* defined (HAVE_LIBIPT) */ | |
1424 | ||
734b0e4b MM |
1425 | /* Compute the function branch trace from a block branch trace BTRACE for |
1426 | a thread given by BTINFO. */ | |
1427 | ||
1428 | static void | |
d87fdac3 MM |
1429 | btrace_compute_ftrace_1 (struct thread_info *tp, struct btrace_data *btrace, |
1430 | VEC (bfun_s) **gaps) | |
734b0e4b MM |
1431 | { |
1432 | DEBUG ("compute ftrace"); | |
1433 | ||
1434 | switch (btrace->format) | |
1435 | { | |
1436 | case BTRACE_FORMAT_NONE: | |
1437 | return; | |
1438 | ||
1439 | case BTRACE_FORMAT_BTS: | |
d87fdac3 | 1440 | btrace_compute_ftrace_bts (tp, &btrace->variant.bts, gaps); |
734b0e4b | 1441 | return; |
b20a6524 MM |
1442 | |
1443 | case BTRACE_FORMAT_PT: | |
d87fdac3 | 1444 | btrace_compute_ftrace_pt (tp, &btrace->variant.pt, gaps); |
b20a6524 | 1445 | return; |
734b0e4b MM |
1446 | } |
1447 | ||
1448 | internal_error (__FILE__, __LINE__, _("Unkown branch trace format.")); | |
1449 | } | |
1450 | ||
d87fdac3 MM |
1451 | static void |
1452 | btrace_finalize_ftrace (struct thread_info *tp, VEC (bfun_s) **gaps) | |
1453 | { | |
1454 | if (!VEC_empty (bfun_s, *gaps)) | |
1455 | { | |
1456 | tp->btrace.ngaps += VEC_length (bfun_s, *gaps); | |
1457 | btrace_bridge_gaps (tp, gaps); | |
1458 | } | |
1459 | } | |
1460 | ||
1461 | static void | |
1462 | btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace) | |
1463 | { | |
1464 | VEC (bfun_s) *gaps; | |
1465 | struct cleanup *old_chain; | |
1466 | ||
1467 | gaps = NULL; | |
1468 | old_chain = make_cleanup (VEC_cleanup (bfun_s), &gaps); | |
1469 | ||
1470 | TRY | |
1471 | { | |
1472 | btrace_compute_ftrace_1 (tp, btrace, &gaps); | |
1473 | } | |
1474 | CATCH (error, RETURN_MASK_ALL) | |
1475 | { | |
1476 | btrace_finalize_ftrace (tp, &gaps); | |
1477 | ||
1478 | throw_exception (error); | |
1479 | } | |
1480 | END_CATCH | |
1481 | ||
1482 | btrace_finalize_ftrace (tp, &gaps); | |
1483 | ||
1484 | do_cleanups (old_chain); | |
1485 | } | |
1486 | ||
6e07b1d2 MM |
1487 | /* Add an entry for the current PC. */ |
1488 | ||
1489 | static void | |
1490 | btrace_add_pc (struct thread_info *tp) | |
1491 | { | |
734b0e4b | 1492 | struct btrace_data btrace; |
6e07b1d2 MM |
1493 | struct btrace_block *block; |
1494 | struct regcache *regcache; | |
1495 | struct cleanup *cleanup; | |
1496 | CORE_ADDR pc; | |
1497 | ||
1498 | regcache = get_thread_regcache (tp->ptid); | |
1499 | pc = regcache_read_pc (regcache); | |
1500 | ||
734b0e4b MM |
1501 | btrace_data_init (&btrace); |
1502 | btrace.format = BTRACE_FORMAT_BTS; | |
1503 | btrace.variant.bts.blocks = NULL; | |
6e07b1d2 | 1504 | |
734b0e4b MM |
1505 | cleanup = make_cleanup_btrace_data (&btrace); |
1506 | ||
1507 | block = VEC_safe_push (btrace_block_s, btrace.variant.bts.blocks, NULL); | |
6e07b1d2 MM |
1508 | block->begin = pc; |
1509 | block->end = pc; | |
1510 | ||
76235df1 | 1511 | btrace_compute_ftrace (tp, &btrace); |
6e07b1d2 MM |
1512 | |
1513 | do_cleanups (cleanup); | |
1514 | } | |
1515 | ||
02d27625 MM |
1516 | /* See btrace.h. */ |
1517 | ||
1518 | void | |
f4abbc16 | 1519 | btrace_enable (struct thread_info *tp, const struct btrace_config *conf) |
02d27625 MM |
1520 | { |
1521 | if (tp->btrace.target != NULL) | |
1522 | return; | |
1523 | ||
46a3515b MM |
1524 | #if !defined (HAVE_LIBIPT) |
1525 | if (conf->format == BTRACE_FORMAT_PT) | |
bc504a31 | 1526 | error (_("GDB does not support Intel Processor Trace.")); |
46a3515b MM |
1527 | #endif /* !defined (HAVE_LIBIPT) */ |
1528 | ||
f4abbc16 | 1529 | if (!target_supports_btrace (conf->format)) |
02d27625 MM |
1530 | error (_("Target does not support branch tracing.")); |
1531 | ||
43792cf0 PA |
1532 | DEBUG ("enable thread %s (%s)", print_thread_id (tp), |
1533 | target_pid_to_str (tp->ptid)); | |
02d27625 | 1534 | |
f4abbc16 | 1535 | tp->btrace.target = target_enable_btrace (tp->ptid, conf); |
6e07b1d2 | 1536 | |
cd4007e4 MM |
1537 | /* We're done if we failed to enable tracing. */ |
1538 | if (tp->btrace.target == NULL) | |
1539 | return; | |
1540 | ||
1541 | /* We need to undo the enable in case of errors. */ | |
1542 | TRY | |
1543 | { | |
1544 | /* Add an entry for the current PC so we start tracing from where we | |
1545 | enabled it. | |
1546 | ||
1547 | If we can't access TP's registers, TP is most likely running. In this | |
1548 | case, we can't really say where tracing was enabled so it should be | |
1549 | safe to simply skip this step. | |
1550 | ||
1551 | This is not relevant for BTRACE_FORMAT_PT since the trace will already | |
1552 | start at the PC at which tracing was enabled. */ | |
1553 | if (conf->format != BTRACE_FORMAT_PT | |
1554 | && can_access_registers_ptid (tp->ptid)) | |
1555 | btrace_add_pc (tp); | |
1556 | } | |
1557 | CATCH (exception, RETURN_MASK_ALL) | |
1558 | { | |
1559 | btrace_disable (tp); | |
1560 | ||
1561 | throw_exception (exception); | |
1562 | } | |
1563 | END_CATCH | |
02d27625 MM |
1564 | } |
1565 | ||
1566 | /* See btrace.h. */ | |
1567 | ||
f4abbc16 MM |
1568 | const struct btrace_config * |
1569 | btrace_conf (const struct btrace_thread_info *btinfo) | |
1570 | { | |
1571 | if (btinfo->target == NULL) | |
1572 | return NULL; | |
1573 | ||
1574 | return target_btrace_conf (btinfo->target); | |
1575 | } | |
1576 | ||
1577 | /* See btrace.h. */ | |
1578 | ||
02d27625 MM |
1579 | void |
1580 | btrace_disable (struct thread_info *tp) | |
1581 | { | |
1582 | struct btrace_thread_info *btp = &tp->btrace; | |
1583 | int errcode = 0; | |
1584 | ||
1585 | if (btp->target == NULL) | |
1586 | return; | |
1587 | ||
43792cf0 PA |
1588 | DEBUG ("disable thread %s (%s)", print_thread_id (tp), |
1589 | target_pid_to_str (tp->ptid)); | |
02d27625 MM |
1590 | |
1591 | target_disable_btrace (btp->target); | |
1592 | btp->target = NULL; | |
1593 | ||
1594 | btrace_clear (tp); | |
1595 | } | |
1596 | ||
1597 | /* See btrace.h. */ | |
1598 | ||
1599 | void | |
1600 | btrace_teardown (struct thread_info *tp) | |
1601 | { | |
1602 | struct btrace_thread_info *btp = &tp->btrace; | |
1603 | int errcode = 0; | |
1604 | ||
1605 | if (btp->target == NULL) | |
1606 | return; | |
1607 | ||
43792cf0 PA |
1608 | DEBUG ("teardown thread %s (%s)", print_thread_id (tp), |
1609 | target_pid_to_str (tp->ptid)); | |
02d27625 MM |
1610 | |
1611 | target_teardown_btrace (btp->target); | |
1612 | btp->target = NULL; | |
1613 | ||
1614 | btrace_clear (tp); | |
1615 | } | |
1616 | ||
734b0e4b | 1617 | /* Stitch branch trace in BTS format. */ |
969c39fb MM |
1618 | |
1619 | static int | |
31fd9caa | 1620 | btrace_stitch_bts (struct btrace_data_bts *btrace, struct thread_info *tp) |
969c39fb | 1621 | { |
31fd9caa | 1622 | struct btrace_thread_info *btinfo; |
969c39fb MM |
1623 | struct btrace_function *last_bfun; |
1624 | struct btrace_insn *last_insn; | |
1625 | btrace_block_s *first_new_block; | |
1626 | ||
31fd9caa | 1627 | btinfo = &tp->btrace; |
b54b03bd | 1628 | gdb_assert (!btinfo->functions.empty ()); |
31fd9caa MM |
1629 | gdb_assert (!VEC_empty (btrace_block_s, btrace->blocks)); |
1630 | ||
b54b03bd TW |
1631 | last_bfun = btinfo->functions.back (); |
1632 | ||
31fd9caa MM |
1633 | /* If the existing trace ends with a gap, we just glue the traces |
1634 | together. We need to drop the last (i.e. chronologically first) block | |
1635 | of the new trace, though, since we can't fill in the start address.*/ | |
1636 | if (VEC_empty (btrace_insn_s, last_bfun->insn)) | |
1637 | { | |
1638 | VEC_pop (btrace_block_s, btrace->blocks); | |
1639 | return 0; | |
1640 | } | |
969c39fb MM |
1641 | |
1642 | /* Beware that block trace starts with the most recent block, so the | |
1643 | chronologically first block in the new trace is the last block in | |
1644 | the new trace's block vector. */ | |
734b0e4b | 1645 | first_new_block = VEC_last (btrace_block_s, btrace->blocks); |
969c39fb MM |
1646 | last_insn = VEC_last (btrace_insn_s, last_bfun->insn); |
1647 | ||
1648 | /* If the current PC at the end of the block is the same as in our current | |
1649 | trace, there are two explanations: | |
1650 | 1. we executed the instruction and some branch brought us back. | |
1651 | 2. we have not made any progress. | |
1652 | In the first case, the delta trace vector should contain at least two | |
1653 | entries. | |
1654 | In the second case, the delta trace vector should contain exactly one | |
1655 | entry for the partial block containing the current PC. Remove it. */ | |
1656 | if (first_new_block->end == last_insn->pc | |
734b0e4b | 1657 | && VEC_length (btrace_block_s, btrace->blocks) == 1) |
969c39fb | 1658 | { |
734b0e4b | 1659 | VEC_pop (btrace_block_s, btrace->blocks); |
969c39fb MM |
1660 | return 0; |
1661 | } | |
1662 | ||
1663 | DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn), | |
1664 | core_addr_to_string_nz (first_new_block->end)); | |
1665 | ||
1666 | /* Do a simple sanity check to make sure we don't accidentally end up | |
1667 | with a bad block. This should not occur in practice. */ | |
1668 | if (first_new_block->end < last_insn->pc) | |
1669 | { | |
1670 | warning (_("Error while trying to read delta trace. Falling back to " | |
1671 | "a full read.")); | |
1672 | return -1; | |
1673 | } | |
1674 | ||
1675 | /* We adjust the last block to start at the end of our current trace. */ | |
1676 | gdb_assert (first_new_block->begin == 0); | |
1677 | first_new_block->begin = last_insn->pc; | |
1678 | ||
1679 | /* We simply pop the last insn so we can insert it again as part of | |
1680 | the normal branch trace computation. | |
1681 | Since instruction iterators are based on indices in the instructions | |
1682 | vector, we don't leave any pointers dangling. */ | |
1683 | DEBUG ("pruning insn at %s for stitching", | |
1684 | ftrace_print_insn_addr (last_insn)); | |
1685 | ||
1686 | VEC_pop (btrace_insn_s, last_bfun->insn); | |
1687 | ||
1688 | /* The instructions vector may become empty temporarily if this has | |
1689 | been the only instruction in this function segment. | |
1690 | This violates the invariant but will be remedied shortly by | |
1691 | btrace_compute_ftrace when we add the new trace. */ | |
31fd9caa MM |
1692 | |
1693 | /* The only case where this would hurt is if the entire trace consisted | |
1694 | of just that one instruction. If we remove it, we might turn the now | |
1695 | empty btrace function segment into a gap. But we don't want gaps at | |
1696 | the beginning. To avoid this, we remove the entire old trace. */ | |
b54b03bd | 1697 | if (last_bfun->number == 1 && VEC_empty (btrace_insn_s, last_bfun->insn)) |
31fd9caa MM |
1698 | btrace_clear (tp); |
1699 | ||
969c39fb MM |
1700 | return 0; |
1701 | } | |
1702 | ||
734b0e4b MM |
1703 | /* Adjust the block trace in order to stitch old and new trace together. |
1704 | BTRACE is the new delta trace between the last and the current stop. | |
31fd9caa MM |
1705 | TP is the traced thread. |
1706 | May modifx BTRACE as well as the existing trace in TP. | |
734b0e4b MM |
1707 | Return 0 on success, -1 otherwise. */ |
1708 | ||
1709 | static int | |
31fd9caa | 1710 | btrace_stitch_trace (struct btrace_data *btrace, struct thread_info *tp) |
734b0e4b MM |
1711 | { |
1712 | /* If we don't have trace, there's nothing to do. */ | |
1713 | if (btrace_data_empty (btrace)) | |
1714 | return 0; | |
1715 | ||
1716 | switch (btrace->format) | |
1717 | { | |
1718 | case BTRACE_FORMAT_NONE: | |
1719 | return 0; | |
1720 | ||
1721 | case BTRACE_FORMAT_BTS: | |
31fd9caa | 1722 | return btrace_stitch_bts (&btrace->variant.bts, tp); |
b20a6524 MM |
1723 | |
1724 | case BTRACE_FORMAT_PT: | |
1725 | /* Delta reads are not supported. */ | |
1726 | return -1; | |
734b0e4b MM |
1727 | } |
1728 | ||
1729 | internal_error (__FILE__, __LINE__, _("Unkown branch trace format.")); | |
1730 | } | |
1731 | ||
969c39fb MM |
1732 | /* Clear the branch trace histories in BTINFO. */ |
1733 | ||
1734 | static void | |
1735 | btrace_clear_history (struct btrace_thread_info *btinfo) | |
1736 | { | |
1737 | xfree (btinfo->insn_history); | |
1738 | xfree (btinfo->call_history); | |
1739 | xfree (btinfo->replay); | |
1740 | ||
1741 | btinfo->insn_history = NULL; | |
1742 | btinfo->call_history = NULL; | |
1743 | btinfo->replay = NULL; | |
1744 | } | |
1745 | ||
b0627500 MM |
1746 | /* Clear the branch trace maintenance histories in BTINFO. */ |
1747 | ||
1748 | static void | |
1749 | btrace_maint_clear (struct btrace_thread_info *btinfo) | |
1750 | { | |
1751 | switch (btinfo->data.format) | |
1752 | { | |
1753 | default: | |
1754 | break; | |
1755 | ||
1756 | case BTRACE_FORMAT_BTS: | |
1757 | btinfo->maint.variant.bts.packet_history.begin = 0; | |
1758 | btinfo->maint.variant.bts.packet_history.end = 0; | |
1759 | break; | |
1760 | ||
1761 | #if defined (HAVE_LIBIPT) | |
1762 | case BTRACE_FORMAT_PT: | |
1763 | xfree (btinfo->maint.variant.pt.packets); | |
1764 | ||
1765 | btinfo->maint.variant.pt.packets = NULL; | |
1766 | btinfo->maint.variant.pt.packet_history.begin = 0; | |
1767 | btinfo->maint.variant.pt.packet_history.end = 0; | |
1768 | break; | |
1769 | #endif /* defined (HAVE_LIBIPT) */ | |
1770 | } | |
1771 | } | |
1772 | ||
02d27625 MM |
1773 | /* See btrace.h. */ |
1774 | ||
508352a9 TW |
1775 | const char * |
1776 | btrace_decode_error (enum btrace_format format, int errcode) | |
1777 | { | |
1778 | switch (format) | |
1779 | { | |
1780 | case BTRACE_FORMAT_BTS: | |
1781 | switch (errcode) | |
1782 | { | |
1783 | case BDE_BTS_OVERFLOW: | |
1784 | return _("instruction overflow"); | |
1785 | ||
1786 | case BDE_BTS_INSN_SIZE: | |
1787 | return _("unknown instruction"); | |
1788 | ||
1789 | default: | |
1790 | break; | |
1791 | } | |
1792 | break; | |
1793 | ||
1794 | #if defined (HAVE_LIBIPT) | |
1795 | case BTRACE_FORMAT_PT: | |
1796 | switch (errcode) | |
1797 | { | |
1798 | case BDE_PT_USER_QUIT: | |
1799 | return _("trace decode cancelled"); | |
1800 | ||
1801 | case BDE_PT_DISABLED: | |
1802 | return _("disabled"); | |
1803 | ||
1804 | case BDE_PT_OVERFLOW: | |
1805 | return _("overflow"); | |
1806 | ||
1807 | default: | |
1808 | if (errcode < 0) | |
1809 | return pt_errstr (pt_errcode (errcode)); | |
1810 | break; | |
1811 | } | |
1812 | break; | |
1813 | #endif /* defined (HAVE_LIBIPT) */ | |
1814 | ||
1815 | default: | |
1816 | break; | |
1817 | } | |
1818 | ||
1819 | return _("unknown"); | |
1820 | } | |
1821 | ||
1822 | /* See btrace.h. */ | |
1823 | ||
02d27625 MM |
1824 | void |
1825 | btrace_fetch (struct thread_info *tp) | |
1826 | { | |
1827 | struct btrace_thread_info *btinfo; | |
969c39fb | 1828 | struct btrace_target_info *tinfo; |
734b0e4b | 1829 | struct btrace_data btrace; |
23a7fe75 | 1830 | struct cleanup *cleanup; |
969c39fb | 1831 | int errcode; |
02d27625 | 1832 | |
43792cf0 PA |
1833 | DEBUG ("fetch thread %s (%s)", print_thread_id (tp), |
1834 | target_pid_to_str (tp->ptid)); | |
02d27625 MM |
1835 | |
1836 | btinfo = &tp->btrace; | |
969c39fb MM |
1837 | tinfo = btinfo->target; |
1838 | if (tinfo == NULL) | |
1839 | return; | |
1840 | ||
1841 | /* There's no way we could get new trace while replaying. | |
1842 | On the other hand, delta trace would return a partial record with the | |
1843 | current PC, which is the replay PC, not the last PC, as expected. */ | |
1844 | if (btinfo->replay != NULL) | |
02d27625 MM |
1845 | return; |
1846 | ||
ae20e79a TW |
1847 | /* With CLI usage, TP->PTID always equals INFERIOR_PTID here. Now that we |
1848 | can store a gdb.Record object in Python referring to a different thread | |
1849 | than the current one, temporarily set INFERIOR_PTID. */ | |
1850 | cleanup = save_inferior_ptid (); | |
1851 | inferior_ptid = tp->ptid; | |
1852 | ||
cd4007e4 MM |
1853 | /* We should not be called on running or exited threads. */ |
1854 | gdb_assert (can_access_registers_ptid (tp->ptid)); | |
1855 | ||
734b0e4b | 1856 | btrace_data_init (&btrace); |
ae20e79a | 1857 | make_cleanup_btrace_data (&btrace); |
02d27625 | 1858 | |
969c39fb | 1859 | /* Let's first try to extend the trace we already have. */ |
b54b03bd | 1860 | if (!btinfo->functions.empty ()) |
969c39fb MM |
1861 | { |
1862 | errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_DELTA); | |
1863 | if (errcode == 0) | |
1864 | { | |
1865 | /* Success. Let's try to stitch the traces together. */ | |
31fd9caa | 1866 | errcode = btrace_stitch_trace (&btrace, tp); |
969c39fb MM |
1867 | } |
1868 | else | |
1869 | { | |
1870 | /* We failed to read delta trace. Let's try to read new trace. */ | |
1871 | errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_NEW); | |
1872 | ||
1873 | /* If we got any new trace, discard what we have. */ | |
734b0e4b | 1874 | if (errcode == 0 && !btrace_data_empty (&btrace)) |
969c39fb MM |
1875 | btrace_clear (tp); |
1876 | } | |
1877 | ||
1878 | /* If we were not able to read the trace, we start over. */ | |
1879 | if (errcode != 0) | |
1880 | { | |
1881 | btrace_clear (tp); | |
1882 | errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL); | |
1883 | } | |
1884 | } | |
1885 | else | |
1886 | errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL); | |
1887 | ||
1888 | /* If we were not able to read the branch trace, signal an error. */ | |
1889 | if (errcode != 0) | |
1890 | error (_("Failed to read branch trace.")); | |
1891 | ||
1892 | /* Compute the trace, provided we have any. */ | |
734b0e4b | 1893 | if (!btrace_data_empty (&btrace)) |
23a7fe75 | 1894 | { |
9be54cae MM |
1895 | /* Store the raw trace data. The stored data will be cleared in |
1896 | btrace_clear, so we always append the new trace. */ | |
1897 | btrace_data_append (&btinfo->data, &btrace); | |
b0627500 | 1898 | btrace_maint_clear (btinfo); |
9be54cae | 1899 | |
969c39fb | 1900 | btrace_clear_history (btinfo); |
76235df1 | 1901 | btrace_compute_ftrace (tp, &btrace); |
23a7fe75 | 1902 | } |
02d27625 | 1903 | |
23a7fe75 | 1904 | do_cleanups (cleanup); |
02d27625 MM |
1905 | } |
1906 | ||
1907 | /* See btrace.h. */ | |
1908 | ||
1909 | void | |
1910 | btrace_clear (struct thread_info *tp) | |
1911 | { | |
1912 | struct btrace_thread_info *btinfo; | |
1913 | ||
43792cf0 PA |
1914 | DEBUG ("clear thread %s (%s)", print_thread_id (tp), |
1915 | target_pid_to_str (tp->ptid)); | |
02d27625 | 1916 | |
0b722aec MM |
1917 | /* Make sure btrace frames that may hold a pointer into the branch |
1918 | trace data are destroyed. */ | |
1919 | reinit_frame_cache (); | |
1920 | ||
02d27625 | 1921 | btinfo = &tp->btrace; |
17b89b34 | 1922 | for (auto &bfun : btinfo->functions) |
23a7fe75 | 1923 | { |
17b89b34 TW |
1924 | VEC_free (btrace_insn_s, bfun->insn); |
1925 | xfree (bfun); | |
23a7fe75 MM |
1926 | } |
1927 | ||
17b89b34 | 1928 | btinfo->functions.clear (); |
31fd9caa | 1929 | btinfo->ngaps = 0; |
23a7fe75 | 1930 | |
b0627500 MM |
1931 | /* Must clear the maint data before - it depends on BTINFO->DATA. */ |
1932 | btrace_maint_clear (btinfo); | |
9be54cae | 1933 | btrace_data_clear (&btinfo->data); |
969c39fb | 1934 | btrace_clear_history (btinfo); |
02d27625 MM |
1935 | } |
1936 | ||
1937 | /* See btrace.h. */ | |
1938 | ||
1939 | void | |
1940 | btrace_free_objfile (struct objfile *objfile) | |
1941 | { | |
1942 | struct thread_info *tp; | |
1943 | ||
1944 | DEBUG ("free objfile"); | |
1945 | ||
034f788c | 1946 | ALL_NON_EXITED_THREADS (tp) |
02d27625 MM |
1947 | btrace_clear (tp); |
1948 | } | |
c12a2917 MM |
1949 | |
1950 | #if defined (HAVE_LIBEXPAT) | |
1951 | ||
1952 | /* Check the btrace document version. */ | |
1953 | ||
1954 | static void | |
1955 | check_xml_btrace_version (struct gdb_xml_parser *parser, | |
1956 | const struct gdb_xml_element *element, | |
1957 | void *user_data, VEC (gdb_xml_value_s) *attributes) | |
1958 | { | |
9a3c8263 SM |
1959 | const char *version |
1960 | = (const char *) xml_find_attribute (attributes, "version")->value; | |
c12a2917 MM |
1961 | |
1962 | if (strcmp (version, "1.0") != 0) | |
1963 | gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version); | |
1964 | } | |
1965 | ||
1966 | /* Parse a btrace "block" xml record. */ | |
1967 | ||
1968 | static void | |
1969 | parse_xml_btrace_block (struct gdb_xml_parser *parser, | |
1970 | const struct gdb_xml_element *element, | |
1971 | void *user_data, VEC (gdb_xml_value_s) *attributes) | |
1972 | { | |
734b0e4b | 1973 | struct btrace_data *btrace; |
c12a2917 MM |
1974 | struct btrace_block *block; |
1975 | ULONGEST *begin, *end; | |
1976 | ||
9a3c8263 | 1977 | btrace = (struct btrace_data *) user_data; |
734b0e4b MM |
1978 | |
1979 | switch (btrace->format) | |
1980 | { | |
1981 | case BTRACE_FORMAT_BTS: | |
1982 | break; | |
1983 | ||
1984 | case BTRACE_FORMAT_NONE: | |
1985 | btrace->format = BTRACE_FORMAT_BTS; | |
1986 | btrace->variant.bts.blocks = NULL; | |
1987 | break; | |
1988 | ||
1989 | default: | |
1990 | gdb_xml_error (parser, _("Btrace format error.")); | |
1991 | } | |
c12a2917 | 1992 | |
bc84451b SM |
1993 | begin = (ULONGEST *) xml_find_attribute (attributes, "begin")->value; |
1994 | end = (ULONGEST *) xml_find_attribute (attributes, "end")->value; | |
c12a2917 | 1995 | |
734b0e4b | 1996 | block = VEC_safe_push (btrace_block_s, btrace->variant.bts.blocks, NULL); |
c12a2917 MM |
1997 | block->begin = *begin; |
1998 | block->end = *end; | |
1999 | } | |
2000 | ||
b20a6524 MM |
2001 | /* Parse a "raw" xml record. */ |
2002 | ||
2003 | static void | |
2004 | parse_xml_raw (struct gdb_xml_parser *parser, const char *body_text, | |
e7b01ce0 | 2005 | gdb_byte **pdata, size_t *psize) |
b20a6524 MM |
2006 | { |
2007 | struct cleanup *cleanup; | |
2008 | gdb_byte *data, *bin; | |
e7b01ce0 | 2009 | size_t len, size; |
b20a6524 MM |
2010 | |
2011 | len = strlen (body_text); | |
e7b01ce0 | 2012 | if (len % 2 != 0) |
b20a6524 MM |
2013 | gdb_xml_error (parser, _("Bad raw data size.")); |
2014 | ||
e7b01ce0 MM |
2015 | size = len / 2; |
2016 | ||
224c3ddb | 2017 | bin = data = (gdb_byte *) xmalloc (size); |
b20a6524 MM |
2018 | cleanup = make_cleanup (xfree, data); |
2019 | ||
2020 | /* We use hex encoding - see common/rsp-low.h. */ | |
2021 | while (len > 0) | |
2022 | { | |
2023 | char hi, lo; | |
2024 | ||
2025 | hi = *body_text++; | |
2026 | lo = *body_text++; | |
2027 | ||
2028 | if (hi == 0 || lo == 0) | |
2029 | gdb_xml_error (parser, _("Bad hex encoding.")); | |
2030 | ||
2031 | *bin++ = fromhex (hi) * 16 + fromhex (lo); | |
2032 | len -= 2; | |
2033 | } | |
2034 | ||
2035 | discard_cleanups (cleanup); | |
2036 | ||
2037 | *pdata = data; | |
2038 | *psize = size; | |
2039 | } | |
2040 | ||
2041 | /* Parse a btrace pt-config "cpu" xml record. */ | |
2042 | ||
2043 | static void | |
2044 | parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser, | |
2045 | const struct gdb_xml_element *element, | |
2046 | void *user_data, | |
2047 | VEC (gdb_xml_value_s) *attributes) | |
2048 | { | |
2049 | struct btrace_data *btrace; | |
2050 | const char *vendor; | |
2051 | ULONGEST *family, *model, *stepping; | |
2052 | ||
9a3c8263 SM |
2053 | vendor = (const char *) xml_find_attribute (attributes, "vendor")->value; |
2054 | family = (ULONGEST *) xml_find_attribute (attributes, "family")->value; | |
2055 | model = (ULONGEST *) xml_find_attribute (attributes, "model")->value; | |
2056 | stepping = (ULONGEST *) xml_find_attribute (attributes, "stepping")->value; | |
b20a6524 | 2057 | |
9a3c8263 | 2058 | btrace = (struct btrace_data *) user_data; |
b20a6524 MM |
2059 | |
2060 | if (strcmp (vendor, "GenuineIntel") == 0) | |
2061 | btrace->variant.pt.config.cpu.vendor = CV_INTEL; | |
2062 | ||
2063 | btrace->variant.pt.config.cpu.family = *family; | |
2064 | btrace->variant.pt.config.cpu.model = *model; | |
2065 | btrace->variant.pt.config.cpu.stepping = *stepping; | |
2066 | } | |
2067 | ||
2068 | /* Parse a btrace pt "raw" xml record. */ | |
2069 | ||
2070 | static void | |
2071 | parse_xml_btrace_pt_raw (struct gdb_xml_parser *parser, | |
2072 | const struct gdb_xml_element *element, | |
2073 | void *user_data, const char *body_text) | |
2074 | { | |
2075 | struct btrace_data *btrace; | |
2076 | ||
9a3c8263 | 2077 | btrace = (struct btrace_data *) user_data; |
b20a6524 MM |
2078 | parse_xml_raw (parser, body_text, &btrace->variant.pt.data, |
2079 | &btrace->variant.pt.size); | |
2080 | } | |
2081 | ||
2082 | /* Parse a btrace "pt" xml record. */ | |
2083 | ||
2084 | static void | |
2085 | parse_xml_btrace_pt (struct gdb_xml_parser *parser, | |
2086 | const struct gdb_xml_element *element, | |
2087 | void *user_data, VEC (gdb_xml_value_s) *attributes) | |
2088 | { | |
2089 | struct btrace_data *btrace; | |
2090 | ||
9a3c8263 | 2091 | btrace = (struct btrace_data *) user_data; |
b20a6524 MM |
2092 | btrace->format = BTRACE_FORMAT_PT; |
2093 | btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN; | |
2094 | btrace->variant.pt.data = NULL; | |
2095 | btrace->variant.pt.size = 0; | |
2096 | } | |
2097 | ||
c12a2917 MM |
2098 | static const struct gdb_xml_attribute block_attributes[] = { |
2099 | { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, | |
2100 | { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, | |
2101 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
2102 | }; | |
2103 | ||
b20a6524 MM |
2104 | static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = { |
2105 | { "vendor", GDB_XML_AF_NONE, NULL, NULL }, | |
2106 | { "family", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, | |
2107 | { "model", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, | |
2108 | { "stepping", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, | |
2109 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
2110 | }; | |
2111 | ||
2112 | static const struct gdb_xml_element btrace_pt_config_children[] = { | |
2113 | { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL, | |
2114 | parse_xml_btrace_pt_config_cpu, NULL }, | |
2115 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
2116 | }; | |
2117 | ||
2118 | static const struct gdb_xml_element btrace_pt_children[] = { | |
2119 | { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL, | |
2120 | NULL }, | |
2121 | { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw }, | |
2122 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
2123 | }; | |
2124 | ||
c12a2917 MM |
2125 | static const struct gdb_xml_attribute btrace_attributes[] = { |
2126 | { "version", GDB_XML_AF_NONE, NULL, NULL }, | |
2127 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
2128 | }; | |
2129 | ||
2130 | static const struct gdb_xml_element btrace_children[] = { | |
2131 | { "block", block_attributes, NULL, | |
2132 | GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL }, | |
b20a6524 MM |
2133 | { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt, |
2134 | NULL }, | |
c12a2917 MM |
2135 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
2136 | }; | |
2137 | ||
2138 | static const struct gdb_xml_element btrace_elements[] = { | |
2139 | { "btrace", btrace_attributes, btrace_children, GDB_XML_EF_NONE, | |
2140 | check_xml_btrace_version, NULL }, | |
2141 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
2142 | }; | |
2143 | ||
2144 | #endif /* defined (HAVE_LIBEXPAT) */ | |
2145 | ||
2146 | /* See btrace.h. */ | |
2147 | ||
734b0e4b MM |
2148 | void |
2149 | parse_xml_btrace (struct btrace_data *btrace, const char *buffer) | |
c12a2917 | 2150 | { |
c12a2917 MM |
2151 | struct cleanup *cleanup; |
2152 | int errcode; | |
2153 | ||
2154 | #if defined (HAVE_LIBEXPAT) | |
2155 | ||
734b0e4b MM |
2156 | btrace->format = BTRACE_FORMAT_NONE; |
2157 | ||
2158 | cleanup = make_cleanup_btrace_data (btrace); | |
c12a2917 | 2159 | errcode = gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements, |
734b0e4b | 2160 | buffer, btrace); |
c12a2917 | 2161 | if (errcode != 0) |
969c39fb | 2162 | error (_("Error parsing branch trace.")); |
c12a2917 MM |
2163 | |
2164 | /* Keep parse results. */ | |
2165 | discard_cleanups (cleanup); | |
2166 | ||
2167 | #else /* !defined (HAVE_LIBEXPAT) */ | |
2168 | ||
2169 | error (_("Cannot process branch trace. XML parsing is not supported.")); | |
2170 | ||
2171 | #endif /* !defined (HAVE_LIBEXPAT) */ | |
c12a2917 | 2172 | } |
23a7fe75 | 2173 | |
f4abbc16 MM |
2174 | #if defined (HAVE_LIBEXPAT) |
2175 | ||
2176 | /* Parse a btrace-conf "bts" xml record. */ | |
2177 | ||
2178 | static void | |
2179 | parse_xml_btrace_conf_bts (struct gdb_xml_parser *parser, | |
2180 | const struct gdb_xml_element *element, | |
2181 | void *user_data, VEC (gdb_xml_value_s) *attributes) | |
2182 | { | |
2183 | struct btrace_config *conf; | |
d33501a5 | 2184 | struct gdb_xml_value *size; |
f4abbc16 | 2185 | |
9a3c8263 | 2186 | conf = (struct btrace_config *) user_data; |
f4abbc16 | 2187 | conf->format = BTRACE_FORMAT_BTS; |
d33501a5 MM |
2188 | conf->bts.size = 0; |
2189 | ||
2190 | size = xml_find_attribute (attributes, "size"); | |
2191 | if (size != NULL) | |
b20a6524 | 2192 | conf->bts.size = (unsigned int) *(ULONGEST *) size->value; |
f4abbc16 MM |
2193 | } |
2194 | ||
b20a6524 MM |
2195 | /* Parse a btrace-conf "pt" xml record. */ |
2196 | ||
2197 | static void | |
2198 | parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser, | |
2199 | const struct gdb_xml_element *element, | |
2200 | void *user_data, VEC (gdb_xml_value_s) *attributes) | |
2201 | { | |
2202 | struct btrace_config *conf; | |
2203 | struct gdb_xml_value *size; | |
2204 | ||
9a3c8263 | 2205 | conf = (struct btrace_config *) user_data; |
b20a6524 MM |
2206 | conf->format = BTRACE_FORMAT_PT; |
2207 | conf->pt.size = 0; | |
2208 | ||
2209 | size = xml_find_attribute (attributes, "size"); | |
2210 | if (size != NULL) | |
2211 | conf->pt.size = (unsigned int) *(ULONGEST *) size->value; | |
2212 | } | |
2213 | ||
2214 | static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = { | |
2215 | { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, | |
2216 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
2217 | }; | |
2218 | ||
d33501a5 MM |
2219 | static const struct gdb_xml_attribute btrace_conf_bts_attributes[] = { |
2220 | { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, | |
2221 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
2222 | }; | |
2223 | ||
f4abbc16 | 2224 | static const struct gdb_xml_element btrace_conf_children[] = { |
d33501a5 MM |
2225 | { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL, |
2226 | parse_xml_btrace_conf_bts, NULL }, | |
b20a6524 MM |
2227 | { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL, |
2228 | parse_xml_btrace_conf_pt, NULL }, | |
f4abbc16 MM |
2229 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
2230 | }; | |
2231 | ||
2232 | static const struct gdb_xml_attribute btrace_conf_attributes[] = { | |
2233 | { "version", GDB_XML_AF_NONE, NULL, NULL }, | |
2234 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
2235 | }; | |
2236 | ||
2237 | static const struct gdb_xml_element btrace_conf_elements[] = { | |
2238 | { "btrace-conf", btrace_conf_attributes, btrace_conf_children, | |
2239 | GDB_XML_EF_NONE, NULL, NULL }, | |
2240 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
2241 | }; | |
2242 | ||
2243 | #endif /* defined (HAVE_LIBEXPAT) */ | |
2244 | ||
2245 | /* See btrace.h. */ | |
2246 | ||
2247 | void | |
2248 | parse_xml_btrace_conf (struct btrace_config *conf, const char *xml) | |
2249 | { | |
2250 | int errcode; | |
2251 | ||
2252 | #if defined (HAVE_LIBEXPAT) | |
2253 | ||
2254 | errcode = gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd", | |
2255 | btrace_conf_elements, xml, conf); | |
2256 | if (errcode != 0) | |
2257 | error (_("Error parsing branch trace configuration.")); | |
2258 | ||
2259 | #else /* !defined (HAVE_LIBEXPAT) */ | |
2260 | ||
2261 | error (_("XML parsing is not supported.")); | |
2262 | ||
2263 | #endif /* !defined (HAVE_LIBEXPAT) */ | |
2264 | } | |
2265 | ||
23a7fe75 MM |
2266 | /* See btrace.h. */ |
2267 | ||
2268 | const struct btrace_insn * | |
2269 | btrace_insn_get (const struct btrace_insn_iterator *it) | |
2270 | { | |
2271 | const struct btrace_function *bfun; | |
2272 | unsigned int index, end; | |
2273 | ||
a0f1b963 TW |
2274 | index = it->insn_index; |
2275 | bfun = it->btinfo->functions[it->call_index]; | |
23a7fe75 | 2276 | |
31fd9caa MM |
2277 | /* Check if the iterator points to a gap in the trace. */ |
2278 | if (bfun->errcode != 0) | |
2279 | return NULL; | |
2280 | ||
23a7fe75 MM |
2281 | /* The index is within the bounds of this function's instruction vector. */ |
2282 | end = VEC_length (btrace_insn_s, bfun->insn); | |
2283 | gdb_assert (0 < end); | |
2284 | gdb_assert (index < end); | |
2285 | ||
2286 | return VEC_index (btrace_insn_s, bfun->insn, index); | |
2287 | } | |
2288 | ||
2289 | /* See btrace.h. */ | |
2290 | ||
69090cee TW |
2291 | int |
2292 | btrace_insn_get_error (const struct btrace_insn_iterator *it) | |
23a7fe75 | 2293 | { |
a0f1b963 TW |
2294 | const struct btrace_function *bfun; |
2295 | ||
2296 | bfun = it->btinfo->functions[it->call_index]; | |
2297 | return bfun->errcode; | |
69090cee | 2298 | } |
31fd9caa | 2299 | |
69090cee | 2300 | /* See btrace.h. */ |
31fd9caa | 2301 | |
69090cee TW |
2302 | unsigned int |
2303 | btrace_insn_number (const struct btrace_insn_iterator *it) | |
2304 | { | |
a0f1b963 TW |
2305 | const struct btrace_function *bfun; |
2306 | ||
2307 | bfun = it->btinfo->functions[it->call_index]; | |
2308 | return bfun->insn_offset + it->insn_index; | |
23a7fe75 MM |
2309 | } |
2310 | ||
2311 | /* See btrace.h. */ | |
2312 | ||
2313 | void | |
2314 | btrace_insn_begin (struct btrace_insn_iterator *it, | |
2315 | const struct btrace_thread_info *btinfo) | |
2316 | { | |
b54b03bd | 2317 | if (btinfo->functions.empty ()) |
23a7fe75 MM |
2318 | error (_("No trace.")); |
2319 | ||
521103fd | 2320 | it->btinfo = btinfo; |
a0f1b963 TW |
2321 | it->call_index = 0; |
2322 | it->insn_index = 0; | |
23a7fe75 MM |
2323 | } |
2324 | ||
2325 | /* See btrace.h. */ | |
2326 | ||
2327 | void | |
2328 | btrace_insn_end (struct btrace_insn_iterator *it, | |
2329 | const struct btrace_thread_info *btinfo) | |
2330 | { | |
2331 | const struct btrace_function *bfun; | |
2332 | unsigned int length; | |
2333 | ||
b54b03bd | 2334 | if (btinfo->functions.empty ()) |
23a7fe75 MM |
2335 | error (_("No trace.")); |
2336 | ||
b54b03bd | 2337 | bfun = btinfo->functions.back (); |
23a7fe75 MM |
2338 | length = VEC_length (btrace_insn_s, bfun->insn); |
2339 | ||
31fd9caa MM |
2340 | /* The last function may either be a gap or it contains the current |
2341 | instruction, which is one past the end of the execution trace; ignore | |
2342 | it. */ | |
2343 | if (length > 0) | |
2344 | length -= 1; | |
2345 | ||
521103fd | 2346 | it->btinfo = btinfo; |
a0f1b963 TW |
2347 | it->call_index = bfun->number - 1; |
2348 | it->insn_index = length; | |
23a7fe75 MM |
2349 | } |
2350 | ||
2351 | /* See btrace.h. */ | |
2352 | ||
2353 | unsigned int | |
2354 | btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride) | |
2355 | { | |
2356 | const struct btrace_function *bfun; | |
2357 | unsigned int index, steps; | |
2358 | ||
a0f1b963 | 2359 | bfun = it->btinfo->functions[it->call_index]; |
23a7fe75 | 2360 | steps = 0; |
a0f1b963 | 2361 | index = it->insn_index; |
23a7fe75 MM |
2362 | |
2363 | while (stride != 0) | |
2364 | { | |
2365 | unsigned int end, space, adv; | |
2366 | ||
2367 | end = VEC_length (btrace_insn_s, bfun->insn); | |
2368 | ||
31fd9caa MM |
2369 | /* An empty function segment represents a gap in the trace. We count |
2370 | it as one instruction. */ | |
2371 | if (end == 0) | |
2372 | { | |
2373 | const struct btrace_function *next; | |
2374 | ||
2375 | next = bfun->flow.next; | |
2376 | if (next == NULL) | |
2377 | break; | |
2378 | ||
2379 | stride -= 1; | |
2380 | steps += 1; | |
2381 | ||
2382 | bfun = next; | |
2383 | index = 0; | |
2384 | ||
2385 | continue; | |
2386 | } | |
2387 | ||
23a7fe75 MM |
2388 | gdb_assert (0 < end); |
2389 | gdb_assert (index < end); | |
2390 | ||
2391 | /* Compute the number of instructions remaining in this segment. */ | |
2392 | space = end - index; | |
2393 | ||
2394 | /* Advance the iterator as far as possible within this segment. */ | |
325fac50 | 2395 | adv = std::min (space, stride); |
23a7fe75 MM |
2396 | stride -= adv; |
2397 | index += adv; | |
2398 | steps += adv; | |
2399 | ||
2400 | /* Move to the next function if we're at the end of this one. */ | |
2401 | if (index == end) | |
2402 | { | |
2403 | const struct btrace_function *next; | |
2404 | ||
2405 | next = bfun->flow.next; | |
2406 | if (next == NULL) | |
2407 | { | |
2408 | /* We stepped past the last function. | |
2409 | ||
2410 | Let's adjust the index to point to the last instruction in | |
2411 | the previous function. */ | |
2412 | index -= 1; | |
2413 | steps -= 1; | |
2414 | break; | |
2415 | } | |
2416 | ||
2417 | /* We now point to the first instruction in the new function. */ | |
2418 | bfun = next; | |
2419 | index = 0; | |
2420 | } | |
2421 | ||
2422 | /* We did make progress. */ | |
2423 | gdb_assert (adv > 0); | |
2424 | } | |
2425 | ||
2426 | /* Update the iterator. */ | |
a0f1b963 TW |
2427 | it->call_index = bfun->number - 1; |
2428 | it->insn_index = index; | |
23a7fe75 MM |
2429 | |
2430 | return steps; | |
2431 | } | |
2432 | ||
2433 | /* See btrace.h. */ | |
2434 | ||
2435 | unsigned int | |
2436 | btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride) | |
2437 | { | |
2438 | const struct btrace_function *bfun; | |
2439 | unsigned int index, steps; | |
2440 | ||
a0f1b963 | 2441 | bfun = it->btinfo->functions[it->call_index]; |
23a7fe75 | 2442 | steps = 0; |
a0f1b963 | 2443 | index = it->insn_index; |
23a7fe75 MM |
2444 | |
2445 | while (stride != 0) | |
2446 | { | |
2447 | unsigned int adv; | |
2448 | ||
2449 | /* Move to the previous function if we're at the start of this one. */ | |
2450 | if (index == 0) | |
2451 | { | |
2452 | const struct btrace_function *prev; | |
2453 | ||
2454 | prev = bfun->flow.prev; | |
2455 | if (prev == NULL) | |
2456 | break; | |
2457 | ||
2458 | /* We point to one after the last instruction in the new function. */ | |
2459 | bfun = prev; | |
2460 | index = VEC_length (btrace_insn_s, bfun->insn); | |
2461 | ||
31fd9caa MM |
2462 | /* An empty function segment represents a gap in the trace. We count |
2463 | it as one instruction. */ | |
2464 | if (index == 0) | |
2465 | { | |
2466 | stride -= 1; | |
2467 | steps += 1; | |
2468 | ||
2469 | continue; | |
2470 | } | |
23a7fe75 MM |
2471 | } |
2472 | ||
2473 | /* Advance the iterator as far as possible within this segment. */ | |
325fac50 | 2474 | adv = std::min (index, stride); |
31fd9caa | 2475 | |
23a7fe75 MM |
2476 | stride -= adv; |
2477 | index -= adv; | |
2478 | steps += adv; | |
2479 | ||
2480 | /* We did make progress. */ | |
2481 | gdb_assert (adv > 0); | |
2482 | } | |
2483 | ||
2484 | /* Update the iterator. */ | |
a0f1b963 TW |
2485 | it->call_index = bfun->number - 1; |
2486 | it->insn_index = index; | |
23a7fe75 MM |
2487 | |
2488 | return steps; | |
2489 | } | |
2490 | ||
2491 | /* See btrace.h. */ | |
2492 | ||
2493 | int | |
2494 | btrace_insn_cmp (const struct btrace_insn_iterator *lhs, | |
2495 | const struct btrace_insn_iterator *rhs) | |
2496 | { | |
a0f1b963 | 2497 | gdb_assert (lhs->btinfo == rhs->btinfo); |
23a7fe75 | 2498 | |
a0f1b963 TW |
2499 | if (lhs->call_index != rhs->call_index) |
2500 | return lhs->call_index - rhs->call_index; | |
23a7fe75 | 2501 | |
a0f1b963 | 2502 | return lhs->insn_index - rhs->insn_index; |
23a7fe75 MM |
2503 | } |
2504 | ||
2505 | /* See btrace.h. */ | |
2506 | ||
2507 | int | |
2508 | btrace_find_insn_by_number (struct btrace_insn_iterator *it, | |
2509 | const struct btrace_thread_info *btinfo, | |
2510 | unsigned int number) | |
2511 | { | |
2512 | const struct btrace_function *bfun; | |
fdd2bd92 | 2513 | unsigned int upper, lower; |
23a7fe75 | 2514 | |
2b51eddc | 2515 | if (btinfo->functions.empty ()) |
fdd2bd92 | 2516 | return 0; |
23a7fe75 | 2517 | |
fdd2bd92 | 2518 | lower = 0; |
2b51eddc | 2519 | bfun = btinfo->functions[lower]; |
fdd2bd92 | 2520 | if (number < bfun->insn_offset) |
23a7fe75 MM |
2521 | return 0; |
2522 | ||
2b51eddc TW |
2523 | upper = btinfo->functions.size () - 1; |
2524 | bfun = btinfo->functions[upper]; | |
fdd2bd92 | 2525 | if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun)) |
23a7fe75 MM |
2526 | return 0; |
2527 | ||
fdd2bd92 TW |
2528 | /* We assume that there are no holes in the numbering. */ |
2529 | for (;;) | |
2530 | { | |
2531 | const unsigned int average = lower + (upper - lower) / 2; | |
2532 | ||
2b51eddc | 2533 | bfun = btinfo->functions[average]; |
fdd2bd92 TW |
2534 | |
2535 | if (number < bfun->insn_offset) | |
2536 | { | |
2537 | upper = average - 1; | |
2538 | continue; | |
2539 | } | |
2540 | ||
2541 | if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun)) | |
2542 | { | |
2543 | lower = average + 1; | |
2544 | continue; | |
2545 | } | |
2546 | ||
2547 | break; | |
2548 | } | |
2549 | ||
521103fd | 2550 | it->btinfo = btinfo; |
a0f1b963 TW |
2551 | it->call_index = bfun->number - 1; |
2552 | it->insn_index = number - bfun->insn_offset; | |
23a7fe75 MM |
2553 | return 1; |
2554 | } | |
2555 | ||
f158f208 TW |
2556 | /* Returns true if the recording ends with a function segment that |
2557 | contains only a single (i.e. the current) instruction. */ | |
2558 | ||
2559 | static bool | |
2560 | btrace_ends_with_single_insn (const struct btrace_thread_info *btinfo) | |
2561 | { | |
2562 | const btrace_function *bfun; | |
2563 | ||
2564 | if (btinfo->functions.empty ()) | |
2565 | return false; | |
2566 | ||
2567 | bfun = btinfo->functions.back (); | |
2568 | if (bfun->errcode != 0) | |
2569 | return false; | |
2570 | ||
2571 | return ftrace_call_num_insn (bfun) == 1; | |
2572 | } | |
2573 | ||
23a7fe75 MM |
2574 | /* See btrace.h. */ |
2575 | ||
2576 | const struct btrace_function * | |
2577 | btrace_call_get (const struct btrace_call_iterator *it) | |
2578 | { | |
f158f208 TW |
2579 | if (it->index >= it->btinfo->functions.size ()) |
2580 | return NULL; | |
2581 | ||
2582 | return it->btinfo->functions[it->index]; | |
23a7fe75 MM |
2583 | } |
2584 | ||
2585 | /* See btrace.h. */ | |
2586 | ||
2587 | unsigned int | |
2588 | btrace_call_number (const struct btrace_call_iterator *it) | |
2589 | { | |
f158f208 | 2590 | const unsigned int length = it->btinfo->functions.size (); |
23a7fe75 | 2591 | |
f158f208 TW |
2592 | /* If the last function segment contains only a single instruction (i.e. the |
2593 | current instruction), skip it. */ | |
2594 | if ((it->index == length) && btrace_ends_with_single_insn (it->btinfo)) | |
2595 | return length; | |
23a7fe75 | 2596 | |
f158f208 | 2597 | return it->index + 1; |
23a7fe75 MM |
2598 | } |
2599 | ||
2600 | /* See btrace.h. */ | |
2601 | ||
2602 | void | |
2603 | btrace_call_begin (struct btrace_call_iterator *it, | |
2604 | const struct btrace_thread_info *btinfo) | |
2605 | { | |
f158f208 | 2606 | if (btinfo->functions.empty ()) |
23a7fe75 MM |
2607 | error (_("No trace.")); |
2608 | ||
2609 | it->btinfo = btinfo; | |
f158f208 | 2610 | it->index = 0; |
23a7fe75 MM |
2611 | } |
2612 | ||
2613 | /* See btrace.h. */ | |
2614 | ||
2615 | void | |
2616 | btrace_call_end (struct btrace_call_iterator *it, | |
2617 | const struct btrace_thread_info *btinfo) | |
2618 | { | |
f158f208 | 2619 | if (btinfo->functions.empty ()) |
23a7fe75 MM |
2620 | error (_("No trace.")); |
2621 | ||
2622 | it->btinfo = btinfo; | |
f158f208 | 2623 | it->index = btinfo->functions.size (); |
23a7fe75 MM |
2624 | } |
2625 | ||
2626 | /* See btrace.h. */ | |
2627 | ||
2628 | unsigned int | |
2629 | btrace_call_next (struct btrace_call_iterator *it, unsigned int stride) | |
2630 | { | |
f158f208 | 2631 | const unsigned int length = it->btinfo->functions.size (); |
23a7fe75 | 2632 | |
f158f208 TW |
2633 | if (it->index + stride < length - 1) |
2634 | /* Default case: Simply advance the iterator. */ | |
2635 | it->index += stride; | |
2636 | else if (it->index + stride == length - 1) | |
23a7fe75 | 2637 | { |
f158f208 TW |
2638 | /* We land exactly at the last function segment. If it contains only one |
2639 | instruction (i.e. the current instruction) it is not actually part of | |
2640 | the trace. */ | |
2641 | if (btrace_ends_with_single_insn (it->btinfo)) | |
2642 | it->index = length; | |
2643 | else | |
2644 | it->index = length - 1; | |
2645 | } | |
2646 | else | |
2647 | { | |
2648 | /* We land past the last function segment and have to adjust the stride. | |
2649 | If the last function segment contains only one instruction (i.e. the | |
2650 | current instruction) it is not actually part of the trace. */ | |
2651 | if (btrace_ends_with_single_insn (it->btinfo)) | |
2652 | stride = length - it->index - 1; | |
2653 | else | |
2654 | stride = length - it->index; | |
23a7fe75 | 2655 | |
f158f208 | 2656 | it->index = length; |
23a7fe75 MM |
2657 | } |
2658 | ||
f158f208 | 2659 | return stride; |
23a7fe75 MM |
2660 | } |
2661 | ||
2662 | /* See btrace.h. */ | |
2663 | ||
2664 | unsigned int | |
2665 | btrace_call_prev (struct btrace_call_iterator *it, unsigned int stride) | |
2666 | { | |
f158f208 TW |
2667 | const unsigned int length = it->btinfo->functions.size (); |
2668 | int steps = 0; | |
23a7fe75 | 2669 | |
f158f208 | 2670 | gdb_assert (it->index <= length); |
23a7fe75 | 2671 | |
f158f208 TW |
2672 | if (stride == 0 || it->index == 0) |
2673 | return 0; | |
23a7fe75 | 2674 | |
f158f208 TW |
2675 | /* If we are at the end, the first step is a special case. If the last |
2676 | function segment contains only one instruction (i.e. the current | |
2677 | instruction) it is not actually part of the trace. To be able to step | |
2678 | over this instruction, we need at least one more function segment. */ | |
2679 | if ((it->index == length) && (length > 1)) | |
23a7fe75 | 2680 | { |
f158f208 TW |
2681 | if (btrace_ends_with_single_insn (it->btinfo)) |
2682 | it->index = length - 2; | |
2683 | else | |
2684 | it->index = length - 1; | |
23a7fe75 | 2685 | |
f158f208 TW |
2686 | steps = 1; |
2687 | stride -= 1; | |
23a7fe75 MM |
2688 | } |
2689 | ||
f158f208 TW |
2690 | stride = std::min (stride, it->index); |
2691 | ||
2692 | it->index -= stride; | |
2693 | return steps + stride; | |
23a7fe75 MM |
2694 | } |
2695 | ||
2696 | /* See btrace.h. */ | |
2697 | ||
2698 | int | |
2699 | btrace_call_cmp (const struct btrace_call_iterator *lhs, | |
2700 | const struct btrace_call_iterator *rhs) | |
2701 | { | |
f158f208 TW |
2702 | gdb_assert (lhs->btinfo == rhs->btinfo); |
2703 | return (int) (lhs->index - rhs->index); | |
23a7fe75 MM |
2704 | } |
2705 | ||
2706 | /* See btrace.h. */ | |
2707 | ||
2708 | int | |
2709 | btrace_find_call_by_number (struct btrace_call_iterator *it, | |
2710 | const struct btrace_thread_info *btinfo, | |
2711 | unsigned int number) | |
2712 | { | |
f158f208 | 2713 | const unsigned int length = btinfo->functions.size (); |
23a7fe75 | 2714 | |
f158f208 TW |
2715 | if ((number == 0) || (number > length)) |
2716 | return 0; | |
23a7fe75 | 2717 | |
f158f208 TW |
2718 | it->btinfo = btinfo; |
2719 | it->index = number - 1; | |
2720 | return 1; | |
23a7fe75 MM |
2721 | } |
2722 | ||
2723 | /* See btrace.h. */ | |
2724 | ||
2725 | void | |
2726 | btrace_set_insn_history (struct btrace_thread_info *btinfo, | |
2727 | const struct btrace_insn_iterator *begin, | |
2728 | const struct btrace_insn_iterator *end) | |
2729 | { | |
2730 | if (btinfo->insn_history == NULL) | |
8d749320 | 2731 | btinfo->insn_history = XCNEW (struct btrace_insn_history); |
23a7fe75 MM |
2732 | |
2733 | btinfo->insn_history->begin = *begin; | |
2734 | btinfo->insn_history->end = *end; | |
2735 | } | |
2736 | ||
2737 | /* See btrace.h. */ | |
2738 | ||
2739 | void | |
2740 | btrace_set_call_history (struct btrace_thread_info *btinfo, | |
2741 | const struct btrace_call_iterator *begin, | |
2742 | const struct btrace_call_iterator *end) | |
2743 | { | |
2744 | gdb_assert (begin->btinfo == end->btinfo); | |
2745 | ||
2746 | if (btinfo->call_history == NULL) | |
8d749320 | 2747 | btinfo->call_history = XCNEW (struct btrace_call_history); |
23a7fe75 MM |
2748 | |
2749 | btinfo->call_history->begin = *begin; | |
2750 | btinfo->call_history->end = *end; | |
2751 | } | |
07bbe694 MM |
2752 | |
2753 | /* See btrace.h. */ | |
2754 | ||
2755 | int | |
2756 | btrace_is_replaying (struct thread_info *tp) | |
2757 | { | |
2758 | return tp->btrace.replay != NULL; | |
2759 | } | |
6e07b1d2 MM |
2760 | |
2761 | /* See btrace.h. */ | |
2762 | ||
2763 | int | |
2764 | btrace_is_empty (struct thread_info *tp) | |
2765 | { | |
2766 | struct btrace_insn_iterator begin, end; | |
2767 | struct btrace_thread_info *btinfo; | |
2768 | ||
2769 | btinfo = &tp->btrace; | |
2770 | ||
b54b03bd | 2771 | if (btinfo->functions.empty ()) |
6e07b1d2 MM |
2772 | return 1; |
2773 | ||
2774 | btrace_insn_begin (&begin, btinfo); | |
2775 | btrace_insn_end (&end, btinfo); | |
2776 | ||
2777 | return btrace_insn_cmp (&begin, &end) == 0; | |
2778 | } | |
734b0e4b MM |
2779 | |
2780 | /* Forward the cleanup request. */ | |
2781 | ||
2782 | static void | |
2783 | do_btrace_data_cleanup (void *arg) | |
2784 | { | |
9a3c8263 | 2785 | btrace_data_fini ((struct btrace_data *) arg); |
734b0e4b MM |
2786 | } |
2787 | ||
2788 | /* See btrace.h. */ | |
2789 | ||
2790 | struct cleanup * | |
2791 | make_cleanup_btrace_data (struct btrace_data *data) | |
2792 | { | |
2793 | return make_cleanup (do_btrace_data_cleanup, data); | |
2794 | } | |
b0627500 MM |
2795 | |
2796 | #if defined (HAVE_LIBIPT) | |
2797 | ||
2798 | /* Print a single packet. */ | |
2799 | ||
2800 | static void | |
2801 | pt_print_packet (const struct pt_packet *packet) | |
2802 | { | |
2803 | switch (packet->type) | |
2804 | { | |
2805 | default: | |
2806 | printf_unfiltered (("[??: %x]"), packet->type); | |
2807 | break; | |
2808 | ||
2809 | case ppt_psb: | |
2810 | printf_unfiltered (("psb")); | |
2811 | break; | |
2812 | ||
2813 | case ppt_psbend: | |
2814 | printf_unfiltered (("psbend")); | |
2815 | break; | |
2816 | ||
2817 | case ppt_pad: | |
2818 | printf_unfiltered (("pad")); | |
2819 | break; | |
2820 | ||
2821 | case ppt_tip: | |
2822 | printf_unfiltered (("tip %u: 0x%" PRIx64 ""), | |
2823 | packet->payload.ip.ipc, | |
2824 | packet->payload.ip.ip); | |
2825 | break; | |
2826 | ||
2827 | case ppt_tip_pge: | |
2828 | printf_unfiltered (("tip.pge %u: 0x%" PRIx64 ""), | |
2829 | packet->payload.ip.ipc, | |
2830 | packet->payload.ip.ip); | |
2831 | break; | |
2832 | ||
2833 | case ppt_tip_pgd: | |
2834 | printf_unfiltered (("tip.pgd %u: 0x%" PRIx64 ""), | |
2835 | packet->payload.ip.ipc, | |
2836 | packet->payload.ip.ip); | |
2837 | break; | |
2838 | ||
2839 | case ppt_fup: | |
2840 | printf_unfiltered (("fup %u: 0x%" PRIx64 ""), | |
2841 | packet->payload.ip.ipc, | |
2842 | packet->payload.ip.ip); | |
2843 | break; | |
2844 | ||
2845 | case ppt_tnt_8: | |
2846 | printf_unfiltered (("tnt-8 %u: 0x%" PRIx64 ""), | |
2847 | packet->payload.tnt.bit_size, | |
2848 | packet->payload.tnt.payload); | |
2849 | break; | |
2850 | ||
2851 | case ppt_tnt_64: | |
2852 | printf_unfiltered (("tnt-64 %u: 0x%" PRIx64 ""), | |
2853 | packet->payload.tnt.bit_size, | |
2854 | packet->payload.tnt.payload); | |
2855 | break; | |
2856 | ||
2857 | case ppt_pip: | |
37fdfe4c MM |
2858 | printf_unfiltered (("pip %" PRIx64 "%s"), packet->payload.pip.cr3, |
2859 | packet->payload.pip.nr ? (" nr") : ("")); | |
b0627500 MM |
2860 | break; |
2861 | ||
2862 | case ppt_tsc: | |
2863 | printf_unfiltered (("tsc %" PRIx64 ""), packet->payload.tsc.tsc); | |
2864 | break; | |
2865 | ||
2866 | case ppt_cbr: | |
2867 | printf_unfiltered (("cbr %u"), packet->payload.cbr.ratio); | |
2868 | break; | |
2869 | ||
2870 | case ppt_mode: | |
2871 | switch (packet->payload.mode.leaf) | |
2872 | { | |
2873 | default: | |
2874 | printf_unfiltered (("mode %u"), packet->payload.mode.leaf); | |
2875 | break; | |
2876 | ||
2877 | case pt_mol_exec: | |
2878 | printf_unfiltered (("mode.exec%s%s"), | |
2879 | packet->payload.mode.bits.exec.csl | |
2880 | ? (" cs.l") : (""), | |
2881 | packet->payload.mode.bits.exec.csd | |
2882 | ? (" cs.d") : ("")); | |
2883 | break; | |
2884 | ||
2885 | case pt_mol_tsx: | |
2886 | printf_unfiltered (("mode.tsx%s%s"), | |
2887 | packet->payload.mode.bits.tsx.intx | |
2888 | ? (" intx") : (""), | |
2889 | packet->payload.mode.bits.tsx.abrt | |
2890 | ? (" abrt") : ("")); | |
2891 | break; | |
2892 | } | |
2893 | break; | |
2894 | ||
2895 | case ppt_ovf: | |
2896 | printf_unfiltered (("ovf")); | |
2897 | break; | |
2898 | ||
37fdfe4c MM |
2899 | case ppt_stop: |
2900 | printf_unfiltered (("stop")); | |
2901 | break; | |
2902 | ||
2903 | case ppt_vmcs: | |
2904 | printf_unfiltered (("vmcs %" PRIx64 ""), packet->payload.vmcs.base); | |
2905 | break; | |
2906 | ||
2907 | case ppt_tma: | |
2908 | printf_unfiltered (("tma %x %x"), packet->payload.tma.ctc, | |
2909 | packet->payload.tma.fc); | |
2910 | break; | |
2911 | ||
2912 | case ppt_mtc: | |
2913 | printf_unfiltered (("mtc %x"), packet->payload.mtc.ctc); | |
2914 | break; | |
2915 | ||
2916 | case ppt_cyc: | |
2917 | printf_unfiltered (("cyc %" PRIx64 ""), packet->payload.cyc.value); | |
2918 | break; | |
2919 | ||
2920 | case ppt_mnt: | |
2921 | printf_unfiltered (("mnt %" PRIx64 ""), packet->payload.mnt.payload); | |
2922 | break; | |
b0627500 MM |
2923 | } |
2924 | } | |
2925 | ||
2926 | /* Decode packets into MAINT using DECODER. */ | |
2927 | ||
2928 | static void | |
2929 | btrace_maint_decode_pt (struct btrace_maint_info *maint, | |
2930 | struct pt_packet_decoder *decoder) | |
2931 | { | |
2932 | int errcode; | |
2933 | ||
2934 | for (;;) | |
2935 | { | |
2936 | struct btrace_pt_packet packet; | |
2937 | ||
2938 | errcode = pt_pkt_sync_forward (decoder); | |
2939 | if (errcode < 0) | |
2940 | break; | |
2941 | ||
2942 | for (;;) | |
2943 | { | |
2944 | pt_pkt_get_offset (decoder, &packet.offset); | |
2945 | ||
2946 | errcode = pt_pkt_next (decoder, &packet.packet, | |
2947 | sizeof(packet.packet)); | |
2948 | if (errcode < 0) | |
2949 | break; | |
2950 | ||
2951 | if (maint_btrace_pt_skip_pad == 0 || packet.packet.type != ppt_pad) | |
2952 | { | |
2953 | packet.errcode = pt_errcode (errcode); | |
2954 | VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets, | |
2955 | &packet); | |
2956 | } | |
2957 | } | |
2958 | ||
2959 | if (errcode == -pte_eos) | |
2960 | break; | |
2961 | ||
2962 | packet.errcode = pt_errcode (errcode); | |
2963 | VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets, | |
2964 | &packet); | |
2965 | ||
2966 | warning (_("Error at trace offset 0x%" PRIx64 ": %s."), | |
2967 | packet.offset, pt_errstr (packet.errcode)); | |
2968 | } | |
2969 | ||
2970 | if (errcode != -pte_eos) | |
bc504a31 | 2971 | warning (_("Failed to synchronize onto the Intel Processor Trace " |
b0627500 MM |
2972 | "stream: %s."), pt_errstr (pt_errcode (errcode))); |
2973 | } | |
2974 | ||
2975 | /* Update the packet history in BTINFO. */ | |
2976 | ||
2977 | static void | |
2978 | btrace_maint_update_pt_packets (struct btrace_thread_info *btinfo) | |
2979 | { | |
2980 | volatile struct gdb_exception except; | |
2981 | struct pt_packet_decoder *decoder; | |
2982 | struct btrace_data_pt *pt; | |
2983 | struct pt_config config; | |
2984 | int errcode; | |
2985 | ||
2986 | pt = &btinfo->data.variant.pt; | |
2987 | ||
2988 | /* Nothing to do if there is no trace. */ | |
2989 | if (pt->size == 0) | |
2990 | return; | |
2991 | ||
2992 | memset (&config, 0, sizeof(config)); | |
2993 | ||
2994 | config.size = sizeof (config); | |
2995 | config.begin = pt->data; | |
2996 | config.end = pt->data + pt->size; | |
2997 | ||
2998 | config.cpu.vendor = pt_translate_cpu_vendor (pt->config.cpu.vendor); | |
2999 | config.cpu.family = pt->config.cpu.family; | |
3000 | config.cpu.model = pt->config.cpu.model; | |
3001 | config.cpu.stepping = pt->config.cpu.stepping; | |
3002 | ||
3003 | errcode = pt_cpu_errata (&config.errata, &config.cpu); | |
3004 | if (errcode < 0) | |
bc504a31 | 3005 | error (_("Failed to configure the Intel Processor Trace decoder: %s."), |
b0627500 MM |
3006 | pt_errstr (pt_errcode (errcode))); |
3007 | ||
3008 | decoder = pt_pkt_alloc_decoder (&config); | |
3009 | if (decoder == NULL) | |
bc504a31 | 3010 | error (_("Failed to allocate the Intel Processor Trace decoder.")); |
b0627500 MM |
3011 | |
3012 | TRY | |
3013 | { | |
3014 | btrace_maint_decode_pt (&btinfo->maint, decoder); | |
3015 | } | |
3016 | CATCH (except, RETURN_MASK_ALL) | |
3017 | { | |
3018 | pt_pkt_free_decoder (decoder); | |
3019 | ||
3020 | if (except.reason < 0) | |
3021 | throw_exception (except); | |
3022 | } | |
3023 | END_CATCH | |
3024 | ||
3025 | pt_pkt_free_decoder (decoder); | |
3026 | } | |
3027 | ||
3028 | #endif /* !defined (HAVE_LIBIPT) */ | |
3029 | ||
3030 | /* Update the packet maintenance information for BTINFO and store the | |
3031 | low and high bounds into BEGIN and END, respectively. | |
3032 | Store the current iterator state into FROM and TO. */ | |
3033 | ||
3034 | static void | |
3035 | btrace_maint_update_packets (struct btrace_thread_info *btinfo, | |
3036 | unsigned int *begin, unsigned int *end, | |
3037 | unsigned int *from, unsigned int *to) | |
3038 | { | |
3039 | switch (btinfo->data.format) | |
3040 | { | |
3041 | default: | |
3042 | *begin = 0; | |
3043 | *end = 0; | |
3044 | *from = 0; | |
3045 | *to = 0; | |
3046 | break; | |
3047 | ||
3048 | case BTRACE_FORMAT_BTS: | |
3049 | /* Nothing to do - we operate directly on BTINFO->DATA. */ | |
3050 | *begin = 0; | |
3051 | *end = VEC_length (btrace_block_s, btinfo->data.variant.bts.blocks); | |
3052 | *from = btinfo->maint.variant.bts.packet_history.begin; | |
3053 | *to = btinfo->maint.variant.bts.packet_history.end; | |
3054 | break; | |
3055 | ||
3056 | #if defined (HAVE_LIBIPT) | |
3057 | case BTRACE_FORMAT_PT: | |
3058 | if (VEC_empty (btrace_pt_packet_s, btinfo->maint.variant.pt.packets)) | |
3059 | btrace_maint_update_pt_packets (btinfo); | |
3060 | ||
3061 | *begin = 0; | |
3062 | *end = VEC_length (btrace_pt_packet_s, btinfo->maint.variant.pt.packets); | |
3063 | *from = btinfo->maint.variant.pt.packet_history.begin; | |
3064 | *to = btinfo->maint.variant.pt.packet_history.end; | |
3065 | break; | |
3066 | #endif /* defined (HAVE_LIBIPT) */ | |
3067 | } | |
3068 | } | |
3069 | ||
3070 | /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and | |
3071 | update the current iterator position. */ | |
3072 | ||
3073 | static void | |
3074 | btrace_maint_print_packets (struct btrace_thread_info *btinfo, | |
3075 | unsigned int begin, unsigned int end) | |
3076 | { | |
3077 | switch (btinfo->data.format) | |
3078 | { | |
3079 | default: | |
3080 | break; | |
3081 | ||
3082 | case BTRACE_FORMAT_BTS: | |
3083 | { | |
3084 | VEC (btrace_block_s) *blocks; | |
3085 | unsigned int blk; | |
3086 | ||
3087 | blocks = btinfo->data.variant.bts.blocks; | |
3088 | for (blk = begin; blk < end; ++blk) | |
3089 | { | |
3090 | const btrace_block_s *block; | |
3091 | ||
3092 | block = VEC_index (btrace_block_s, blocks, blk); | |
3093 | ||
3094 | printf_unfiltered ("%u\tbegin: %s, end: %s\n", blk, | |
3095 | core_addr_to_string_nz (block->begin), | |
3096 | core_addr_to_string_nz (block->end)); | |
3097 | } | |
3098 | ||
3099 | btinfo->maint.variant.bts.packet_history.begin = begin; | |
3100 | btinfo->maint.variant.bts.packet_history.end = end; | |
3101 | } | |
3102 | break; | |
3103 | ||
3104 | #if defined (HAVE_LIBIPT) | |
3105 | case BTRACE_FORMAT_PT: | |
3106 | { | |
3107 | VEC (btrace_pt_packet_s) *packets; | |
3108 | unsigned int pkt; | |
3109 | ||
3110 | packets = btinfo->maint.variant.pt.packets; | |
3111 | for (pkt = begin; pkt < end; ++pkt) | |
3112 | { | |
3113 | const struct btrace_pt_packet *packet; | |
3114 | ||
3115 | packet = VEC_index (btrace_pt_packet_s, packets, pkt); | |
3116 | ||
3117 | printf_unfiltered ("%u\t", pkt); | |
3118 | printf_unfiltered ("0x%" PRIx64 "\t", packet->offset); | |
3119 | ||
3120 | if (packet->errcode == pte_ok) | |
3121 | pt_print_packet (&packet->packet); | |
3122 | else | |
3123 | printf_unfiltered ("[error: %s]", pt_errstr (packet->errcode)); | |
3124 | ||
3125 | printf_unfiltered ("\n"); | |
3126 | } | |
3127 | ||
3128 | btinfo->maint.variant.pt.packet_history.begin = begin; | |
3129 | btinfo->maint.variant.pt.packet_history.end = end; | |
3130 | } | |
3131 | break; | |
3132 | #endif /* defined (HAVE_LIBIPT) */ | |
3133 | } | |
3134 | } | |
3135 | ||
3136 | /* Read a number from an argument string. */ | |
3137 | ||
3138 | static unsigned int | |
3139 | get_uint (char **arg) | |
3140 | { | |
3141 | char *begin, *end, *pos; | |
3142 | unsigned long number; | |
3143 | ||
3144 | begin = *arg; | |
3145 | pos = skip_spaces (begin); | |
3146 | ||
3147 | if (!isdigit (*pos)) | |
3148 | error (_("Expected positive number, got: %s."), pos); | |
3149 | ||
3150 | number = strtoul (pos, &end, 10); | |
3151 | if (number > UINT_MAX) | |
3152 | error (_("Number too big.")); | |
3153 | ||
3154 | *arg += (end - begin); | |
3155 | ||
3156 | return (unsigned int) number; | |
3157 | } | |
3158 | ||
3159 | /* Read a context size from an argument string. */ | |
3160 | ||
3161 | static int | |
3162 | get_context_size (char **arg) | |
3163 | { | |
3164 | char *pos; | |
3165 | int number; | |
3166 | ||
3167 | pos = skip_spaces (*arg); | |
3168 | ||
3169 | if (!isdigit (*pos)) | |
3170 | error (_("Expected positive number, got: %s."), pos); | |
3171 | ||
3172 | return strtol (pos, arg, 10); | |
3173 | } | |
3174 | ||
3175 | /* Complain about junk at the end of an argument string. */ | |
3176 | ||
3177 | static void | |
3178 | no_chunk (char *arg) | |
3179 | { | |
3180 | if (*arg != 0) | |
3181 | error (_("Junk after argument: %s."), arg); | |
3182 | } | |
3183 | ||
3184 | /* The "maintenance btrace packet-history" command. */ | |
3185 | ||
3186 | static void | |
3187 | maint_btrace_packet_history_cmd (char *arg, int from_tty) | |
3188 | { | |
3189 | struct btrace_thread_info *btinfo; | |
3190 | struct thread_info *tp; | |
3191 | unsigned int size, begin, end, from, to; | |
3192 | ||
3193 | tp = find_thread_ptid (inferior_ptid); | |
3194 | if (tp == NULL) | |
3195 | error (_("No thread.")); | |
3196 | ||
3197 | size = 10; | |
3198 | btinfo = &tp->btrace; | |
3199 | ||
3200 | btrace_maint_update_packets (btinfo, &begin, &end, &from, &to); | |
3201 | if (begin == end) | |
3202 | { | |
3203 | printf_unfiltered (_("No trace.\n")); | |
3204 | return; | |
3205 | } | |
3206 | ||
3207 | if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0) | |
3208 | { | |
3209 | from = to; | |
3210 | ||
3211 | if (end - from < size) | |
3212 | size = end - from; | |
3213 | to = from + size; | |
3214 | } | |
3215 | else if (strcmp (arg, "-") == 0) | |
3216 | { | |
3217 | to = from; | |
3218 | ||
3219 | if (to - begin < size) | |
3220 | size = to - begin; | |
3221 | from = to - size; | |
3222 | } | |
3223 | else | |
3224 | { | |
3225 | from = get_uint (&arg); | |
3226 | if (end <= from) | |
3227 | error (_("'%u' is out of range."), from); | |
3228 | ||
3229 | arg = skip_spaces (arg); | |
3230 | if (*arg == ',') | |
3231 | { | |
3232 | arg = skip_spaces (++arg); | |
3233 | ||
3234 | if (*arg == '+') | |
3235 | { | |
3236 | arg += 1; | |
3237 | size = get_context_size (&arg); | |
3238 | ||
3239 | no_chunk (arg); | |
3240 | ||
3241 | if (end - from < size) | |
3242 | size = end - from; | |
3243 | to = from + size; | |
3244 | } | |
3245 | else if (*arg == '-') | |
3246 | { | |
3247 | arg += 1; | |
3248 | size = get_context_size (&arg); | |
3249 | ||
3250 | no_chunk (arg); | |
3251 | ||
3252 | /* Include the packet given as first argument. */ | |
3253 | from += 1; | |
3254 | to = from; | |
3255 | ||
3256 | if (to - begin < size) | |
3257 | size = to - begin; | |
3258 | from = to - size; | |
3259 | } | |
3260 | else | |
3261 | { | |
3262 | to = get_uint (&arg); | |
3263 | ||
3264 | /* Include the packet at the second argument and silently | |
3265 | truncate the range. */ | |
3266 | if (to < end) | |
3267 | to += 1; | |
3268 | else | |
3269 | to = end; | |
3270 | ||
3271 | no_chunk (arg); | |
3272 | } | |
3273 | } | |
3274 | else | |
3275 | { | |
3276 | no_chunk (arg); | |
3277 | ||
3278 | if (end - from < size) | |
3279 | size = end - from; | |
3280 | to = from + size; | |
3281 | } | |
3282 | ||
3283 | dont_repeat (); | |
3284 | } | |
3285 | ||
3286 | btrace_maint_print_packets (btinfo, from, to); | |
3287 | } | |
3288 | ||
3289 | /* The "maintenance btrace clear-packet-history" command. */ | |
3290 | ||
3291 | static void | |
3292 | maint_btrace_clear_packet_history_cmd (char *args, int from_tty) | |
3293 | { | |
3294 | struct btrace_thread_info *btinfo; | |
3295 | struct thread_info *tp; | |
3296 | ||
3297 | if (args != NULL && *args != 0) | |
3298 | error (_("Invalid argument.")); | |
3299 | ||
3300 | tp = find_thread_ptid (inferior_ptid); | |
3301 | if (tp == NULL) | |
3302 | error (_("No thread.")); | |
3303 | ||
3304 | btinfo = &tp->btrace; | |
3305 | ||
3306 | /* Must clear the maint data before - it depends on BTINFO->DATA. */ | |
3307 | btrace_maint_clear (btinfo); | |
3308 | btrace_data_clear (&btinfo->data); | |
3309 | } | |
3310 | ||
3311 | /* The "maintenance btrace clear" command. */ | |
3312 | ||
3313 | static void | |
3314 | maint_btrace_clear_cmd (char *args, int from_tty) | |
3315 | { | |
3316 | struct btrace_thread_info *btinfo; | |
3317 | struct thread_info *tp; | |
3318 | ||
3319 | if (args != NULL && *args != 0) | |
3320 | error (_("Invalid argument.")); | |
3321 | ||
3322 | tp = find_thread_ptid (inferior_ptid); | |
3323 | if (tp == NULL) | |
3324 | error (_("No thread.")); | |
3325 | ||
3326 | btrace_clear (tp); | |
3327 | } | |
3328 | ||
3329 | /* The "maintenance btrace" command. */ | |
3330 | ||
3331 | static void | |
3332 | maint_btrace_cmd (char *args, int from_tty) | |
3333 | { | |
3334 | help_list (maint_btrace_cmdlist, "maintenance btrace ", all_commands, | |
3335 | gdb_stdout); | |
3336 | } | |
3337 | ||
3338 | /* The "maintenance set btrace" command. */ | |
3339 | ||
3340 | static void | |
3341 | maint_btrace_set_cmd (char *args, int from_tty) | |
3342 | { | |
3343 | help_list (maint_btrace_set_cmdlist, "maintenance set btrace ", all_commands, | |
3344 | gdb_stdout); | |
3345 | } | |
3346 | ||
3347 | /* The "maintenance show btrace" command. */ | |
3348 | ||
3349 | static void | |
3350 | maint_btrace_show_cmd (char *args, int from_tty) | |
3351 | { | |
3352 | help_list (maint_btrace_show_cmdlist, "maintenance show btrace ", | |
3353 | all_commands, gdb_stdout); | |
3354 | } | |
3355 | ||
3356 | /* The "maintenance set btrace pt" command. */ | |
3357 | ||
3358 | static void | |
3359 | maint_btrace_pt_set_cmd (char *args, int from_tty) | |
3360 | { | |
3361 | help_list (maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ", | |
3362 | all_commands, gdb_stdout); | |
3363 | } | |
3364 | ||
3365 | /* The "maintenance show btrace pt" command. */ | |
3366 | ||
3367 | static void | |
3368 | maint_btrace_pt_show_cmd (char *args, int from_tty) | |
3369 | { | |
3370 | help_list (maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ", | |
3371 | all_commands, gdb_stdout); | |
3372 | } | |
3373 | ||
3374 | /* The "maintenance info btrace" command. */ | |
3375 | ||
3376 | static void | |
3377 | maint_info_btrace_cmd (char *args, int from_tty) | |
3378 | { | |
3379 | struct btrace_thread_info *btinfo; | |
3380 | struct thread_info *tp; | |
3381 | const struct btrace_config *conf; | |
3382 | ||
3383 | if (args != NULL && *args != 0) | |
3384 | error (_("Invalid argument.")); | |
3385 | ||
3386 | tp = find_thread_ptid (inferior_ptid); | |
3387 | if (tp == NULL) | |
3388 | error (_("No thread.")); | |
3389 | ||
3390 | btinfo = &tp->btrace; | |
3391 | ||
3392 | conf = btrace_conf (btinfo); | |
3393 | if (conf == NULL) | |
3394 | error (_("No btrace configuration.")); | |
3395 | ||
3396 | printf_unfiltered (_("Format: %s.\n"), | |
3397 | btrace_format_string (conf->format)); | |
3398 | ||
3399 | switch (conf->format) | |
3400 | { | |
3401 | default: | |
3402 | break; | |
3403 | ||
3404 | case BTRACE_FORMAT_BTS: | |
3405 | printf_unfiltered (_("Number of packets: %u.\n"), | |
3406 | VEC_length (btrace_block_s, | |
3407 | btinfo->data.variant.bts.blocks)); | |
3408 | break; | |
3409 | ||
3410 | #if defined (HAVE_LIBIPT) | |
3411 | case BTRACE_FORMAT_PT: | |
3412 | { | |
3413 | struct pt_version version; | |
3414 | ||
3415 | version = pt_library_version (); | |
3416 | printf_unfiltered (_("Version: %u.%u.%u%s.\n"), version.major, | |
3417 | version.minor, version.build, | |
3418 | version.ext != NULL ? version.ext : ""); | |
3419 | ||
3420 | btrace_maint_update_pt_packets (btinfo); | |
3421 | printf_unfiltered (_("Number of packets: %u.\n"), | |
3422 | VEC_length (btrace_pt_packet_s, | |
3423 | btinfo->maint.variant.pt.packets)); | |
3424 | } | |
3425 | break; | |
3426 | #endif /* defined (HAVE_LIBIPT) */ | |
3427 | } | |
3428 | } | |
3429 | ||
3430 | /* The "maint show btrace pt skip-pad" show value function. */ | |
3431 | ||
3432 | static void | |
3433 | show_maint_btrace_pt_skip_pad (struct ui_file *file, int from_tty, | |
3434 | struct cmd_list_element *c, | |
3435 | const char *value) | |
3436 | { | |
3437 | fprintf_filtered (file, _("Skip PAD packets is %s.\n"), value); | |
3438 | } | |
3439 | ||
3440 | ||
3441 | /* Initialize btrace maintenance commands. */ | |
3442 | ||
3443 | void _initialize_btrace (void); | |
3444 | void | |
3445 | _initialize_btrace (void) | |
3446 | { | |
3447 | add_cmd ("btrace", class_maintenance, maint_info_btrace_cmd, | |
3448 | _("Info about branch tracing data."), &maintenanceinfolist); | |
3449 | ||
3450 | add_prefix_cmd ("btrace", class_maintenance, maint_btrace_cmd, | |
3451 | _("Branch tracing maintenance commands."), | |
3452 | &maint_btrace_cmdlist, "maintenance btrace ", | |
3453 | 0, &maintenancelist); | |
3454 | ||
3455 | add_prefix_cmd ("btrace", class_maintenance, maint_btrace_set_cmd, _("\ | |
3456 | Set branch tracing specific variables."), | |
3457 | &maint_btrace_set_cmdlist, "maintenance set btrace ", | |
3458 | 0, &maintenance_set_cmdlist); | |
3459 | ||
3460 | add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_set_cmd, _("\ | |
bc504a31 | 3461 | Set Intel Processor Trace specific variables."), |
b0627500 MM |
3462 | &maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ", |
3463 | 0, &maint_btrace_set_cmdlist); | |
3464 | ||
3465 | add_prefix_cmd ("btrace", class_maintenance, maint_btrace_show_cmd, _("\ | |
3466 | Show branch tracing specific variables."), | |
3467 | &maint_btrace_show_cmdlist, "maintenance show btrace ", | |
3468 | 0, &maintenance_show_cmdlist); | |
3469 | ||
3470 | add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_show_cmd, _("\ | |
bc504a31 | 3471 | Show Intel Processor Trace specific variables."), |
b0627500 MM |
3472 | &maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ", |
3473 | 0, &maint_btrace_show_cmdlist); | |
3474 | ||
3475 | add_setshow_boolean_cmd ("skip-pad", class_maintenance, | |
3476 | &maint_btrace_pt_skip_pad, _("\ | |
3477 | Set whether PAD packets should be skipped in the btrace packet history."), _("\ | |
3478 | Show whether PAD packets should be skipped in the btrace packet history."),_("\ | |
3479 | When enabled, PAD packets are ignored in the btrace packet history."), | |
3480 | NULL, show_maint_btrace_pt_skip_pad, | |
3481 | &maint_btrace_pt_set_cmdlist, | |
3482 | &maint_btrace_pt_show_cmdlist); | |
3483 | ||
3484 | add_cmd ("packet-history", class_maintenance, maint_btrace_packet_history_cmd, | |
3485 | _("Print the raw branch tracing data.\n\ | |
3486 | With no argument, print ten more packets after the previous ten-line print.\n\ | |
3487 | With '-' as argument print ten packets before a previous ten-line print.\n\ | |
3488 | One argument specifies the starting packet of a ten-line print.\n\ | |
3489 | Two arguments with comma between specify starting and ending packets to \ | |
3490 | print.\n\ | |
3491 | Preceded with '+'/'-' the second argument specifies the distance from the \ | |
3492 | first.\n"), | |
3493 | &maint_btrace_cmdlist); | |
3494 | ||
3495 | add_cmd ("clear-packet-history", class_maintenance, | |
3496 | maint_btrace_clear_packet_history_cmd, | |
3497 | _("Clears the branch tracing packet history.\n\ | |
3498 | Discards the raw branch tracing data but not the execution history data.\n\ | |
3499 | "), | |
3500 | &maint_btrace_cmdlist); | |
3501 | ||
3502 | add_cmd ("clear", class_maintenance, maint_btrace_clear_cmd, | |
3503 | _("Clears the branch tracing data.\n\ | |
3504 | Discards the raw branch tracing data and the execution history data.\n\ | |
3505 | The next 'record' command will fetch the branch tracing data anew.\n\ | |
3506 | "), | |
3507 | &maint_btrace_cmdlist); | |
3508 | ||
3509 | } |