1 /* Tracing functionality for remote targets in custom GDB protocol 2 3 Copyright (C) 1997-2023 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 #include "arch-utils.h" 22 #include "symtab.h" 23 #include "frame.h" 24 #include "gdbtypes.h" 25 #include "expression.h" 26 #include "gdbcmd.h" 27 #include "value.h" 28 #include "target.h" 29 #include "target-dcache.h" 30 #include "language.h" 31 #include "inferior.h" 32 #include "breakpoint.h" 33 #include "tracepoint.h" 34 #include "linespec.h" 35 #include "regcache.h" 36 #include "completer.h" 37 #include "block.h" 38 #include "dictionary.h" 39 #include "observable.h" 40 #include "user-regs.h" 41 #include "valprint.h" 42 #include "gdbcore.h" 43 #include "objfiles.h" 44 #include "filenames.h" 45 #include "gdbthread.h" 46 #include "stack.h" 47 #include "remote.h" 48 #include "source.h" 49 #include "ax.h" 50 #include "ax-gdb.h" 51 #include "memrange.h" 52 #include "cli/cli-utils.h" 53 #include "probe.h" 54 #include "gdbsupport/filestuff.h" 55 #include "gdbsupport/rsp-low.h" 56 #include "tracefile.h" 57 #include "location.h" 58 #include <algorithm> 59 #include "cli/cli-style.h" 60 #include "expop.h" 61 #include "gdbsupport/buildargv.h" 62 63 #include <unistd.h> 64 65 /* Maximum length of an agent aexpression. 66 This accounts for the fact that packets are limited to 400 bytes 67 (which includes everything -- including the checksum), and assumes 68 the worst case of maximum length for each of the pieces of a 69 continuation packet. 70 71 NOTE: expressions get mem2hex'ed otherwise this would be twice as 72 large. (400 - 31)/2 == 184 */ 73 #define MAX_AGENT_EXPR_LEN 184 74 75 /* 76 Tracepoint.c: 77 78 This module defines the following debugger commands: 79 trace : set a tracepoint on a function, line, or address. 80 info trace : list all debugger-defined tracepoints. 81 delete trace : delete one or more tracepoints. 82 enable trace : enable one or more tracepoints. 83 disable trace : disable one or more tracepoints. 84 actions : specify actions to be taken at a tracepoint. 85 passcount : specify a pass count for a tracepoint. 86 tstart : start a trace experiment. 87 tstop : stop a trace experiment. 88 tstatus : query the status of a trace experiment. 89 tfind : find a trace frame in the trace buffer. 90 tdump : print everything collected at the current tracepoint. 91 save-tracepoints : write tracepoint setup into a file. 92 93 This module defines the following user-visible debugger variables: 94 $trace_frame : sequence number of trace frame currently being debugged. 95 $trace_line : source line of trace frame currently being debugged. 96 $trace_file : source file of trace frame currently being debugged. 97 $tracepoint : tracepoint number of trace frame currently being debugged. 98 */ 99 100 101 /* ======= Important global variables: ======= */ 102 103 /* The list of all trace state variables. We don't retain pointers to 104 any of these for any reason - API is by name or number only - so it 105 works to have a vector of objects. */ 106 107 static std::vector<trace_state_variable> tvariables; 108 109 /* The next integer to assign to a variable. */ 110 111 static int next_tsv_number = 1; 112 113 /* Number of last traceframe collected. */ 114 static int traceframe_number; 115 116 /* Tracepoint for last traceframe collected. */ 117 static int tracepoint_number; 118 119 /* The traceframe info of the current traceframe. NULL if we haven't 120 yet attempted to fetch it, or if the target does not support 121 fetching this object, or if we're not inspecting a traceframe 122 presently. */ 123 static traceframe_info_up current_traceframe_info; 124 125 /* Tracing command lists. */ 126 static struct cmd_list_element *tfindlist; 127 128 /* List of expressions to collect by default at each tracepoint hit. */ 129 std::string default_collect; 130 131 static bool disconnected_tracing; 132 133 /* This variable controls whether we ask the target for a linear or 134 circular trace buffer. */ 135 136 static bool circular_trace_buffer; 137 138 /* This variable is the requested trace buffer size, or -1 to indicate 139 that we don't care and leave it up to the target to set a size. */ 140 141 static int trace_buffer_size = -1; 142 143 /* Textual notes applying to the current and/or future trace runs. */ 144 145 static std::string trace_user; 146 147 /* Textual notes applying to the current and/or future trace runs. */ 148 149 static std::string trace_notes; 150 151 /* Textual notes applying to the stopping of a trace. */ 152 153 static std::string trace_stop_notes; 154 155 /* support routines */ 156 157 struct collection_list; 158 static char *mem2hex (gdb_byte *, char *, int); 159 160 static counted_command_line all_tracepoint_actions (struct breakpoint *); 161 162 static struct trace_status trace_status; 163 164 const char *stop_reason_names[] = { 165 "tunknown", 166 "tnotrun", 167 "tstop", 168 "tfull", 169 "tdisconnected", 170 "tpasscount", 171 "terror" 172 }; 173 174 struct trace_status * 175 current_trace_status (void) 176 { 177 return &trace_status; 178 } 179 180 /* Free and clear the traceframe info cache of the current 181 traceframe. */ 182 183 static void 184 clear_traceframe_info (void) 185 { 186 current_traceframe_info = NULL; 187 } 188 189 /* Set traceframe number to NUM. */ 190 static void 191 set_traceframe_num (int num) 192 { 193 traceframe_number = num; 194 set_internalvar_integer (lookup_internalvar ("trace_frame"), num); 195 } 196 197 /* Set tracepoint number to NUM. */ 198 static void 199 set_tracepoint_num (int num) 200 { 201 tracepoint_number = num; 202 set_internalvar_integer (lookup_internalvar ("tracepoint"), num); 203 } 204 205 /* Set externally visible debug variables for querying/printing 206 the traceframe context (line, function, file). */ 207 208 static void 209 set_traceframe_context (frame_info_ptr trace_frame) 210 { 211 CORE_ADDR trace_pc; 212 struct symbol *traceframe_fun; 213 symtab_and_line traceframe_sal; 214 215 /* Save as globals for internal use. */ 216 if (trace_frame != NULL 217 && get_frame_pc_if_available (trace_frame, &trace_pc)) 218 { 219 traceframe_sal = find_pc_line (trace_pc, 0); 220 traceframe_fun = find_pc_function (trace_pc); 221 222 /* Save linenumber as "$trace_line", a debugger variable visible to 223 users. */ 224 set_internalvar_integer (lookup_internalvar ("trace_line"), 225 traceframe_sal.line); 226 } 227 else 228 { 229 traceframe_fun = NULL; 230 set_internalvar_integer (lookup_internalvar ("trace_line"), -1); 231 } 232 233 /* Save func name as "$trace_func", a debugger variable visible to 234 users. */ 235 if (traceframe_fun == NULL 236 || traceframe_fun->linkage_name () == NULL) 237 clear_internalvar (lookup_internalvar ("trace_func")); 238 else 239 set_internalvar_string (lookup_internalvar ("trace_func"), 240 traceframe_fun->linkage_name ()); 241 242 /* Save file name as "$trace_file", a debugger variable visible to 243 users. */ 244 if (traceframe_sal.symtab == NULL) 245 clear_internalvar (lookup_internalvar ("trace_file")); 246 else 247 set_internalvar_string (lookup_internalvar ("trace_file"), 248 symtab_to_filename_for_display (traceframe_sal.symtab)); 249 } 250 251 /* Create a new trace state variable with the given name. */ 252 253 struct trace_state_variable * 254 create_trace_state_variable (const char *name) 255 { 256 tvariables.emplace_back (name, next_tsv_number++); 257 return &tvariables.back (); 258 } 259 260 /* Look for a trace state variable of the given name. */ 261 262 struct trace_state_variable * 263 find_trace_state_variable (const char *name) 264 { 265 for (trace_state_variable &tsv : tvariables) 266 if (tsv.name == name) 267 return &tsv; 268 269 return NULL; 270 } 271 272 /* Look for a trace state variable of the given number. Return NULL if 273 not found. */ 274 275 struct trace_state_variable * 276 find_trace_state_variable_by_number (int number) 277 { 278 for (trace_state_variable &tsv : tvariables) 279 if (tsv.number == number) 280 return &tsv; 281 282 return NULL; 283 } 284 285 static void 286 delete_trace_state_variable (const char *name) 287 { 288 for (auto it = tvariables.begin (); it != tvariables.end (); it++) 289 if (it->name == name) 290 { 291 gdb::observers::tsv_deleted.notify (&*it); 292 tvariables.erase (it); 293 return; 294 } 295 296 warning (_("No trace variable named \"$%s\", not deleting"), name); 297 } 298 299 /* Throws an error if NAME is not valid syntax for a trace state 300 variable's name. */ 301 302 void 303 validate_trace_state_variable_name (const char *name) 304 { 305 const char *p; 306 307 if (*name == '\0') 308 error (_("Must supply a non-empty variable name")); 309 310 /* All digits in the name is reserved for value history 311 references. */ 312 for (p = name; isdigit (*p); p++) 313 ; 314 if (*p == '\0') 315 error (_("$%s is not a valid trace state variable name"), name); 316 317 for (p = name; isalnum (*p) || *p == '_'; p++) 318 ; 319 if (*p != '\0') 320 error (_("$%s is not a valid trace state variable name"), name); 321 } 322 323 /* The 'tvariable' command collects a name and optional expression to 324 evaluate into an initial value. */ 325 326 static void 327 trace_variable_command (const char *args, int from_tty) 328 { 329 LONGEST initval = 0; 330 struct trace_state_variable *tsv; 331 const char *name_start, *p; 332 333 if (!args || !*args) 334 error_no_arg (_("Syntax is $NAME [ = EXPR ]")); 335 336 /* Only allow two syntaxes; "$name" and "$name=value". */ 337 p = skip_spaces (args); 338 339 if (*p++ != '$') 340 error (_("Name of trace variable should start with '$'")); 341 342 name_start = p; 343 while (isalnum (*p) || *p == '_') 344 p++; 345 std::string name (name_start, p - name_start); 346 347 p = skip_spaces (p); 348 if (*p != '=' && *p != '\0') 349 error (_("Syntax must be $NAME [ = EXPR ]")); 350 351 validate_trace_state_variable_name (name.c_str ()); 352 353 if (*p == '=') 354 initval = value_as_long (parse_and_eval (++p)); 355 356 /* If the variable already exists, just change its initial value. */ 357 tsv = find_trace_state_variable (name.c_str ()); 358 if (tsv) 359 { 360 if (tsv->initial_value != initval) 361 { 362 tsv->initial_value = initval; 363 gdb::observers::tsv_modified.notify (tsv); 364 } 365 gdb_printf (_("Trace state variable $%s " 366 "now has initial value %s.\n"), 367 tsv->name.c_str (), plongest (tsv->initial_value)); 368 return; 369 } 370 371 /* Create a new variable. */ 372 tsv = create_trace_state_variable (name.c_str ()); 373 tsv->initial_value = initval; 374 375 gdb::observers::tsv_created.notify (tsv); 376 377 gdb_printf (_("Trace state variable $%s " 378 "created, with initial value %s.\n"), 379 tsv->name.c_str (), plongest (tsv->initial_value)); 380 } 381 382 static void 383 delete_trace_variable_command (const char *args, int from_tty) 384 { 385 if (args == NULL) 386 { 387 if (query (_("Delete all trace state variables? "))) 388 tvariables.clear (); 389 dont_repeat (); 390 gdb::observers::tsv_deleted.notify (NULL); 391 return; 392 } 393 394 gdb_argv argv (args); 395 396 for (char *arg : argv) 397 { 398 if (*arg == '$') 399 delete_trace_state_variable (arg + 1); 400 else 401 warning (_("Name \"%s\" not prefixed with '$', ignoring"), arg); 402 } 403 404 dont_repeat (); 405 } 406 407 void 408 tvariables_info_1 (void) 409 { 410 struct ui_out *uiout = current_uiout; 411 412 /* Try to acquire values from the target. */ 413 for (trace_state_variable &tsv : tvariables) 414 tsv.value_known 415 = target_get_trace_state_variable_value (tsv.number, &tsv.value); 416 417 { 418 ui_out_emit_table table_emitter (uiout, 3, tvariables.size (), 419 "trace-variables"); 420 uiout->table_header (15, ui_left, "name", "Name"); 421 uiout->table_header (11, ui_left, "initial", "Initial"); 422 uiout->table_header (11, ui_left, "current", "Current"); 423 424 uiout->table_body (); 425 426 for (const trace_state_variable &tsv : tvariables) 427 { 428 const char *c; 429 430 ui_out_emit_tuple tuple_emitter (uiout, "variable"); 431 432 uiout->field_string ("name", std::string ("$") + tsv.name); 433 uiout->field_string ("initial", plongest (tsv.initial_value)); 434 435 ui_file_style style; 436 if (tsv.value_known) 437 c = plongest (tsv.value); 438 else if (uiout->is_mi_like_p ()) 439 /* For MI, we prefer not to use magic string constants, but rather 440 omit the field completely. The difference between unknown and 441 undefined does not seem important enough to represent. */ 442 c = NULL; 443 else if (current_trace_status ()->running || traceframe_number >= 0) 444 { 445 /* The value is/was defined, but we don't have it. */ 446 c = "<unknown>"; 447 style = metadata_style.style (); 448 } 449 else 450 { 451 /* It is not meaningful to ask about the value. */ 452 c = "<undefined>"; 453 style = metadata_style.style (); 454 } 455 if (c) 456 uiout->field_string ("current", c, style); 457 uiout->text ("\n"); 458 } 459 } 460 461 if (tvariables.empty ()) 462 uiout->text (_("No trace state variables.\n")); 463 } 464 465 /* List all the trace state variables. */ 466 467 static void 468 info_tvariables_command (const char *args, int from_tty) 469 { 470 tvariables_info_1 (); 471 } 472 473 /* Stash definitions of tsvs into the given file. */ 474 475 void 476 save_trace_state_variables (struct ui_file *fp) 477 { 478 for (const trace_state_variable &tsv : tvariables) 479 { 480 gdb_printf (fp, "tvariable $%s", tsv.name.c_str ()); 481 if (tsv.initial_value) 482 gdb_printf (fp, " = %s", plongest (tsv.initial_value)); 483 gdb_printf (fp, "\n"); 484 } 485 } 486 487 /* ACTIONS functions: */ 488 489 /* The three functions: 490 collect_pseudocommand, 491 while_stepping_pseudocommand, and 492 end_actions_pseudocommand 493 are placeholders for "commands" that are actually ONLY to be used 494 within a tracepoint action list. If the actual function is ever called, 495 it means that somebody issued the "command" at the top level, 496 which is always an error. */ 497 498 static void 499 end_actions_pseudocommand (const char *args, int from_tty) 500 { 501 error (_("This command cannot be used at the top level.")); 502 } 503 504 static void 505 while_stepping_pseudocommand (const char *args, int from_tty) 506 { 507 error (_("This command can only be used in a tracepoint actions list.")); 508 } 509 510 static void 511 collect_pseudocommand (const char *args, int from_tty) 512 { 513 error (_("This command can only be used in a tracepoint actions list.")); 514 } 515 516 static void 517 teval_pseudocommand (const char *args, int from_tty) 518 { 519 error (_("This command can only be used in a tracepoint actions list.")); 520 } 521 522 /* Parse any collection options, such as /s for strings. */ 523 524 const char * 525 decode_agent_options (const char *exp, int *trace_string) 526 { 527 struct value_print_options opts; 528 529 *trace_string = 0; 530 531 if (*exp != '/') 532 return exp; 533 534 /* Call this to borrow the print elements default for collection 535 size. */ 536 get_user_print_options (&opts); 537 538 exp++; 539 if (*exp == 's') 540 { 541 if (target_supports_string_tracing ()) 542 { 543 /* Allow an optional decimal number giving an explicit maximum 544 string length, defaulting it to the "print elements" value; 545 so "collect/s80 mystr" gets at most 80 bytes of string. */ 546 *trace_string = opts.print_max; 547 exp++; 548 if (*exp >= '0' && *exp <= '9') 549 *trace_string = atoi (exp); 550 while (*exp >= '0' && *exp <= '9') 551 exp++; 552 } 553 else 554 error (_("Target does not support \"/s\" option for string tracing.")); 555 } 556 else 557 error (_("Undefined collection format \"%c\"."), *exp); 558 559 exp = skip_spaces (exp); 560 561 return exp; 562 } 563 564 /* Enter a list of actions for a tracepoint. */ 565 static void 566 actions_command (const char *args, int from_tty) 567 { 568 struct tracepoint *t; 569 570 t = get_tracepoint_by_number (&args, NULL); 571 if (t) 572 { 573 std::string tmpbuf = 574 string_printf ("Enter actions for tracepoint %d, one per line.", 575 t->number); 576 577 counted_command_line l = read_command_lines (tmpbuf.c_str (), 578 from_tty, 1, 579 [=] (const char *line) 580 { 581 validate_actionline (line, t); 582 }); 583 breakpoint_set_commands (t, std::move (l)); 584 } 585 /* else just return */ 586 } 587 588 /* Report the results of checking the agent expression, as errors or 589 internal errors. */ 590 591 static void 592 report_agent_reqs_errors (struct agent_expr *aexpr) 593 { 594 /* All of the "flaws" are serious bytecode generation issues that 595 should never occur. */ 596 if (aexpr->flaw != agent_flaw_none) 597 internal_error (_("expression is malformed")); 598 599 /* If analysis shows a stack underflow, GDB must have done something 600 badly wrong in its bytecode generation. */ 601 if (aexpr->min_height < 0) 602 internal_error (_("expression has min height < 0")); 603 604 /* Issue this error if the stack is predicted to get too deep. The 605 limit is rather arbitrary; a better scheme might be for the 606 target to report how much stack it will have available. The 607 depth roughly corresponds to parenthesization, so a limit of 20 608 amounts to 20 levels of expression nesting, which is actually 609 a pretty big hairy expression. */ 610 if (aexpr->max_height > 20) 611 error (_("Expression is too complicated.")); 612 } 613 614 /* Call ax_reqs on AEXPR and raise an error if something is wrong. */ 615 616 static void 617 finalize_tracepoint_aexpr (struct agent_expr *aexpr) 618 { 619 ax_reqs (aexpr); 620 621 if (aexpr->len > MAX_AGENT_EXPR_LEN) 622 error (_("Expression is too complicated.")); 623 624 report_agent_reqs_errors (aexpr); 625 } 626 627 /* worker function */ 628 void 629 validate_actionline (const char *line, struct breakpoint *b) 630 { 631 struct cmd_list_element *c; 632 const char *tmp_p; 633 const char *p; 634 struct tracepoint *t = (struct tracepoint *) b; 635 636 /* If EOF is typed, *line is NULL. */ 637 if (line == NULL) 638 return; 639 640 p = skip_spaces (line); 641 642 /* Symbol lookup etc. */ 643 if (*p == '\0') /* empty line: just prompt for another line. */ 644 return; 645 646 if (*p == '#') /* comment line */ 647 return; 648 649 c = lookup_cmd (&p, cmdlist, "", NULL, -1, 1); 650 if (c == 0) 651 error (_("`%s' is not a tracepoint action, or is ambiguous."), p); 652 653 if (cmd_simple_func_eq (c, collect_pseudocommand)) 654 { 655 int trace_string = 0; 656 657 if (*p == '/') 658 p = decode_agent_options (p, &trace_string); 659 660 do 661 { /* Repeat over a comma-separated list. */ 662 QUIT; /* Allow user to bail out with ^C. */ 663 p = skip_spaces (p); 664 665 if (*p == '$') /* Look for special pseudo-symbols. */ 666 { 667 if (0 == strncasecmp ("reg", p + 1, 3) 668 || 0 == strncasecmp ("arg", p + 1, 3) 669 || 0 == strncasecmp ("loc", p + 1, 3) 670 || 0 == strncasecmp ("_ret", p + 1, 4) 671 || 0 == strncasecmp ("_sdata", p + 1, 6)) 672 { 673 p = strchr (p, ','); 674 continue; 675 } 676 /* else fall thru, treat p as an expression and parse it! */ 677 } 678 tmp_p = p; 679 for (bp_location *loc : t->locations ()) 680 { 681 p = tmp_p; 682 expression_up exp = parse_exp_1 (&p, loc->address, 683 block_for_pc (loc->address), 1); 684 685 if (exp->first_opcode () == OP_VAR_VALUE) 686 { 687 symbol *sym; 688 expr::var_value_operation *vvop 689 = (gdb::checked_static_cast<expr::var_value_operation *> 690 (exp->op.get ())); 691 sym = vvop->get_symbol (); 692 693 if (sym->aclass () == LOC_CONST) 694 { 695 error (_("constant `%s' (value %s) " 696 "will not be collected."), 697 sym->print_name (), 698 plongest (sym->value_longest ())); 699 } 700 else if (sym->aclass () == LOC_OPTIMIZED_OUT) 701 { 702 error (_("`%s' is optimized away " 703 "and cannot be collected."), 704 sym->print_name ()); 705 } 706 } 707 708 /* We have something to collect, make sure that the expr to 709 bytecode translator can handle it and that it's not too 710 long. */ 711 agent_expr_up aexpr = gen_trace_for_expr (loc->address, 712 exp.get (), 713 trace_string); 714 715 finalize_tracepoint_aexpr (aexpr.get ()); 716 } 717 } 718 while (p && *p++ == ','); 719 } 720 721 else if (cmd_simple_func_eq (c, teval_pseudocommand)) 722 { 723 do 724 { /* Repeat over a comma-separated list. */ 725 QUIT; /* Allow user to bail out with ^C. */ 726 p = skip_spaces (p); 727 728 tmp_p = p; 729 for (bp_location *loc : t->locations ()) 730 { 731 p = tmp_p; 732 733 /* Only expressions are allowed for this action. */ 734 expression_up exp = parse_exp_1 (&p, loc->address, 735 block_for_pc (loc->address), 1); 736 737 /* We have something to evaluate, make sure that the expr to 738 bytecode translator can handle it and that it's not too 739 long. */ 740 agent_expr_up aexpr = gen_eval_for_expr (loc->address, exp.get ()); 741 742 finalize_tracepoint_aexpr (aexpr.get ()); 743 } 744 } 745 while (p && *p++ == ','); 746 } 747 748 else if (cmd_simple_func_eq (c, while_stepping_pseudocommand)) 749 { 750 char *endp; 751 752 p = skip_spaces (p); 753 t->step_count = strtol (p, &endp, 0); 754 if (endp == p || t->step_count == 0) 755 error (_("while-stepping step count `%s' is malformed."), line); 756 p = endp; 757 } 758 759 else if (cmd_simple_func_eq (c, end_actions_pseudocommand)) 760 ; 761 762 else 763 error (_("`%s' is not a supported tracepoint action."), line); 764 } 765 766 enum { 767 memrange_absolute = -1 768 }; 769 770 /* MEMRANGE functions: */ 771 772 /* Compare memranges for std::sort. */ 773 774 static bool 775 memrange_comp (const memrange &a, const memrange &b) 776 { 777 if (a.type == b.type) 778 { 779 if (a.type == memrange_absolute) 780 return (bfd_vma) a.start < (bfd_vma) b.start; 781 else 782 return a.start < b.start; 783 } 784 785 return a.type < b.type; 786 } 787 788 /* Sort the memrange list using std::sort, and merge adjacent memranges. */ 789 790 static void 791 memrange_sortmerge (std::vector<memrange> &memranges) 792 { 793 if (!memranges.empty ()) 794 { 795 int a, b; 796 797 std::sort (memranges.begin (), memranges.end (), memrange_comp); 798 799 for (a = 0, b = 1; b < memranges.size (); b++) 800 { 801 /* If memrange b overlaps or is adjacent to memrange a, 802 merge them. */ 803 if (memranges[a].type == memranges[b].type 804 && memranges[b].start <= memranges[a].end) 805 { 806 if (memranges[b].end > memranges[a].end) 807 memranges[a].end = memranges[b].end; 808 continue; /* next b, same a */ 809 } 810 a++; /* next a */ 811 if (a != b) 812 memranges[a] = memranges[b]; 813 } 814 memranges.resize (a + 1); 815 } 816 } 817 818 /* Add remote register number REGNO to the collection list mask. */ 819 820 void 821 collection_list::add_remote_register (unsigned int regno) 822 { 823 if (info_verbose) 824 gdb_printf ("collect register %d\n", regno); 825 826 m_regs_mask.at (regno / 8) |= 1 << (regno % 8); 827 } 828 829 /* Add all the registers from the mask in AEXPR to the mask in the 830 collection list. Registers in the AEXPR mask are already remote 831 register numbers. */ 832 833 void 834 collection_list::add_ax_registers (struct agent_expr *aexpr) 835 { 836 if (aexpr->reg_mask_len > 0) 837 { 838 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++) 839 { 840 QUIT; /* Allow user to bail out with ^C. */ 841 if (aexpr->reg_mask[ndx1] != 0) 842 { 843 /* Assume chars have 8 bits. */ 844 for (int ndx2 = 0; ndx2 < 8; ndx2++) 845 if (aexpr->reg_mask[ndx1] & (1 << ndx2)) 846 /* It's used -- record it. */ 847 add_remote_register (ndx1 * 8 + ndx2); 848 } 849 } 850 } 851 } 852 853 /* If REGNO is raw, add its corresponding remote register number to 854 the mask. If REGNO is a pseudo-register, figure out the necessary 855 registers using a temporary agent expression, and add it to the 856 list if it needs more than just a mask. */ 857 858 void 859 collection_list::add_local_register (struct gdbarch *gdbarch, 860 unsigned int regno, 861 CORE_ADDR scope) 862 { 863 if (regno < gdbarch_num_regs (gdbarch)) 864 { 865 int remote_regno = gdbarch_remote_register_number (gdbarch, regno); 866 867 if (remote_regno < 0) 868 error (_("Can't collect register %d"), regno); 869 870 add_remote_register (remote_regno); 871 } 872 else 873 { 874 agent_expr_up aexpr (new agent_expr (gdbarch, scope)); 875 876 ax_reg_mask (aexpr.get (), regno); 877 878 finalize_tracepoint_aexpr (aexpr.get ()); 879 880 add_ax_registers (aexpr.get ()); 881 882 /* Usually ax_reg_mask for a pseudo-regiser only sets the 883 corresponding raw registers in the ax mask, but if this isn't 884 the case add the expression that is generated to the 885 collection list. */ 886 if (aexpr->len > 0) 887 add_aexpr (std::move (aexpr)); 888 } 889 } 890 891 /* Add a memrange to a collection list. */ 892 893 void 894 collection_list::add_memrange (struct gdbarch *gdbarch, 895 int type, bfd_signed_vma base, 896 unsigned long len, CORE_ADDR scope) 897 { 898 if (info_verbose) 899 gdb_printf ("(%d,%s,%ld)\n", type, paddress (gdbarch, base), len); 900 901 /* type: memrange_absolute == memory, other n == basereg */ 902 /* base: addr if memory, offset if reg relative. */ 903 /* len: we actually save end (base + len) for convenience */ 904 m_memranges.emplace_back (type, base, base + len); 905 906 if (type != memrange_absolute) /* Better collect the base register! */ 907 add_local_register (gdbarch, type, scope); 908 } 909 910 /* Add a symbol to a collection list. */ 911 912 void 913 collection_list::collect_symbol (struct symbol *sym, 914 struct gdbarch *gdbarch, 915 long frame_regno, long frame_offset, 916 CORE_ADDR scope, 917 int trace_string) 918 { 919 unsigned long len; 920 unsigned int reg; 921 bfd_signed_vma offset; 922 int treat_as_expr = 0; 923 924 len = check_typedef (sym->type ())->length (); 925 switch (sym->aclass ()) 926 { 927 default: 928 gdb_printf ("%s: don't know symbol class %d\n", 929 sym->print_name (), sym->aclass ()); 930 break; 931 case LOC_CONST: 932 gdb_printf ("constant %s (value %s) will not be collected.\n", 933 sym->print_name (), plongest (sym->value_longest ())); 934 break; 935 case LOC_STATIC: 936 offset = sym->value_address (); 937 if (info_verbose) 938 { 939 gdb_printf ("LOC_STATIC %s: collect %ld bytes at %s.\n", 940 sym->print_name (), len, 941 paddress (gdbarch, offset)); 942 } 943 /* A struct may be a C++ class with static fields, go to general 944 expression handling. */ 945 if (sym->type ()->code () == TYPE_CODE_STRUCT) 946 treat_as_expr = 1; 947 else 948 add_memrange (gdbarch, memrange_absolute, offset, len, scope); 949 break; 950 case LOC_REGISTER: 951 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch); 952 if (info_verbose) 953 gdb_printf ("LOC_REG[parm] %s: ", sym->print_name ()); 954 add_local_register (gdbarch, reg, scope); 955 /* Check for doubles stored in two registers. */ 956 /* FIXME: how about larger types stored in 3 or more regs? */ 957 if (sym->type ()->code () == TYPE_CODE_FLT && 958 len > register_size (gdbarch, reg)) 959 add_local_register (gdbarch, reg + 1, scope); 960 break; 961 case LOC_REF_ARG: 962 gdb_printf ("Sorry, don't know how to do LOC_REF_ARG yet.\n"); 963 gdb_printf (" (will not collect %s)\n", sym->print_name ()); 964 break; 965 case LOC_ARG: 966 reg = frame_regno; 967 offset = frame_offset + sym->value_longest (); 968 if (info_verbose) 969 { 970 gdb_printf ("LOC_LOCAL %s: Collect %ld bytes at offset %s" 971 " from frame ptr reg %d\n", sym->print_name (), len, 972 paddress (gdbarch, offset), reg); 973 } 974 add_memrange (gdbarch, reg, offset, len, scope); 975 break; 976 case LOC_REGPARM_ADDR: 977 reg = sym->value_longest (); 978 offset = 0; 979 if (info_verbose) 980 { 981 gdb_printf ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset %s" 982 " from reg %d\n", sym->print_name (), len, 983 paddress (gdbarch, offset), reg); 984 } 985 add_memrange (gdbarch, reg, offset, len, scope); 986 break; 987 case LOC_LOCAL: 988 reg = frame_regno; 989 offset = frame_offset + sym->value_longest (); 990 if (info_verbose) 991 { 992 gdb_printf ("LOC_LOCAL %s: Collect %ld bytes at offset %s" 993 " from frame ptr reg %d\n", sym->print_name (), len, 994 paddress (gdbarch, offset), reg); 995 } 996 add_memrange (gdbarch, reg, offset, len, scope); 997 break; 998 999 case LOC_UNRESOLVED: 1000 treat_as_expr = 1; 1001 break; 1002 1003 case LOC_OPTIMIZED_OUT: 1004 gdb_printf ("%s has been optimized out of existence.\n", 1005 sym->print_name ()); 1006 break; 1007 1008 case LOC_COMPUTED: 1009 treat_as_expr = 1; 1010 break; 1011 } 1012 1013 /* Expressions are the most general case. */ 1014 if (treat_as_expr) 1015 { 1016 agent_expr_up aexpr = gen_trace_for_var (scope, gdbarch, 1017 sym, trace_string); 1018 1019 /* It can happen that the symbol is recorded as a computed 1020 location, but it's been optimized away and doesn't actually 1021 have a location expression. */ 1022 if (!aexpr) 1023 { 1024 gdb_printf ("%s has been optimized out of existence.\n", 1025 sym->print_name ()); 1026 return; 1027 } 1028 1029 finalize_tracepoint_aexpr (aexpr.get ()); 1030 1031 /* Take care of the registers. */ 1032 add_ax_registers (aexpr.get ()); 1033 1034 add_aexpr (std::move (aexpr)); 1035 } 1036 } 1037 1038 void 1039 collection_list::add_wholly_collected (const char *print_name) 1040 { 1041 m_wholly_collected.push_back (print_name); 1042 } 1043 1044 /* Add all locals (or args) symbols to collection list. */ 1045 1046 void 1047 collection_list::add_local_symbols (struct gdbarch *gdbarch, CORE_ADDR pc, 1048 long frame_regno, long frame_offset, int type, 1049 int trace_string) 1050 { 1051 const struct block *block; 1052 int count = 0; 1053 1054 auto do_collect_symbol = [&] (const char *print_name, 1055 struct symbol *sym) 1056 { 1057 collect_symbol (sym, gdbarch, frame_regno, 1058 frame_offset, pc, trace_string); 1059 count++; 1060 add_wholly_collected (print_name); 1061 }; 1062 1063 if (type == 'L') 1064 { 1065 block = block_for_pc (pc); 1066 if (block == NULL) 1067 { 1068 warning (_("Can't collect locals; " 1069 "no symbol table info available.\n")); 1070 return; 1071 } 1072 1073 iterate_over_block_local_vars (block, do_collect_symbol); 1074 if (count == 0) 1075 warning (_("No locals found in scope.")); 1076 } 1077 else 1078 { 1079 CORE_ADDR fn_pc = get_pc_function_start (pc); 1080 block = block_for_pc (fn_pc); 1081 if (block == NULL) 1082 { 1083 warning (_("Can't collect args; no symbol table info available.")); 1084 return; 1085 } 1086 1087 iterate_over_block_arg_vars (block, do_collect_symbol); 1088 if (count == 0) 1089 warning (_("No args found in scope.")); 1090 } 1091 } 1092 1093 void 1094 collection_list::add_static_trace_data () 1095 { 1096 if (info_verbose) 1097 gdb_printf ("collect static trace data\n"); 1098 m_strace_data = true; 1099 } 1100 1101 collection_list::collection_list () 1102 : m_strace_data (false) 1103 { 1104 int max_remote_regno = 0; 1105 for (int i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++) 1106 { 1107 int remote_regno = (gdbarch_remote_register_number 1108 (target_gdbarch (), i)); 1109 1110 if (remote_regno >= 0 && remote_regno > max_remote_regno) 1111 max_remote_regno = remote_regno; 1112 } 1113 1114 m_regs_mask.resize ((max_remote_regno / 8) + 1); 1115 1116 m_memranges.reserve (128); 1117 m_aexprs.reserve (128); 1118 } 1119 1120 /* Reduce a collection list to string form (for gdb protocol). */ 1121 1122 std::vector<std::string> 1123 collection_list::stringify () 1124 { 1125 gdb::char_vector temp_buf (2048); 1126 1127 int count; 1128 char *end; 1129 long i; 1130 std::vector<std::string> str_list; 1131 1132 if (m_strace_data) 1133 { 1134 if (info_verbose) 1135 gdb_printf ("\nCollecting static trace data\n"); 1136 end = temp_buf.data (); 1137 *end++ = 'L'; 1138 str_list.emplace_back (temp_buf.data (), end - temp_buf.data ()); 1139 } 1140 1141 for (i = m_regs_mask.size () - 1; i > 0; i--) 1142 if (m_regs_mask[i] != 0) /* Skip leading zeroes in regs_mask. */ 1143 break; 1144 if (m_regs_mask[i] != 0) /* Prepare to send regs_mask to the stub. */ 1145 { 1146 if (info_verbose) 1147 gdb_printf ("\nCollecting registers (mask): 0x"); 1148 1149 /* One char for 'R', one for the null terminator and two per 1150 mask byte. */ 1151 std::size_t new_size = (i + 1) * 2 + 2; 1152 if (new_size > temp_buf.size ()) 1153 temp_buf.resize (new_size); 1154 1155 end = temp_buf.data (); 1156 *end++ = 'R'; 1157 for (; i >= 0; i--) 1158 { 1159 QUIT; /* Allow user to bail out with ^C. */ 1160 if (info_verbose) 1161 gdb_printf ("%02X", m_regs_mask[i]); 1162 1163 end = pack_hex_byte (end, m_regs_mask[i]); 1164 } 1165 *end = '\0'; 1166 1167 str_list.emplace_back (temp_buf.data ()); 1168 } 1169 if (info_verbose) 1170 gdb_printf ("\n"); 1171 if (!m_memranges.empty () && info_verbose) 1172 gdb_printf ("Collecting memranges: \n"); 1173 for (i = 0, count = 0, end = temp_buf.data (); 1174 i < m_memranges.size (); i++) 1175 { 1176 QUIT; /* Allow user to bail out with ^C. */ 1177 if (info_verbose) 1178 { 1179 gdb_printf ("(%d, %s, %ld)\n", 1180 m_memranges[i].type, 1181 paddress (target_gdbarch (), 1182 m_memranges[i].start), 1183 (long) (m_memranges[i].end 1184 - m_memranges[i].start)); 1185 } 1186 if (count + 27 > MAX_AGENT_EXPR_LEN) 1187 { 1188 str_list.emplace_back (temp_buf.data (), count); 1189 count = 0; 1190 end = temp_buf.data (); 1191 } 1192 1193 { 1194 bfd_signed_vma length 1195 = m_memranges[i].end - m_memranges[i].start; 1196 1197 /* The "%X" conversion specifier expects an unsigned argument, 1198 so passing -1 (memrange_absolute) to it directly gives you 1199 "FFFFFFFF" (or more, depending on sizeof (unsigned)). 1200 Special-case it. */ 1201 if (m_memranges[i].type == memrange_absolute) 1202 sprintf (end, "M-1,%s,%lX", phex_nz (m_memranges[i].start, 0), 1203 (long) length); 1204 else 1205 sprintf (end, "M%X,%s,%lX", m_memranges[i].type, 1206 phex_nz (m_memranges[i].start, 0), (long) length); 1207 } 1208 1209 count += strlen (end); 1210 end = temp_buf.data () + count; 1211 } 1212 1213 for (i = 0; i < m_aexprs.size (); i++) 1214 { 1215 QUIT; /* Allow user to bail out with ^C. */ 1216 if ((count + 10 + 2 * m_aexprs[i]->len) > MAX_AGENT_EXPR_LEN) 1217 { 1218 str_list.emplace_back (temp_buf.data (), count); 1219 count = 0; 1220 end = temp_buf.data (); 1221 } 1222 sprintf (end, "X%08X,", m_aexprs[i]->len); 1223 end += 10; /* 'X' + 8 hex digits + ',' */ 1224 count += 10; 1225 1226 end = mem2hex (m_aexprs[i]->buf, end, m_aexprs[i]->len); 1227 count += 2 * m_aexprs[i]->len; 1228 } 1229 1230 if (count != 0) 1231 { 1232 str_list.emplace_back (temp_buf.data (), count); 1233 count = 0; 1234 end = temp_buf.data (); 1235 } 1236 1237 return str_list; 1238 } 1239 1240 /* Add the expression STR to M_COMPUTED. */ 1241 1242 void 1243 collection_list::append_exp (std::string &&str) 1244 { 1245 m_computed.push_back (std::move (str)); 1246 } 1247 1248 void 1249 collection_list::finish () 1250 { 1251 memrange_sortmerge (m_memranges); 1252 } 1253 1254 static void 1255 encode_actions_1 (struct command_line *action, 1256 struct bp_location *tloc, 1257 int frame_reg, 1258 LONGEST frame_offset, 1259 struct collection_list *collect, 1260 struct collection_list *stepping_list) 1261 { 1262 const char *action_exp; 1263 int i; 1264 struct value *tempval; 1265 struct cmd_list_element *cmd; 1266 1267 for (; action; action = action->next) 1268 { 1269 QUIT; /* Allow user to bail out with ^C. */ 1270 action_exp = action->line; 1271 action_exp = skip_spaces (action_exp); 1272 1273 cmd = lookup_cmd (&action_exp, cmdlist, "", NULL, -1, 1); 1274 if (cmd == 0) 1275 error (_("Bad action list item: %s"), action_exp); 1276 1277 if (cmd_simple_func_eq (cmd, collect_pseudocommand)) 1278 { 1279 int trace_string = 0; 1280 1281 if (*action_exp == '/') 1282 action_exp = decode_agent_options (action_exp, &trace_string); 1283 1284 do 1285 { /* Repeat over a comma-separated list. */ 1286 QUIT; /* Allow user to bail out with ^C. */ 1287 action_exp = skip_spaces (action_exp); 1288 1289 if (0 == strncasecmp ("$reg", action_exp, 4)) 1290 { 1291 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); 1292 i++) 1293 { 1294 int remote_regno = (gdbarch_remote_register_number 1295 (target_gdbarch (), i)); 1296 1297 /* Ignore arch regnos without a corresponding 1298 remote regno. This can happen for regnos not 1299 in the tdesc. */ 1300 if (remote_regno >= 0) 1301 collect->add_remote_register (remote_regno); 1302 } 1303 action_exp = strchr (action_exp, ','); /* more? */ 1304 } 1305 else if (0 == strncasecmp ("$arg", action_exp, 4)) 1306 { 1307 collect->add_local_symbols (target_gdbarch (), 1308 tloc->address, 1309 frame_reg, 1310 frame_offset, 1311 'A', 1312 trace_string); 1313 action_exp = strchr (action_exp, ','); /* more? */ 1314 } 1315 else if (0 == strncasecmp ("$loc", action_exp, 4)) 1316 { 1317 collect->add_local_symbols (target_gdbarch (), 1318 tloc->address, 1319 frame_reg, 1320 frame_offset, 1321 'L', 1322 trace_string); 1323 action_exp = strchr (action_exp, ','); /* more? */ 1324 } 1325 else if (0 == strncasecmp ("$_ret", action_exp, 5)) 1326 { 1327 agent_expr_up aexpr 1328 = gen_trace_for_return_address (tloc->address, 1329 target_gdbarch (), 1330 trace_string); 1331 1332 finalize_tracepoint_aexpr (aexpr.get ()); 1333 1334 /* take care of the registers */ 1335 collect->add_ax_registers (aexpr.get ()); 1336 1337 collect->add_aexpr (std::move (aexpr)); 1338 action_exp = strchr (action_exp, ','); /* more? */ 1339 } 1340 else if (0 == strncasecmp ("$_sdata", action_exp, 7)) 1341 { 1342 collect->add_static_trace_data (); 1343 action_exp = strchr (action_exp, ','); /* more? */ 1344 } 1345 else 1346 { 1347 unsigned long addr; 1348 1349 const char *exp_start = action_exp; 1350 expression_up exp = parse_exp_1 (&action_exp, tloc->address, 1351 block_for_pc (tloc->address), 1352 1); 1353 1354 switch (exp->first_opcode ()) 1355 { 1356 case OP_REGISTER: 1357 { 1358 expr::register_operation *regop 1359 = (gdb::checked_static_cast<expr::register_operation *> 1360 (exp->op.get ())); 1361 const char *name = regop->get_name (); 1362 1363 i = user_reg_map_name_to_regnum (target_gdbarch (), 1364 name, strlen (name)); 1365 if (i == -1) 1366 internal_error (_("Register $%s not available"), 1367 name); 1368 if (info_verbose) 1369 gdb_printf ("OP_REGISTER: "); 1370 collect->add_local_register (target_gdbarch (), 1371 i, tloc->address); 1372 break; 1373 } 1374 1375 case UNOP_MEMVAL: 1376 { 1377 /* Safe because we know it's a simple expression. */ 1378 tempval = evaluate_expression (exp.get ()); 1379 addr = value_address (tempval); 1380 expr::unop_memval_operation *memop 1381 = (gdb::checked_static_cast<expr::unop_memval_operation *> 1382 (exp->op.get ())); 1383 struct type *type = memop->get_type (); 1384 /* Initialize the TYPE_LENGTH if it is a typedef. */ 1385 check_typedef (type); 1386 collect->add_memrange (target_gdbarch (), 1387 memrange_absolute, addr, 1388 type->length (), 1389 tloc->address); 1390 collect->append_exp (std::string (exp_start, 1391 action_exp)); 1392 } 1393 break; 1394 1395 case OP_VAR_VALUE: 1396 { 1397 expr::var_value_operation *vvo 1398 = (gdb::checked_static_cast<expr::var_value_operation *> 1399 (exp->op.get ())); 1400 struct symbol *sym = vvo->get_symbol (); 1401 const char *name = sym->natural_name (); 1402 1403 collect->collect_symbol (sym, 1404 target_gdbarch (), 1405 frame_reg, 1406 frame_offset, 1407 tloc->address, 1408 trace_string); 1409 collect->add_wholly_collected (name); 1410 } 1411 break; 1412 1413 default: /* Full-fledged expression. */ 1414 agent_expr_up aexpr = gen_trace_for_expr (tloc->address, 1415 exp.get (), 1416 trace_string); 1417 1418 finalize_tracepoint_aexpr (aexpr.get ()); 1419 1420 /* Take care of the registers. */ 1421 collect->add_ax_registers (aexpr.get ()); 1422 1423 collect->add_aexpr (std::move (aexpr)); 1424 collect->append_exp (std::string (exp_start, 1425 action_exp)); 1426 break; 1427 } /* switch */ 1428 } /* do */ 1429 } 1430 while (action_exp && *action_exp++ == ','); 1431 } /* if */ 1432 else if (cmd_simple_func_eq (cmd, teval_pseudocommand)) 1433 { 1434 do 1435 { /* Repeat over a comma-separated list. */ 1436 QUIT; /* Allow user to bail out with ^C. */ 1437 action_exp = skip_spaces (action_exp); 1438 1439 { 1440 expression_up exp = parse_exp_1 (&action_exp, tloc->address, 1441 block_for_pc (tloc->address), 1442 1); 1443 1444 agent_expr_up aexpr = gen_eval_for_expr (tloc->address, 1445 exp.get ()); 1446 1447 finalize_tracepoint_aexpr (aexpr.get ()); 1448 1449 /* Even though we're not officially collecting, add 1450 to the collect list anyway. */ 1451 collect->add_aexpr (std::move (aexpr)); 1452 } /* do */ 1453 } 1454 while (action_exp && *action_exp++ == ','); 1455 } /* if */ 1456 else if (cmd_simple_func_eq (cmd, while_stepping_pseudocommand)) 1457 { 1458 /* We check against nested while-stepping when setting 1459 breakpoint action, so no way to run into nested 1460 here. */ 1461 gdb_assert (stepping_list); 1462 1463 encode_actions_1 (action->body_list_0.get (), tloc, frame_reg, 1464 frame_offset, stepping_list, NULL); 1465 } 1466 else 1467 error (_("Invalid tracepoint command '%s'"), action->line); 1468 } /* for */ 1469 } 1470 1471 /* Encode actions of tracepoint TLOC->owner and fill TRACEPOINT_LIST 1472 and STEPPING_LIST. */ 1473 1474 void 1475 encode_actions (struct bp_location *tloc, 1476 struct collection_list *tracepoint_list, 1477 struct collection_list *stepping_list) 1478 { 1479 int frame_reg; 1480 LONGEST frame_offset; 1481 1482 gdbarch_virtual_frame_pointer (tloc->gdbarch, 1483 tloc->address, &frame_reg, &frame_offset); 1484 1485 counted_command_line actions = all_tracepoint_actions (tloc->owner); 1486 encode_actions_1 (actions.get (), tloc, frame_reg, frame_offset, 1487 tracepoint_list, stepping_list); 1488 encode_actions_1 (breakpoint_commands (tloc->owner), tloc, 1489 frame_reg, frame_offset, tracepoint_list, stepping_list); 1490 1491 tracepoint_list->finish (); 1492 stepping_list->finish (); 1493 } 1494 1495 /* Render all actions into gdb protocol. */ 1496 1497 void 1498 encode_actions_rsp (struct bp_location *tloc, 1499 std::vector<std::string> *tdp_actions, 1500 std::vector<std::string> *stepping_actions) 1501 { 1502 struct collection_list tracepoint_list, stepping_list; 1503 1504 encode_actions (tloc, &tracepoint_list, &stepping_list); 1505 1506 *tdp_actions = tracepoint_list.stringify (); 1507 *stepping_actions = stepping_list.stringify (); 1508 } 1509 1510 void 1511 collection_list::add_aexpr (agent_expr_up aexpr) 1512 { 1513 m_aexprs.push_back (std::move (aexpr)); 1514 } 1515 1516 static void 1517 process_tracepoint_on_disconnect (void) 1518 { 1519 int has_pending_p = 0; 1520 1521 /* Check whether we still have pending tracepoint. If we have, warn the 1522 user that pending tracepoint will no longer work. */ 1523 for (breakpoint *b : all_tracepoints ()) 1524 { 1525 if (b->loc == NULL) 1526 { 1527 has_pending_p = 1; 1528 break; 1529 } 1530 else 1531 { 1532 for (bp_location *loc1 : b->locations ()) 1533 { 1534 if (loc1->shlib_disabled) 1535 { 1536 has_pending_p = 1; 1537 break; 1538 } 1539 } 1540 1541 if (has_pending_p) 1542 break; 1543 } 1544 } 1545 1546 if (has_pending_p) 1547 warning (_("Pending tracepoints will not be resolved while" 1548 " GDB is disconnected\n")); 1549 } 1550 1551 /* Reset local state of tracing. */ 1552 1553 void 1554 trace_reset_local_state (void) 1555 { 1556 set_traceframe_num (-1); 1557 set_tracepoint_num (-1); 1558 set_traceframe_context (NULL); 1559 clear_traceframe_info (); 1560 } 1561 1562 void 1563 start_tracing (const char *notes) 1564 { 1565 int any_enabled = 0, num_to_download = 0; 1566 int ret; 1567 1568 auto tracepoint_range = all_tracepoints (); 1569 1570 /* No point in tracing without any tracepoints... */ 1571 if (tracepoint_range.begin () == tracepoint_range.end ()) 1572 error (_("No tracepoints defined, not starting trace")); 1573 1574 for (breakpoint *b : tracepoint_range) 1575 { 1576 if (b->enable_state == bp_enabled) 1577 any_enabled = 1; 1578 1579 if ((b->type == bp_fast_tracepoint 1580 ? may_insert_fast_tracepoints 1581 : may_insert_tracepoints)) 1582 ++num_to_download; 1583 else 1584 warning (_("May not insert %stracepoints, skipping tracepoint %d"), 1585 (b->type == bp_fast_tracepoint ? "fast " : ""), b->number); 1586 } 1587 1588 if (!any_enabled) 1589 { 1590 if (target_supports_enable_disable_tracepoint ()) 1591 warning (_("No tracepoints enabled")); 1592 else 1593 { 1594 /* No point in tracing with only disabled tracepoints that 1595 cannot be re-enabled. */ 1596 error (_("No tracepoints enabled, not starting trace")); 1597 } 1598 } 1599 1600 if (num_to_download <= 0) 1601 error (_("No tracepoints that may be downloaded, not starting trace")); 1602 1603 target_trace_init (); 1604 1605 for (breakpoint *b : tracepoint_range) 1606 { 1607 struct tracepoint *t = (struct tracepoint *) b; 1608 int bp_location_downloaded = 0; 1609 1610 /* Clear `inserted' flag. */ 1611 for (bp_location *loc : b->locations ()) 1612 loc->inserted = 0; 1613 1614 if ((b->type == bp_fast_tracepoint 1615 ? !may_insert_fast_tracepoints 1616 : !may_insert_tracepoints)) 1617 continue; 1618 1619 t->number_on_target = 0; 1620 1621 for (bp_location *loc : b->locations ()) 1622 { 1623 /* Since tracepoint locations are never duplicated, `inserted' 1624 flag should be zero. */ 1625 gdb_assert (!loc->inserted); 1626 1627 target_download_tracepoint (loc); 1628 1629 loc->inserted = 1; 1630 bp_location_downloaded = 1; 1631 } 1632 1633 t->number_on_target = b->number; 1634 1635 for (bp_location *loc : b->locations ()) 1636 if (loc->probe.prob != NULL) 1637 loc->probe.prob->set_semaphore (loc->probe.objfile, 1638 loc->gdbarch); 1639 1640 if (bp_location_downloaded) 1641 gdb::observers::breakpoint_modified.notify (b); 1642 } 1643 1644 /* Send down all the trace state variables too. */ 1645 for (const trace_state_variable &tsv : tvariables) 1646 target_download_trace_state_variable (tsv); 1647 1648 /* Tell target to treat text-like sections as transparent. */ 1649 target_trace_set_readonly_regions (); 1650 /* Set some mode flags. */ 1651 target_set_disconnected_tracing (disconnected_tracing); 1652 target_set_circular_trace_buffer (circular_trace_buffer); 1653 target_set_trace_buffer_size (trace_buffer_size); 1654 1655 if (!notes) 1656 notes = trace_notes.c_str (); 1657 1658 ret = target_set_trace_notes (trace_user.c_str (), notes, NULL); 1659 1660 if (!ret && (!trace_user.empty () || notes)) 1661 warning (_("Target does not support trace user/notes, info ignored")); 1662 1663 /* Now insert traps and begin collecting data. */ 1664 target_trace_start (); 1665 1666 /* Reset our local state. */ 1667 trace_reset_local_state (); 1668 current_trace_status()->running = 1; 1669 } 1670 1671 /* The tstart command requests the target to start a new trace run. 1672 The command passes any arguments it has to the target verbatim, as 1673 an optional "trace note". This is useful as for instance a warning 1674 to other users if the trace runs disconnected, and you don't want 1675 anybody else messing with the target. */ 1676 1677 static void 1678 tstart_command (const char *args, int from_tty) 1679 { 1680 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */ 1681 1682 if (current_trace_status ()->running) 1683 { 1684 if (from_tty 1685 && !query (_("A trace is running already. Start a new run? "))) 1686 error (_("New trace run not started.")); 1687 } 1688 1689 start_tracing (args); 1690 } 1691 1692 /* The tstop command stops the tracing run. The command passes any 1693 supplied arguments to the target verbatim as a "stop note"; if the 1694 target supports trace notes, then it will be reported back as part 1695 of the trace run's status. */ 1696 1697 static void 1698 tstop_command (const char *args, int from_tty) 1699 { 1700 if (!current_trace_status ()->running) 1701 error (_("Trace is not running.")); 1702 1703 stop_tracing (args); 1704 } 1705 1706 void 1707 stop_tracing (const char *note) 1708 { 1709 int ret; 1710 1711 target_trace_stop (); 1712 1713 for (breakpoint *t : all_tracepoints ()) 1714 { 1715 if ((t->type == bp_fast_tracepoint 1716 ? !may_insert_fast_tracepoints 1717 : !may_insert_tracepoints)) 1718 continue; 1719 1720 for (bp_location *loc : t->locations ()) 1721 { 1722 /* GDB can be totally absent in some disconnected trace scenarios, 1723 but we don't really care if this semaphore goes out of sync. 1724 That's why we are decrementing it here, but not taking care 1725 in other places. */ 1726 if (loc->probe.prob != NULL) 1727 loc->probe.prob->clear_semaphore (loc->probe.objfile, 1728 loc->gdbarch); 1729 } 1730 } 1731 1732 if (!note) 1733 note = trace_stop_notes.c_str (); 1734 1735 ret = target_set_trace_notes (NULL, NULL, note); 1736 1737 if (!ret && note) 1738 warning (_("Target does not support trace notes, note ignored")); 1739 1740 /* Should change in response to reply? */ 1741 current_trace_status ()->running = 0; 1742 } 1743 1744 /* tstatus command */ 1745 static void 1746 tstatus_command (const char *args, int from_tty) 1747 { 1748 struct trace_status *ts = current_trace_status (); 1749 int status; 1750 1751 status = target_get_trace_status (ts); 1752 1753 if (status == -1) 1754 { 1755 if (ts->filename != NULL) 1756 gdb_printf (_("Using a trace file.\n")); 1757 else 1758 { 1759 gdb_printf (_("Trace can not be run on this target.\n")); 1760 return; 1761 } 1762 } 1763 1764 if (!ts->running_known) 1765 { 1766 gdb_printf (_("Run/stop status is unknown.\n")); 1767 } 1768 else if (ts->running) 1769 { 1770 gdb_printf (_("Trace is running on the target.\n")); 1771 } 1772 else 1773 { 1774 switch (ts->stop_reason) 1775 { 1776 case trace_never_run: 1777 gdb_printf (_("No trace has been run on the target.\n")); 1778 break; 1779 case trace_stop_command: 1780 if (ts->stop_desc) 1781 gdb_printf (_("Trace stopped by a tstop command (%s).\n"), 1782 ts->stop_desc); 1783 else 1784 gdb_printf (_("Trace stopped by a tstop command.\n")); 1785 break; 1786 case trace_buffer_full: 1787 gdb_printf (_("Trace stopped because the buffer was full.\n")); 1788 break; 1789 case trace_disconnected: 1790 gdb_printf (_("Trace stopped because of disconnection.\n")); 1791 break; 1792 case tracepoint_passcount: 1793 gdb_printf (_("Trace stopped by tracepoint %d.\n"), 1794 ts->stopping_tracepoint); 1795 break; 1796 case tracepoint_error: 1797 if (ts->stopping_tracepoint) 1798 gdb_printf (_("Trace stopped by an " 1799 "error (%s, tracepoint %d).\n"), 1800 ts->stop_desc, ts->stopping_tracepoint); 1801 else 1802 gdb_printf (_("Trace stopped by an error (%s).\n"), 1803 ts->stop_desc); 1804 break; 1805 case trace_stop_reason_unknown: 1806 gdb_printf (_("Trace stopped for an unknown reason.\n")); 1807 break; 1808 default: 1809 gdb_printf (_("Trace stopped for some other reason (%d).\n"), 1810 ts->stop_reason); 1811 break; 1812 } 1813 } 1814 1815 if (ts->traceframes_created >= 0 1816 && ts->traceframe_count != ts->traceframes_created) 1817 { 1818 gdb_printf (_("Buffer contains %d trace " 1819 "frames (of %d created total).\n"), 1820 ts->traceframe_count, ts->traceframes_created); 1821 } 1822 else if (ts->traceframe_count >= 0) 1823 { 1824 gdb_printf (_("Collected %d trace frames.\n"), 1825 ts->traceframe_count); 1826 } 1827 1828 if (ts->buffer_free >= 0) 1829 { 1830 if (ts->buffer_size >= 0) 1831 { 1832 gdb_printf (_("Trace buffer has %d bytes of %d bytes free"), 1833 ts->buffer_free, ts->buffer_size); 1834 if (ts->buffer_size > 0) 1835 gdb_printf (_(" (%d%% full)"), 1836 ((int) ((((long long) (ts->buffer_size 1837 - ts->buffer_free)) * 100) 1838 / ts->buffer_size))); 1839 gdb_printf (_(".\n")); 1840 } 1841 else 1842 gdb_printf (_("Trace buffer has %d bytes free.\n"), 1843 ts->buffer_free); 1844 } 1845 1846 if (ts->disconnected_tracing) 1847 gdb_printf (_("Trace will continue if GDB disconnects.\n")); 1848 else 1849 gdb_printf (_("Trace will stop if GDB disconnects.\n")); 1850 1851 if (ts->circular_buffer) 1852 gdb_printf (_("Trace buffer is circular.\n")); 1853 1854 if (ts->user_name && strlen (ts->user_name) > 0) 1855 gdb_printf (_("Trace user is %s.\n"), ts->user_name); 1856 1857 if (ts->notes && strlen (ts->notes) > 0) 1858 gdb_printf (_("Trace notes: %s.\n"), ts->notes); 1859 1860 /* Now report on what we're doing with tfind. */ 1861 if (traceframe_number >= 0) 1862 gdb_printf (_("Looking at trace frame %d, tracepoint %d.\n"), 1863 traceframe_number, tracepoint_number); 1864 else 1865 gdb_printf (_("Not looking at any trace frame.\n")); 1866 1867 /* Report start/stop times if supplied. */ 1868 if (ts->start_time) 1869 { 1870 if (ts->stop_time) 1871 { 1872 LONGEST run_time = ts->stop_time - ts->start_time; 1873 1874 /* Reporting a run time is more readable than two long numbers. */ 1875 gdb_printf (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"), 1876 (long int) (ts->start_time / 1000000), 1877 (long int) (ts->start_time % 1000000), 1878 (long int) (run_time / 1000000), 1879 (long int) (run_time % 1000000)); 1880 } 1881 else 1882 gdb_printf (_("Trace started at %ld.%06ld secs.\n"), 1883 (long int) (ts->start_time / 1000000), 1884 (long int) (ts->start_time % 1000000)); 1885 } 1886 else if (ts->stop_time) 1887 gdb_printf (_("Trace stopped at %ld.%06ld secs.\n"), 1888 (long int) (ts->stop_time / 1000000), 1889 (long int) (ts->stop_time % 1000000)); 1890 1891 /* Now report any per-tracepoint status available. */ 1892 for (breakpoint *t : all_tracepoints ()) 1893 target_get_tracepoint_status (t, NULL); 1894 } 1895 1896 /* Report the trace status to uiout, in a way suitable for MI, and not 1897 suitable for CLI. If ON_STOP is true, suppress a few fields that 1898 are not meaningful in the -trace-stop response. 1899 1900 The implementation is essentially parallel to trace_status_command, but 1901 merging them will result in unreadable code. */ 1902 void 1903 trace_status_mi (int on_stop) 1904 { 1905 struct ui_out *uiout = current_uiout; 1906 struct trace_status *ts = current_trace_status (); 1907 int status; 1908 1909 status = target_get_trace_status (ts); 1910 1911 if (status == -1 && ts->filename == NULL) 1912 { 1913 uiout->field_string ("supported", "0"); 1914 return; 1915 } 1916 1917 if (ts->filename != NULL) 1918 uiout->field_string ("supported", "file"); 1919 else if (!on_stop) 1920 uiout->field_string ("supported", "1"); 1921 1922 if (ts->filename != NULL) 1923 uiout->field_string ("trace-file", ts->filename); 1924 1925 gdb_assert (ts->running_known); 1926 1927 if (ts->running) 1928 { 1929 uiout->field_string ("running", "1"); 1930 1931 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable. 1932 Given that the frontend gets the status either on -trace-stop, or from 1933 -trace-status after re-connection, it does not seem like this 1934 information is necessary for anything. It is not necessary for either 1935 figuring the vital state of the target nor for navigation of trace 1936 frames. If the frontend wants to show the current state is some 1937 configure dialog, it can request the value when such dialog is 1938 invoked by the user. */ 1939 } 1940 else 1941 { 1942 const char *stop_reason = NULL; 1943 int stopping_tracepoint = -1; 1944 1945 if (!on_stop) 1946 uiout->field_string ("running", "0"); 1947 1948 if (ts->stop_reason != trace_stop_reason_unknown) 1949 { 1950 switch (ts->stop_reason) 1951 { 1952 case trace_stop_command: 1953 stop_reason = "request"; 1954 break; 1955 case trace_buffer_full: 1956 stop_reason = "overflow"; 1957 break; 1958 case trace_disconnected: 1959 stop_reason = "disconnection"; 1960 break; 1961 case tracepoint_passcount: 1962 stop_reason = "passcount"; 1963 stopping_tracepoint = ts->stopping_tracepoint; 1964 break; 1965 case tracepoint_error: 1966 stop_reason = "error"; 1967 stopping_tracepoint = ts->stopping_tracepoint; 1968 break; 1969 } 1970 1971 if (stop_reason) 1972 { 1973 uiout->field_string ("stop-reason", stop_reason); 1974 if (stopping_tracepoint != -1) 1975 uiout->field_signed ("stopping-tracepoint", 1976 stopping_tracepoint); 1977 if (ts->stop_reason == tracepoint_error) 1978 uiout->field_string ("error-description", 1979 ts->stop_desc); 1980 } 1981 } 1982 } 1983 1984 if (ts->traceframe_count != -1) 1985 uiout->field_signed ("frames", ts->traceframe_count); 1986 if (ts->traceframes_created != -1) 1987 uiout->field_signed ("frames-created", ts->traceframes_created); 1988 if (ts->buffer_size != -1) 1989 uiout->field_signed ("buffer-size", ts->buffer_size); 1990 if (ts->buffer_free != -1) 1991 uiout->field_signed ("buffer-free", ts->buffer_free); 1992 1993 uiout->field_signed ("disconnected", ts->disconnected_tracing); 1994 uiout->field_signed ("circular", ts->circular_buffer); 1995 1996 uiout->field_string ("user-name", ts->user_name); 1997 uiout->field_string ("notes", ts->notes); 1998 1999 { 2000 char buf[100]; 2001 2002 xsnprintf (buf, sizeof buf, "%ld.%06ld", 2003 (long int) (ts->start_time / 1000000), 2004 (long int) (ts->start_time % 1000000)); 2005 uiout->field_string ("start-time", buf); 2006 xsnprintf (buf, sizeof buf, "%ld.%06ld", 2007 (long int) (ts->stop_time / 1000000), 2008 (long int) (ts->stop_time % 1000000)); 2009 uiout->field_string ("stop-time", buf); 2010 } 2011 } 2012 2013 /* Check if a trace run is ongoing. If so, and FROM_TTY, query the 2014 user if she really wants to detach. */ 2015 2016 void 2017 query_if_trace_running (int from_tty) 2018 { 2019 if (!from_tty) 2020 return; 2021 2022 /* It can happen that the target that was tracing went away on its 2023 own, and we didn't notice. Get a status update, and if the 2024 current target doesn't even do tracing, then assume it's not 2025 running anymore. */ 2026 if (target_get_trace_status (current_trace_status ()) < 0) 2027 current_trace_status ()->running = 0; 2028 2029 /* If running interactively, give the user the option to cancel and 2030 then decide what to do differently with the run. Scripts are 2031 just going to disconnect and let the target deal with it, 2032 according to how it's been instructed previously via 2033 disconnected-tracing. */ 2034 if (current_trace_status ()->running) 2035 { 2036 process_tracepoint_on_disconnect (); 2037 2038 if (current_trace_status ()->disconnected_tracing) 2039 { 2040 if (!query (_("Trace is running and will " 2041 "continue after detach; detach anyway? "))) 2042 error (_("Not confirmed.")); 2043 } 2044 else 2045 { 2046 if (!query (_("Trace is running but will " 2047 "stop on detach; detach anyway? "))) 2048 error (_("Not confirmed.")); 2049 } 2050 } 2051 } 2052 2053 /* This function handles the details of what to do about an ongoing 2054 tracing run if the user has asked to detach or otherwise disconnect 2055 from the target. */ 2056 2057 void 2058 disconnect_tracing (void) 2059 { 2060 /* Also we want to be out of tfind mode, otherwise things can get 2061 confusing upon reconnection. Just use these calls instead of 2062 full tfind_1 behavior because we're in the middle of detaching, 2063 and there's no point to updating current stack frame etc. */ 2064 trace_reset_local_state (); 2065 } 2066 2067 /* Worker function for the various flavors of the tfind command. */ 2068 void 2069 tfind_1 (enum trace_find_type type, int num, 2070 CORE_ADDR addr1, CORE_ADDR addr2, 2071 int from_tty) 2072 { 2073 int target_frameno = -1, target_tracept = -1; 2074 struct frame_id old_frame_id = null_frame_id; 2075 struct tracepoint *tp; 2076 struct ui_out *uiout = current_uiout; 2077 2078 /* Only try to get the current stack frame if we have a chance of 2079 succeeding. In particular, if we're trying to get a first trace 2080 frame while all threads are running, it's not going to succeed, 2081 so leave it with a default value and let the frame comparison 2082 below (correctly) decide to print out the source location of the 2083 trace frame. */ 2084 if (!(type == tfind_number && num == -1) 2085 && (has_stack_frames () || traceframe_number >= 0)) 2086 old_frame_id = get_frame_id (get_current_frame ()); 2087 2088 target_frameno = target_trace_find (type, num, addr1, addr2, 2089 &target_tracept); 2090 2091 if (type == tfind_number 2092 && num == -1 2093 && target_frameno == -1) 2094 { 2095 /* We told the target to get out of tfind mode, and it did. */ 2096 } 2097 else if (target_frameno == -1) 2098 { 2099 /* A request for a non-existent trace frame has failed. 2100 Our response will be different, depending on FROM_TTY: 2101 2102 If FROM_TTY is true, meaning that this command was 2103 typed interactively by the user, then give an error 2104 and DO NOT change the state of traceframe_number etc. 2105 2106 However if FROM_TTY is false, meaning that we're either 2107 in a script, a loop, or a user-defined command, then 2108 DON'T give an error, but DO change the state of 2109 traceframe_number etc. to invalid. 2110 2111 The rationale is that if you typed the command, you 2112 might just have committed a typo or something, and you'd 2113 like to NOT lose your current debugging state. However 2114 if you're in a user-defined command or especially in a 2115 loop, then you need a way to detect that the command 2116 failed WITHOUT aborting. This allows you to write 2117 scripts that search thru the trace buffer until the end, 2118 and then continue on to do something else. */ 2119 2120 if (from_tty) 2121 error (_("Target failed to find requested trace frame.")); 2122 else 2123 { 2124 if (info_verbose) 2125 gdb_printf ("End of trace buffer.\n"); 2126 #if 0 /* dubious now? */ 2127 /* The following will not recurse, since it's 2128 special-cased. */ 2129 tfind_command ("-1", from_tty); 2130 #endif 2131 } 2132 } 2133 2134 tp = get_tracepoint_by_number_on_target (target_tracept); 2135 2136 reinit_frame_cache (); 2137 target_dcache_invalidate (); 2138 2139 set_tracepoint_num (tp ? tp->number : target_tracept); 2140 2141 if (target_frameno != get_traceframe_number ()) 2142 gdb::observers::traceframe_changed.notify (target_frameno, tracepoint_number); 2143 2144 set_current_traceframe (target_frameno); 2145 2146 if (target_frameno == -1) 2147 set_traceframe_context (NULL); 2148 else 2149 set_traceframe_context (get_current_frame ()); 2150 2151 if (traceframe_number >= 0) 2152 { 2153 /* Use different branches for MI and CLI to make CLI messages 2154 i18n-eable. */ 2155 if (uiout->is_mi_like_p ()) 2156 { 2157 uiout->field_string ("found", "1"); 2158 uiout->field_signed ("tracepoint", tracepoint_number); 2159 uiout->field_signed ("traceframe", traceframe_number); 2160 } 2161 else 2162 { 2163 gdb_printf (_("Found trace frame %d, tracepoint %d\n"), 2164 traceframe_number, tracepoint_number); 2165 } 2166 } 2167 else 2168 { 2169 if (uiout->is_mi_like_p ()) 2170 uiout->field_string ("found", "0"); 2171 else if (type == tfind_number && num == -1) 2172 gdb_printf (_("No longer looking at any trace frame\n")); 2173 else /* This case may never occur, check. */ 2174 gdb_printf (_("No trace frame found\n")); 2175 } 2176 2177 /* If we're in nonstop mode and getting out of looking at trace 2178 frames, there won't be any current frame to go back to and 2179 display. */ 2180 if (from_tty 2181 && (has_stack_frames () || traceframe_number >= 0)) 2182 { 2183 enum print_what print_what; 2184 2185 /* NOTE: in imitation of the step command, try to determine 2186 whether we have made a transition from one function to 2187 another. If so, we'll print the "stack frame" (ie. the new 2188 function and it's arguments) -- otherwise we'll just show the 2189 new source line. */ 2190 2191 if (old_frame_id == get_frame_id (get_current_frame ())) 2192 print_what = SRC_LINE; 2193 else 2194 print_what = SRC_AND_LOC; 2195 2196 print_stack_frame (get_selected_frame (NULL), 1, print_what, 1); 2197 do_displays (); 2198 } 2199 } 2200 2201 /* Error on looking at traceframes while trace is running. */ 2202 2203 void 2204 check_trace_running (struct trace_status *status) 2205 { 2206 if (status->running && status->filename == NULL) 2207 error (_("May not look at trace frames while trace is running.")); 2208 } 2209 2210 /* trace_find_command takes a trace frame number n, 2211 sends "QTFrame:<n>" to the target, 2212 and accepts a reply that may contain several optional pieces 2213 of information: a frame number, a tracepoint number, and an 2214 indication of whether this is a trap frame or a stepping frame. 2215 2216 The minimal response is just "OK" (which indicates that the 2217 target does not give us a frame number or a tracepoint number). 2218 Instead of that, the target may send us a string containing 2219 any combination of: 2220 F<hexnum> (gives the selected frame number) 2221 T<hexnum> (gives the selected tracepoint number) 2222 */ 2223 2224 /* tfind command */ 2225 static void 2226 tfind_command_1 (const char *args, int from_tty) 2227 { /* This should only be called with a numeric argument. */ 2228 int frameno = -1; 2229 2230 check_trace_running (current_trace_status ()); 2231 2232 if (args == 0 || *args == 0) 2233 { /* TFIND with no args means find NEXT trace frame. */ 2234 if (traceframe_number == -1) 2235 frameno = 0; /* "next" is first one. */ 2236 else 2237 frameno = traceframe_number + 1; 2238 } 2239 else if (0 == strcmp (args, "-")) 2240 { 2241 if (traceframe_number == -1) 2242 error (_("not debugging trace buffer")); 2243 else if (from_tty && traceframe_number == 0) 2244 error (_("already at start of trace buffer")); 2245 2246 frameno = traceframe_number - 1; 2247 } 2248 /* A hack to work around eval's need for fp to have been collected. */ 2249 else if (0 == strcmp (args, "-1")) 2250 frameno = -1; 2251 else 2252 frameno = parse_and_eval_long (args); 2253 2254 if (frameno < -1) 2255 error (_("invalid input (%d is less than zero)"), frameno); 2256 2257 tfind_1 (tfind_number, frameno, 0, 0, from_tty); 2258 } 2259 2260 static void 2261 tfind_command (const char *args, int from_tty) 2262 { 2263 tfind_command_1 (args, from_tty); 2264 } 2265 2266 /* tfind end */ 2267 static void 2268 tfind_end_command (const char *args, int from_tty) 2269 { 2270 tfind_command_1 ("-1", from_tty); 2271 } 2272 2273 /* tfind start */ 2274 static void 2275 tfind_start_command (const char *args, int from_tty) 2276 { 2277 tfind_command_1 ("0", from_tty); 2278 } 2279 2280 /* tfind pc command */ 2281 static void 2282 tfind_pc_command (const char *args, int from_tty) 2283 { 2284 CORE_ADDR pc; 2285 2286 check_trace_running (current_trace_status ()); 2287 2288 if (args == 0 || *args == 0) 2289 pc = regcache_read_pc (get_current_regcache ()); 2290 else 2291 pc = parse_and_eval_address (args); 2292 2293 tfind_1 (tfind_pc, 0, pc, 0, from_tty); 2294 } 2295 2296 /* tfind tracepoint command */ 2297 static void 2298 tfind_tracepoint_command (const char *args, int from_tty) 2299 { 2300 int tdp; 2301 struct tracepoint *tp; 2302 2303 check_trace_running (current_trace_status ()); 2304 2305 if (args == 0 || *args == 0) 2306 { 2307 if (tracepoint_number == -1) 2308 error (_("No current tracepoint -- please supply an argument.")); 2309 else 2310 tdp = tracepoint_number; /* Default is current TDP. */ 2311 } 2312 else 2313 tdp = parse_and_eval_long (args); 2314 2315 /* If we have the tracepoint on hand, use the number that the 2316 target knows about (which may be different if we disconnected 2317 and reconnected). */ 2318 tp = get_tracepoint (tdp); 2319 if (tp) 2320 tdp = tp->number_on_target; 2321 2322 tfind_1 (tfind_tp, tdp, 0, 0, from_tty); 2323 } 2324 2325 /* TFIND LINE command: 2326 2327 This command will take a sourceline for argument, just like BREAK 2328 or TRACE (ie. anything that "decode_line_1" can handle). 2329 2330 With no argument, this command will find the next trace frame 2331 corresponding to a source line OTHER THAN THE CURRENT ONE. */ 2332 2333 static void 2334 tfind_line_command (const char *args, int from_tty) 2335 { 2336 check_trace_running (current_trace_status ()); 2337 2338 symtab_and_line sal; 2339 if (args == 0 || *args == 0) 2340 { 2341 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0); 2342 } 2343 else 2344 { 2345 std::vector<symtab_and_line> sals 2346 = decode_line_with_current_source (args, DECODE_LINE_FUNFIRSTLINE); 2347 sal = sals[0]; 2348 } 2349 2350 if (sal.symtab == 0) 2351 error (_("No line number information available.")); 2352 2353 CORE_ADDR start_pc, end_pc; 2354 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc)) 2355 { 2356 if (start_pc == end_pc) 2357 { 2358 gdb_printf ("Line %d of \"%s\"", 2359 sal.line, 2360 symtab_to_filename_for_display (sal.symtab)); 2361 gdb_stdout->wrap_here (2); 2362 gdb_printf (" is at address "); 2363 print_address (get_current_arch (), start_pc, gdb_stdout); 2364 gdb_stdout->wrap_here (2); 2365 gdb_printf (" but contains no code.\n"); 2366 sal = find_pc_line (start_pc, 0); 2367 if (sal.line > 0 2368 && find_line_pc_range (sal, &start_pc, &end_pc) 2369 && start_pc != end_pc) 2370 gdb_printf ("Attempting to find line %d instead.\n", 2371 sal.line); 2372 else 2373 error (_("Cannot find a good line.")); 2374 } 2375 } 2376 else 2377 { 2378 /* Is there any case in which we get here, and have an address 2379 which the user would want to see? If we have debugging 2380 symbols and no line numbers? */ 2381 error (_("Line number %d is out of range for \"%s\"."), 2382 sal.line, symtab_to_filename_for_display (sal.symtab)); 2383 } 2384 2385 /* Find within range of stated line. */ 2386 if (args && *args) 2387 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty); 2388 else 2389 tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty); 2390 } 2391 2392 /* tfind range command */ 2393 static void 2394 tfind_range_command (const char *args, int from_tty) 2395 { 2396 static CORE_ADDR start, stop; 2397 const char *tmp; 2398 2399 check_trace_running (current_trace_status ()); 2400 2401 if (args == 0 || *args == 0) 2402 { /* XXX FIXME: what should default behavior be? */ 2403 gdb_printf ("Usage: tfind range STARTADDR, ENDADDR\n"); 2404 return; 2405 } 2406 2407 if (0 != (tmp = strchr (args, ','))) 2408 { 2409 std::string start_addr (args, tmp); 2410 ++tmp; 2411 tmp = skip_spaces (tmp); 2412 start = parse_and_eval_address (start_addr.c_str ()); 2413 stop = parse_and_eval_address (tmp); 2414 } 2415 else 2416 { /* No explicit end address? */ 2417 start = parse_and_eval_address (args); 2418 stop = start + 1; /* ??? */ 2419 } 2420 2421 tfind_1 (tfind_range, 0, start, stop, from_tty); 2422 } 2423 2424 /* tfind outside command */ 2425 static void 2426 tfind_outside_command (const char *args, int from_tty) 2427 { 2428 CORE_ADDR start, stop; 2429 const char *tmp; 2430 2431 if (current_trace_status ()->running 2432 && current_trace_status ()->filename == NULL) 2433 error (_("May not look at trace frames while trace is running.")); 2434 2435 if (args == 0 || *args == 0) 2436 { /* XXX FIXME: what should default behavior be? */ 2437 gdb_printf ("Usage: tfind outside STARTADDR, ENDADDR\n"); 2438 return; 2439 } 2440 2441 if (0 != (tmp = strchr (args, ','))) 2442 { 2443 std::string start_addr (args, tmp); 2444 ++tmp; 2445 tmp = skip_spaces (tmp); 2446 start = parse_and_eval_address (start_addr.c_str ()); 2447 stop = parse_and_eval_address (tmp); 2448 } 2449 else 2450 { /* No explicit end address? */ 2451 start = parse_and_eval_address (args); 2452 stop = start + 1; /* ??? */ 2453 } 2454 2455 tfind_1 (tfind_outside, 0, start, stop, from_tty); 2456 } 2457 2458 /* info scope command: list the locals for a scope. */ 2459 static void 2460 info_scope_command (const char *args_in, int from_tty) 2461 { 2462 struct symbol *sym; 2463 struct bound_minimal_symbol msym; 2464 const struct block *block; 2465 const char *symname; 2466 const char *save_args = args_in; 2467 struct block_iterator iter; 2468 int j, count = 0; 2469 struct gdbarch *gdbarch; 2470 int regno; 2471 const char *args = args_in; 2472 2473 if (args == 0 || *args == 0) 2474 error (_("requires an argument (function, " 2475 "line or *addr) to define a scope")); 2476 2477 location_spec_up locspec = string_to_location_spec (&args, 2478 current_language); 2479 std::vector<symtab_and_line> sals 2480 = decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, 2481 NULL, NULL, 0); 2482 if (sals.empty ()) 2483 { 2484 /* Presumably decode_line_1 has already warned. */ 2485 return; 2486 } 2487 2488 /* Resolve line numbers to PC. */ 2489 resolve_sal_pc (&sals[0]); 2490 block = block_for_pc (sals[0].pc); 2491 2492 while (block != 0) 2493 { 2494 QUIT; /* Allow user to bail out with ^C. */ 2495 ALL_BLOCK_SYMBOLS (block, iter, sym) 2496 { 2497 QUIT; /* Allow user to bail out with ^C. */ 2498 if (count == 0) 2499 gdb_printf ("Scope for %s:\n", save_args); 2500 count++; 2501 2502 symname = sym->print_name (); 2503 if (symname == NULL || *symname == '\0') 2504 continue; /* Probably botched, certainly useless. */ 2505 2506 gdbarch = sym->arch (); 2507 2508 gdb_printf ("Symbol %s is ", symname); 2509 2510 if (SYMBOL_COMPUTED_OPS (sym) != NULL) 2511 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, 2512 block->entry_pc (), 2513 gdb_stdout); 2514 else 2515 { 2516 switch (sym->aclass ()) 2517 { 2518 default: 2519 case LOC_UNDEF: /* Messed up symbol? */ 2520 gdb_printf ("a bogus symbol, class %d.\n", 2521 sym->aclass ()); 2522 count--; /* Don't count this one. */ 2523 continue; 2524 case LOC_CONST: 2525 gdb_printf ("a constant with value %s (%s)", 2526 plongest (sym->value_longest ()), 2527 hex_string (sym->value_longest ())); 2528 break; 2529 case LOC_CONST_BYTES: 2530 gdb_printf ("constant bytes: "); 2531 if (sym->type ()) 2532 for (j = 0; j < sym->type ()->length (); j++) 2533 gdb_printf (" %02x", (unsigned) sym->value_bytes ()[j]); 2534 break; 2535 case LOC_STATIC: 2536 gdb_printf ("in static storage at address "); 2537 gdb_printf ("%s", paddress (gdbarch, sym->value_address ())); 2538 break; 2539 case LOC_REGISTER: 2540 /* GDBARCH is the architecture associated with the objfile 2541 the symbol is defined in; the target architecture may be 2542 different, and may provide additional registers. However, 2543 we do not know the target architecture at this point. 2544 We assume the objfile architecture will contain all the 2545 standard registers that occur in debug info in that 2546 objfile. */ 2547 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, 2548 gdbarch); 2549 2550 if (sym->is_argument ()) 2551 gdb_printf ("an argument in register $%s", 2552 gdbarch_register_name (gdbarch, regno)); 2553 else 2554 gdb_printf ("a local variable in register $%s", 2555 gdbarch_register_name (gdbarch, regno)); 2556 break; 2557 case LOC_ARG: 2558 gdb_printf ("an argument at stack/frame offset %s", 2559 plongest (sym->value_longest ())); 2560 break; 2561 case LOC_LOCAL: 2562 gdb_printf ("a local variable at frame offset %s", 2563 plongest (sym->value_longest ())); 2564 break; 2565 case LOC_REF_ARG: 2566 gdb_printf ("a reference argument at offset %s", 2567 plongest (sym->value_longest ())); 2568 break; 2569 case LOC_REGPARM_ADDR: 2570 /* Note comment at LOC_REGISTER. */ 2571 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, 2572 gdbarch); 2573 gdb_printf ("the address of an argument, in register $%s", 2574 gdbarch_register_name (gdbarch, regno)); 2575 break; 2576 case LOC_TYPEDEF: 2577 gdb_printf ("a typedef.\n"); 2578 continue; 2579 case LOC_LABEL: 2580 gdb_printf ("a label at address "); 2581 gdb_printf ("%s", paddress (gdbarch, sym->value_address ())); 2582 break; 2583 case LOC_BLOCK: 2584 gdb_printf ("a function at address "); 2585 gdb_printf ("%s", 2586 paddress (gdbarch, 2587 sym->value_block ()->entry_pc ())); 2588 break; 2589 case LOC_UNRESOLVED: 2590 msym = lookup_minimal_symbol (sym->linkage_name (), 2591 NULL, NULL); 2592 if (msym.minsym == NULL) 2593 gdb_printf ("Unresolved Static"); 2594 else 2595 { 2596 gdb_printf ("static storage at address "); 2597 gdb_printf ("%s", 2598 paddress (gdbarch, msym.value_address ())); 2599 } 2600 break; 2601 case LOC_OPTIMIZED_OUT: 2602 gdb_printf ("optimized out.\n"); 2603 continue; 2604 case LOC_COMPUTED: 2605 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method"); 2606 } 2607 } 2608 if (sym->type ()) 2609 { 2610 struct type *t = check_typedef (sym->type ()); 2611 2612 gdb_printf (", length %s.\n", pulongest (t->length ())); 2613 } 2614 } 2615 if (block->function ()) 2616 break; 2617 else 2618 block = block->superblock (); 2619 } 2620 if (count <= 0) 2621 gdb_printf ("Scope for %s contains no locals or arguments.\n", 2622 save_args); 2623 } 2624 2625 /* Helper for trace_dump_command. Dump the action list starting at 2626 ACTION. STEPPING_ACTIONS is true if we're iterating over the 2627 actions of the body of a while-stepping action. STEPPING_FRAME is 2628 set if the current traceframe was determined to be a while-stepping 2629 traceframe. */ 2630 2631 static void 2632 trace_dump_actions (struct command_line *action, 2633 int stepping_actions, int stepping_frame, 2634 int from_tty) 2635 { 2636 const char *action_exp, *next_comma; 2637 2638 for (; action != NULL; action = action->next) 2639 { 2640 struct cmd_list_element *cmd; 2641 2642 QUIT; /* Allow user to bail out with ^C. */ 2643 action_exp = action->line; 2644 action_exp = skip_spaces (action_exp); 2645 2646 /* The collection actions to be done while stepping are 2647 bracketed by the commands "while-stepping" and "end". */ 2648 2649 if (*action_exp == '#') /* comment line */ 2650 continue; 2651 2652 cmd = lookup_cmd (&action_exp, cmdlist, "", NULL, -1, 1); 2653 if (cmd == 0) 2654 error (_("Bad action list item: %s"), action_exp); 2655 2656 if (cmd_simple_func_eq (cmd, while_stepping_pseudocommand)) 2657 { 2658 gdb_assert (action->body_list_1 == nullptr); 2659 trace_dump_actions (action->body_list_0.get (), 2660 1, stepping_frame, from_tty); 2661 } 2662 else if (cmd_simple_func_eq (cmd, collect_pseudocommand)) 2663 { 2664 /* Display the collected data. 2665 For the trap frame, display only what was collected at 2666 the trap. Likewise for stepping frames, display only 2667 what was collected while stepping. This means that the 2668 two boolean variables, STEPPING_FRAME and 2669 STEPPING_ACTIONS should be equal. */ 2670 if (stepping_frame == stepping_actions) 2671 { 2672 int trace_string = 0; 2673 2674 if (*action_exp == '/') 2675 action_exp = decode_agent_options (action_exp, &trace_string); 2676 2677 do 2678 { /* Repeat over a comma-separated list. */ 2679 QUIT; /* Allow user to bail out with ^C. */ 2680 if (*action_exp == ',') 2681 action_exp++; 2682 action_exp = skip_spaces (action_exp); 2683 2684 next_comma = strchr (action_exp, ','); 2685 2686 if (0 == strncasecmp (action_exp, "$reg", 4)) 2687 registers_info (NULL, from_tty); 2688 else if (0 == strncasecmp (action_exp, "$_ret", 5)) 2689 ; 2690 else if (0 == strncasecmp (action_exp, "$loc", 4)) 2691 info_locals_command (NULL, from_tty); 2692 else if (0 == strncasecmp (action_exp, "$arg", 4)) 2693 info_args_command (NULL, from_tty); 2694 else 2695 { /* variable */ 2696 std::string contents; 2697 const char *exp = action_exp; 2698 if (next_comma != NULL) 2699 { 2700 size_t len = next_comma - action_exp; 2701 contents = std::string (action_exp, len); 2702 exp = contents.c_str (); 2703 } 2704 2705 gdb_printf ("%s = ", exp); 2706 output_command (exp, from_tty); 2707 gdb_printf ("\n"); 2708 } 2709 action_exp = next_comma; 2710 } 2711 while (action_exp && *action_exp == ','); 2712 } 2713 } 2714 } 2715 } 2716 2717 /* Return bp_location of the tracepoint associated with the current 2718 traceframe. Set *STEPPING_FRAME_P to 1 if the current traceframe 2719 is a stepping traceframe. */ 2720 2721 struct bp_location * 2722 get_traceframe_location (int *stepping_frame_p) 2723 { 2724 struct tracepoint *t; 2725 struct regcache *regcache; 2726 2727 if (tracepoint_number == -1) 2728 error (_("No current trace frame.")); 2729 2730 t = get_tracepoint (tracepoint_number); 2731 2732 if (t == NULL) 2733 error (_("No known tracepoint matches 'current' tracepoint #%d."), 2734 tracepoint_number); 2735 2736 /* The current frame is a trap frame if the frame PC is equal to the 2737 tracepoint PC. If not, then the current frame was collected 2738 during single-stepping. */ 2739 regcache = get_current_regcache (); 2740 2741 /* If the traceframe's address matches any of the tracepoint's 2742 locations, assume it is a direct hit rather than a while-stepping 2743 frame. (FIXME this is not reliable, should record each frame's 2744 type.) */ 2745 for (bp_location *tloc : t->locations ()) 2746 if (tloc->address == regcache_read_pc (regcache)) 2747 { 2748 *stepping_frame_p = 0; 2749 return tloc; 2750 } 2751 2752 /* If this is a stepping frame, we don't know which location 2753 triggered. The first is as good (or bad) a guess as any... */ 2754 *stepping_frame_p = 1; 2755 return t->loc; 2756 } 2757 2758 /* Return the default collect actions of a tracepoint T. */ 2759 2760 static counted_command_line 2761 all_tracepoint_actions (struct breakpoint *t) 2762 { 2763 counted_command_line actions (nullptr, command_lines_deleter ()); 2764 2765 /* If there are default expressions to collect, make up a collect 2766 action and prepend to the action list to encode. Note that since 2767 validation is per-tracepoint (local var "xyz" might be valid for 2768 one tracepoint and not another, etc), we make up the action on 2769 the fly, and don't cache it. */ 2770 if (!default_collect.empty ()) 2771 { 2772 gdb::unique_xmalloc_ptr<char> default_collect_line 2773 = xstrprintf ("collect %s", default_collect.c_str ()); 2774 2775 validate_actionline (default_collect_line.get (), t); 2776 actions.reset (new struct command_line (simple_control, 2777 default_collect_line.release ()), 2778 command_lines_deleter ()); 2779 } 2780 2781 return actions; 2782 } 2783 2784 /* The tdump command. */ 2785 2786 static void 2787 tdump_command (const char *args, int from_tty) 2788 { 2789 int stepping_frame = 0; 2790 struct bp_location *loc; 2791 2792 /* This throws an error is not inspecting a trace frame. */ 2793 loc = get_traceframe_location (&stepping_frame); 2794 2795 gdb_printf ("Data collected at tracepoint %d, trace frame %d:\n", 2796 tracepoint_number, traceframe_number); 2797 2798 /* This command only makes sense for the current frame, not the 2799 selected frame. */ 2800 scoped_restore_current_thread restore_thread; 2801 2802 select_frame (get_current_frame ()); 2803 2804 counted_command_line actions = all_tracepoint_actions (loc->owner); 2805 2806 trace_dump_actions (actions.get (), 0, stepping_frame, from_tty); 2807 trace_dump_actions (breakpoint_commands (loc->owner), 0, stepping_frame, 2808 from_tty); 2809 } 2810 2811 /* Encode a piece of a tracepoint's source-level definition in a form 2812 that is suitable for both protocol and saving in files. */ 2813 /* This version does not do multiple encodes for long strings; it should 2814 return an offset to the next piece to encode. FIXME */ 2815 2816 int 2817 encode_source_string (int tpnum, ULONGEST addr, 2818 const char *srctype, const char *src, 2819 char *buf, int buf_size) 2820 { 2821 if (80 + strlen (srctype) > buf_size) 2822 error (_("Buffer too small for source encoding")); 2823 sprintf (buf, "%x:%s:%s:%x:%x:", 2824 tpnum, phex_nz (addr, sizeof (addr)), 2825 srctype, 0, (int) strlen (src)); 2826 if (strlen (buf) + strlen (src) * 2 >= buf_size) 2827 error (_("Source string too long for buffer")); 2828 bin2hex ((gdb_byte *) src, buf + strlen (buf), strlen (src)); 2829 return -1; 2830 } 2831 2832 /* Tell the target what to do with an ongoing tracing run if GDB 2833 disconnects for some reason. */ 2834 2835 static void 2836 set_disconnected_tracing (const char *args, int from_tty, 2837 struct cmd_list_element *c) 2838 { 2839 target_set_disconnected_tracing (disconnected_tracing); 2840 } 2841 2842 static void 2843 set_circular_trace_buffer (const char *args, int from_tty, 2844 struct cmd_list_element *c) 2845 { 2846 target_set_circular_trace_buffer (circular_trace_buffer); 2847 } 2848 2849 static void 2850 set_trace_buffer_size (const char *args, int from_tty, 2851 struct cmd_list_element *c) 2852 { 2853 target_set_trace_buffer_size (trace_buffer_size); 2854 } 2855 2856 static void 2857 set_trace_user (const char *args, int from_tty, 2858 struct cmd_list_element *c) 2859 { 2860 int ret; 2861 2862 ret = target_set_trace_notes (trace_user.c_str (), NULL, NULL); 2863 2864 if (!ret) 2865 warning (_("Target does not support trace notes, user ignored")); 2866 } 2867 2868 static void 2869 set_trace_notes (const char *args, int from_tty, 2870 struct cmd_list_element *c) 2871 { 2872 int ret; 2873 2874 ret = target_set_trace_notes (NULL, trace_notes.c_str (), NULL); 2875 2876 if (!ret) 2877 warning (_("Target does not support trace notes, note ignored")); 2878 } 2879 2880 static void 2881 set_trace_stop_notes (const char *args, int from_tty, 2882 struct cmd_list_element *c) 2883 { 2884 int ret; 2885 2886 ret = target_set_trace_notes (NULL, NULL, trace_stop_notes.c_str ()); 2887 2888 if (!ret) 2889 warning (_("Target does not support trace notes, stop note ignored")); 2890 } 2891 2892 /* Convert the memory pointed to by mem into hex, placing result in buf. 2893 * Return a pointer to the last char put in buf (null) 2894 * "stolen" from sparc-stub.c 2895 */ 2896 2897 static const char hexchars[] = "0123456789abcdef"; 2898 2899 static char * 2900 mem2hex (gdb_byte *mem, char *buf, int count) 2901 { 2902 gdb_byte ch; 2903 2904 while (count-- > 0) 2905 { 2906 ch = *mem++; 2907 2908 *buf++ = hexchars[ch >> 4]; 2909 *buf++ = hexchars[ch & 0xf]; 2910 } 2911 2912 *buf = 0; 2913 2914 return buf; 2915 } 2916 2917 int 2918 get_traceframe_number (void) 2919 { 2920 return traceframe_number; 2921 } 2922 2923 int 2924 get_tracepoint_number (void) 2925 { 2926 return tracepoint_number; 2927 } 2928 2929 /* Make the traceframe NUM be the current trace frame. Does nothing 2930 if NUM is already current. */ 2931 2932 void 2933 set_current_traceframe (int num) 2934 { 2935 int newnum; 2936 2937 if (traceframe_number == num) 2938 { 2939 /* Nothing to do. */ 2940 return; 2941 } 2942 2943 newnum = target_trace_find (tfind_number, num, 0, 0, NULL); 2944 2945 if (newnum != num) 2946 warning (_("could not change traceframe")); 2947 2948 set_traceframe_num (newnum); 2949 2950 /* Changing the traceframe changes our view of registers and of the 2951 frame chain. */ 2952 registers_changed (); 2953 2954 clear_traceframe_info (); 2955 } 2956 2957 scoped_restore_current_traceframe::scoped_restore_current_traceframe () 2958 : m_traceframe_number (traceframe_number) 2959 {} 2960 2961 /* Given a number and address, return an uploaded tracepoint with that 2962 number, creating if necessary. */ 2963 2964 struct uploaded_tp * 2965 get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp) 2966 { 2967 struct uploaded_tp *utp; 2968 2969 for (utp = *utpp; utp; utp = utp->next) 2970 if (utp->number == num && utp->addr == addr) 2971 return utp; 2972 2973 utp = new uploaded_tp; 2974 utp->number = num; 2975 utp->addr = addr; 2976 utp->next = *utpp; 2977 *utpp = utp; 2978 2979 return utp; 2980 } 2981 2982 void 2983 free_uploaded_tps (struct uploaded_tp **utpp) 2984 { 2985 struct uploaded_tp *next_one; 2986 2987 while (*utpp) 2988 { 2989 next_one = (*utpp)->next; 2990 delete *utpp; 2991 *utpp = next_one; 2992 } 2993 } 2994 2995 /* Given a number and address, return an uploaded tracepoint with that 2996 number, creating if necessary. */ 2997 2998 struct uploaded_tsv * 2999 get_uploaded_tsv (int num, struct uploaded_tsv **utsvp) 3000 { 3001 struct uploaded_tsv *utsv; 3002 3003 for (utsv = *utsvp; utsv; utsv = utsv->next) 3004 if (utsv->number == num) 3005 return utsv; 3006 3007 utsv = XCNEW (struct uploaded_tsv); 3008 utsv->number = num; 3009 utsv->next = *utsvp; 3010 *utsvp = utsv; 3011 3012 return utsv; 3013 } 3014 3015 void 3016 free_uploaded_tsvs (struct uploaded_tsv **utsvp) 3017 { 3018 struct uploaded_tsv *next_one; 3019 3020 while (*utsvp) 3021 { 3022 next_one = (*utsvp)->next; 3023 xfree (*utsvp); 3024 *utsvp = next_one; 3025 } 3026 } 3027 3028 /* FIXME this function is heuristic and will miss the cases where the 3029 conditional is semantically identical but differs in whitespace, 3030 such as "x == 0" vs "x==0". */ 3031 3032 static int 3033 cond_string_is_same (char *str1, char *str2) 3034 { 3035 if (str1 == NULL || str2 == NULL) 3036 return (str1 == str2); 3037 3038 return (strcmp (str1, str2) == 0); 3039 } 3040 3041 /* Look for an existing tracepoint that seems similar enough to the 3042 uploaded one. Enablement isn't compared, because the user can 3043 toggle that freely, and may have done so in anticipation of the 3044 next trace run. Return the location of matched tracepoint. */ 3045 3046 static struct bp_location * 3047 find_matching_tracepoint_location (struct uploaded_tp *utp) 3048 { 3049 struct bp_location *loc; 3050 3051 for (breakpoint *b : all_tracepoints ()) 3052 { 3053 struct tracepoint *t = (struct tracepoint *) b; 3054 3055 if (b->type == utp->type 3056 && t->step_count == utp->step 3057 && t->pass_count == utp->pass 3058 && cond_string_is_same (t->cond_string.get (), 3059 utp->cond_string.get ()) 3060 /* FIXME also test actions. */ 3061 ) 3062 { 3063 /* Scan the locations for an address match. */ 3064 for (loc = b->loc; loc; loc = loc->next) 3065 { 3066 if (loc->address == utp->addr) 3067 return loc; 3068 } 3069 } 3070 } 3071 return NULL; 3072 } 3073 3074 /* Given a list of tracepoints uploaded from a target, attempt to 3075 match them up with existing tracepoints, and create new ones if not 3076 found. */ 3077 3078 void 3079 merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps) 3080 { 3081 struct uploaded_tp *utp; 3082 /* A set of tracepoints which are modified. */ 3083 std::vector<breakpoint *> modified_tp; 3084 3085 /* Look for GDB tracepoints that match up with our uploaded versions. */ 3086 for (utp = *uploaded_tps; utp; utp = utp->next) 3087 { 3088 struct bp_location *loc; 3089 struct tracepoint *t; 3090 3091 loc = find_matching_tracepoint_location (utp); 3092 if (loc) 3093 { 3094 int found = 0; 3095 3096 /* Mark this location as already inserted. */ 3097 loc->inserted = 1; 3098 t = (struct tracepoint *) loc->owner; 3099 gdb_printf (_("Assuming tracepoint %d is same " 3100 "as target's tracepoint %d at %s.\n"), 3101 loc->owner->number, utp->number, 3102 paddress (loc->gdbarch, utp->addr)); 3103 3104 /* The tracepoint LOC->owner was modified (the location LOC 3105 was marked as inserted in the target). Save it in 3106 MODIFIED_TP if not there yet. The 'breakpoint-modified' 3107 observers will be notified later once for each tracepoint 3108 saved in MODIFIED_TP. */ 3109 for (breakpoint *b : modified_tp) 3110 if (b == loc->owner) 3111 { 3112 found = 1; 3113 break; 3114 } 3115 if (!found) 3116 modified_tp.push_back (loc->owner); 3117 } 3118 else 3119 { 3120 t = create_tracepoint_from_upload (utp); 3121 if (t) 3122 gdb_printf (_("Created tracepoint %d for " 3123 "target's tracepoint %d at %s.\n"), 3124 t->number, utp->number, 3125 paddress (get_current_arch (), utp->addr)); 3126 else 3127 gdb_printf (_("Failed to create tracepoint for target's " 3128 "tracepoint %d at %s, skipping it.\n"), 3129 utp->number, 3130 paddress (get_current_arch (), utp->addr)); 3131 } 3132 /* Whether found or created, record the number used by the 3133 target, to help with mapping target tracepoints back to their 3134 counterparts here. */ 3135 if (t) 3136 t->number_on_target = utp->number; 3137 } 3138 3139 /* Notify 'breakpoint-modified' observer that at least one of B's 3140 locations was changed. */ 3141 for (breakpoint *b : modified_tp) 3142 gdb::observers::breakpoint_modified.notify (b); 3143 3144 free_uploaded_tps (uploaded_tps); 3145 } 3146 3147 /* Trace state variables don't have much to identify them beyond their 3148 name, so just use that to detect matches. */ 3149 3150 static struct trace_state_variable * 3151 find_matching_tsv (struct uploaded_tsv *utsv) 3152 { 3153 if (!utsv->name) 3154 return NULL; 3155 3156 return find_trace_state_variable (utsv->name); 3157 } 3158 3159 static struct trace_state_variable * 3160 create_tsv_from_upload (struct uploaded_tsv *utsv) 3161 { 3162 const char *namebase; 3163 std::string buf; 3164 int try_num = 0; 3165 struct trace_state_variable *tsv; 3166 3167 if (utsv->name) 3168 { 3169 namebase = utsv->name; 3170 buf = namebase; 3171 } 3172 else 3173 { 3174 namebase = "__tsv"; 3175 buf = string_printf ("%s_%d", namebase, try_num++); 3176 } 3177 3178 /* Fish for a name that is not in use. */ 3179 /* (should check against all internal vars?) */ 3180 while (find_trace_state_variable (buf.c_str ())) 3181 buf = string_printf ("%s_%d", namebase, try_num++); 3182 3183 /* We have an available name, create the variable. */ 3184 tsv = create_trace_state_variable (buf.c_str ()); 3185 tsv->initial_value = utsv->initial_value; 3186 tsv->builtin = utsv->builtin; 3187 3188 gdb::observers::tsv_created.notify (tsv); 3189 3190 return tsv; 3191 } 3192 3193 /* Given a list of uploaded trace state variables, try to match them 3194 up with existing variables, or create additional ones. */ 3195 3196 void 3197 merge_uploaded_trace_state_variables (struct uploaded_tsv **uploaded_tsvs) 3198 { 3199 struct uploaded_tsv *utsv; 3200 int highest; 3201 3202 /* Most likely some numbers will have to be reassigned as part of 3203 the merge, so clear them all in anticipation. */ 3204 for (trace_state_variable &tsv : tvariables) 3205 tsv.number = 0; 3206 3207 for (utsv = *uploaded_tsvs; utsv; utsv = utsv->next) 3208 { 3209 struct trace_state_variable *tsv = find_matching_tsv (utsv); 3210 if (tsv) 3211 { 3212 if (info_verbose) 3213 gdb_printf (_("Assuming trace state variable $%s " 3214 "is same as target's variable %d.\n"), 3215 tsv->name.c_str (), utsv->number); 3216 } 3217 else 3218 { 3219 tsv = create_tsv_from_upload (utsv); 3220 if (info_verbose) 3221 gdb_printf (_("Created trace state variable " 3222 "$%s for target's variable %d.\n"), 3223 tsv->name.c_str (), utsv->number); 3224 } 3225 /* Give precedence to numberings that come from the target. */ 3226 if (tsv) 3227 tsv->number = utsv->number; 3228 } 3229 3230 /* Renumber everything that didn't get a target-assigned number. */ 3231 highest = 0; 3232 for (const trace_state_variable &tsv : tvariables) 3233 highest = std::max (tsv.number, highest); 3234 3235 ++highest; 3236 for (trace_state_variable &tsv : tvariables) 3237 if (tsv.number == 0) 3238 tsv.number = highest++; 3239 3240 free_uploaded_tsvs (uploaded_tsvs); 3241 } 3242 3243 /* Parse the part of trace status syntax that is shared between 3244 the remote protocol and the trace file reader. */ 3245 3246 void 3247 parse_trace_status (const char *line, struct trace_status *ts) 3248 { 3249 const char *p = line, *p1, *p2, *p3, *p_temp; 3250 int end; 3251 ULONGEST val; 3252 3253 ts->running_known = 1; 3254 ts->running = (*p++ == '1'); 3255 ts->stop_reason = trace_stop_reason_unknown; 3256 xfree (ts->stop_desc); 3257 ts->stop_desc = NULL; 3258 ts->traceframe_count = -1; 3259 ts->traceframes_created = -1; 3260 ts->buffer_free = -1; 3261 ts->buffer_size = -1; 3262 ts->disconnected_tracing = 0; 3263 ts->circular_buffer = 0; 3264 xfree (ts->user_name); 3265 ts->user_name = NULL; 3266 xfree (ts->notes); 3267 ts->notes = NULL; 3268 ts->start_time = ts->stop_time = 0; 3269 3270 while (*p++) 3271 { 3272 p1 = strchr (p, ':'); 3273 if (p1 == NULL) 3274 error (_("Malformed trace status, at %s\n\ 3275 Status line: '%s'\n"), p, line); 3276 p3 = strchr (p, ';'); 3277 if (p3 == NULL) 3278 p3 = p + strlen (p); 3279 if (strncmp (p, stop_reason_names[trace_buffer_full], p1 - p) == 0) 3280 { 3281 p = unpack_varlen_hex (++p1, &val); 3282 ts->stop_reason = trace_buffer_full; 3283 } 3284 else if (strncmp (p, stop_reason_names[trace_never_run], p1 - p) == 0) 3285 { 3286 p = unpack_varlen_hex (++p1, &val); 3287 ts->stop_reason = trace_never_run; 3288 } 3289 else if (strncmp (p, stop_reason_names[tracepoint_passcount], 3290 p1 - p) == 0) 3291 { 3292 p = unpack_varlen_hex (++p1, &val); 3293 ts->stop_reason = tracepoint_passcount; 3294 ts->stopping_tracepoint = val; 3295 } 3296 else if (strncmp (p, stop_reason_names[trace_stop_command], p1 - p) == 0) 3297 { 3298 p2 = strchr (++p1, ':'); 3299 if (!p2 || p2 > p3) 3300 { 3301 /*older style*/ 3302 p2 = p1; 3303 } 3304 else if (p2 != p1) 3305 { 3306 ts->stop_desc = (char *) xmalloc (strlen (line)); 3307 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2); 3308 ts->stop_desc[end] = '\0'; 3309 } 3310 else 3311 ts->stop_desc = xstrdup (""); 3312 3313 p = unpack_varlen_hex (++p2, &val); 3314 ts->stop_reason = trace_stop_command; 3315 } 3316 else if (strncmp (p, stop_reason_names[trace_disconnected], p1 - p) == 0) 3317 { 3318 p = unpack_varlen_hex (++p1, &val); 3319 ts->stop_reason = trace_disconnected; 3320 } 3321 else if (strncmp (p, stop_reason_names[tracepoint_error], p1 - p) == 0) 3322 { 3323 p2 = strchr (++p1, ':'); 3324 if (p2 != p1) 3325 { 3326 ts->stop_desc = (char *) xmalloc ((p2 - p1) / 2 + 1); 3327 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2); 3328 ts->stop_desc[end] = '\0'; 3329 } 3330 else 3331 ts->stop_desc = xstrdup (""); 3332 3333 p = unpack_varlen_hex (++p2, &val); 3334 ts->stopping_tracepoint = val; 3335 ts->stop_reason = tracepoint_error; 3336 } 3337 else if (strncmp (p, "tframes", p1 - p) == 0) 3338 { 3339 p = unpack_varlen_hex (++p1, &val); 3340 ts->traceframe_count = val; 3341 } 3342 else if (strncmp (p, "tcreated", p1 - p) == 0) 3343 { 3344 p = unpack_varlen_hex (++p1, &val); 3345 ts->traceframes_created = val; 3346 } 3347 else if (strncmp (p, "tfree", p1 - p) == 0) 3348 { 3349 p = unpack_varlen_hex (++p1, &val); 3350 ts->buffer_free = val; 3351 } 3352 else if (strncmp (p, "tsize", p1 - p) == 0) 3353 { 3354 p = unpack_varlen_hex (++p1, &val); 3355 ts->buffer_size = val; 3356 } 3357 else if (strncmp (p, "disconn", p1 - p) == 0) 3358 { 3359 p = unpack_varlen_hex (++p1, &val); 3360 ts->disconnected_tracing = val; 3361 } 3362 else if (strncmp (p, "circular", p1 - p) == 0) 3363 { 3364 p = unpack_varlen_hex (++p1, &val); 3365 ts->circular_buffer = val; 3366 } 3367 else if (strncmp (p, "starttime", p1 - p) == 0) 3368 { 3369 p = unpack_varlen_hex (++p1, &val); 3370 ts->start_time = val; 3371 } 3372 else if (strncmp (p, "stoptime", p1 - p) == 0) 3373 { 3374 p = unpack_varlen_hex (++p1, &val); 3375 ts->stop_time = val; 3376 } 3377 else if (strncmp (p, "username", p1 - p) == 0) 3378 { 3379 ++p1; 3380 ts->user_name = (char *) xmalloc (strlen (p) / 2); 3381 end = hex2bin (p1, (gdb_byte *) ts->user_name, (p3 - p1) / 2); 3382 ts->user_name[end] = '\0'; 3383 p = p3; 3384 } 3385 else if (strncmp (p, "notes", p1 - p) == 0) 3386 { 3387 ++p1; 3388 ts->notes = (char *) xmalloc (strlen (p) / 2); 3389 end = hex2bin (p1, (gdb_byte *) ts->notes, (p3 - p1) / 2); 3390 ts->notes[end] = '\0'; 3391 p = p3; 3392 } 3393 else 3394 { 3395 /* Silently skip unknown optional info. */ 3396 p_temp = strchr (p1 + 1, ';'); 3397 if (p_temp) 3398 p = p_temp; 3399 else 3400 /* Must be at the end. */ 3401 break; 3402 } 3403 } 3404 } 3405 3406 void 3407 parse_tracepoint_status (const char *p, struct breakpoint *bp, 3408 struct uploaded_tp *utp) 3409 { 3410 ULONGEST uval; 3411 struct tracepoint *tp = (struct tracepoint *) bp; 3412 3413 p = unpack_varlen_hex (p, &uval); 3414 if (tp) 3415 tp->hit_count += uval; 3416 else 3417 utp->hit_count += uval; 3418 p = unpack_varlen_hex (p + 1, &uval); 3419 if (tp) 3420 tp->traceframe_usage += uval; 3421 else 3422 utp->traceframe_usage += uval; 3423 /* Ignore any extra, allowing for future extensions. */ 3424 } 3425 3426 /* Given a line of text defining a part of a tracepoint, parse it into 3427 an "uploaded tracepoint". */ 3428 3429 void 3430 parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp) 3431 { 3432 const char *p; 3433 char piece; 3434 ULONGEST num, addr, step, pass, orig_size, xlen, start; 3435 int enabled, end; 3436 enum bptype type; 3437 const char *srctype; 3438 char *buf; 3439 struct uploaded_tp *utp = NULL; 3440 3441 p = line; 3442 /* Both tracepoint and action definitions start with the same number 3443 and address sequence. */ 3444 piece = *p++; 3445 p = unpack_varlen_hex (p, &num); 3446 p++; /* skip a colon */ 3447 p = unpack_varlen_hex (p, &addr); 3448 p++; /* skip a colon */ 3449 if (piece == 'T') 3450 { 3451 gdb::unique_xmalloc_ptr<char[]> cond; 3452 3453 enabled = (*p++ == 'E'); 3454 p++; /* skip a colon */ 3455 p = unpack_varlen_hex (p, &step); 3456 p++; /* skip a colon */ 3457 p = unpack_varlen_hex (p, &pass); 3458 type = bp_tracepoint; 3459 /* Thumb through optional fields. */ 3460 while (*p == ':') 3461 { 3462 p++; /* skip a colon */ 3463 if (*p == 'F') 3464 { 3465 type = bp_fast_tracepoint; 3466 p++; 3467 p = unpack_varlen_hex (p, &orig_size); 3468 } 3469 else if (*p == 'S') 3470 { 3471 type = bp_static_tracepoint; 3472 p++; 3473 } 3474 else if (*p == 'X') 3475 { 3476 p++; 3477 p = unpack_varlen_hex (p, &xlen); 3478 p++; /* skip a comma */ 3479 cond.reset ((char *) xmalloc (2 * xlen + 1)); 3480 strncpy (&cond[0], p, 2 * xlen); 3481 cond[2 * xlen] = '\0'; 3482 p += 2 * xlen; 3483 } 3484 else 3485 warning (_("Unrecognized char '%c' in tracepoint " 3486 "definition, skipping rest"), *p); 3487 } 3488 utp = get_uploaded_tp (num, addr, utpp); 3489 utp->type = type; 3490 utp->enabled = enabled; 3491 utp->step = step; 3492 utp->pass = pass; 3493 utp->cond = std::move (cond); 3494 } 3495 else if (piece == 'A') 3496 { 3497 utp = get_uploaded_tp (num, addr, utpp); 3498 utp->actions.emplace_back (xstrdup (p)); 3499 } 3500 else if (piece == 'S') 3501 { 3502 utp = get_uploaded_tp (num, addr, utpp); 3503 utp->step_actions.emplace_back (xstrdup (p)); 3504 } 3505 else if (piece == 'Z') 3506 { 3507 /* Parse a chunk of source form definition. */ 3508 utp = get_uploaded_tp (num, addr, utpp); 3509 srctype = p; 3510 p = strchr (p, ':'); 3511 p++; /* skip a colon */ 3512 p = unpack_varlen_hex (p, &start); 3513 p++; /* skip a colon */ 3514 p = unpack_varlen_hex (p, &xlen); 3515 p++; /* skip a colon */ 3516 3517 buf = (char *) alloca (strlen (line)); 3518 3519 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2); 3520 buf[end] = '\0'; 3521 3522 if (startswith (srctype, "at:")) 3523 utp->at_string.reset (xstrdup (buf)); 3524 else if (startswith (srctype, "cond:")) 3525 utp->cond_string.reset (xstrdup (buf)); 3526 else if (startswith (srctype, "cmd:")) 3527 utp->cmd_strings.emplace_back (xstrdup (buf)); 3528 } 3529 else if (piece == 'V') 3530 { 3531 utp = get_uploaded_tp (num, addr, utpp); 3532 3533 parse_tracepoint_status (p, NULL, utp); 3534 } 3535 else 3536 { 3537 /* Don't error out, the target might be sending us optional 3538 info that we don't care about. */ 3539 warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece); 3540 } 3541 } 3542 3543 /* Convert a textual description of a trace state variable into an 3544 uploaded object. */ 3545 3546 void 3547 parse_tsv_definition (const char *line, struct uploaded_tsv **utsvp) 3548 { 3549 const char *p; 3550 char *buf; 3551 ULONGEST num, initval, builtin; 3552 int end; 3553 struct uploaded_tsv *utsv = NULL; 3554 3555 buf = (char *) alloca (strlen (line)); 3556 3557 p = line; 3558 p = unpack_varlen_hex (p, &num); 3559 p++; /* skip a colon */ 3560 p = unpack_varlen_hex (p, &initval); 3561 p++; /* skip a colon */ 3562 p = unpack_varlen_hex (p, &builtin); 3563 p++; /* skip a colon */ 3564 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2); 3565 buf[end] = '\0'; 3566 3567 utsv = get_uploaded_tsv (num, utsvp); 3568 utsv->initial_value = initval; 3569 utsv->builtin = builtin; 3570 utsv->name = xstrdup (buf); 3571 } 3572 3573 /* Given a line of text defining a static tracepoint marker, parse it 3574 into a "static tracepoint marker" object. Throws an error is 3575 parsing fails. If PP is non-null, it points to one past the end of 3576 the parsed marker definition. */ 3577 3578 void 3579 parse_static_tracepoint_marker_definition (const char *line, const char **pp, 3580 static_tracepoint_marker *marker) 3581 { 3582 const char *p, *endp; 3583 ULONGEST addr; 3584 3585 p = line; 3586 p = unpack_varlen_hex (p, &addr); 3587 p++; /* skip a colon */ 3588 3589 marker->gdbarch = target_gdbarch (); 3590 marker->address = (CORE_ADDR) addr; 3591 3592 endp = strchr (p, ':'); 3593 if (endp == NULL) 3594 error (_("bad marker definition: %s"), line); 3595 3596 marker->str_id = hex2str (p, (endp - p) / 2); 3597 3598 p = endp; 3599 p++; /* skip a colon */ 3600 3601 /* This definition may be followed by another one, separated by a comma. */ 3602 int hex_len; 3603 endp = strchr (p, ','); 3604 if (endp != nullptr) 3605 hex_len = endp - p; 3606 else 3607 hex_len = strlen (p); 3608 3609 marker->extra = hex2str (p, hex_len / 2); 3610 3611 if (pp != nullptr) 3612 *pp = p + hex_len; 3613 } 3614 3615 /* Print MARKER to gdb_stdout. */ 3616 3617 static void 3618 print_one_static_tracepoint_marker (int count, 3619 const static_tracepoint_marker &marker) 3620 { 3621 struct symbol *sym; 3622 3623 struct ui_out *uiout = current_uiout; 3624 3625 symtab_and_line sal; 3626 sal.pc = marker.address; 3627 3628 std::vector<breakpoint *> tracepoints 3629 = static_tracepoints_here (marker.address); 3630 3631 ui_out_emit_tuple tuple_emitter (uiout, "marker"); 3632 3633 /* A counter field to help readability. This is not a stable 3634 identifier! */ 3635 uiout->field_signed ("count", count); 3636 3637 uiout->field_string ("marker-id", marker.str_id); 3638 3639 uiout->field_fmt ("enabled", "%c", 3640 !tracepoints.empty () ? 'y' : 'n'); 3641 uiout->spaces (2); 3642 3643 int wrap_indent = 35; 3644 if (gdbarch_addr_bit (marker.gdbarch) <= 32) 3645 wrap_indent += 11; 3646 else 3647 wrap_indent += 19; 3648 3649 const char *extra_field_indent = " "; 3650 3651 uiout->field_core_addr ("addr", marker.gdbarch, marker.address); 3652 3653 sal = find_pc_line (marker.address, 0); 3654 sym = find_pc_sect_function (marker.address, NULL); 3655 if (sym) 3656 { 3657 uiout->text ("in "); 3658 uiout->field_string ("func", sym->print_name (), 3659 function_name_style.style ()); 3660 uiout->wrap_hint (wrap_indent); 3661 uiout->text (" at "); 3662 } 3663 else 3664 uiout->field_skip ("func"); 3665 3666 if (sal.symtab != NULL) 3667 { 3668 uiout->field_string ("file", 3669 symtab_to_filename_for_display (sal.symtab), 3670 file_name_style.style ()); 3671 uiout->text (":"); 3672 3673 if (uiout->is_mi_like_p ()) 3674 { 3675 const char *fullname = symtab_to_fullname (sal.symtab); 3676 3677 uiout->field_string ("fullname", fullname); 3678 } 3679 else 3680 uiout->field_skip ("fullname"); 3681 3682 uiout->field_signed ("line", sal.line); 3683 } 3684 else 3685 { 3686 uiout->field_skip ("fullname"); 3687 uiout->field_skip ("line"); 3688 } 3689 3690 uiout->text ("\n"); 3691 uiout->text (extra_field_indent); 3692 uiout->text (_("Data: \"")); 3693 uiout->field_string ("extra-data", marker.extra); 3694 uiout->text ("\"\n"); 3695 3696 if (!tracepoints.empty ()) 3697 { 3698 int ix; 3699 3700 { 3701 ui_out_emit_tuple inner_tuple_emitter (uiout, "tracepoints-at"); 3702 3703 uiout->text (extra_field_indent); 3704 uiout->text (_("Probed by static tracepoints: ")); 3705 for (ix = 0; ix < tracepoints.size (); ix++) 3706 { 3707 if (ix > 0) 3708 uiout->text (", "); 3709 uiout->text ("#"); 3710 uiout->field_signed ("tracepoint-id", tracepoints[ix]->number); 3711 } 3712 } 3713 3714 if (uiout->is_mi_like_p ()) 3715 uiout->field_signed ("number-of-tracepoints", tracepoints.size ()); 3716 else 3717 uiout->text ("\n"); 3718 } 3719 } 3720 3721 static void 3722 info_static_tracepoint_markers_command (const char *arg, int from_tty) 3723 { 3724 struct ui_out *uiout = current_uiout; 3725 std::vector<static_tracepoint_marker> markers 3726 = target_static_tracepoint_markers_by_strid (NULL); 3727 3728 /* We don't have to check target_can_use_agent and agent's capability on 3729 static tracepoint here, in order to be compatible with older GDBserver. 3730 We don't check USE_AGENT is true or not, because static tracepoints 3731 don't work without in-process agent, so we don't bother users to type 3732 `set agent on' when to use static tracepoint. */ 3733 3734 ui_out_emit_table table_emitter (uiout, 5, -1, 3735 "StaticTracepointMarkersTable"); 3736 3737 uiout->table_header (7, ui_left, "counter", "Cnt"); 3738 3739 uiout->table_header (40, ui_left, "marker-id", "ID"); 3740 3741 uiout->table_header (3, ui_left, "enabled", "Enb"); 3742 if (gdbarch_addr_bit (target_gdbarch ()) <= 32) 3743 uiout->table_header (10, ui_left, "addr", "Address"); 3744 else 3745 uiout->table_header (18, ui_left, "addr", "Address"); 3746 uiout->table_header (40, ui_noalign, "what", "What"); 3747 3748 uiout->table_body (); 3749 3750 for (int i = 0; i < markers.size (); i++) 3751 print_one_static_tracepoint_marker (i + 1, markers[i]); 3752 } 3753 3754 /* The $_sdata convenience variable is a bit special. We don't know 3755 for sure type of the value until we actually have a chance to fetch 3756 the data --- the size of the object depends on what has been 3757 collected. We solve this by making $_sdata be an internalvar that 3758 creates a new value on access. */ 3759 3760 /* Return a new value with the correct type for the sdata object of 3761 the current trace frame. Return a void value if there's no object 3762 available. */ 3763 3764 static struct value * 3765 sdata_make_value (struct gdbarch *gdbarch, struct internalvar *var, 3766 void *ignore) 3767 { 3768 /* We need to read the whole object before we know its size. */ 3769 gdb::optional<gdb::byte_vector> buf 3770 = target_read_alloc (current_inferior ()->top_target (), 3771 TARGET_OBJECT_STATIC_TRACE_DATA, 3772 NULL); 3773 if (buf) 3774 { 3775 struct value *v; 3776 struct type *type; 3777 3778 type = init_vector_type (builtin_type (gdbarch)->builtin_true_char, 3779 buf->size ()); 3780 v = allocate_value (type); 3781 memcpy (value_contents_raw (v).data (), buf->data (), buf->size ()); 3782 return v; 3783 } 3784 else 3785 return allocate_value (builtin_type (gdbarch)->builtin_void); 3786 } 3787 3788 #if !defined(HAVE_LIBEXPAT) 3789 3790 struct std::unique_ptr<traceframe_info> 3791 parse_traceframe_info (const char *tframe_info) 3792 { 3793 static int have_warned; 3794 3795 if (!have_warned) 3796 { 3797 have_warned = 1; 3798 warning (_("Can not parse XML trace frame info; XML support " 3799 "was disabled at compile time")); 3800 } 3801 3802 return NULL; 3803 } 3804 3805 #else /* HAVE_LIBEXPAT */ 3806 3807 #include "xml-support.h" 3808 3809 /* Handle the start of a <memory> element. */ 3810 3811 static void 3812 traceframe_info_start_memory (struct gdb_xml_parser *parser, 3813 const struct gdb_xml_element *element, 3814 void *user_data, 3815 std::vector<gdb_xml_value> &attributes) 3816 { 3817 struct traceframe_info *info = (struct traceframe_info *) user_data; 3818 ULONGEST *start_p, *length_p; 3819 3820 start_p 3821 = (ULONGEST *) xml_find_attribute (attributes, "start")->value.get (); 3822 length_p 3823 = (ULONGEST *) xml_find_attribute (attributes, "length")->value.get (); 3824 3825 info->memory.emplace_back (*start_p, *length_p); 3826 } 3827 3828 /* Handle the start of a <tvar> element. */ 3829 3830 static void 3831 traceframe_info_start_tvar (struct gdb_xml_parser *parser, 3832 const struct gdb_xml_element *element, 3833 void *user_data, 3834 std::vector<gdb_xml_value> &attributes) 3835 { 3836 struct traceframe_info *info = (struct traceframe_info *) user_data; 3837 const char *id_attrib 3838 = (const char *) xml_find_attribute (attributes, "id")->value.get (); 3839 int id = gdb_xml_parse_ulongest (parser, id_attrib); 3840 3841 info->tvars.push_back (id); 3842 } 3843 3844 /* The allowed elements and attributes for an XML memory map. */ 3845 3846 static const struct gdb_xml_attribute memory_attributes[] = { 3847 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, 3848 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, 3849 { NULL, GDB_XML_AF_NONE, NULL, NULL } 3850 }; 3851 3852 static const struct gdb_xml_attribute tvar_attributes[] = { 3853 { "id", GDB_XML_AF_NONE, NULL, NULL }, 3854 { NULL, GDB_XML_AF_NONE, NULL, NULL } 3855 }; 3856 3857 static const struct gdb_xml_element traceframe_info_children[] = { 3858 { "memory", memory_attributes, NULL, 3859 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, 3860 traceframe_info_start_memory, NULL }, 3861 { "tvar", tvar_attributes, NULL, 3862 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, 3863 traceframe_info_start_tvar, NULL }, 3864 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } 3865 }; 3866 3867 static const struct gdb_xml_element traceframe_info_elements[] = { 3868 { "traceframe-info", NULL, traceframe_info_children, GDB_XML_EF_NONE, 3869 NULL, NULL }, 3870 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } 3871 }; 3872 3873 /* Parse a traceframe-info XML document. */ 3874 3875 traceframe_info_up 3876 parse_traceframe_info (const char *tframe_info) 3877 { 3878 traceframe_info_up result (new traceframe_info); 3879 3880 if (gdb_xml_parse_quick (_("trace frame info"), 3881 "traceframe-info.dtd", traceframe_info_elements, 3882 tframe_info, result.get ()) == 0) 3883 return result; 3884 3885 return NULL; 3886 } 3887 3888 #endif /* HAVE_LIBEXPAT */ 3889 3890 /* Returns the traceframe_info object for the current traceframe. 3891 This is where we avoid re-fetching the object from the target if we 3892 already have it cached. */ 3893 3894 struct traceframe_info * 3895 get_traceframe_info (void) 3896 { 3897 if (current_traceframe_info == NULL) 3898 current_traceframe_info = target_traceframe_info (); 3899 3900 return current_traceframe_info.get (); 3901 } 3902 3903 /* If the target supports the query, return in RESULT the set of 3904 collected memory in the current traceframe, found within the LEN 3905 bytes range starting at MEMADDR. Returns true if the target 3906 supports the query, otherwise returns false, and RESULT is left 3907 undefined. */ 3908 3909 int 3910 traceframe_available_memory (std::vector<mem_range> *result, 3911 CORE_ADDR memaddr, ULONGEST len) 3912 { 3913 struct traceframe_info *info = get_traceframe_info (); 3914 3915 if (info != NULL) 3916 { 3917 result->clear (); 3918 3919 for (mem_range &r : info->memory) 3920 if (mem_ranges_overlap (r.start, r.length, memaddr, len)) 3921 { 3922 ULONGEST lo1, hi1, lo2, hi2; 3923 3924 lo1 = memaddr; 3925 hi1 = memaddr + len; 3926 3927 lo2 = r.start; 3928 hi2 = r.start + r.length; 3929 3930 CORE_ADDR start = std::max (lo1, lo2); 3931 int length = std::min (hi1, hi2) - start; 3932 3933 result->emplace_back (start, length); 3934 } 3935 3936 normalize_mem_ranges (result); 3937 return 1; 3938 } 3939 3940 return 0; 3941 } 3942 3943 /* Implementation of `sdata' variable. */ 3944 3945 static const struct internalvar_funcs sdata_funcs = 3946 { 3947 sdata_make_value, 3948 NULL 3949 }; 3950 3951 /* See tracepoint.h. */ 3952 cmd_list_element *while_stepping_cmd_element = nullptr; 3953 3954 /* module initialization */ 3955 void _initialize_tracepoint (); 3956 void 3957 _initialize_tracepoint () 3958 { 3959 struct cmd_list_element *c; 3960 3961 /* Explicitly create without lookup, since that tries to create a 3962 value with a void typed value, and when we get here, gdbarch 3963 isn't initialized yet. At this point, we're quite sure there 3964 isn't another convenience variable of the same name. */ 3965 create_internalvar_type_lazy ("_sdata", &sdata_funcs, NULL); 3966 3967 traceframe_number = -1; 3968 tracepoint_number = -1; 3969 3970 add_info ("scope", info_scope_command, 3971 _("List the variables local to a scope.")); 3972 3973 add_cmd ("tracepoints", class_trace, 3974 _("Tracing of program execution without stopping the program."), 3975 &cmdlist); 3976 3977 add_com ("tdump", class_trace, tdump_command, 3978 _("Print everything collected at the current tracepoint.")); 3979 3980 c = add_com ("tvariable", class_trace, trace_variable_command,_("\ 3981 Define a trace state variable.\n\ 3982 Argument is a $-prefixed name, optionally followed\n\ 3983 by '=' and an expression that sets the initial value\n\ 3984 at the start of tracing.")); 3985 set_cmd_completer (c, expression_completer); 3986 3987 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\ 3988 Delete one or more trace state variables.\n\ 3989 Arguments are the names of the variables to delete.\n\ 3990 If no arguments are supplied, delete all variables."), &deletelist); 3991 /* FIXME add a trace variable completer. */ 3992 3993 add_info ("tvariables", info_tvariables_command, _("\ 3994 Status of trace state variables and their values.")); 3995 3996 add_info ("static-tracepoint-markers", 3997 info_static_tracepoint_markers_command, _("\ 3998 List target static tracepoints markers.")); 3999 4000 add_prefix_cmd ("tfind", class_trace, tfind_command, _("\ 4001 Select a trace frame.\n\ 4002 No argument means forward by one frame; '-' means backward by one frame."), 4003 &tfindlist, 1, &cmdlist); 4004 4005 add_cmd ("outside", class_trace, tfind_outside_command, _("\ 4006 Select a trace frame whose PC is outside the given range (exclusive).\n\ 4007 Usage: tfind outside ADDR1, ADDR2"), 4008 &tfindlist); 4009 4010 add_cmd ("range", class_trace, tfind_range_command, _("\ 4011 Select a trace frame whose PC is in the given range (inclusive).\n\ 4012 Usage: tfind range ADDR1, ADDR2"), 4013 &tfindlist); 4014 4015 add_cmd ("line", class_trace, tfind_line_command, _("\ 4016 Select a trace frame by source line.\n\ 4017 Argument can be a line number (with optional source file),\n\ 4018 a function name, or '*' followed by an address.\n\ 4019 Default argument is 'the next source line that was traced'."), 4020 &tfindlist); 4021 4022 add_cmd ("tracepoint", class_trace, tfind_tracepoint_command, _("\ 4023 Select a trace frame by tracepoint number.\n\ 4024 Default is the tracepoint for the current trace frame."), 4025 &tfindlist); 4026 4027 add_cmd ("pc", class_trace, tfind_pc_command, _("\ 4028 Select a trace frame by PC.\n\ 4029 Default is the current PC, or the PC of the current trace frame."), 4030 &tfindlist); 4031 4032 cmd_list_element *tfind_end_cmd 4033 = add_cmd ("end", class_trace, tfind_end_command, _("\ 4034 De-select any trace frame and resume 'live' debugging."), &tfindlist); 4035 4036 add_alias_cmd ("none", tfind_end_cmd, class_trace, 0, &tfindlist); 4037 4038 add_cmd ("start", class_trace, tfind_start_command, 4039 _("Select the first trace frame in the trace buffer."), 4040 &tfindlist); 4041 4042 add_com ("tstatus", class_trace, tstatus_command, 4043 _("Display the status of the current trace data collection.")); 4044 4045 add_com ("tstop", class_trace, tstop_command, _("\ 4046 Stop trace data collection.\n\ 4047 Usage: tstop [NOTES]...\n\ 4048 Any arguments supplied are recorded with the trace as a stop reason and\n\ 4049 reported by tstatus (if the target supports trace notes).")); 4050 4051 add_com ("tstart", class_trace, tstart_command, _("\ 4052 Start trace data collection.\n\ 4053 Usage: tstart [NOTES]...\n\ 4054 Any arguments supplied are recorded with the trace as a note and\n\ 4055 reported by tstatus (if the target supports trace notes).")); 4056 4057 add_com ("end", class_trace, end_actions_pseudocommand, _("\ 4058 Ends a list of commands or actions.\n\ 4059 Several GDB commands allow you to enter a list of commands or actions.\n\ 4060 Entering \"end\" on a line by itself is the normal way to terminate\n\ 4061 such a list.\n\n\ 4062 Note: the \"end\" command cannot be used at the gdb prompt.")); 4063 4064 while_stepping_cmd_element = add_com ("while-stepping", class_trace, 4065 while_stepping_pseudocommand, _("\ 4066 Specify single-stepping behavior at a tracepoint.\n\ 4067 Argument is number of instructions to trace in single-step mode\n\ 4068 following the tracepoint. This command is normally followed by\n\ 4069 one or more \"collect\" commands, to specify what to collect\n\ 4070 while single-stepping.\n\n\ 4071 Note: this command can only be used in a tracepoint \"actions\" list.")); 4072 4073 add_com_alias ("ws", while_stepping_cmd_element, class_trace, 0); 4074 add_com_alias ("stepping", while_stepping_cmd_element, class_trace, 0); 4075 4076 add_com ("collect", class_trace, collect_pseudocommand, _("\ 4077 Specify one or more data items to be collected at a tracepoint.\n\ 4078 Accepts a comma-separated list of (one or more) expressions. GDB will\n\ 4079 collect all data (variables, registers) referenced by that expression.\n\ 4080 Also accepts the following special arguments:\n\ 4081 $regs -- all registers.\n\ 4082 $args -- all function arguments.\n\ 4083 $locals -- all variables local to the block/function scope.\n\ 4084 $_sdata -- static tracepoint data (ignored for non-static tracepoints).\n\ 4085 Note: this command can only be used in a tracepoint \"actions\" list.")); 4086 4087 add_com ("teval", class_trace, teval_pseudocommand, _("\ 4088 Specify one or more expressions to be evaluated at a tracepoint.\n\ 4089 Accepts a comma-separated list of (one or more) expressions.\n\ 4090 The result of each evaluation will be discarded.\n\ 4091 Note: this command can only be used in a tracepoint \"actions\" list.")); 4092 4093 add_com ("actions", class_trace, actions_command, _("\ 4094 Specify the actions to be taken at a tracepoint.\n\ 4095 Tracepoint actions may include collecting of specified data,\n\ 4096 single-stepping, or enabling/disabling other tracepoints,\n\ 4097 depending on target's capabilities.")); 4098 4099 add_setshow_string_cmd ("default-collect", class_trace, 4100 &default_collect, _("\ 4101 Set the list of expressions to collect by default."), _("\ 4102 Show the list of expressions to collect by default."), NULL, 4103 NULL, NULL, 4104 &setlist, &showlist); 4105 4106 add_setshow_boolean_cmd ("disconnected-tracing", no_class, 4107 &disconnected_tracing, _("\ 4108 Set whether tracing continues after GDB disconnects."), _("\ 4109 Show whether tracing continues after GDB disconnects."), _("\ 4110 Use this to continue a tracing run even if GDB disconnects\n\ 4111 or detaches from the target. You can reconnect later and look at\n\ 4112 trace data collected in the meantime."), 4113 set_disconnected_tracing, 4114 NULL, 4115 &setlist, 4116 &showlist); 4117 4118 add_setshow_boolean_cmd ("circular-trace-buffer", no_class, 4119 &circular_trace_buffer, _("\ 4120 Set target's use of circular trace buffer."), _("\ 4121 Show target's use of circular trace buffer."), _("\ 4122 Use this to make the trace buffer into a circular buffer,\n\ 4123 which will discard traceframes (oldest first) instead of filling\n\ 4124 up and stopping the trace run."), 4125 set_circular_trace_buffer, 4126 NULL, 4127 &setlist, 4128 &showlist); 4129 4130 add_setshow_zuinteger_unlimited_cmd ("trace-buffer-size", no_class, 4131 &trace_buffer_size, _("\ 4132 Set requested size of trace buffer."), _("\ 4133 Show requested size of trace buffer."), _("\ 4134 Use this to choose a size for the trace buffer. Some targets\n\ 4135 may have fixed or limited buffer sizes. Specifying \"unlimited\" or -1\n\ 4136 disables any attempt to set the buffer size and lets the target choose."), 4137 set_trace_buffer_size, NULL, 4138 &setlist, &showlist); 4139 4140 add_setshow_string_cmd ("trace-user", class_trace, 4141 &trace_user, _("\ 4142 Set the user name to use for current and future trace runs."), _("\ 4143 Show the user name to use for current and future trace runs."), NULL, 4144 set_trace_user, NULL, 4145 &setlist, &showlist); 4146 4147 add_setshow_string_cmd ("trace-notes", class_trace, 4148 &trace_notes, _("\ 4149 Set notes string to use for current and future trace runs."), _("\ 4150 Show the notes string to use for current and future trace runs."), NULL, 4151 set_trace_notes, NULL, 4152 &setlist, &showlist); 4153 4154 add_setshow_string_cmd ("trace-stop-notes", class_trace, 4155 &trace_stop_notes, _("\ 4156 Set notes string to use for future tstop commands."), _("\ 4157 Show the notes string to use for future tstop commands."), NULL, 4158 set_trace_stop_notes, NULL, 4159 &setlist, &showlist); 4160 } 4161