1 /* Tracepoint code for remote server for GDB. 2 Copyright (C) 2009-2020 Free Software Foundation, Inc. 3 4 This file is part of GDB. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 19 #include "server.h" 20 #include "tracepoint.h" 21 #include "gdbthread.h" 22 #include "gdbsupport/rsp-low.h" 23 24 #include <ctype.h> 25 #include <fcntl.h> 26 #include <unistd.h> 27 #include <chrono> 28 #include <inttypes.h> 29 #include "ax.h" 30 #include "tdesc.h" 31 32 #define IPA_SYM_STRUCT_NAME ipa_sym_addresses 33 #include "gdbsupport/agent.h" 34 35 #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */ 36 37 /* This file is built for both GDBserver, and the in-process 38 agent (IPA), a shared library that includes a tracing agent that is 39 loaded by the inferior to support fast tracepoints. Fast 40 tracepoints (or more accurately, jump based tracepoints) are 41 implemented by patching the tracepoint location with a jump into a 42 small trampoline function whose job is to save the register state, 43 call the in-process tracing agent, and then execute the original 44 instruction that was under the tracepoint jump (possibly adjusted, 45 if PC-relative, or some such). 46 47 The current synchronization design is pull based. That means, 48 GDBserver does most of the work, by peeking/poking at the inferior 49 agent's memory directly for downloading tracepoint and associated 50 objects, and for uploading trace frames. Whenever the IPA needs 51 something from GDBserver (trace buffer is full, tracing stopped for 52 some reason, etc.) the IPA calls a corresponding hook function 53 where GDBserver has placed a breakpoint. 54 55 Each of the agents has its own trace buffer. When browsing the 56 trace frames built from slow and fast tracepoints from GDB (tfind 57 mode), there's no guarantee the user is seeing the trace frames in 58 strict chronological creation order, although, GDBserver tries to 59 keep the order relatively reasonable, by syncing the trace buffers 60 at appropriate times. 61 62 */ 63 64 #ifdef IN_PROCESS_AGENT 65 66 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2); 67 68 static void 69 trace_vdebug (const char *fmt, ...) 70 { 71 char buf[1024]; 72 va_list ap; 73 74 va_start (ap, fmt); 75 vsprintf (buf, fmt, ap); 76 fprintf (stderr, PROG "/tracepoint: %s\n", buf); 77 va_end (ap); 78 } 79 80 #define trace_debug_1(level, fmt, args...) \ 81 do { \ 82 if (level <= debug_threads) \ 83 trace_vdebug ((fmt), ##args); \ 84 } while (0) 85 86 #else 87 88 #define trace_debug_1(level, fmt, args...) \ 89 do { \ 90 if (level <= debug_threads) \ 91 { \ 92 debug_printf ((fmt), ##args); \ 93 debug_printf ("\n"); \ 94 } \ 95 } while (0) 96 97 #endif 98 99 #define trace_debug(FMT, args...) \ 100 trace_debug_1 (1, FMT, ##args) 101 102 /* Prefix exported symbols, for good citizenship. All the symbols 103 that need exporting are defined in this module. Note that all 104 these symbols must be tagged with IP_AGENT_EXPORT_*. */ 105 #ifdef IN_PROCESS_AGENT 106 # define gdb_tp_heap_buffer IPA_SYM_EXPORTED_NAME (gdb_tp_heap_buffer) 107 # define gdb_jump_pad_buffer IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer) 108 # define gdb_jump_pad_buffer_end IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer_end) 109 # define gdb_trampoline_buffer IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer) 110 # define gdb_trampoline_buffer_end IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_end) 111 # define gdb_trampoline_buffer_error IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_error) 112 # define collecting IPA_SYM_EXPORTED_NAME (collecting) 113 # define gdb_collect_ptr IPA_SYM_EXPORTED_NAME (gdb_collect_ptr) 114 # define stop_tracing IPA_SYM_EXPORTED_NAME (stop_tracing) 115 # define flush_trace_buffer IPA_SYM_EXPORTED_NAME (flush_trace_buffer) 116 # define about_to_request_buffer_space IPA_SYM_EXPORTED_NAME (about_to_request_buffer_space) 117 # define trace_buffer_is_full IPA_SYM_EXPORTED_NAME (trace_buffer_is_full) 118 # define stopping_tracepoint IPA_SYM_EXPORTED_NAME (stopping_tracepoint) 119 # define expr_eval_result IPA_SYM_EXPORTED_NAME (expr_eval_result) 120 # define error_tracepoint IPA_SYM_EXPORTED_NAME (error_tracepoint) 121 # define tracepoints IPA_SYM_EXPORTED_NAME (tracepoints) 122 # define tracing IPA_SYM_EXPORTED_NAME (tracing) 123 # define trace_buffer_ctrl IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl) 124 # define trace_buffer_ctrl_curr IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl_curr) 125 # define trace_buffer_lo IPA_SYM_EXPORTED_NAME (trace_buffer_lo) 126 # define trace_buffer_hi IPA_SYM_EXPORTED_NAME (trace_buffer_hi) 127 # define traceframe_read_count IPA_SYM_EXPORTED_NAME (traceframe_read_count) 128 # define traceframe_write_count IPA_SYM_EXPORTED_NAME (traceframe_write_count) 129 # define traceframes_created IPA_SYM_EXPORTED_NAME (traceframes_created) 130 # define trace_state_variables IPA_SYM_EXPORTED_NAME (trace_state_variables) 131 # define get_raw_reg_ptr IPA_SYM_EXPORTED_NAME (get_raw_reg_ptr) 132 # define get_trace_state_variable_value_ptr \ 133 IPA_SYM_EXPORTED_NAME (get_trace_state_variable_value_ptr) 134 # define set_trace_state_variable_value_ptr \ 135 IPA_SYM_EXPORTED_NAME (set_trace_state_variable_value_ptr) 136 # define ust_loaded IPA_SYM_EXPORTED_NAME (ust_loaded) 137 # define helper_thread_id IPA_SYM_EXPORTED_NAME (helper_thread_id) 138 # define cmd_buf IPA_SYM_EXPORTED_NAME (cmd_buf) 139 # define ipa_tdesc_idx IPA_SYM_EXPORTED_NAME (ipa_tdesc_idx) 140 #endif 141 142 #ifndef IN_PROCESS_AGENT 143 144 /* Addresses of in-process agent's symbols GDBserver cares about. */ 145 146 struct ipa_sym_addresses 147 { 148 CORE_ADDR addr_gdb_tp_heap_buffer; 149 CORE_ADDR addr_gdb_jump_pad_buffer; 150 CORE_ADDR addr_gdb_jump_pad_buffer_end; 151 CORE_ADDR addr_gdb_trampoline_buffer; 152 CORE_ADDR addr_gdb_trampoline_buffer_end; 153 CORE_ADDR addr_gdb_trampoline_buffer_error; 154 CORE_ADDR addr_collecting; 155 CORE_ADDR addr_gdb_collect_ptr; 156 CORE_ADDR addr_stop_tracing; 157 CORE_ADDR addr_flush_trace_buffer; 158 CORE_ADDR addr_about_to_request_buffer_space; 159 CORE_ADDR addr_trace_buffer_is_full; 160 CORE_ADDR addr_stopping_tracepoint; 161 CORE_ADDR addr_expr_eval_result; 162 CORE_ADDR addr_error_tracepoint; 163 CORE_ADDR addr_tracepoints; 164 CORE_ADDR addr_tracing; 165 CORE_ADDR addr_trace_buffer_ctrl; 166 CORE_ADDR addr_trace_buffer_ctrl_curr; 167 CORE_ADDR addr_trace_buffer_lo; 168 CORE_ADDR addr_trace_buffer_hi; 169 CORE_ADDR addr_traceframe_read_count; 170 CORE_ADDR addr_traceframe_write_count; 171 CORE_ADDR addr_traceframes_created; 172 CORE_ADDR addr_trace_state_variables; 173 CORE_ADDR addr_get_raw_reg_ptr; 174 CORE_ADDR addr_get_trace_state_variable_value_ptr; 175 CORE_ADDR addr_set_trace_state_variable_value_ptr; 176 CORE_ADDR addr_ust_loaded; 177 CORE_ADDR addr_ipa_tdesc_idx; 178 }; 179 180 static struct 181 { 182 const char *name; 183 int offset; 184 } symbol_list[] = { 185 IPA_SYM(gdb_tp_heap_buffer), 186 IPA_SYM(gdb_jump_pad_buffer), 187 IPA_SYM(gdb_jump_pad_buffer_end), 188 IPA_SYM(gdb_trampoline_buffer), 189 IPA_SYM(gdb_trampoline_buffer_end), 190 IPA_SYM(gdb_trampoline_buffer_error), 191 IPA_SYM(collecting), 192 IPA_SYM(gdb_collect_ptr), 193 IPA_SYM(stop_tracing), 194 IPA_SYM(flush_trace_buffer), 195 IPA_SYM(about_to_request_buffer_space), 196 IPA_SYM(trace_buffer_is_full), 197 IPA_SYM(stopping_tracepoint), 198 IPA_SYM(expr_eval_result), 199 IPA_SYM(error_tracepoint), 200 IPA_SYM(tracepoints), 201 IPA_SYM(tracing), 202 IPA_SYM(trace_buffer_ctrl), 203 IPA_SYM(trace_buffer_ctrl_curr), 204 IPA_SYM(trace_buffer_lo), 205 IPA_SYM(trace_buffer_hi), 206 IPA_SYM(traceframe_read_count), 207 IPA_SYM(traceframe_write_count), 208 IPA_SYM(traceframes_created), 209 IPA_SYM(trace_state_variables), 210 IPA_SYM(get_raw_reg_ptr), 211 IPA_SYM(get_trace_state_variable_value_ptr), 212 IPA_SYM(set_trace_state_variable_value_ptr), 213 IPA_SYM(ust_loaded), 214 IPA_SYM(ipa_tdesc_idx), 215 }; 216 217 static struct ipa_sym_addresses ipa_sym_addrs; 218 219 static int read_inferior_integer (CORE_ADDR symaddr, int *val); 220 221 /* Returns true if both the in-process agent library and the static 222 tracepoints libraries are loaded in the inferior, and agent has 223 capability on static tracepoints. */ 224 225 static int 226 in_process_agent_supports_ust (void) 227 { 228 int loaded = 0; 229 230 if (!agent_loaded_p ()) 231 { 232 warning ("In-process agent not loaded"); 233 return 0; 234 } 235 236 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE)) 237 { 238 /* Agent understands static tracepoint, then check whether UST is in 239 fact loaded in the inferior. */ 240 if (read_inferior_integer (ipa_sym_addrs.addr_ust_loaded, &loaded)) 241 { 242 warning ("Error reading ust_loaded in lib"); 243 return 0; 244 } 245 246 return loaded; 247 } 248 else 249 return 0; 250 } 251 252 static void 253 write_e_ipa_not_loaded (char *buffer) 254 { 255 sprintf (buffer, 256 "E.In-process agent library not loaded in process. " 257 "Fast and static tracepoints unavailable."); 258 } 259 260 /* Write an error to BUFFER indicating that UST isn't loaded in the 261 inferior. */ 262 263 static void 264 write_e_ust_not_loaded (char *buffer) 265 { 266 #ifdef HAVE_UST 267 sprintf (buffer, 268 "E.UST library not loaded in process. " 269 "Static tracepoints unavailable."); 270 #else 271 sprintf (buffer, "E.GDBserver was built without static tracepoints support"); 272 #endif 273 } 274 275 /* If the in-process agent library isn't loaded in the inferior, write 276 an error to BUFFER, and return 1. Otherwise, return 0. */ 277 278 static int 279 maybe_write_ipa_not_loaded (char *buffer) 280 { 281 if (!agent_loaded_p ()) 282 { 283 write_e_ipa_not_loaded (buffer); 284 return 1; 285 } 286 return 0; 287 } 288 289 /* If the in-process agent library and the ust (static tracepoints) 290 library aren't loaded in the inferior, write an error to BUFFER, 291 and return 1. Otherwise, return 0. */ 292 293 static int 294 maybe_write_ipa_ust_not_loaded (char *buffer) 295 { 296 if (!agent_loaded_p ()) 297 { 298 write_e_ipa_not_loaded (buffer); 299 return 1; 300 } 301 else if (!in_process_agent_supports_ust ()) 302 { 303 write_e_ust_not_loaded (buffer); 304 return 1; 305 } 306 return 0; 307 } 308 309 /* Cache all future symbols that the tracepoints module might request. 310 We can not request symbols at arbitrary states in the remote 311 protocol, only when the client tells us that new symbols are 312 available. So when we load the in-process library, make sure to 313 check the entire list. */ 314 315 void 316 tracepoint_look_up_symbols (void) 317 { 318 int i; 319 320 if (agent_loaded_p ()) 321 return; 322 323 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++) 324 { 325 CORE_ADDR *addrp = 326 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset); 327 328 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0) 329 { 330 if (debug_threads) 331 debug_printf ("symbol `%s' not found\n", symbol_list[i].name); 332 return; 333 } 334 } 335 336 agent_look_up_symbols (NULL); 337 } 338 339 #endif 340 341 /* GDBserver places a breakpoint on the IPA's version (which is a nop) 342 of the "stop_tracing" function. When this breakpoint is hit, 343 tracing stopped in the IPA for some reason. E.g., due to 344 tracepoint reaching the pass count, hitting conditional expression 345 evaluation error, etc. 346 347 The IPA's trace buffer is never in circular tracing mode: instead, 348 GDBserver's is, and whenever the in-process buffer fills, it calls 349 "flush_trace_buffer", which triggers an internal breakpoint. 350 GDBserver reacts to this breakpoint by pulling the meanwhile 351 collected data. Old frames discarding is always handled on the 352 GDBserver side. */ 353 354 #ifdef IN_PROCESS_AGENT 355 int 356 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) 357 { 358 memcpy (myaddr, (void *) (uintptr_t) memaddr, len); 359 return 0; 360 } 361 362 /* Call this in the functions where GDBserver places a breakpoint, so 363 that the compiler doesn't try to be clever and skip calling the 364 function at all. This is necessary, even if we tell the compiler 365 to not inline said functions. */ 366 367 #if defined(__GNUC__) 368 # define UNKNOWN_SIDE_EFFECTS() asm ("") 369 #else 370 # define UNKNOWN_SIDE_EFFECTS() do {} while (0) 371 #endif 372 373 /* This is needed for -Wmissing-declarations. */ 374 IP_AGENT_EXPORT_FUNC void stop_tracing (void); 375 376 IP_AGENT_EXPORT_FUNC void 377 stop_tracing (void) 378 { 379 /* GDBserver places breakpoint here. */ 380 UNKNOWN_SIDE_EFFECTS(); 381 } 382 383 /* This is needed for -Wmissing-declarations. */ 384 IP_AGENT_EXPORT_FUNC void flush_trace_buffer (void); 385 386 IP_AGENT_EXPORT_FUNC void 387 flush_trace_buffer (void) 388 { 389 /* GDBserver places breakpoint here. */ 390 UNKNOWN_SIDE_EFFECTS(); 391 } 392 393 #endif 394 395 #ifndef IN_PROCESS_AGENT 396 static int 397 tracepoint_handler (CORE_ADDR address) 398 { 399 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit", 400 paddress (address)); 401 return 0; 402 } 403 404 /* Breakpoint at "stop_tracing" in the inferior lib. */ 405 struct breakpoint *stop_tracing_bkpt; 406 static int stop_tracing_handler (CORE_ADDR); 407 408 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */ 409 struct breakpoint *flush_trace_buffer_bkpt; 410 static int flush_trace_buffer_handler (CORE_ADDR); 411 412 static void download_trace_state_variables (void); 413 static void upload_fast_traceframes (void); 414 415 static int run_inferior_command (char *cmd, int len); 416 417 static int 418 read_inferior_integer (CORE_ADDR symaddr, int *val) 419 { 420 return read_inferior_memory (symaddr, (unsigned char *) val, 421 sizeof (*val)); 422 } 423 424 struct tracepoint; 425 static int tracepoint_send_agent (struct tracepoint *tpoint); 426 427 static int 428 read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val) 429 { 430 return read_inferior_memory (symaddr, (unsigned char *) val, 431 sizeof (*val)); 432 } 433 434 static int 435 read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val) 436 { 437 void *pval = (void *) (uintptr_t) val; 438 int ret; 439 440 ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval)); 441 *val = (uintptr_t) pval; 442 return ret; 443 } 444 445 static int 446 write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val) 447 { 448 void *pval = (void *) (uintptr_t) val; 449 return target_write_memory (symaddr, 450 (unsigned char *) &pval, sizeof (pval)); 451 } 452 453 static int 454 write_inferior_integer (CORE_ADDR symaddr, int val) 455 { 456 return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val)); 457 } 458 459 static int 460 write_inferior_int8 (CORE_ADDR symaddr, int8_t val) 461 { 462 return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val)); 463 } 464 465 static int 466 write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val) 467 { 468 return target_write_memory (symaddr, (unsigned char *) &val, sizeof (val)); 469 } 470 471 static CORE_ADDR target_malloc (ULONGEST size); 472 473 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \ 474 do { \ 475 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \ 476 BUF += sizeof ((OBJ)->FIELD); \ 477 } while (0) 478 479 #endif 480 481 /* Base action. Concrete actions inherit this. */ 482 483 struct tracepoint_action 484 { 485 char type; 486 }; 487 488 /* An 'M' (collect memory) action. */ 489 struct collect_memory_action 490 { 491 struct tracepoint_action base; 492 493 ULONGEST addr; 494 ULONGEST len; 495 int32_t basereg; 496 }; 497 498 /* An 'R' (collect registers) action. */ 499 500 struct collect_registers_action 501 { 502 struct tracepoint_action base; 503 }; 504 505 /* An 'X' (evaluate expression) action. */ 506 507 struct eval_expr_action 508 { 509 struct tracepoint_action base; 510 511 struct agent_expr *expr; 512 }; 513 514 /* An 'L' (collect static trace data) action. */ 515 struct collect_static_trace_data_action 516 { 517 struct tracepoint_action base; 518 }; 519 520 #ifndef IN_PROCESS_AGENT 521 static CORE_ADDR 522 m_tracepoint_action_download (const struct tracepoint_action *action) 523 { 524 CORE_ADDR ipa_action = target_malloc (sizeof (struct collect_memory_action)); 525 526 target_write_memory (ipa_action, (unsigned char *) action, 527 sizeof (struct collect_memory_action)); 528 529 return ipa_action; 530 } 531 static char * 532 m_tracepoint_action_send (char *buffer, const struct tracepoint_action *action) 533 { 534 struct collect_memory_action *maction 535 = (struct collect_memory_action *) action; 536 537 COPY_FIELD_TO_BUF (buffer, maction, addr); 538 COPY_FIELD_TO_BUF (buffer, maction, len); 539 COPY_FIELD_TO_BUF (buffer, maction, basereg); 540 541 return buffer; 542 } 543 544 static CORE_ADDR 545 r_tracepoint_action_download (const struct tracepoint_action *action) 546 { 547 CORE_ADDR ipa_action = target_malloc (sizeof (struct collect_registers_action)); 548 549 target_write_memory (ipa_action, (unsigned char *) action, 550 sizeof (struct collect_registers_action)); 551 552 return ipa_action; 553 } 554 555 static char * 556 r_tracepoint_action_send (char *buffer, const struct tracepoint_action *action) 557 { 558 return buffer; 559 } 560 561 static CORE_ADDR download_agent_expr (struct agent_expr *expr); 562 563 static CORE_ADDR 564 x_tracepoint_action_download (const struct tracepoint_action *action) 565 { 566 CORE_ADDR ipa_action = target_malloc (sizeof (struct eval_expr_action)); 567 CORE_ADDR expr; 568 569 target_write_memory (ipa_action, (unsigned char *) action, 570 sizeof (struct eval_expr_action)); 571 expr = download_agent_expr (((struct eval_expr_action *) action)->expr); 572 write_inferior_data_pointer (ipa_action 573 + offsetof (struct eval_expr_action, expr), 574 expr); 575 576 return ipa_action; 577 } 578 579 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL, 580 copy 0 to P. Return updated header of buffer. */ 581 582 static char * 583 agent_expr_send (char *p, const struct agent_expr *aexpr) 584 { 585 /* Copy the length of condition first, and then copy its 586 content. */ 587 if (aexpr == NULL) 588 { 589 memset (p, 0, 4); 590 p += 4; 591 } 592 else 593 { 594 memcpy (p, &aexpr->length, 4); 595 p +=4; 596 597 memcpy (p, aexpr->bytes, aexpr->length); 598 p += aexpr->length; 599 } 600 return p; 601 } 602 603 static char * 604 x_tracepoint_action_send ( char *buffer, const struct tracepoint_action *action) 605 { 606 struct eval_expr_action *eaction = (struct eval_expr_action *) action; 607 608 return agent_expr_send (buffer, eaction->expr); 609 } 610 611 static CORE_ADDR 612 l_tracepoint_action_download (const struct tracepoint_action *action) 613 { 614 CORE_ADDR ipa_action 615 = target_malloc (sizeof (struct collect_static_trace_data_action)); 616 617 target_write_memory (ipa_action, (unsigned char *) action, 618 sizeof (struct collect_static_trace_data_action)); 619 620 return ipa_action; 621 } 622 623 static char * 624 l_tracepoint_action_send (char *buffer, const struct tracepoint_action *action) 625 { 626 return buffer; 627 } 628 629 static char * 630 tracepoint_action_send (char *buffer, const struct tracepoint_action *action) 631 { 632 switch (action->type) 633 { 634 case 'M': 635 return m_tracepoint_action_send (buffer, action); 636 case 'R': 637 return r_tracepoint_action_send (buffer, action); 638 case 'X': 639 return x_tracepoint_action_send (buffer, action); 640 case 'L': 641 return l_tracepoint_action_send (buffer, action); 642 } 643 error ("Unknown trace action '%c'.", action->type); 644 } 645 646 static CORE_ADDR 647 tracepoint_action_download (const struct tracepoint_action *action) 648 { 649 switch (action->type) 650 { 651 case 'M': 652 return m_tracepoint_action_download (action); 653 case 'R': 654 return r_tracepoint_action_download (action); 655 case 'X': 656 return x_tracepoint_action_download (action); 657 case 'L': 658 return l_tracepoint_action_download (action); 659 } 660 error ("Unknown trace action '%c'.", action->type); 661 } 662 #endif 663 664 /* This structure describes a piece of the source-level definition of 665 the tracepoint. The contents are not interpreted by the target, 666 but preserved verbatim for uploading upon reconnection. */ 667 668 struct source_string 669 { 670 /* The type of string, such as "cond" for a conditional. */ 671 char *type; 672 673 /* The source-level string itself. For the sake of target 674 debugging, we store it in plaintext, even though it is always 675 transmitted in hex. */ 676 char *str; 677 678 /* Link to the next one in the list. We link them in the order 679 received, in case some make up an ordered list of commands or 680 some such. */ 681 struct source_string *next; 682 }; 683 684 enum tracepoint_type 685 { 686 /* Trap based tracepoint. */ 687 trap_tracepoint, 688 689 /* A fast tracepoint implemented with a jump instead of a trap. */ 690 fast_tracepoint, 691 692 /* A static tracepoint, implemented by a program call into a tracing 693 library. */ 694 static_tracepoint 695 }; 696 697 struct tracepoint_hit_ctx; 698 699 typedef enum eval_result_type (*condfn) (unsigned char *, 700 ULONGEST *); 701 702 /* The definition of a tracepoint. */ 703 704 /* Tracepoints may have multiple locations, each at a different 705 address. This can occur with optimizations, template 706 instantiation, etc. Since the locations may be in different 707 scopes, the conditions and actions may be different for each 708 location. Our target version of tracepoints is more like GDB's 709 notion of "breakpoint locations", but we have almost nothing that 710 is not per-location, so we bother having two kinds of objects. The 711 key consequence is that numbers are not unique, and that it takes 712 both number and address to identify a tracepoint uniquely. */ 713 714 struct tracepoint 715 { 716 /* The number of the tracepoint, as specified by GDB. Several 717 tracepoint objects here may share a number. */ 718 uint32_t number; 719 720 /* Address at which the tracepoint is supposed to trigger. Several 721 tracepoints may share an address. */ 722 CORE_ADDR address; 723 724 /* Tracepoint type. */ 725 enum tracepoint_type type; 726 727 /* True if the tracepoint is currently enabled. */ 728 int8_t enabled; 729 730 /* The number of single steps that will be performed after each 731 tracepoint hit. */ 732 uint64_t step_count; 733 734 /* The number of times the tracepoint may be hit before it will 735 terminate the entire tracing run. */ 736 uint64_t pass_count; 737 738 /* Pointer to the agent expression that is the tracepoint's 739 conditional, or NULL if the tracepoint is unconditional. */ 740 struct agent_expr *cond; 741 742 /* The list of actions to take when the tracepoint triggers. */ 743 uint32_t numactions; 744 struct tracepoint_action **actions; 745 746 /* Count of the times we've hit this tracepoint during the run. 747 Note that while-stepping steps are not counted as "hits". */ 748 uint64_t hit_count; 749 750 /* Cached sum of the sizes of traceframes created by this point. */ 751 uint64_t traceframe_usage; 752 753 CORE_ADDR compiled_cond; 754 755 /* Link to the next tracepoint in the list. */ 756 struct tracepoint *next; 757 758 #ifndef IN_PROCESS_AGENT 759 /* The list of actions to take when the tracepoint triggers, in 760 string/packet form. */ 761 char **actions_str; 762 763 /* The collection of strings that describe the tracepoint as it was 764 entered into GDB. These are not used by the target, but are 765 reported back to GDB upon reconnection. */ 766 struct source_string *source_strings; 767 768 /* The number of bytes displaced by fast tracepoints. It may subsume 769 multiple instructions, for multi-byte fast tracepoints. This 770 field is only valid for fast tracepoints. */ 771 uint32_t orig_size; 772 773 /* Only for fast tracepoints. */ 774 CORE_ADDR obj_addr_on_target; 775 776 /* Address range where the original instruction under a fast 777 tracepoint was relocated to. (_end is actually one byte past 778 the end). */ 779 CORE_ADDR adjusted_insn_addr; 780 CORE_ADDR adjusted_insn_addr_end; 781 782 /* The address range of the piece of the jump pad buffer that was 783 assigned to this fast tracepoint. (_end is actually one byte 784 past the end).*/ 785 CORE_ADDR jump_pad; 786 CORE_ADDR jump_pad_end; 787 788 /* The address range of the piece of the trampoline buffer that was 789 assigned to this fast tracepoint. (_end is actually one byte 790 past the end). */ 791 CORE_ADDR trampoline; 792 CORE_ADDR trampoline_end; 793 794 /* The list of actions to take while in a stepping loop. These 795 fields are only valid for patch-based tracepoints. */ 796 int num_step_actions; 797 struct tracepoint_action **step_actions; 798 /* Same, but in string/packet form. */ 799 char **step_actions_str; 800 801 /* Handle returned by the breakpoint or tracepoint module when we 802 inserted the trap or jump, or hooked into a static tracepoint. 803 NULL if we haven't inserted it yet. */ 804 void *handle; 805 #endif 806 807 }; 808 809 #ifndef IN_PROCESS_AGENT 810 811 /* Given `while-stepping', a thread may be collecting data for more 812 than one tracepoint simultaneously. On the other hand, the same 813 tracepoint with a while-stepping action may be hit by more than one 814 thread simultaneously (but not quite, each thread could be handling 815 a different step). Each thread holds a list of these objects, 816 representing the current step of each while-stepping action being 817 collected. */ 818 819 struct wstep_state 820 { 821 struct wstep_state *next; 822 823 /* The tracepoint number. */ 824 int tp_number; 825 /* The tracepoint's address. */ 826 CORE_ADDR tp_address; 827 828 /* The number of the current step in this 'while-stepping' 829 action. */ 830 long current_step; 831 }; 832 833 #endif 834 835 EXTERN_C_PUSH 836 837 /* The linked list of all tracepoints. Marked explicitly as used as 838 the in-process library doesn't use it for the fast tracepoints 839 support. */ 840 IP_AGENT_EXPORT_VAR struct tracepoint *tracepoints; 841 842 /* The first tracepoint to exceed its pass count. */ 843 844 IP_AGENT_EXPORT_VAR struct tracepoint *stopping_tracepoint; 845 846 /* True if the trace buffer is full or otherwise no longer usable. */ 847 848 IP_AGENT_EXPORT_VAR int trace_buffer_is_full; 849 850 /* The first error that occurred during expression evaluation. */ 851 852 /* Stored as an int to avoid the IPA ABI being dependent on whatever 853 the compiler decides to use for the enum's underlying type. Holds 854 enum eval_result_type values. */ 855 IP_AGENT_EXPORT_VAR int expr_eval_result = expr_eval_no_error; 856 857 EXTERN_C_POP 858 859 #ifndef IN_PROCESS_AGENT 860 861 /* Pointer to the last tracepoint in the list, new tracepoints are 862 linked in at the end. */ 863 864 static struct tracepoint *last_tracepoint; 865 866 static const char *eval_result_names[] = 867 { 868 "terror:in the attic", /* this should never be reported */ 869 "terror:empty expression", 870 "terror:empty stack", 871 "terror:stack overflow", 872 "terror:stack underflow", 873 "terror:unhandled opcode", 874 "terror:unrecognized opcode", 875 "terror:divide by zero" 876 }; 877 878 #endif 879 880 /* The tracepoint in which the error occurred. */ 881 882 EXTERN_C_PUSH 883 IP_AGENT_EXPORT_VAR struct tracepoint *error_tracepoint; 884 EXTERN_C_POP 885 886 struct trace_state_variable 887 { 888 /* This is the name of the variable as used in GDB. The target 889 doesn't use the name, but needs to have it for saving and 890 reconnection purposes. */ 891 char *name; 892 893 /* This number identifies the variable uniquely. Numbers may be 894 assigned either by the target (in the case of builtin variables), 895 or by GDB, and are presumed unique during the course of a trace 896 experiment. */ 897 int number; 898 899 /* The variable's initial value, a 64-bit signed integer always. */ 900 LONGEST initial_value; 901 902 /* The variable's value, a 64-bit signed integer always. */ 903 LONGEST value; 904 905 /* Pointer to a getter function, used to supply computed values. */ 906 LONGEST (*getter) (void); 907 908 /* Link to the next variable. */ 909 struct trace_state_variable *next; 910 }; 911 912 /* Linked list of all trace state variables. */ 913 914 #ifdef IN_PROCESS_AGENT 915 struct trace_state_variable *alloced_trace_state_variables; 916 #endif 917 918 IP_AGENT_EXPORT_VAR struct trace_state_variable *trace_state_variables; 919 920 /* The results of tracing go into a fixed-size space known as the 921 "trace buffer". Because usage follows a limited number of 922 patterns, we manage it ourselves rather than with malloc. Basic 923 rules are that we create only one trace frame at a time, each is 924 variable in size, they are never moved once created, and we only 925 discard if we are doing a circular buffer, and then only the oldest 926 ones. Each trace frame includes its own size, so we don't need to 927 link them together, and the trace frame number is relative to the 928 first one, so we don't need to record numbers. A trace frame also 929 records the number of the tracepoint that created it. The data 930 itself is a series of blocks, each introduced by a single character 931 and with a defined format. Each type of block has enough 932 type/length info to allow scanners to jump quickly from one block 933 to the next without reading each byte in the block. */ 934 935 /* Trace buffer management would be simple - advance a free pointer 936 from beginning to end, then stop - were it not for the circular 937 buffer option, which is a useful way to prevent a trace run from 938 stopping prematurely because the buffer filled up. In the circular 939 case, the location of the first trace frame (trace_buffer_start) 940 moves as old trace frames are discarded. Also, since we grow trace 941 frames incrementally as actions are performed, we wrap around to 942 the beginning of the trace buffer. This is per-block, so each 943 block within a trace frame remains contiguous. Things get messy 944 when the wrapped-around trace frame is the one being discarded; the 945 free space ends up in two parts at opposite ends of the buffer. */ 946 947 #ifndef ATTR_PACKED 948 # if defined(__GNUC__) 949 # define ATTR_PACKED __attribute__ ((packed)) 950 # else 951 # define ATTR_PACKED /* nothing */ 952 # endif 953 #endif 954 955 /* The data collected at a tracepoint hit. This object should be as 956 small as possible, since there may be a great many of them. We do 957 not need to keep a frame number, because they are all sequential 958 and there are no deletions; so the Nth frame in the buffer is 959 always frame number N. */ 960 961 struct traceframe 962 { 963 /* Number of the tracepoint that collected this traceframe. A value 964 of 0 indicates the current end of the trace buffer. We make this 965 a 16-bit field because it's never going to happen that GDB's 966 numbering of tracepoints reaches 32,000. */ 967 int tpnum : 16; 968 969 /* The size of the data in this trace frame. We limit this to 32 970 bits, even on a 64-bit target, because it's just implausible that 971 one is validly going to collect 4 gigabytes of data at a single 972 tracepoint hit. */ 973 unsigned int data_size : 32; 974 975 /* The base of the trace data, which is contiguous from this point. */ 976 unsigned char data[0]; 977 978 } ATTR_PACKED; 979 980 /* The size of the EOB marker, in bytes. A traceframe with zeroed 981 fields (and no data) marks the end of trace data. */ 982 #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data) 983 984 /* This flag is true if the trace buffer is circular, meaning that 985 when it fills, the oldest trace frames are discarded in order to 986 make room. */ 987 988 #ifndef IN_PROCESS_AGENT 989 static int circular_trace_buffer; 990 #endif 991 992 /* Size of the trace buffer. */ 993 994 static LONGEST trace_buffer_size; 995 996 EXTERN_C_PUSH 997 998 /* Pointer to the block of memory that traceframes all go into. */ 999 1000 IP_AGENT_EXPORT_VAR unsigned char *trace_buffer_lo; 1001 1002 /* Pointer to the end of the trace buffer, more precisely to the byte 1003 after the end of the buffer. */ 1004 1005 IP_AGENT_EXPORT_VAR unsigned char *trace_buffer_hi; 1006 1007 EXTERN_C_POP 1008 1009 /* Control structure holding the read/write/etc. pointers into the 1010 trace buffer. We need more than one of these to implement a 1011 transaction-like mechanism to guarantees that both GDBserver and the 1012 in-process agent can try to change the trace buffer 1013 simultaneously. */ 1014 1015 struct trace_buffer_control 1016 { 1017 /* Pointer to the first trace frame in the buffer. In the 1018 non-circular case, this is equal to trace_buffer_lo, otherwise it 1019 moves around in the buffer. */ 1020 unsigned char *start; 1021 1022 /* Pointer to the free part of the trace buffer. Note that we clear 1023 several bytes at and after this pointer, so that traceframe 1024 scans/searches terminate properly. */ 1025 unsigned char *free; 1026 1027 /* Pointer to the byte after the end of the free part. Note that 1028 this may be smaller than trace_buffer_free in the circular case, 1029 and means that the free part is in two pieces. Initially it is 1030 equal to trace_buffer_hi, then is generally equivalent to 1031 trace_buffer_start. */ 1032 unsigned char *end_free; 1033 1034 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then 1035 this is the point at which the trace data breaks, and resumes at 1036 trace_buffer_lo. */ 1037 unsigned char *wrap; 1038 }; 1039 1040 /* Same as above, to be used by GDBserver when updating the in-process 1041 agent. */ 1042 struct ipa_trace_buffer_control 1043 { 1044 uintptr_t start; 1045 uintptr_t free; 1046 uintptr_t end_free; 1047 uintptr_t wrap; 1048 }; 1049 1050 1051 /* We have possibly both GDBserver and an inferior thread accessing 1052 the same IPA trace buffer memory. The IPA is the producer (tries 1053 to put new frames in the buffer), while GDBserver occasionally 1054 consumes them, that is, flushes the IPA's buffer into its own 1055 buffer. Both sides need to update the trace buffer control 1056 pointers (current head, tail, etc.). We can't use a global lock to 1057 synchronize the accesses, as otherwise we could deadlock GDBserver 1058 (if the thread holding the lock stops for a signal, say). So 1059 instead of that, we use a transaction scheme where GDBserver writes 1060 always prevail over the IPAs writes, and, we have the IPA detect 1061 the commit failure/overwrite, and retry the whole attempt. This is 1062 mainly implemented by having a global token object that represents 1063 who wrote last to the buffer control structure. We need to freeze 1064 any inferior writing to the buffer while GDBserver touches memory, 1065 so that the inferior can correctly detect that GDBserver had been 1066 there, otherwise, it could mistakingly think its commit was 1067 successful; that's implemented by simply having GDBserver set a 1068 breakpoint the inferior hits if it is the critical region. 1069 1070 There are three cycling trace buffer control structure copies 1071 (buffer head, tail, etc.), with the token object including an index 1072 indicating which is current live copy. The IPA tentatively builds 1073 an updated copy in a non-current control structure, while GDBserver 1074 always clobbers the current version directly. The IPA then tries 1075 to atomically "commit" its version; if GDBserver clobbered the 1076 structure meanwhile, that will fail, and the IPA restarts the 1077 allocation process. 1078 1079 Listing the step in further detail, we have: 1080 1081 In-process agent (producer): 1082 1083 - passes by `about_to_request_buffer_space' breakpoint/lock 1084 1085 - reads current token, extracts current trace buffer control index, 1086 and starts tentatively updating the rightmost one (0->1, 1->2, 1087 2->0). Note that only one inferior thread is executing this code 1088 at any given time, due to an outer lock in the jump pads. 1089 1090 - updates counters, and tries to commit the token. 1091 1092 - passes by second `about_to_request_buffer_space' breakpoint/lock, 1093 leaving the sync region. 1094 1095 - checks if the update was effective. 1096 1097 - if trace buffer was found full, hits flush_trace_buffer 1098 breakpoint, and restarts later afterwards. 1099 1100 GDBserver (consumer): 1101 1102 - sets `about_to_request_buffer_space' breakpoint/lock. 1103 1104 - updates the token unconditionally, using the current buffer 1105 control index, since it knows that the IP agent always writes to 1106 the rightmost, and due to the breakpoint, at most one IP thread 1107 can try to update the trace buffer concurrently to GDBserver, so 1108 there will be no danger of trace buffer control index wrap making 1109 the IPA write to the same index as GDBserver. 1110 1111 - flushes the IP agent's trace buffer completely, and updates the 1112 current trace buffer control structure. GDBserver *always* wins. 1113 1114 - removes the `about_to_request_buffer_space' breakpoint. 1115 1116 The token is stored in the `trace_buffer_ctrl_curr' variable. 1117 Internally, it's bits are defined as: 1118 1119 |-------------+-----+-------------+--------+-------------+--------------| 1120 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 | 1121 |-------------+-----+-------------+--------+-------------+--------------| 1122 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) | 1123 |-------------+-----+-------------+--------+-------------+--------------| 1124 1125 GSB - GDBserver Stamp Bit 1126 PC - Previous Counter 1127 CC - Current Counter 1128 TBCI - Trace Buffer Control Index 1129 1130 1131 An IPA update of `trace_buffer_ctrl_curr' does: 1132 1133 - read CC from the current token, save as PC. 1134 - updates pointers 1135 - atomically tries to write PC+1,CC 1136 1137 A GDBserver update of `trace_buffer_ctrl_curr' does: 1138 1139 - reads PC and CC from the current token. 1140 - updates pointers 1141 - writes GSB,PC,CC 1142 */ 1143 1144 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved 1145 for the counters described below. The cleared bits are used to 1146 hold the index of the items of the `trace_buffer_ctrl' array that 1147 is "current". */ 1148 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0 1149 1150 /* `trace_buffer_ctrl_curr' contains two counters. The `previous' 1151 counter, and the `current' counter. */ 1152 1153 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000 1154 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00 1155 1156 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it 1157 always stamps this bit as set. */ 1158 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000 1159 1160 #ifdef IN_PROCESS_AGENT 1161 IP_AGENT_EXPORT_VAR struct trace_buffer_control trace_buffer_ctrl[3]; 1162 IP_AGENT_EXPORT_VAR unsigned int trace_buffer_ctrl_curr; 1163 1164 # define TRACE_BUFFER_CTRL_CURR \ 1165 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK) 1166 1167 #else 1168 1169 /* The GDBserver side agent only needs one instance of this object, as 1170 it doesn't need to sync with itself. Define it as array anyway so 1171 that the rest of the code base doesn't need to care for the 1172 difference. */ 1173 struct trace_buffer_control trace_buffer_ctrl[1]; 1174 # define TRACE_BUFFER_CTRL_CURR 0 1175 #endif 1176 1177 /* These are convenience macros used to access the current trace 1178 buffer control in effect. */ 1179 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start) 1180 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free) 1181 #define trace_buffer_end_free \ 1182 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free) 1183 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap) 1184 1185 1186 /* Macro that returns a pointer to the first traceframe in the buffer. */ 1187 1188 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start) 1189 1190 /* Macro that returns a pointer to the next traceframe in the buffer. 1191 If the computed location is beyond the wraparound point, subtract 1192 the offset of the wraparound. */ 1193 1194 #define NEXT_TRACEFRAME_1(TF) \ 1195 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size) 1196 1197 #define NEXT_TRACEFRAME(TF) \ 1198 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \ 1199 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \ 1200 ? (trace_buffer_wrap - trace_buffer_lo) \ 1201 : 0))) 1202 1203 /* The difference between these counters represents the total number 1204 of complete traceframes present in the trace buffer. The IP agent 1205 writes to the write count, GDBserver writes to read count. */ 1206 1207 IP_AGENT_EXPORT_VAR unsigned int traceframe_write_count; 1208 IP_AGENT_EXPORT_VAR unsigned int traceframe_read_count; 1209 1210 /* Convenience macro. */ 1211 1212 #define traceframe_count \ 1213 ((unsigned int) (traceframe_write_count - traceframe_read_count)) 1214 1215 /* The count of all traceframes created in the current run, including 1216 ones that were discarded to make room. */ 1217 1218 IP_AGENT_EXPORT_VAR int traceframes_created; 1219 1220 #ifndef IN_PROCESS_AGENT 1221 1222 /* Read-only regions are address ranges whose contents don't change, 1223 and so can be read from target memory even while looking at a trace 1224 frame. Without these, disassembly for instance will likely fail, 1225 because the program code is not usually collected into a trace 1226 frame. This data structure does not need to be very complicated or 1227 particularly efficient, it's only going to be used occasionally, 1228 and only by some commands. */ 1229 1230 struct readonly_region 1231 { 1232 /* The bounds of the region. */ 1233 CORE_ADDR start, end; 1234 1235 /* Link to the next one. */ 1236 struct readonly_region *next; 1237 }; 1238 1239 /* Linked list of readonly regions. This list stays in effect from 1240 one tstart to the next. */ 1241 1242 static struct readonly_region *readonly_regions; 1243 1244 #endif 1245 1246 /* The global that controls tracing overall. */ 1247 1248 IP_AGENT_EXPORT_VAR int tracing; 1249 1250 #ifndef IN_PROCESS_AGENT 1251 1252 /* Controls whether tracing should continue after GDB disconnects. */ 1253 1254 int disconnected_tracing; 1255 1256 /* The reason for the last tracing run to have stopped. We initialize 1257 to a distinct string so that GDB can distinguish between "stopped 1258 after running" and "stopped because never run" cases. */ 1259 1260 static const char *tracing_stop_reason = "tnotrun"; 1261 1262 static int tracing_stop_tpnum; 1263 1264 /* 64-bit timestamps for the trace run's start and finish, expressed 1265 in microseconds from the Unix epoch. */ 1266 1267 LONGEST tracing_start_time; 1268 LONGEST tracing_stop_time; 1269 1270 /* The (optional) user-supplied name of the user that started the run. 1271 This is an arbitrary string, and may be NULL. */ 1272 1273 char *tracing_user_name; 1274 1275 /* Optional user-supplied text describing the run. This is 1276 an arbitrary string, and may be NULL. */ 1277 1278 char *tracing_notes; 1279 1280 /* Optional user-supplied text explaining a tstop command. This is an 1281 arbitrary string, and may be NULL. */ 1282 1283 char *tracing_stop_note; 1284 1285 #endif 1286 1287 /* Functions local to this file. */ 1288 1289 /* Base "class" for tracepoint type specific data to be passed down to 1290 collect_data_at_tracepoint. */ 1291 struct tracepoint_hit_ctx 1292 { 1293 enum tracepoint_type type; 1294 }; 1295 1296 #ifdef IN_PROCESS_AGENT 1297 1298 /* Fast/jump tracepoint specific data to be passed down to 1299 collect_data_at_tracepoint. */ 1300 struct fast_tracepoint_ctx 1301 { 1302 struct tracepoint_hit_ctx base; 1303 1304 struct regcache regcache; 1305 int regcache_initted; 1306 unsigned char *regspace; 1307 1308 unsigned char *regs; 1309 struct tracepoint *tpoint; 1310 }; 1311 1312 /* Static tracepoint specific data to be passed down to 1313 collect_data_at_tracepoint. */ 1314 struct static_tracepoint_ctx 1315 { 1316 struct tracepoint_hit_ctx base; 1317 1318 /* The regcache corresponding to the registers state at the time of 1319 the tracepoint hit. Initialized lazily, from REGS. */ 1320 struct regcache regcache; 1321 int regcache_initted; 1322 1323 /* The buffer space REGCACHE above uses. We use a separate buffer 1324 instead of letting the regcache malloc for both signal safety and 1325 performance reasons; this is allocated on the stack instead. */ 1326 unsigned char *regspace; 1327 1328 /* The register buffer as passed on by lttng/ust. */ 1329 struct registers *regs; 1330 1331 /* The "printf" formatter and the args the user passed to the marker 1332 call. We use this to be able to collect "static trace data" 1333 ($_sdata). */ 1334 const char *fmt; 1335 va_list *args; 1336 1337 /* The GDB tracepoint matching the probed marker that was "hit". */ 1338 struct tracepoint *tpoint; 1339 }; 1340 1341 #else 1342 1343 /* Static tracepoint specific data to be passed down to 1344 collect_data_at_tracepoint. */ 1345 struct trap_tracepoint_ctx 1346 { 1347 struct tracepoint_hit_ctx base; 1348 1349 struct regcache *regcache; 1350 }; 1351 1352 #endif 1353 1354 #ifndef IN_PROCESS_AGENT 1355 static CORE_ADDR traceframe_get_pc (struct traceframe *tframe); 1356 static int traceframe_read_tsv (int num, LONGEST *val); 1357 #endif 1358 1359 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx, 1360 struct tracepoint *tpoint); 1361 1362 #ifndef IN_PROCESS_AGENT 1363 static void clear_readonly_regions (void); 1364 static void clear_installed_tracepoints (void); 1365 #endif 1366 1367 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, 1368 CORE_ADDR stop_pc, 1369 struct tracepoint *tpoint); 1370 #ifndef IN_PROCESS_AGENT 1371 static void collect_data_at_step (struct tracepoint_hit_ctx *ctx, 1372 CORE_ADDR stop_pc, 1373 struct tracepoint *tpoint, int current_step); 1374 static void compile_tracepoint_condition (struct tracepoint *tpoint, 1375 CORE_ADDR *jump_entry); 1376 #endif 1377 static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx, 1378 CORE_ADDR stop_pc, 1379 struct tracepoint *tpoint, 1380 struct traceframe *tframe, 1381 struct tracepoint_action *taction); 1382 1383 #ifndef IN_PROCESS_AGENT 1384 static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR); 1385 1386 static void install_tracepoint (struct tracepoint *, char *own_buf); 1387 static void download_tracepoint (struct tracepoint *); 1388 static int install_fast_tracepoint (struct tracepoint *, char *errbuf); 1389 static void clone_fast_tracepoint (struct tracepoint *to, 1390 const struct tracepoint *from); 1391 #endif 1392 1393 static LONGEST get_timestamp (void); 1394 1395 #if defined(__GNUC__) 1396 # define memory_barrier() asm volatile ("" : : : "memory") 1397 #else 1398 # define memory_barrier() do {} while (0) 1399 #endif 1400 1401 /* We only build the IPA if this builtin is supported, and there are 1402 no uses of this in GDBserver itself, so we're safe in defining this 1403 unconditionally. */ 1404 #define cmpxchg(mem, oldval, newval) \ 1405 __sync_val_compare_and_swap (mem, oldval, newval) 1406 1407 /* Record that an error occurred during expression evaluation. */ 1408 1409 static void 1410 record_tracepoint_error (struct tracepoint *tpoint, const char *which, 1411 enum eval_result_type rtype) 1412 { 1413 trace_debug ("Tracepoint %d at %s %s eval reports error %d", 1414 tpoint->number, paddress (tpoint->address), which, rtype); 1415 1416 #ifdef IN_PROCESS_AGENT 1417 /* Only record the first error we get. */ 1418 if (cmpxchg (&expr_eval_result, 1419 expr_eval_no_error, 1420 rtype) != expr_eval_no_error) 1421 return; 1422 #else 1423 if (expr_eval_result != expr_eval_no_error) 1424 return; 1425 #endif 1426 1427 error_tracepoint = tpoint; 1428 } 1429 1430 /* Trace buffer management. */ 1431 1432 static void 1433 clear_trace_buffer (void) 1434 { 1435 trace_buffer_start = trace_buffer_lo; 1436 trace_buffer_free = trace_buffer_lo; 1437 trace_buffer_end_free = trace_buffer_hi; 1438 trace_buffer_wrap = trace_buffer_hi; 1439 /* A traceframe with zeroed fields marks the end of trace data. */ 1440 ((struct traceframe *) trace_buffer_free)->tpnum = 0; 1441 ((struct traceframe *) trace_buffer_free)->data_size = 0; 1442 traceframe_read_count = traceframe_write_count = 0; 1443 traceframes_created = 0; 1444 } 1445 1446 #ifndef IN_PROCESS_AGENT 1447 1448 static void 1449 clear_inferior_trace_buffer (void) 1450 { 1451 CORE_ADDR ipa_trace_buffer_lo; 1452 CORE_ADDR ipa_trace_buffer_hi; 1453 struct traceframe ipa_traceframe = { 0 }; 1454 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl; 1455 1456 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo, 1457 &ipa_trace_buffer_lo); 1458 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi, 1459 &ipa_trace_buffer_hi); 1460 1461 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo; 1462 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo; 1463 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi; 1464 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi; 1465 1466 /* A traceframe with zeroed fields marks the end of trace data. */ 1467 target_write_memory (ipa_sym_addrs.addr_trace_buffer_ctrl, 1468 (unsigned char *) &ipa_trace_buffer_ctrl, 1469 sizeof (ipa_trace_buffer_ctrl)); 1470 1471 write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0); 1472 1473 /* A traceframe with zeroed fields marks the end of trace data. */ 1474 target_write_memory (ipa_trace_buffer_lo, 1475 (unsigned char *) &ipa_traceframe, 1476 sizeof (ipa_traceframe)); 1477 1478 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0); 1479 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0); 1480 write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0); 1481 } 1482 1483 #endif 1484 1485 static void 1486 init_trace_buffer (LONGEST bufsize) 1487 { 1488 size_t alloc_size; 1489 1490 trace_buffer_size = bufsize; 1491 1492 /* Make sure to internally allocate at least space for the EOB 1493 marker. */ 1494 alloc_size = (bufsize < TRACEFRAME_EOB_MARKER_SIZE 1495 ? TRACEFRAME_EOB_MARKER_SIZE : bufsize); 1496 trace_buffer_lo = (unsigned char *) xrealloc (trace_buffer_lo, alloc_size); 1497 1498 trace_buffer_hi = trace_buffer_lo + trace_buffer_size; 1499 1500 clear_trace_buffer (); 1501 } 1502 1503 #ifdef IN_PROCESS_AGENT 1504 1505 /* This is needed for -Wmissing-declarations. */ 1506 IP_AGENT_EXPORT_FUNC void about_to_request_buffer_space (void); 1507 1508 IP_AGENT_EXPORT_FUNC void 1509 about_to_request_buffer_space (void) 1510 { 1511 /* GDBserver places breakpoint here while it goes about to flush 1512 data at random times. */ 1513 UNKNOWN_SIDE_EFFECTS(); 1514 } 1515 1516 #endif 1517 1518 /* Carve out a piece of the trace buffer, returning NULL in case of 1519 failure. */ 1520 1521 static void * 1522 trace_buffer_alloc (size_t amt) 1523 { 1524 unsigned char *rslt; 1525 struct trace_buffer_control *tbctrl; 1526 unsigned int curr; 1527 #ifdef IN_PROCESS_AGENT 1528 unsigned int prev, prev_filtered; 1529 unsigned int commit_count; 1530 unsigned int commit; 1531 unsigned int readout; 1532 #else 1533 struct traceframe *oldest; 1534 unsigned char *new_start; 1535 #endif 1536 1537 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer", 1538 (long) amt, (long) sizeof (struct traceframe)); 1539 1540 /* Account for the EOB marker. */ 1541 amt += TRACEFRAME_EOB_MARKER_SIZE; 1542 1543 #ifdef IN_PROCESS_AGENT 1544 again: 1545 memory_barrier (); 1546 1547 /* Read the current token and extract the index to try to write to, 1548 storing it in CURR. */ 1549 prev = trace_buffer_ctrl_curr; 1550 prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK; 1551 curr = prev_filtered + 1; 1552 if (curr > 2) 1553 curr = 0; 1554 1555 about_to_request_buffer_space (); 1556 1557 /* Start out with a copy of the current state. GDBserver may be 1558 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't 1559 be able to commit anyway if that happens. */ 1560 trace_buffer_ctrl[curr] 1561 = trace_buffer_ctrl[prev_filtered]; 1562 trace_debug ("trying curr=%u", curr); 1563 #else 1564 /* The GDBserver's agent doesn't need all that syncing, and always 1565 updates TCB 0 (there's only one, mind you). */ 1566 curr = 0; 1567 #endif 1568 tbctrl = &trace_buffer_ctrl[curr]; 1569 1570 /* Offsets are easier to grok for debugging than raw addresses, 1571 especially for the small trace buffer sizes that are useful for 1572 testing. */ 1573 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d", 1574 curr, 1575 (int) (tbctrl->start - trace_buffer_lo), 1576 (int) (tbctrl->free - trace_buffer_lo), 1577 (int) (tbctrl->end_free - trace_buffer_lo), 1578 (int) (tbctrl->wrap - trace_buffer_lo), 1579 (int) (trace_buffer_hi - trace_buffer_lo)); 1580 1581 /* The algorithm here is to keep trying to get a contiguous block of 1582 the requested size, possibly discarding older traceframes to free 1583 up space. Since free space might come in one or two pieces, 1584 depending on whether discarded traceframes wrapped around at the 1585 high end of the buffer, we test both pieces after each 1586 discard. */ 1587 while (1) 1588 { 1589 /* First, if we have two free parts, try the upper one first. */ 1590 if (tbctrl->end_free < tbctrl->free) 1591 { 1592 if (tbctrl->free + amt <= trace_buffer_hi) 1593 /* We have enough in the upper part. */ 1594 break; 1595 else 1596 { 1597 /* Our high part of free space wasn't enough. Give up 1598 on it for now, set wraparound. We will recover the 1599 space later, if/when the wrapped-around traceframe is 1600 discarded. */ 1601 trace_debug ("Upper part too small, setting wraparound"); 1602 tbctrl->wrap = tbctrl->free; 1603 tbctrl->free = trace_buffer_lo; 1604 } 1605 } 1606 1607 /* The normal case. */ 1608 if (tbctrl->free + amt <= tbctrl->end_free) 1609 break; 1610 1611 #ifdef IN_PROCESS_AGENT 1612 /* The IP Agent's buffer is always circular. It isn't used 1613 currently, but `circular_trace_buffer' could represent 1614 GDBserver's mode. If we didn't find space, ask GDBserver to 1615 flush. */ 1616 1617 flush_trace_buffer (); 1618 memory_barrier (); 1619 if (tracing) 1620 { 1621 trace_debug ("gdbserver flushed buffer, retrying"); 1622 goto again; 1623 } 1624 1625 /* GDBserver cancelled the tracing. Bail out as well. */ 1626 return NULL; 1627 #else 1628 /* If we're here, then neither part is big enough, and 1629 non-circular trace buffers are now full. */ 1630 if (!circular_trace_buffer) 1631 { 1632 trace_debug ("Not enough space in the trace buffer"); 1633 return NULL; 1634 } 1635 1636 trace_debug ("Need more space in the trace buffer"); 1637 1638 /* If we have a circular buffer, we can try discarding the 1639 oldest traceframe and see if that helps. */ 1640 oldest = FIRST_TRACEFRAME (); 1641 if (oldest->tpnum == 0) 1642 { 1643 /* Not good; we have no traceframes to free. Perhaps we're 1644 asking for a block that is larger than the buffer? In 1645 any case, give up. */ 1646 trace_debug ("No traceframes to discard"); 1647 return NULL; 1648 } 1649 1650 /* We don't run this code in the in-process agent currently. 1651 E.g., we could leave the in-process agent in autonomous 1652 circular mode if we only have fast tracepoints. If we do 1653 that, then this bit becomes racy with GDBserver, which also 1654 writes to this counter. */ 1655 --traceframe_write_count; 1656 1657 new_start = (unsigned char *) NEXT_TRACEFRAME (oldest); 1658 /* If we freed the traceframe that wrapped around, go back 1659 to the non-wrap case. */ 1660 if (new_start < tbctrl->start) 1661 { 1662 trace_debug ("Discarding past the wraparound"); 1663 tbctrl->wrap = trace_buffer_hi; 1664 } 1665 tbctrl->start = new_start; 1666 tbctrl->end_free = tbctrl->start; 1667 1668 trace_debug ("Discarded a traceframe\n" 1669 "Trace buffer [%d], start=%d free=%d " 1670 "endfree=%d wrap=%d hi=%d", 1671 curr, 1672 (int) (tbctrl->start - trace_buffer_lo), 1673 (int) (tbctrl->free - trace_buffer_lo), 1674 (int) (tbctrl->end_free - trace_buffer_lo), 1675 (int) (tbctrl->wrap - trace_buffer_lo), 1676 (int) (trace_buffer_hi - trace_buffer_lo)); 1677 1678 /* Now go back around the loop. The discard might have resulted 1679 in either one or two pieces of free space, so we want to try 1680 both before freeing any more traceframes. */ 1681 #endif 1682 } 1683 1684 /* If we get here, we know we can provide the asked-for space. */ 1685 1686 rslt = tbctrl->free; 1687 1688 /* Adjust the request back down, now that we know we have space for 1689 the marker, but don't commit to AMT yet, we may still need to 1690 restart the operation if GDBserver touches the trace buffer 1691 (obviously only important in the in-process agent's version). */ 1692 tbctrl->free += (amt - sizeof (struct traceframe)); 1693 1694 /* Or not. If GDBserver changed the trace buffer behind our back, 1695 we get to restart a new allocation attempt. */ 1696 1697 #ifdef IN_PROCESS_AGENT 1698 /* Build the tentative token. */ 1699 commit_count = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) + 0x100) 1700 & GDBSERVER_FLUSH_COUNT_MASK_CURR); 1701 commit = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) << 12) 1702 | commit_count 1703 | curr); 1704 1705 /* Try to commit it. */ 1706 readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit); 1707 if (readout != prev) 1708 { 1709 trace_debug ("GDBserver has touched the trace buffer, restarting." 1710 " (prev=%08x, commit=%08x, readout=%08x)", 1711 prev, commit, readout); 1712 goto again; 1713 } 1714 1715 /* Hold your horses here. Even if that change was committed, 1716 GDBserver could come in, and clobber it. We need to hold to be 1717 able to tell if GDBserver clobbers before or after we committed 1718 the change. Whenever GDBserver goes about touching the IPA 1719 buffer, it sets a breakpoint in this routine, so we have a sync 1720 point here. */ 1721 about_to_request_buffer_space (); 1722 1723 /* Check if the change has been effective, even if GDBserver stopped 1724 us at the breakpoint. */ 1725 1726 { 1727 unsigned int refetch; 1728 1729 memory_barrier (); 1730 1731 refetch = trace_buffer_ctrl_curr; 1732 1733 if (refetch == commit 1734 || ((refetch & GDBSERVER_FLUSH_COUNT_MASK_PREV) >> 12) == commit_count) 1735 { 1736 /* effective */ 1737 trace_debug ("change is effective: (prev=%08x, commit=%08x, " 1738 "readout=%08x, refetch=%08x)", 1739 prev, commit, readout, refetch); 1740 } 1741 else 1742 { 1743 trace_debug ("GDBserver has touched the trace buffer, not effective." 1744 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)", 1745 prev, commit, readout, refetch); 1746 goto again; 1747 } 1748 } 1749 #endif 1750 1751 /* We have a new piece of the trace buffer. Hurray! */ 1752 1753 /* Add an EOB marker just past this allocation. */ 1754 ((struct traceframe *) tbctrl->free)->tpnum = 0; 1755 ((struct traceframe *) tbctrl->free)->data_size = 0; 1756 1757 /* Adjust the request back down, now that we know we have space for 1758 the marker. */ 1759 amt -= sizeof (struct traceframe); 1760 1761 if (debug_threads) 1762 { 1763 trace_debug ("Allocated %d bytes", (int) amt); 1764 trace_debug ("Trace buffer [%d] start=%d free=%d " 1765 "endfree=%d wrap=%d hi=%d", 1766 curr, 1767 (int) (tbctrl->start - trace_buffer_lo), 1768 (int) (tbctrl->free - trace_buffer_lo), 1769 (int) (tbctrl->end_free - trace_buffer_lo), 1770 (int) (tbctrl->wrap - trace_buffer_lo), 1771 (int) (trace_buffer_hi - trace_buffer_lo)); 1772 } 1773 1774 return rslt; 1775 } 1776 1777 #ifndef IN_PROCESS_AGENT 1778 1779 /* Return the total free space. This is not necessarily the largest 1780 block we can allocate, because of the two-part case. */ 1781 1782 static int 1783 free_space (void) 1784 { 1785 if (trace_buffer_free <= trace_buffer_end_free) 1786 return trace_buffer_end_free - trace_buffer_free; 1787 else 1788 return ((trace_buffer_end_free - trace_buffer_lo) 1789 + (trace_buffer_hi - trace_buffer_free)); 1790 } 1791 1792 /* An 'S' in continuation packets indicates remainder are for 1793 while-stepping. */ 1794 1795 static int seen_step_action_flag; 1796 1797 /* Create a tracepoint (location) with given number and address. Add this 1798 new tracepoint to list and sort this list. */ 1799 1800 static struct tracepoint * 1801 add_tracepoint (int num, CORE_ADDR addr) 1802 { 1803 struct tracepoint *tpoint, **tp_next; 1804 1805 tpoint = XNEW (struct tracepoint); 1806 tpoint->number = num; 1807 tpoint->address = addr; 1808 tpoint->numactions = 0; 1809 tpoint->actions = NULL; 1810 tpoint->actions_str = NULL; 1811 tpoint->cond = NULL; 1812 tpoint->num_step_actions = 0; 1813 tpoint->step_actions = NULL; 1814 tpoint->step_actions_str = NULL; 1815 /* Start all off as regular (slow) tracepoints. */ 1816 tpoint->type = trap_tracepoint; 1817 tpoint->orig_size = -1; 1818 tpoint->source_strings = NULL; 1819 tpoint->compiled_cond = 0; 1820 tpoint->handle = NULL; 1821 tpoint->next = NULL; 1822 1823 /* Find a place to insert this tracepoint into list in order to keep 1824 the tracepoint list still in the ascending order. There may be 1825 multiple tracepoints at the same address as TPOINT's, and this 1826 guarantees TPOINT is inserted after all the tracepoints which are 1827 set at the same address. For example, fast tracepoints A, B, C are 1828 set at the same address, and D is to be insert at the same place as 1829 well, 1830 1831 -->| A |--> | B |-->| C |->... 1832 1833 One jump pad was created for tracepoint A, B, and C, and the target 1834 address of A is referenced/used in jump pad. So jump pad will let 1835 inferior jump to A. If D is inserted in front of A, like this, 1836 1837 -->| D |-->| A |--> | B |-->| C |->... 1838 1839 without updating jump pad, D is not reachable during collect, which 1840 is wrong. As we can see, the order of B, C and D doesn't matter, but 1841 A should always be the `first' one. */ 1842 for (tp_next = &tracepoints; 1843 (*tp_next) != NULL && (*tp_next)->address <= tpoint->address; 1844 tp_next = &(*tp_next)->next) 1845 ; 1846 tpoint->next = *tp_next; 1847 *tp_next = tpoint; 1848 last_tracepoint = tpoint; 1849 1850 seen_step_action_flag = 0; 1851 1852 return tpoint; 1853 } 1854 1855 #ifndef IN_PROCESS_AGENT 1856 1857 /* Return the tracepoint with the given number and address, or NULL. */ 1858 1859 static struct tracepoint * 1860 find_tracepoint (int id, CORE_ADDR addr) 1861 { 1862 struct tracepoint *tpoint; 1863 1864 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) 1865 if (tpoint->number == id && tpoint->address == addr) 1866 return tpoint; 1867 1868 return NULL; 1869 } 1870 1871 /* Remove TPOINT from global list. */ 1872 1873 static void 1874 remove_tracepoint (struct tracepoint *tpoint) 1875 { 1876 struct tracepoint *tp, *tp_prev; 1877 1878 for (tp = tracepoints, tp_prev = NULL; tp && tp != tpoint; 1879 tp_prev = tp, tp = tp->next) 1880 ; 1881 1882 if (tp) 1883 { 1884 if (tp_prev) 1885 tp_prev->next = tp->next; 1886 else 1887 tracepoints = tp->next; 1888 1889 xfree (tp); 1890 } 1891 } 1892 1893 /* There may be several tracepoints with the same number (because they 1894 are "locations", in GDB parlance); return the next one after the 1895 given tracepoint, or search from the beginning of the list if the 1896 first argument is NULL. */ 1897 1898 static struct tracepoint * 1899 find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num) 1900 { 1901 struct tracepoint *tpoint; 1902 1903 if (prev_tp) 1904 tpoint = prev_tp->next; 1905 else 1906 tpoint = tracepoints; 1907 for (; tpoint; tpoint = tpoint->next) 1908 if (tpoint->number == num) 1909 return tpoint; 1910 1911 return NULL; 1912 } 1913 1914 #endif 1915 1916 /* Append another action to perform when the tracepoint triggers. */ 1917 1918 static void 1919 add_tracepoint_action (struct tracepoint *tpoint, const char *packet) 1920 { 1921 const char *act; 1922 1923 if (*packet == 'S') 1924 { 1925 seen_step_action_flag = 1; 1926 ++packet; 1927 } 1928 1929 act = packet; 1930 1931 while (*act) 1932 { 1933 const char *act_start = act; 1934 struct tracepoint_action *action = NULL; 1935 1936 switch (*act) 1937 { 1938 case 'M': 1939 { 1940 struct collect_memory_action *maction = 1941 XNEW (struct collect_memory_action); 1942 ULONGEST basereg; 1943 int is_neg; 1944 1945 maction->base.type = *act; 1946 action = &maction->base; 1947 1948 ++act; 1949 is_neg = (*act == '-'); 1950 if (*act == '-') 1951 ++act; 1952 act = unpack_varlen_hex (act, &basereg); 1953 ++act; 1954 act = unpack_varlen_hex (act, &maction->addr); 1955 ++act; 1956 act = unpack_varlen_hex (act, &maction->len); 1957 maction->basereg = (is_neg 1958 ? - (int) basereg 1959 : (int) basereg); 1960 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)", 1961 pulongest (maction->len), 1962 paddress (maction->addr), maction->basereg); 1963 break; 1964 } 1965 case 'R': 1966 { 1967 struct collect_registers_action *raction = 1968 XNEW (struct collect_registers_action); 1969 1970 raction->base.type = *act; 1971 action = &raction->base; 1972 1973 trace_debug ("Want to collect registers"); 1974 ++act; 1975 /* skip past hex digits of mask for now */ 1976 while (isxdigit(*act)) 1977 ++act; 1978 break; 1979 } 1980 case 'L': 1981 { 1982 struct collect_static_trace_data_action *raction = 1983 XNEW (struct collect_static_trace_data_action); 1984 1985 raction->base.type = *act; 1986 action = &raction->base; 1987 1988 trace_debug ("Want to collect static trace data"); 1989 ++act; 1990 break; 1991 } 1992 case 'S': 1993 trace_debug ("Unexpected step action, ignoring"); 1994 ++act; 1995 break; 1996 case 'X': 1997 { 1998 struct eval_expr_action *xaction = XNEW (struct eval_expr_action); 1999 2000 xaction->base.type = *act; 2001 action = &xaction->base; 2002 2003 trace_debug ("Want to evaluate expression"); 2004 xaction->expr = gdb_parse_agent_expr (&act); 2005 break; 2006 } 2007 default: 2008 trace_debug ("unknown trace action '%c', ignoring...", *act); 2009 break; 2010 case '-': 2011 break; 2012 } 2013 2014 if (action == NULL) 2015 break; 2016 2017 if (seen_step_action_flag) 2018 { 2019 tpoint->num_step_actions++; 2020 2021 tpoint->step_actions 2022 = XRESIZEVEC (struct tracepoint_action *, tpoint->step_actions, 2023 tpoint->num_step_actions); 2024 tpoint->step_actions_str 2025 = XRESIZEVEC (char *, tpoint->step_actions_str, 2026 tpoint->num_step_actions); 2027 tpoint->step_actions[tpoint->num_step_actions - 1] = action; 2028 tpoint->step_actions_str[tpoint->num_step_actions - 1] 2029 = savestring (act_start, act - act_start); 2030 } 2031 else 2032 { 2033 tpoint->numactions++; 2034 tpoint->actions 2035 = XRESIZEVEC (struct tracepoint_action *, tpoint->actions, 2036 tpoint->numactions); 2037 tpoint->actions_str 2038 = XRESIZEVEC (char *, tpoint->actions_str, tpoint->numactions); 2039 tpoint->actions[tpoint->numactions - 1] = action; 2040 tpoint->actions_str[tpoint->numactions - 1] 2041 = savestring (act_start, act - act_start); 2042 } 2043 } 2044 } 2045 2046 #endif 2047 2048 /* Find or create a trace state variable with the given number. */ 2049 2050 static struct trace_state_variable * 2051 get_trace_state_variable (int num) 2052 { 2053 struct trace_state_variable *tsv; 2054 2055 #ifdef IN_PROCESS_AGENT 2056 /* Search for an existing variable. */ 2057 for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next) 2058 if (tsv->number == num) 2059 return tsv; 2060 #endif 2061 2062 /* Search for an existing variable. */ 2063 for (tsv = trace_state_variables; tsv; tsv = tsv->next) 2064 if (tsv->number == num) 2065 return tsv; 2066 2067 return NULL; 2068 } 2069 2070 /* Find or create a trace state variable with the given number. */ 2071 2072 static struct trace_state_variable * 2073 create_trace_state_variable (int num, int gdb) 2074 { 2075 struct trace_state_variable *tsv; 2076 2077 tsv = get_trace_state_variable (num); 2078 if (tsv != NULL) 2079 return tsv; 2080 2081 /* Create a new variable. */ 2082 tsv = XNEW (struct trace_state_variable); 2083 tsv->number = num; 2084 tsv->initial_value = 0; 2085 tsv->value = 0; 2086 tsv->getter = NULL; 2087 tsv->name = NULL; 2088 #ifdef IN_PROCESS_AGENT 2089 if (!gdb) 2090 { 2091 tsv->next = alloced_trace_state_variables; 2092 alloced_trace_state_variables = tsv; 2093 } 2094 else 2095 #endif 2096 { 2097 tsv->next = trace_state_variables; 2098 trace_state_variables = tsv; 2099 } 2100 return tsv; 2101 } 2102 2103 /* This is needed for -Wmissing-declarations. */ 2104 IP_AGENT_EXPORT_FUNC LONGEST get_trace_state_variable_value (int num); 2105 2106 IP_AGENT_EXPORT_FUNC LONGEST 2107 get_trace_state_variable_value (int num) 2108 { 2109 struct trace_state_variable *tsv; 2110 2111 tsv = get_trace_state_variable (num); 2112 2113 if (!tsv) 2114 { 2115 trace_debug ("No trace state variable %d, skipping value get", num); 2116 return 0; 2117 } 2118 2119 /* Call a getter function if we have one. While it's tempting to 2120 set up something to only call the getter once per tracepoint hit, 2121 it could run afoul of thread races. Better to let the getter 2122 handle it directly, if necessary to worry about it. */ 2123 if (tsv->getter) 2124 tsv->value = (tsv->getter) (); 2125 2126 trace_debug ("get_trace_state_variable_value(%d) ==> %s", 2127 num, plongest (tsv->value)); 2128 2129 return tsv->value; 2130 } 2131 2132 /* This is needed for -Wmissing-declarations. */ 2133 IP_AGENT_EXPORT_FUNC void set_trace_state_variable_value (int num, 2134 LONGEST val); 2135 2136 IP_AGENT_EXPORT_FUNC void 2137 set_trace_state_variable_value (int num, LONGEST val) 2138 { 2139 struct trace_state_variable *tsv; 2140 2141 tsv = get_trace_state_variable (num); 2142 2143 if (!tsv) 2144 { 2145 trace_debug ("No trace state variable %d, skipping value set", num); 2146 return; 2147 } 2148 2149 tsv->value = val; 2150 } 2151 2152 LONGEST 2153 agent_get_trace_state_variable_value (int num) 2154 { 2155 return get_trace_state_variable_value (num); 2156 } 2157 2158 void 2159 agent_set_trace_state_variable_value (int num, LONGEST val) 2160 { 2161 set_trace_state_variable_value (num, val); 2162 } 2163 2164 static void 2165 set_trace_state_variable_name (int num, const char *name) 2166 { 2167 struct trace_state_variable *tsv; 2168 2169 tsv = get_trace_state_variable (num); 2170 2171 if (!tsv) 2172 { 2173 trace_debug ("No trace state variable %d, skipping name set", num); 2174 return; 2175 } 2176 2177 tsv->name = (char *) name; 2178 } 2179 2180 static void 2181 set_trace_state_variable_getter (int num, LONGEST (*getter) (void)) 2182 { 2183 struct trace_state_variable *tsv; 2184 2185 tsv = get_trace_state_variable (num); 2186 2187 if (!tsv) 2188 { 2189 trace_debug ("No trace state variable %d, skipping getter set", num); 2190 return; 2191 } 2192 2193 tsv->getter = getter; 2194 } 2195 2196 /* Add a raw traceframe for the given tracepoint. */ 2197 2198 static struct traceframe * 2199 add_traceframe (struct tracepoint *tpoint) 2200 { 2201 struct traceframe *tframe; 2202 2203 tframe 2204 = (struct traceframe *) trace_buffer_alloc (sizeof (struct traceframe)); 2205 2206 if (tframe == NULL) 2207 return NULL; 2208 2209 tframe->tpnum = tpoint->number; 2210 tframe->data_size = 0; 2211 2212 return tframe; 2213 } 2214 2215 /* Add a block to the traceframe currently being worked on. */ 2216 2217 static unsigned char * 2218 add_traceframe_block (struct traceframe *tframe, 2219 struct tracepoint *tpoint, int amt) 2220 { 2221 unsigned char *block; 2222 2223 if (!tframe) 2224 return NULL; 2225 2226 block = (unsigned char *) trace_buffer_alloc (amt); 2227 2228 if (!block) 2229 return NULL; 2230 2231 gdb_assert (tframe->tpnum == tpoint->number); 2232 2233 tframe->data_size += amt; 2234 tpoint->traceframe_usage += amt; 2235 2236 return block; 2237 } 2238 2239 /* Flag that the current traceframe is finished. */ 2240 2241 static void 2242 finish_traceframe (struct traceframe *tframe) 2243 { 2244 ++traceframe_write_count; 2245 ++traceframes_created; 2246 } 2247 2248 #ifndef IN_PROCESS_AGENT 2249 2250 /* Given a traceframe number NUM, find the NUMth traceframe in the 2251 buffer. */ 2252 2253 static struct traceframe * 2254 find_traceframe (int num) 2255 { 2256 struct traceframe *tframe; 2257 int tfnum = 0; 2258 2259 for (tframe = FIRST_TRACEFRAME (); 2260 tframe->tpnum != 0; 2261 tframe = NEXT_TRACEFRAME (tframe)) 2262 { 2263 if (tfnum == num) 2264 return tframe; 2265 ++tfnum; 2266 } 2267 2268 return NULL; 2269 } 2270 2271 static CORE_ADDR 2272 get_traceframe_address (struct traceframe *tframe) 2273 { 2274 CORE_ADDR addr; 2275 struct tracepoint *tpoint; 2276 2277 addr = traceframe_get_pc (tframe); 2278 2279 if (addr) 2280 return addr; 2281 2282 /* Fallback strategy, will be incorrect for while-stepping frames 2283 and multi-location tracepoints. */ 2284 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum); 2285 return tpoint->address; 2286 } 2287 2288 /* Search for the next traceframe whose address is inside or outside 2289 the given range. */ 2290 2291 static struct traceframe * 2292 find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p, 2293 int *tfnump) 2294 { 2295 client_state &cs = get_client_state (); 2296 struct traceframe *tframe; 2297 CORE_ADDR tfaddr; 2298 2299 *tfnump = cs.current_traceframe + 1; 2300 tframe = find_traceframe (*tfnump); 2301 /* The search is not supposed to wrap around. */ 2302 if (!tframe) 2303 { 2304 *tfnump = -1; 2305 return NULL; 2306 } 2307 2308 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe)) 2309 { 2310 tfaddr = get_traceframe_address (tframe); 2311 if (inside_p 2312 ? (lo <= tfaddr && tfaddr <= hi) 2313 : (lo > tfaddr || tfaddr > hi)) 2314 return tframe; 2315 ++*tfnump; 2316 } 2317 2318 *tfnump = -1; 2319 return NULL; 2320 } 2321 2322 /* Search for the next traceframe recorded by the given tracepoint. 2323 Note that for multi-location tracepoints, this will find whatever 2324 location appears first. */ 2325 2326 static struct traceframe * 2327 find_next_traceframe_by_tracepoint (int num, int *tfnump) 2328 { 2329 client_state &cs = get_client_state (); 2330 struct traceframe *tframe; 2331 2332 *tfnump = cs.current_traceframe + 1; 2333 tframe = find_traceframe (*tfnump); 2334 /* The search is not supposed to wrap around. */ 2335 if (!tframe) 2336 { 2337 *tfnump = -1; 2338 return NULL; 2339 } 2340 2341 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe)) 2342 { 2343 if (tframe->tpnum == num) 2344 return tframe; 2345 ++*tfnump; 2346 } 2347 2348 *tfnump = -1; 2349 return NULL; 2350 } 2351 2352 #endif 2353 2354 #ifndef IN_PROCESS_AGENT 2355 2356 /* Clear all past trace state. */ 2357 2358 static void 2359 cmd_qtinit (char *packet) 2360 { 2361 client_state &cs = get_client_state (); 2362 struct trace_state_variable *tsv, *prev, *next; 2363 2364 /* Can't do this command without a pid attached. */ 2365 if (current_thread == NULL) 2366 { 2367 write_enn (packet); 2368 return; 2369 } 2370 2371 /* Make sure we don't try to read from a trace frame. */ 2372 cs.current_traceframe = -1; 2373 2374 stop_tracing (); 2375 2376 trace_debug ("Initializing the trace"); 2377 2378 clear_installed_tracepoints (); 2379 clear_readonly_regions (); 2380 2381 tracepoints = NULL; 2382 last_tracepoint = NULL; 2383 2384 /* Clear out any leftover trace state variables. Ones with target 2385 defined getters should be kept however. */ 2386 prev = NULL; 2387 tsv = trace_state_variables; 2388 while (tsv) 2389 { 2390 trace_debug ("Looking at var %d", tsv->number); 2391 if (tsv->getter == NULL) 2392 { 2393 next = tsv->next; 2394 if (prev) 2395 prev->next = next; 2396 else 2397 trace_state_variables = next; 2398 trace_debug ("Deleting var %d", tsv->number); 2399 free (tsv); 2400 tsv = next; 2401 } 2402 else 2403 { 2404 prev = tsv; 2405 tsv = tsv->next; 2406 } 2407 } 2408 2409 clear_trace_buffer (); 2410 clear_inferior_trace_buffer (); 2411 2412 write_ok (packet); 2413 } 2414 2415 /* Unprobe the UST marker at ADDRESS. */ 2416 2417 static void 2418 unprobe_marker_at (CORE_ADDR address) 2419 { 2420 char cmd[IPA_CMD_BUF_SIZE]; 2421 2422 sprintf (cmd, "unprobe_marker_at:%s", paddress (address)); 2423 run_inferior_command (cmd, strlen (cmd) + 1); 2424 } 2425 2426 /* Restore the program to its pre-tracing state. This routine may be called 2427 in error situations, so it needs to be careful about only restoring 2428 from known-valid bits. */ 2429 2430 static void 2431 clear_installed_tracepoints (void) 2432 { 2433 struct tracepoint *tpoint; 2434 struct tracepoint *prev_stpoint; 2435 2436 target_pause_all (true); 2437 2438 prev_stpoint = NULL; 2439 2440 /* Restore any bytes overwritten by tracepoints. */ 2441 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) 2442 { 2443 /* Catch the case where we might try to remove a tracepoint that 2444 was never actually installed. */ 2445 if (tpoint->handle == NULL) 2446 { 2447 trace_debug ("Tracepoint %d at 0x%s was " 2448 "never installed, nothing to clear", 2449 tpoint->number, paddress (tpoint->address)); 2450 continue; 2451 } 2452 2453 switch (tpoint->type) 2454 { 2455 case trap_tracepoint: 2456 { 2457 struct breakpoint *bp 2458 = (struct breakpoint *) tpoint->handle; 2459 2460 delete_breakpoint (bp); 2461 } 2462 break; 2463 case fast_tracepoint: 2464 { 2465 struct fast_tracepoint_jump *jump 2466 = (struct fast_tracepoint_jump *) tpoint->handle; 2467 2468 delete_fast_tracepoint_jump (jump); 2469 } 2470 break; 2471 case static_tracepoint: 2472 if (prev_stpoint != NULL 2473 && prev_stpoint->address == tpoint->address) 2474 /* Nothing to do. We already unprobed a tracepoint set at 2475 this marker address (and there can only be one probe 2476 per marker). */ 2477 ; 2478 else 2479 { 2480 unprobe_marker_at (tpoint->address); 2481 prev_stpoint = tpoint; 2482 } 2483 break; 2484 } 2485 2486 tpoint->handle = NULL; 2487 } 2488 2489 target_unpause_all (true); 2490 } 2491 2492 /* Parse a packet that defines a tracepoint. */ 2493 2494 static void 2495 cmd_qtdp (char *own_buf) 2496 { 2497 int tppacket; 2498 /* Whether there is a trailing hyphen at the end of the QTDP packet. */ 2499 int trail_hyphen = 0; 2500 ULONGEST num; 2501 ULONGEST addr; 2502 ULONGEST count; 2503 struct tracepoint *tpoint; 2504 const char *packet = own_buf; 2505 2506 packet += strlen ("QTDP:"); 2507 2508 /* A hyphen at the beginning marks a packet specifying actions for a 2509 tracepoint already supplied. */ 2510 tppacket = 1; 2511 if (*packet == '-') 2512 { 2513 tppacket = 0; 2514 ++packet; 2515 } 2516 packet = unpack_varlen_hex (packet, &num); 2517 ++packet; /* skip a colon */ 2518 packet = unpack_varlen_hex (packet, &addr); 2519 ++packet; /* skip a colon */ 2520 2521 /* See if we already have this tracepoint. */ 2522 tpoint = find_tracepoint (num, addr); 2523 2524 if (tppacket) 2525 { 2526 /* Duplicate tracepoints are never allowed. */ 2527 if (tpoint) 2528 { 2529 trace_debug ("Tracepoint error: tracepoint %d" 2530 " at 0x%s already exists", 2531 (int) num, paddress (addr)); 2532 write_enn (own_buf); 2533 return; 2534 } 2535 2536 tpoint = add_tracepoint (num, addr); 2537 2538 tpoint->enabled = (*packet == 'E'); 2539 ++packet; /* skip 'E' */ 2540 ++packet; /* skip a colon */ 2541 packet = unpack_varlen_hex (packet, &count); 2542 tpoint->step_count = count; 2543 ++packet; /* skip a colon */ 2544 packet = unpack_varlen_hex (packet, &count); 2545 tpoint->pass_count = count; 2546 /* See if we have any of the additional optional fields. */ 2547 while (*packet == ':') 2548 { 2549 ++packet; 2550 if (*packet == 'F') 2551 { 2552 tpoint->type = fast_tracepoint; 2553 ++packet; 2554 packet = unpack_varlen_hex (packet, &count); 2555 tpoint->orig_size = count; 2556 } 2557 else if (*packet == 'S') 2558 { 2559 tpoint->type = static_tracepoint; 2560 ++packet; 2561 } 2562 else if (*packet == 'X') 2563 { 2564 tpoint->cond = gdb_parse_agent_expr (&packet); 2565 } 2566 else if (*packet == '-') 2567 break; 2568 else if (*packet == '\0') 2569 break; 2570 else 2571 trace_debug ("Unknown optional tracepoint field"); 2572 } 2573 if (*packet == '-') 2574 { 2575 trail_hyphen = 1; 2576 trace_debug ("Also has actions\n"); 2577 } 2578 2579 trace_debug ("Defined %stracepoint %d at 0x%s, " 2580 "enabled %d step %" PRIu64 " pass %" PRIu64, 2581 tpoint->type == fast_tracepoint ? "fast " 2582 : tpoint->type == static_tracepoint ? "static " : "", 2583 tpoint->number, paddress (tpoint->address), tpoint->enabled, 2584 tpoint->step_count, tpoint->pass_count); 2585 } 2586 else if (tpoint) 2587 add_tracepoint_action (tpoint, packet); 2588 else 2589 { 2590 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found", 2591 (int) num, paddress (addr)); 2592 write_enn (own_buf); 2593 return; 2594 } 2595 2596 /* Install tracepoint during tracing only once for each tracepoint location. 2597 For each tracepoint loc, GDB may send multiple QTDP packets, and we can 2598 determine the last QTDP packet for one tracepoint location by checking 2599 trailing hyphen in QTDP packet. */ 2600 if (tracing && !trail_hyphen) 2601 { 2602 struct tracepoint *tp = NULL; 2603 2604 /* Pause all threads temporarily while we patch tracepoints. */ 2605 target_pause_all (false); 2606 2607 /* download_tracepoint will update global `tracepoints' 2608 list, so it is unsafe to leave threads in jump pad. */ 2609 target_stabilize_threads (); 2610 2611 /* Freeze threads. */ 2612 target_pause_all (true); 2613 2614 2615 if (tpoint->type != trap_tracepoint) 2616 { 2617 /* Find another fast or static tracepoint at the same address. */ 2618 for (tp = tracepoints; tp; tp = tp->next) 2619 { 2620 if (tp->address == tpoint->address && tp->type == tpoint->type 2621 && tp->number != tpoint->number) 2622 break; 2623 } 2624 2625 /* TPOINT is installed at the same address as TP. */ 2626 if (tp) 2627 { 2628 if (tpoint->type == fast_tracepoint) 2629 clone_fast_tracepoint (tpoint, tp); 2630 else if (tpoint->type == static_tracepoint) 2631 tpoint->handle = (void *) -1; 2632 } 2633 } 2634 2635 if (use_agent && tpoint->type == fast_tracepoint 2636 && agent_capability_check (AGENT_CAPA_FAST_TRACE)) 2637 { 2638 /* Download and install fast tracepoint by agent. */ 2639 if (tracepoint_send_agent (tpoint) == 0) 2640 write_ok (own_buf); 2641 else 2642 { 2643 write_enn (own_buf); 2644 remove_tracepoint (tpoint); 2645 } 2646 } 2647 else 2648 { 2649 download_tracepoint (tpoint); 2650 2651 if (tpoint->type == trap_tracepoint || tp == NULL) 2652 { 2653 install_tracepoint (tpoint, own_buf); 2654 if (strcmp (own_buf, "OK") != 0) 2655 remove_tracepoint (tpoint); 2656 } 2657 else 2658 write_ok (own_buf); 2659 } 2660 2661 target_unpause_all (true); 2662 return; 2663 } 2664 2665 write_ok (own_buf); 2666 } 2667 2668 static void 2669 cmd_qtdpsrc (char *own_buf) 2670 { 2671 ULONGEST num, addr, start, slen; 2672 struct tracepoint *tpoint; 2673 const char *packet = own_buf; 2674 const char *saved; 2675 char *srctype, *src; 2676 size_t nbytes; 2677 struct source_string *last, *newlast; 2678 2679 packet += strlen ("QTDPsrc:"); 2680 2681 packet = unpack_varlen_hex (packet, &num); 2682 ++packet; /* skip a colon */ 2683 packet = unpack_varlen_hex (packet, &addr); 2684 ++packet; /* skip a colon */ 2685 2686 /* See if we already have this tracepoint. */ 2687 tpoint = find_tracepoint (num, addr); 2688 2689 if (!tpoint) 2690 { 2691 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found", 2692 (int) num, paddress (addr)); 2693 write_enn (own_buf); 2694 return; 2695 } 2696 2697 saved = packet; 2698 packet = strchr (packet, ':'); 2699 srctype = (char *) xmalloc (packet - saved + 1); 2700 memcpy (srctype, saved, packet - saved); 2701 srctype[packet - saved] = '\0'; 2702 ++packet; 2703 packet = unpack_varlen_hex (packet, &start); 2704 ++packet; /* skip a colon */ 2705 packet = unpack_varlen_hex (packet, &slen); 2706 ++packet; /* skip a colon */ 2707 src = (char *) xmalloc (slen + 1); 2708 nbytes = hex2bin (packet, (gdb_byte *) src, strlen (packet) / 2); 2709 src[nbytes] = '\0'; 2710 2711 newlast = XNEW (struct source_string); 2712 newlast->type = srctype; 2713 newlast->str = src; 2714 newlast->next = NULL; 2715 /* Always add a source string to the end of the list; 2716 this keeps sequences of actions/commands in the right 2717 order. */ 2718 if (tpoint->source_strings) 2719 { 2720 for (last = tpoint->source_strings; last->next; last = last->next) 2721 ; 2722 last->next = newlast; 2723 } 2724 else 2725 tpoint->source_strings = newlast; 2726 2727 write_ok (own_buf); 2728 } 2729 2730 static void 2731 cmd_qtdv (char *own_buf) 2732 { 2733 ULONGEST num, val, builtin; 2734 char *varname; 2735 size_t nbytes; 2736 struct trace_state_variable *tsv; 2737 const char *packet = own_buf; 2738 2739 packet += strlen ("QTDV:"); 2740 2741 packet = unpack_varlen_hex (packet, &num); 2742 ++packet; /* skip a colon */ 2743 packet = unpack_varlen_hex (packet, &val); 2744 ++packet; /* skip a colon */ 2745 packet = unpack_varlen_hex (packet, &builtin); 2746 ++packet; /* skip a colon */ 2747 2748 nbytes = strlen (packet) / 2; 2749 varname = (char *) xmalloc (nbytes + 1); 2750 nbytes = hex2bin (packet, (gdb_byte *) varname, nbytes); 2751 varname[nbytes] = '\0'; 2752 2753 tsv = create_trace_state_variable (num, 1); 2754 tsv->initial_value = (LONGEST) val; 2755 tsv->name = varname; 2756 2757 set_trace_state_variable_value (num, (LONGEST) val); 2758 2759 write_ok (own_buf); 2760 } 2761 2762 static void 2763 cmd_qtenable_disable (char *own_buf, int enable) 2764 { 2765 const char *packet = own_buf; 2766 ULONGEST num, addr; 2767 struct tracepoint *tp; 2768 2769 packet += strlen (enable ? "QTEnable:" : "QTDisable:"); 2770 packet = unpack_varlen_hex (packet, &num); 2771 ++packet; /* skip a colon */ 2772 packet = unpack_varlen_hex (packet, &addr); 2773 2774 tp = find_tracepoint (num, addr); 2775 2776 if (tp) 2777 { 2778 if ((enable && tp->enabled) || (!enable && !tp->enabled)) 2779 { 2780 trace_debug ("Tracepoint %d at 0x%s is already %s", 2781 (int) num, paddress (addr), 2782 enable ? "enabled" : "disabled"); 2783 write_ok (own_buf); 2784 return; 2785 } 2786 2787 trace_debug ("%s tracepoint %d at 0x%s", 2788 enable ? "Enabling" : "Disabling", 2789 (int) num, paddress (addr)); 2790 2791 tp->enabled = enable; 2792 2793 if (tp->type == fast_tracepoint || tp->type == static_tracepoint) 2794 { 2795 int ret; 2796 int offset = offsetof (struct tracepoint, enabled); 2797 CORE_ADDR obj_addr = tp->obj_addr_on_target + offset; 2798 2799 ret = prepare_to_access_memory (); 2800 if (ret) 2801 { 2802 trace_debug ("Failed to temporarily stop inferior threads"); 2803 write_enn (own_buf); 2804 return; 2805 } 2806 2807 ret = write_inferior_int8 (obj_addr, enable); 2808 done_accessing_memory (); 2809 2810 if (ret) 2811 { 2812 trace_debug ("Cannot write enabled flag into " 2813 "inferior process memory"); 2814 write_enn (own_buf); 2815 return; 2816 } 2817 } 2818 2819 write_ok (own_buf); 2820 } 2821 else 2822 { 2823 trace_debug ("Tracepoint %d at 0x%s not found", 2824 (int) num, paddress (addr)); 2825 write_enn (own_buf); 2826 } 2827 } 2828 2829 static void 2830 cmd_qtv (char *own_buf) 2831 { 2832 client_state &cs = get_client_state (); 2833 ULONGEST num; 2834 LONGEST val = 0; 2835 int err; 2836 char *packet = own_buf; 2837 2838 packet += strlen ("qTV:"); 2839 unpack_varlen_hex (packet, &num); 2840 2841 if (cs.current_traceframe >= 0) 2842 { 2843 err = traceframe_read_tsv ((int) num, &val); 2844 if (err) 2845 { 2846 strcpy (own_buf, "U"); 2847 return; 2848 } 2849 } 2850 /* Only make tsv's be undefined before the first trace run. After a 2851 trace run is over, the user might want to see the last value of 2852 the tsv, and it might not be available in a traceframe. */ 2853 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0) 2854 { 2855 strcpy (own_buf, "U"); 2856 return; 2857 } 2858 else 2859 val = get_trace_state_variable_value (num); 2860 2861 sprintf (own_buf, "V%s", phex_nz (val, 0)); 2862 } 2863 2864 /* Clear out the list of readonly regions. */ 2865 2866 static void 2867 clear_readonly_regions (void) 2868 { 2869 struct readonly_region *roreg; 2870 2871 while (readonly_regions) 2872 { 2873 roreg = readonly_regions; 2874 readonly_regions = readonly_regions->next; 2875 free (roreg); 2876 } 2877 } 2878 2879 /* Parse the collection of address ranges whose contents GDB believes 2880 to be unchanging and so can be read directly from target memory 2881 even while looking at a traceframe. */ 2882 2883 static void 2884 cmd_qtro (char *own_buf) 2885 { 2886 ULONGEST start, end; 2887 struct readonly_region *roreg; 2888 const char *packet = own_buf; 2889 2890 trace_debug ("Want to mark readonly regions"); 2891 2892 clear_readonly_regions (); 2893 2894 packet += strlen ("QTro"); 2895 2896 while (*packet == ':') 2897 { 2898 ++packet; /* skip a colon */ 2899 packet = unpack_varlen_hex (packet, &start); 2900 ++packet; /* skip a comma */ 2901 packet = unpack_varlen_hex (packet, &end); 2902 2903 roreg = XNEW (struct readonly_region); 2904 roreg->start = start; 2905 roreg->end = end; 2906 roreg->next = readonly_regions; 2907 readonly_regions = roreg; 2908 trace_debug ("Added readonly region from 0x%s to 0x%s", 2909 paddress (roreg->start), paddress (roreg->end)); 2910 } 2911 2912 write_ok (own_buf); 2913 } 2914 2915 /* Test to see if the given range is in our list of readonly ranges. 2916 We only test for being entirely within a range, GDB is not going to 2917 send a single memory packet that spans multiple regions. */ 2918 2919 int 2920 in_readonly_region (CORE_ADDR addr, ULONGEST length) 2921 { 2922 struct readonly_region *roreg; 2923 2924 for (roreg = readonly_regions; roreg; roreg = roreg->next) 2925 if (roreg->start <= addr && (addr + length - 1) <= roreg->end) 2926 return 1; 2927 2928 return 0; 2929 } 2930 2931 static CORE_ADDR gdb_jump_pad_head; 2932 2933 /* Return the address of the next free jump space. */ 2934 2935 static CORE_ADDR 2936 get_jump_space_head (void) 2937 { 2938 if (gdb_jump_pad_head == 0) 2939 { 2940 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer, 2941 &gdb_jump_pad_head)) 2942 { 2943 internal_error (__FILE__, __LINE__, 2944 "error extracting jump_pad_buffer"); 2945 } 2946 } 2947 2948 return gdb_jump_pad_head; 2949 } 2950 2951 /* Reserve USED bytes from the jump space. */ 2952 2953 static void 2954 claim_jump_space (ULONGEST used) 2955 { 2956 trace_debug ("claim_jump_space reserves %s bytes at %s", 2957 pulongest (used), paddress (gdb_jump_pad_head)); 2958 gdb_jump_pad_head += used; 2959 } 2960 2961 static CORE_ADDR trampoline_buffer_head = 0; 2962 static CORE_ADDR trampoline_buffer_tail; 2963 2964 /* Reserve USED bytes from the trampoline buffer and return the 2965 address of the start of the reserved space in TRAMPOLINE. Returns 2966 non-zero if the space is successfully claimed. */ 2967 2968 int 2969 claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline) 2970 { 2971 if (!trampoline_buffer_head) 2972 { 2973 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer, 2974 &trampoline_buffer_tail)) 2975 { 2976 internal_error (__FILE__, __LINE__, 2977 "error extracting trampoline_buffer"); 2978 } 2979 2980 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end, 2981 &trampoline_buffer_head)) 2982 { 2983 internal_error (__FILE__, __LINE__, 2984 "error extracting trampoline_buffer_end"); 2985 } 2986 } 2987 2988 /* Start claiming space from the top of the trampoline space. If 2989 the space is located at the bottom of the virtual address space, 2990 this reduces the possibility that corruption will occur if a null 2991 pointer is used to write to memory. */ 2992 if (trampoline_buffer_head - trampoline_buffer_tail < used) 2993 { 2994 trace_debug ("claim_trampoline_space failed to reserve %s bytes", 2995 pulongest (used)); 2996 return 0; 2997 } 2998 2999 trampoline_buffer_head -= used; 3000 3001 trace_debug ("claim_trampoline_space reserves %s bytes at %s", 3002 pulongest (used), paddress (trampoline_buffer_head)); 3003 3004 *trampoline = trampoline_buffer_head; 3005 return 1; 3006 } 3007 3008 /* Returns non-zero if there is space allocated for use in trampolines 3009 for fast tracepoints. */ 3010 3011 int 3012 have_fast_tracepoint_trampoline_buffer (char *buf) 3013 { 3014 CORE_ADDR trampoline_end, errbuf; 3015 3016 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end, 3017 &trampoline_end)) 3018 { 3019 internal_error (__FILE__, __LINE__, 3020 "error extracting trampoline_buffer_end"); 3021 } 3022 3023 if (buf) 3024 { 3025 buf[0] = '\0'; 3026 strcpy (buf, "was claiming"); 3027 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error, 3028 &errbuf)) 3029 { 3030 internal_error (__FILE__, __LINE__, 3031 "error extracting errbuf"); 3032 } 3033 3034 read_inferior_memory (errbuf, (unsigned char *) buf, 100); 3035 } 3036 3037 return trampoline_end != 0; 3038 } 3039 3040 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running 3041 the command fails, or 0 otherwise. If the command ran 3042 successfully, but probing the marker failed, ERROUT will be filled 3043 with the error to reply to GDB, and -1 is also returned. This 3044 allows directly passing IPA errors to GDB. */ 3045 3046 static int 3047 probe_marker_at (CORE_ADDR address, char *errout) 3048 { 3049 char cmd[IPA_CMD_BUF_SIZE]; 3050 int err; 3051 3052 sprintf (cmd, "probe_marker_at:%s", paddress (address)); 3053 err = run_inferior_command (cmd, strlen (cmd) + 1); 3054 3055 if (err == 0) 3056 { 3057 if (*cmd == 'E') 3058 { 3059 strcpy (errout, cmd); 3060 return -1; 3061 } 3062 } 3063 3064 return err; 3065 } 3066 3067 static void 3068 clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from) 3069 { 3070 to->jump_pad = from->jump_pad; 3071 to->jump_pad_end = from->jump_pad_end; 3072 to->trampoline = from->trampoline; 3073 to->trampoline_end = from->trampoline_end; 3074 to->adjusted_insn_addr = from->adjusted_insn_addr; 3075 to->adjusted_insn_addr_end = from->adjusted_insn_addr_end; 3076 to->handle = from->handle; 3077 3078 gdb_assert (from->handle); 3079 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle); 3080 } 3081 3082 #define MAX_JUMP_SIZE 20 3083 3084 /* Install fast tracepoint. Return 0 if successful, otherwise return 3085 non-zero. */ 3086 3087 static int 3088 install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf) 3089 { 3090 CORE_ADDR jentry, jump_entry; 3091 CORE_ADDR trampoline; 3092 CORE_ADDR collect; 3093 ULONGEST trampoline_size; 3094 int err = 0; 3095 /* The jump to the jump pad of the last fast tracepoint 3096 installed. */ 3097 unsigned char fjump[MAX_JUMP_SIZE]; 3098 ULONGEST fjump_size; 3099 3100 if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ()) 3101 { 3102 trace_debug ("Requested a fast tracepoint on an instruction " 3103 "that is of less than the minimum length."); 3104 return 0; 3105 } 3106 3107 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_collect_ptr, 3108 &collect)) 3109 { 3110 error ("error extracting gdb_collect_ptr"); 3111 return 1; 3112 } 3113 3114 jentry = jump_entry = get_jump_space_head (); 3115 3116 trampoline = 0; 3117 trampoline_size = 0; 3118 3119 /* Install the jump pad. */ 3120 err = target_install_fast_tracepoint_jump_pad 3121 (tpoint->obj_addr_on_target, tpoint->address, collect, 3122 ipa_sym_addrs.addr_collecting, tpoint->orig_size, &jentry, 3123 &trampoline, &trampoline_size, fjump, &fjump_size, 3124 &tpoint->adjusted_insn_addr, &tpoint->adjusted_insn_addr_end, errbuf); 3125 3126 if (err) 3127 return 1; 3128 3129 /* Wire it in. */ 3130 tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump, 3131 fjump_size); 3132 3133 if (tpoint->handle != NULL) 3134 { 3135 tpoint->jump_pad = jump_entry; 3136 tpoint->jump_pad_end = jentry; 3137 tpoint->trampoline = trampoline; 3138 tpoint->trampoline_end = trampoline + trampoline_size; 3139 3140 /* Pad to 8-byte alignment. */ 3141 jentry = ((jentry + 7) & ~0x7); 3142 claim_jump_space (jentry - jump_entry); 3143 } 3144 3145 return 0; 3146 } 3147 3148 3149 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */ 3150 3151 static void 3152 install_tracepoint (struct tracepoint *tpoint, char *own_buf) 3153 { 3154 tpoint->handle = NULL; 3155 *own_buf = '\0'; 3156 3157 if (tpoint->type == trap_tracepoint) 3158 { 3159 /* Tracepoints are installed as memory breakpoints. Just go 3160 ahead and install the trap. The breakpoints module 3161 handles duplicated breakpoints, and the memory read 3162 routine handles un-patching traps from memory reads. */ 3163 tpoint->handle = set_breakpoint_at (tpoint->address, 3164 tracepoint_handler); 3165 } 3166 else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint) 3167 { 3168 if (!agent_loaded_p ()) 3169 { 3170 trace_debug ("Requested a %s tracepoint, but fast " 3171 "tracepoints aren't supported.", 3172 tpoint->type == static_tracepoint ? "static" : "fast"); 3173 write_e_ipa_not_loaded (own_buf); 3174 return; 3175 } 3176 if (tpoint->type == static_tracepoint 3177 && !in_process_agent_supports_ust ()) 3178 { 3179 trace_debug ("Requested a static tracepoint, but static " 3180 "tracepoints are not supported."); 3181 write_e_ust_not_loaded (own_buf); 3182 return; 3183 } 3184 3185 if (tpoint->type == fast_tracepoint) 3186 install_fast_tracepoint (tpoint, own_buf); 3187 else 3188 { 3189 if (probe_marker_at (tpoint->address, own_buf) == 0) 3190 tpoint->handle = (void *) -1; 3191 } 3192 3193 } 3194 else 3195 internal_error (__FILE__, __LINE__, "Unknown tracepoint type"); 3196 3197 if (tpoint->handle == NULL) 3198 { 3199 if (*own_buf == '\0') 3200 write_enn (own_buf); 3201 } 3202 else 3203 write_ok (own_buf); 3204 } 3205 3206 static void download_tracepoint_1 (struct tracepoint *tpoint); 3207 3208 static void 3209 cmd_qtstart (char *packet) 3210 { 3211 struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint; 3212 CORE_ADDR tpptr = 0, prev_tpptr = 0; 3213 3214 trace_debug ("Starting the trace"); 3215 3216 /* Pause all threads temporarily while we patch tracepoints. */ 3217 target_pause_all (false); 3218 3219 /* Get threads out of jump pads. Safe to do here, since this is a 3220 top level command. And, required to do here, since we're 3221 deleting/rewriting jump pads. */ 3222 3223 target_stabilize_threads (); 3224 3225 /* Freeze threads. */ 3226 target_pause_all (true); 3227 3228 /* Sync the fast tracepoints list in the inferior ftlib. */ 3229 if (agent_loaded_p ()) 3230 download_trace_state_variables (); 3231 3232 /* No previous fast tpoint yet. */ 3233 prev_ftpoint = NULL; 3234 3235 /* No previous static tpoint yet. */ 3236 prev_stpoint = NULL; 3237 3238 *packet = '\0'; 3239 3240 if (agent_loaded_p ()) 3241 { 3242 /* Tell IPA about the correct tdesc. */ 3243 if (write_inferior_integer (ipa_sym_addrs.addr_ipa_tdesc_idx, 3244 target_get_ipa_tdesc_idx ())) 3245 error ("Error setting ipa_tdesc_idx variable in lib"); 3246 } 3247 3248 /* Start out empty. */ 3249 if (agent_loaded_p ()) 3250 write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints, 0); 3251 3252 /* Download and install tracepoints. */ 3253 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) 3254 { 3255 /* Ensure all the hit counts start at zero. */ 3256 tpoint->hit_count = 0; 3257 tpoint->traceframe_usage = 0; 3258 3259 if (tpoint->type == trap_tracepoint) 3260 { 3261 /* Tracepoints are installed as memory breakpoints. Just go 3262 ahead and install the trap. The breakpoints module 3263 handles duplicated breakpoints, and the memory read 3264 routine handles un-patching traps from memory reads. */ 3265 tpoint->handle = set_breakpoint_at (tpoint->address, 3266 tracepoint_handler); 3267 } 3268 else if (tpoint->type == fast_tracepoint 3269 || tpoint->type == static_tracepoint) 3270 { 3271 if (maybe_write_ipa_not_loaded (packet)) 3272 { 3273 trace_debug ("Requested a %s tracepoint, but fast " 3274 "tracepoints aren't supported.", 3275 tpoint->type == static_tracepoint 3276 ? "static" : "fast"); 3277 break; 3278 } 3279 3280 if (tpoint->type == fast_tracepoint) 3281 { 3282 int use_agent_p 3283 = use_agent && agent_capability_check (AGENT_CAPA_FAST_TRACE); 3284 3285 if (prev_ftpoint != NULL 3286 && prev_ftpoint->address == tpoint->address) 3287 { 3288 if (use_agent_p) 3289 tracepoint_send_agent (tpoint); 3290 else 3291 download_tracepoint_1 (tpoint); 3292 3293 clone_fast_tracepoint (tpoint, prev_ftpoint); 3294 } 3295 else 3296 { 3297 /* Tracepoint is installed successfully? */ 3298 int installed = 0; 3299 3300 /* Download and install fast tracepoint by agent. */ 3301 if (use_agent_p) 3302 installed = !tracepoint_send_agent (tpoint); 3303 else 3304 { 3305 download_tracepoint_1 (tpoint); 3306 installed = !install_fast_tracepoint (tpoint, packet); 3307 } 3308 3309 if (installed) 3310 prev_ftpoint = tpoint; 3311 } 3312 } 3313 else 3314 { 3315 if (!in_process_agent_supports_ust ()) 3316 { 3317 trace_debug ("Requested a static tracepoint, but static " 3318 "tracepoints are not supported."); 3319 break; 3320 } 3321 3322 download_tracepoint_1 (tpoint); 3323 /* Can only probe a given marker once. */ 3324 if (prev_stpoint != NULL 3325 && prev_stpoint->address == tpoint->address) 3326 tpoint->handle = (void *) -1; 3327 else 3328 { 3329 if (probe_marker_at (tpoint->address, packet) == 0) 3330 { 3331 tpoint->handle = (void *) -1; 3332 3333 /* So that we can handle multiple static tracepoints 3334 at the same address easily. */ 3335 prev_stpoint = tpoint; 3336 } 3337 } 3338 } 3339 3340 prev_tpptr = tpptr; 3341 tpptr = tpoint->obj_addr_on_target; 3342 3343 if (tpoint == tracepoints) 3344 /* First object in list, set the head pointer in the 3345 inferior. */ 3346 write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints, tpptr); 3347 else 3348 write_inferior_data_pointer (prev_tpptr 3349 + offsetof (struct tracepoint, next), 3350 tpptr); 3351 } 3352 3353 /* Any failure in the inner loop is sufficient cause to give 3354 up. */ 3355 if (tpoint->handle == NULL) 3356 break; 3357 } 3358 3359 /* Any error in tracepoint insertion is unacceptable; better to 3360 address the problem now, than end up with a useless or misleading 3361 trace run. */ 3362 if (tpoint != NULL) 3363 { 3364 clear_installed_tracepoints (); 3365 if (*packet == '\0') 3366 write_enn (packet); 3367 target_unpause_all (true); 3368 return; 3369 } 3370 3371 stopping_tracepoint = NULL; 3372 trace_buffer_is_full = 0; 3373 expr_eval_result = expr_eval_no_error; 3374 error_tracepoint = NULL; 3375 tracing_start_time = get_timestamp (); 3376 3377 /* Tracing is now active, hits will now start being logged. */ 3378 tracing = 1; 3379 3380 if (agent_loaded_p ()) 3381 { 3382 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1)) 3383 { 3384 internal_error (__FILE__, __LINE__, 3385 "Error setting tracing variable in lib"); 3386 } 3387 3388 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 3389 0)) 3390 { 3391 internal_error (__FILE__, __LINE__, 3392 "Error clearing stopping_tracepoint variable" 3393 " in lib"); 3394 } 3395 3396 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0)) 3397 { 3398 internal_error (__FILE__, __LINE__, 3399 "Error clearing trace_buffer_is_full variable" 3400 " in lib"); 3401 } 3402 3403 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing, 3404 stop_tracing_handler); 3405 if (stop_tracing_bkpt == NULL) 3406 error ("Error setting stop_tracing breakpoint"); 3407 3408 flush_trace_buffer_bkpt 3409 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer, 3410 flush_trace_buffer_handler); 3411 if (flush_trace_buffer_bkpt == NULL) 3412 error ("Error setting flush_trace_buffer breakpoint"); 3413 } 3414 3415 target_unpause_all (true); 3416 3417 write_ok (packet); 3418 } 3419 3420 /* End a tracing run, filling in a stop reason to report back to GDB, 3421 and removing the tracepoints from the code. */ 3422 3423 void 3424 stop_tracing (void) 3425 { 3426 if (!tracing) 3427 { 3428 trace_debug ("Tracing is already off, ignoring"); 3429 return; 3430 } 3431 3432 trace_debug ("Stopping the trace"); 3433 3434 /* Pause all threads before removing fast jumps from memory, 3435 breakpoints, and touching IPA state variables (inferior memory). 3436 Some thread may hit the internal tracing breakpoints, or be 3437 collecting this moment, but that's ok, we don't release the 3438 tpoint object's memory or the jump pads here (we only do that 3439 when we're sure we can move all threads out of the jump pads). 3440 We can't now, since we may be getting here due to the inferior 3441 agent calling us. */ 3442 target_pause_all (true); 3443 3444 /* Stop logging. Tracepoints can still be hit, but they will not be 3445 recorded. */ 3446 tracing = 0; 3447 if (agent_loaded_p ()) 3448 { 3449 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0)) 3450 { 3451 internal_error (__FILE__, __LINE__, 3452 "Error clearing tracing variable in lib"); 3453 } 3454 } 3455 3456 tracing_stop_time = get_timestamp (); 3457 tracing_stop_reason = "t???"; 3458 tracing_stop_tpnum = 0; 3459 if (stopping_tracepoint) 3460 { 3461 trace_debug ("Stopping the trace because " 3462 "tracepoint %d was hit %" PRIu64 " times", 3463 stopping_tracepoint->number, 3464 stopping_tracepoint->pass_count); 3465 tracing_stop_reason = "tpasscount"; 3466 tracing_stop_tpnum = stopping_tracepoint->number; 3467 } 3468 else if (trace_buffer_is_full) 3469 { 3470 trace_debug ("Stopping the trace because the trace buffer is full"); 3471 tracing_stop_reason = "tfull"; 3472 } 3473 else if (expr_eval_result != expr_eval_no_error) 3474 { 3475 trace_debug ("Stopping the trace because of an expression eval error"); 3476 tracing_stop_reason = eval_result_names[expr_eval_result]; 3477 tracing_stop_tpnum = error_tracepoint->number; 3478 } 3479 #ifndef IN_PROCESS_AGENT 3480 else if (!gdb_connected ()) 3481 { 3482 trace_debug ("Stopping the trace because GDB disconnected"); 3483 tracing_stop_reason = "tdisconnected"; 3484 } 3485 #endif 3486 else 3487 { 3488 trace_debug ("Stopping the trace because of a tstop command"); 3489 tracing_stop_reason = "tstop"; 3490 } 3491 3492 stopping_tracepoint = NULL; 3493 error_tracepoint = NULL; 3494 3495 /* Clear out the tracepoints. */ 3496 clear_installed_tracepoints (); 3497 3498 if (agent_loaded_p ()) 3499 { 3500 /* Pull in fast tracepoint trace frames from the inferior lib 3501 buffer into our buffer, even if our buffer is already full, 3502 because we want to present the full number of created frames 3503 in addition to what fit in the trace buffer. */ 3504 upload_fast_traceframes (); 3505 } 3506 3507 if (stop_tracing_bkpt != NULL) 3508 { 3509 delete_breakpoint (stop_tracing_bkpt); 3510 stop_tracing_bkpt = NULL; 3511 } 3512 3513 if (flush_trace_buffer_bkpt != NULL) 3514 { 3515 delete_breakpoint (flush_trace_buffer_bkpt); 3516 flush_trace_buffer_bkpt = NULL; 3517 } 3518 3519 target_unpause_all (true); 3520 } 3521 3522 static int 3523 stop_tracing_handler (CORE_ADDR addr) 3524 { 3525 trace_debug ("lib hit stop_tracing"); 3526 3527 /* Don't actually handle it here. When we stop tracing we remove 3528 breakpoints from the inferior, and that is not allowed in a 3529 breakpoint handler (as the caller is walking the breakpoint 3530 list). */ 3531 return 0; 3532 } 3533 3534 static int 3535 flush_trace_buffer_handler (CORE_ADDR addr) 3536 { 3537 trace_debug ("lib hit flush_trace_buffer"); 3538 return 0; 3539 } 3540 3541 static void 3542 cmd_qtstop (char *packet) 3543 { 3544 stop_tracing (); 3545 write_ok (packet); 3546 } 3547 3548 static void 3549 cmd_qtdisconnected (char *own_buf) 3550 { 3551 ULONGEST setting; 3552 char *packet = own_buf; 3553 3554 packet += strlen ("QTDisconnected:"); 3555 3556 unpack_varlen_hex (packet, &setting); 3557 3558 write_ok (own_buf); 3559 3560 disconnected_tracing = setting; 3561 } 3562 3563 static void 3564 cmd_qtframe (char *own_buf) 3565 { 3566 client_state &cs = get_client_state (); 3567 ULONGEST frame, pc, lo, hi, num; 3568 int tfnum, tpnum; 3569 struct traceframe *tframe; 3570 const char *packet = own_buf; 3571 3572 packet += strlen ("QTFrame:"); 3573 3574 if (startswith (packet, "pc:")) 3575 { 3576 packet += strlen ("pc:"); 3577 unpack_varlen_hex (packet, &pc); 3578 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc)); 3579 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum); 3580 } 3581 else if (startswith (packet, "range:")) 3582 { 3583 packet += strlen ("range:"); 3584 packet = unpack_varlen_hex (packet, &lo); 3585 ++packet; 3586 unpack_varlen_hex (packet, &hi); 3587 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s", 3588 paddress (lo), paddress (hi)); 3589 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum); 3590 } 3591 else if (startswith (packet, "outside:")) 3592 { 3593 packet += strlen ("outside:"); 3594 packet = unpack_varlen_hex (packet, &lo); 3595 ++packet; 3596 unpack_varlen_hex (packet, &hi); 3597 trace_debug ("Want to find next traceframe " 3598 "outside the range 0x%s to 0x%s", 3599 paddress (lo), paddress (hi)); 3600 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum); 3601 } 3602 else if (startswith (packet, "tdp:")) 3603 { 3604 packet += strlen ("tdp:"); 3605 unpack_varlen_hex (packet, &num); 3606 tpnum = (int) num; 3607 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum); 3608 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum); 3609 } 3610 else 3611 { 3612 unpack_varlen_hex (packet, &frame); 3613 tfnum = (int) frame; 3614 if (tfnum == -1) 3615 { 3616 trace_debug ("Want to stop looking at traceframes"); 3617 cs.current_traceframe = -1; 3618 write_ok (own_buf); 3619 return; 3620 } 3621 trace_debug ("Want to look at traceframe %d", tfnum); 3622 tframe = find_traceframe (tfnum); 3623 } 3624 3625 if (tframe) 3626 { 3627 cs.current_traceframe = tfnum; 3628 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum); 3629 } 3630 else 3631 sprintf (own_buf, "F-1"); 3632 } 3633 3634 static void 3635 cmd_qtstatus (char *packet) 3636 { 3637 char *stop_reason_rsp = NULL; 3638 char *buf1, *buf2, *buf3; 3639 const char *str; 3640 int slen; 3641 3642 /* Translate the plain text of the notes back into hex for 3643 transmission. */ 3644 3645 str = (tracing_user_name ? tracing_user_name : ""); 3646 slen = strlen (str); 3647 buf1 = (char *) alloca (slen * 2 + 1); 3648 bin2hex ((gdb_byte *) str, buf1, slen); 3649 3650 str = (tracing_notes ? tracing_notes : ""); 3651 slen = strlen (str); 3652 buf2 = (char *) alloca (slen * 2 + 1); 3653 bin2hex ((gdb_byte *) str, buf2, slen); 3654 3655 str = (tracing_stop_note ? tracing_stop_note : ""); 3656 slen = strlen (str); 3657 buf3 = (char *) alloca (slen * 2 + 1); 3658 bin2hex ((gdb_byte *) str, buf3, slen); 3659 3660 trace_debug ("Returning trace status as %d, stop reason %s", 3661 tracing, tracing_stop_reason); 3662 3663 if (agent_loaded_p ()) 3664 { 3665 target_pause_all (true); 3666 3667 upload_fast_traceframes (); 3668 3669 target_unpause_all (true); 3670 } 3671 3672 stop_reason_rsp = (char *) tracing_stop_reason; 3673 3674 /* The user visible error string in terror needs to be hex encoded. 3675 We leave it as plain string in `tracing_stop_reason' to ease 3676 debugging. */ 3677 if (startswith (stop_reason_rsp, "terror:")) 3678 { 3679 const char *result_name; 3680 int hexstr_len; 3681 char *p; 3682 3683 result_name = stop_reason_rsp + strlen ("terror:"); 3684 hexstr_len = strlen (result_name) * 2; 3685 p = stop_reason_rsp 3686 = (char *) alloca (strlen ("terror:") + hexstr_len + 1); 3687 strcpy (p, "terror:"); 3688 p += strlen (p); 3689 bin2hex ((gdb_byte *) result_name, p, strlen (result_name)); 3690 } 3691 3692 /* If this was a forced stop, include any stop note that was supplied. */ 3693 if (strcmp (stop_reason_rsp, "tstop") == 0) 3694 { 3695 stop_reason_rsp = (char *) alloca (strlen ("tstop:") + strlen (buf3) + 1); 3696 strcpy (stop_reason_rsp, "tstop:"); 3697 strcat (stop_reason_rsp, buf3); 3698 } 3699 3700 sprintf (packet, 3701 "T%d;" 3702 "%s:%x;" 3703 "tframes:%x;tcreated:%x;" 3704 "tfree:%x;tsize:%s;" 3705 "circular:%d;" 3706 "disconn:%d;" 3707 "starttime:%s;stoptime:%s;" 3708 "username:%s;notes:%s:", 3709 tracing ? 1 : 0, 3710 stop_reason_rsp, tracing_stop_tpnum, 3711 traceframe_count, traceframes_created, 3712 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0), 3713 circular_trace_buffer, 3714 disconnected_tracing, 3715 phex_nz (tracing_start_time, sizeof (tracing_start_time)), 3716 phex_nz (tracing_stop_time, sizeof (tracing_stop_time)), 3717 buf1, buf2); 3718 } 3719 3720 static void 3721 cmd_qtp (char *own_buf) 3722 { 3723 ULONGEST num, addr; 3724 struct tracepoint *tpoint; 3725 const char *packet = own_buf; 3726 3727 packet += strlen ("qTP:"); 3728 3729 packet = unpack_varlen_hex (packet, &num); 3730 ++packet; /* skip a colon */ 3731 packet = unpack_varlen_hex (packet, &addr); 3732 3733 /* See if we already have this tracepoint. */ 3734 tpoint = find_tracepoint (num, addr); 3735 3736 if (!tpoint) 3737 { 3738 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found", 3739 (int) num, paddress (addr)); 3740 write_enn (own_buf); 3741 return; 3742 } 3743 3744 sprintf (own_buf, "V%" PRIu64 ":%" PRIu64 "", tpoint->hit_count, 3745 tpoint->traceframe_usage); 3746 } 3747 3748 /* State variables to help return all the tracepoint bits. */ 3749 static struct tracepoint *cur_tpoint; 3750 static unsigned int cur_action; 3751 static unsigned int cur_step_action; 3752 static struct source_string *cur_source_string; 3753 static struct trace_state_variable *cur_tsv; 3754 3755 /* Compose a response that is an imitation of the syntax by which the 3756 tracepoint was originally downloaded. */ 3757 3758 static void 3759 response_tracepoint (char *packet, struct tracepoint *tpoint) 3760 { 3761 char *buf; 3762 3763 sprintf (packet, "T%x:%s:%c:%" PRIx64 ":%" PRIx64, tpoint->number, 3764 paddress (tpoint->address), 3765 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count, 3766 tpoint->pass_count); 3767 if (tpoint->type == fast_tracepoint) 3768 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size); 3769 else if (tpoint->type == static_tracepoint) 3770 sprintf (packet + strlen (packet), ":S"); 3771 3772 if (tpoint->cond) 3773 { 3774 buf = gdb_unparse_agent_expr (tpoint->cond); 3775 sprintf (packet + strlen (packet), ":X%x,%s", 3776 tpoint->cond->length, buf); 3777 free (buf); 3778 } 3779 } 3780 3781 /* Compose a response that is an imitation of the syntax by which the 3782 tracepoint action was originally downloaded (with the difference 3783 that due to the way we store the actions, this will output a packet 3784 per action, while GDB could have combined more than one action 3785 per-packet. */ 3786 3787 static void 3788 response_action (char *packet, struct tracepoint *tpoint, 3789 char *taction, int step) 3790 { 3791 sprintf (packet, "%c%x:%s:%s", 3792 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address), 3793 taction); 3794 } 3795 3796 /* Compose a response that is an imitation of the syntax by which the 3797 tracepoint source piece was originally downloaded. */ 3798 3799 static void 3800 response_source (char *packet, 3801 struct tracepoint *tpoint, struct source_string *src) 3802 { 3803 char *buf; 3804 int len; 3805 3806 len = strlen (src->str); 3807 buf = (char *) alloca (len * 2 + 1); 3808 bin2hex ((gdb_byte *) src->str, buf, len); 3809 3810 sprintf (packet, "Z%x:%s:%s:%x:%x:%s", 3811 tpoint->number, paddress (tpoint->address), 3812 src->type, 0, len, buf); 3813 } 3814 3815 /* Return the first piece of tracepoint definition, and initialize the 3816 state machine that will iterate through all the tracepoint 3817 bits. */ 3818 3819 static void 3820 cmd_qtfp (char *packet) 3821 { 3822 trace_debug ("Returning first tracepoint definition piece"); 3823 3824 cur_tpoint = tracepoints; 3825 cur_action = cur_step_action = 0; 3826 cur_source_string = NULL; 3827 3828 if (cur_tpoint) 3829 response_tracepoint (packet, cur_tpoint); 3830 else 3831 strcpy (packet, "l"); 3832 } 3833 3834 /* Return additional pieces of tracepoint definition. Each action and 3835 stepping action must go into its own packet, because of packet size 3836 limits, and so we use state variables to deliver one piece at a 3837 time. */ 3838 3839 static void 3840 cmd_qtsp (char *packet) 3841 { 3842 trace_debug ("Returning subsequent tracepoint definition piece"); 3843 3844 if (!cur_tpoint) 3845 { 3846 /* This case would normally never occur, but be prepared for 3847 GDB misbehavior. */ 3848 strcpy (packet, "l"); 3849 } 3850 else if (cur_action < cur_tpoint->numactions) 3851 { 3852 response_action (packet, cur_tpoint, 3853 cur_tpoint->actions_str[cur_action], 0); 3854 ++cur_action; 3855 } 3856 else if (cur_step_action < cur_tpoint->num_step_actions) 3857 { 3858 response_action (packet, cur_tpoint, 3859 cur_tpoint->step_actions_str[cur_step_action], 1); 3860 ++cur_step_action; 3861 } 3862 else if ((cur_source_string 3863 ? cur_source_string->next 3864 : cur_tpoint->source_strings)) 3865 { 3866 if (cur_source_string) 3867 cur_source_string = cur_source_string->next; 3868 else 3869 cur_source_string = cur_tpoint->source_strings; 3870 response_source (packet, cur_tpoint, cur_source_string); 3871 } 3872 else 3873 { 3874 cur_tpoint = cur_tpoint->next; 3875 cur_action = cur_step_action = 0; 3876 cur_source_string = NULL; 3877 if (cur_tpoint) 3878 response_tracepoint (packet, cur_tpoint); 3879 else 3880 strcpy (packet, "l"); 3881 } 3882 } 3883 3884 /* Compose a response that is an imitation of the syntax by which the 3885 trace state variable was originally downloaded. */ 3886 3887 static void 3888 response_tsv (char *packet, struct trace_state_variable *tsv) 3889 { 3890 char *buf = (char *) ""; 3891 int namelen; 3892 3893 if (tsv->name) 3894 { 3895 namelen = strlen (tsv->name); 3896 buf = (char *) alloca (namelen * 2 + 1); 3897 bin2hex ((gdb_byte *) tsv->name, buf, namelen); 3898 } 3899 3900 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0), 3901 tsv->getter ? 1 : 0, buf); 3902 } 3903 3904 /* Return the first trace state variable definition, and initialize 3905 the state machine that will iterate through all the tsv bits. */ 3906 3907 static void 3908 cmd_qtfv (char *packet) 3909 { 3910 trace_debug ("Returning first trace state variable definition"); 3911 3912 cur_tsv = trace_state_variables; 3913 3914 if (cur_tsv) 3915 response_tsv (packet, cur_tsv); 3916 else 3917 strcpy (packet, "l"); 3918 } 3919 3920 /* Return additional trace state variable definitions. */ 3921 3922 static void 3923 cmd_qtsv (char *packet) 3924 { 3925 trace_debug ("Returning additional trace state variable definition"); 3926 3927 if (cur_tsv) 3928 { 3929 cur_tsv = cur_tsv->next; 3930 if (cur_tsv) 3931 response_tsv (packet, cur_tsv); 3932 else 3933 strcpy (packet, "l"); 3934 } 3935 else 3936 strcpy (packet, "l"); 3937 } 3938 3939 /* Return the first static tracepoint marker, and initialize the state 3940 machine that will iterate through all the static tracepoints 3941 markers. */ 3942 3943 static void 3944 cmd_qtfstm (char *packet) 3945 { 3946 if (!maybe_write_ipa_ust_not_loaded (packet)) 3947 run_inferior_command (packet, strlen (packet) + 1); 3948 } 3949 3950 /* Return additional static tracepoints markers. */ 3951 3952 static void 3953 cmd_qtsstm (char *packet) 3954 { 3955 if (!maybe_write_ipa_ust_not_loaded (packet)) 3956 run_inferior_command (packet, strlen (packet) + 1); 3957 } 3958 3959 /* Return the definition of the static tracepoint at a given address. 3960 Result packet is the same as qTsST's. */ 3961 3962 static void 3963 cmd_qtstmat (char *packet) 3964 { 3965 if (!maybe_write_ipa_ust_not_loaded (packet)) 3966 run_inferior_command (packet, strlen (packet) + 1); 3967 } 3968 3969 /* Sent the agent a command to close it. */ 3970 3971 void 3972 gdb_agent_about_to_close (int pid) 3973 { 3974 char buf[IPA_CMD_BUF_SIZE]; 3975 3976 if (!maybe_write_ipa_not_loaded (buf)) 3977 { 3978 struct thread_info *saved_thread; 3979 3980 saved_thread = current_thread; 3981 3982 /* Find any thread which belongs to process PID. */ 3983 current_thread = find_any_thread_of_pid (pid); 3984 3985 strcpy (buf, "close"); 3986 3987 run_inferior_command (buf, strlen (buf) + 1); 3988 3989 current_thread = saved_thread; 3990 } 3991 } 3992 3993 /* Return the minimum instruction size needed for fast tracepoints as a 3994 hexadecimal number. */ 3995 3996 static void 3997 cmd_qtminftpilen (char *packet) 3998 { 3999 if (current_thread == NULL) 4000 { 4001 /* Indicate that the minimum length is currently unknown. */ 4002 strcpy (packet, "0"); 4003 return; 4004 } 4005 4006 sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ()); 4007 } 4008 4009 /* Respond to qTBuffer packet with a block of raw data from the trace 4010 buffer. GDB may ask for a lot, but we are allowed to reply with 4011 only as much as will fit within packet limits or whatever. */ 4012 4013 static void 4014 cmd_qtbuffer (char *own_buf) 4015 { 4016 ULONGEST offset, num, tot; 4017 unsigned char *tbp; 4018 const char *packet = own_buf; 4019 4020 packet += strlen ("qTBuffer:"); 4021 4022 packet = unpack_varlen_hex (packet, &offset); 4023 ++packet; /* skip a comma */ 4024 unpack_varlen_hex (packet, &num); 4025 4026 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s", 4027 (int) num, phex_nz (offset, 0)); 4028 4029 tot = (trace_buffer_hi - trace_buffer_lo) - free_space (); 4030 4031 /* If we're right at the end, reply specially that we're done. */ 4032 if (offset == tot) 4033 { 4034 strcpy (own_buf, "l"); 4035 return; 4036 } 4037 4038 /* Object to any other out-of-bounds request. */ 4039 if (offset > tot) 4040 { 4041 write_enn (own_buf); 4042 return; 4043 } 4044 4045 /* Compute the pointer corresponding to the given offset, accounting 4046 for wraparound. */ 4047 tbp = trace_buffer_start + offset; 4048 if (tbp >= trace_buffer_wrap) 4049 tbp -= (trace_buffer_wrap - trace_buffer_lo); 4050 4051 /* Trim to the remaining bytes if we're close to the end. */ 4052 if (num > tot - offset) 4053 num = tot - offset; 4054 4055 /* Trim to available packet size. */ 4056 if (num >= (PBUFSIZ - 16) / 2 ) 4057 num = (PBUFSIZ - 16) / 2; 4058 4059 bin2hex (tbp, own_buf, num); 4060 } 4061 4062 static void 4063 cmd_bigqtbuffer_circular (char *own_buf) 4064 { 4065 ULONGEST val; 4066 char *packet = own_buf; 4067 4068 packet += strlen ("QTBuffer:circular:"); 4069 4070 unpack_varlen_hex (packet, &val); 4071 circular_trace_buffer = val; 4072 trace_debug ("Trace buffer is now %s", 4073 circular_trace_buffer ? "circular" : "linear"); 4074 write_ok (own_buf); 4075 } 4076 4077 static void 4078 cmd_bigqtbuffer_size (char *own_buf) 4079 { 4080 ULONGEST val; 4081 LONGEST sval; 4082 char *packet = own_buf; 4083 4084 /* Can't change the size during a tracing run. */ 4085 if (tracing) 4086 { 4087 write_enn (own_buf); 4088 return; 4089 } 4090 4091 packet += strlen ("QTBuffer:size:"); 4092 4093 /* -1 is sent as literal "-1". */ 4094 if (strcmp (packet, "-1") == 0) 4095 sval = DEFAULT_TRACE_BUFFER_SIZE; 4096 else 4097 { 4098 unpack_varlen_hex (packet, &val); 4099 sval = (LONGEST) val; 4100 } 4101 4102 init_trace_buffer (sval); 4103 trace_debug ("Trace buffer is now %s bytes", 4104 plongest (trace_buffer_size)); 4105 write_ok (own_buf); 4106 } 4107 4108 static void 4109 cmd_qtnotes (char *own_buf) 4110 { 4111 size_t nbytes; 4112 char *saved, *user, *notes, *stopnote; 4113 char *packet = own_buf; 4114 4115 packet += strlen ("QTNotes:"); 4116 4117 while (*packet) 4118 { 4119 if (startswith (packet, "user:")) 4120 { 4121 packet += strlen ("user:"); 4122 saved = packet; 4123 packet = strchr (packet, ';'); 4124 nbytes = (packet - saved) / 2; 4125 user = (char *) xmalloc (nbytes + 1); 4126 nbytes = hex2bin (saved, (gdb_byte *) user, nbytes); 4127 user[nbytes] = '\0'; 4128 ++packet; /* skip the semicolon */ 4129 trace_debug ("User is '%s'", user); 4130 xfree (tracing_user_name); 4131 tracing_user_name = user; 4132 } 4133 else if (startswith (packet, "notes:")) 4134 { 4135 packet += strlen ("notes:"); 4136 saved = packet; 4137 packet = strchr (packet, ';'); 4138 nbytes = (packet - saved) / 2; 4139 notes = (char *) xmalloc (nbytes + 1); 4140 nbytes = hex2bin (saved, (gdb_byte *) notes, nbytes); 4141 notes[nbytes] = '\0'; 4142 ++packet; /* skip the semicolon */ 4143 trace_debug ("Notes is '%s'", notes); 4144 xfree (tracing_notes); 4145 tracing_notes = notes; 4146 } 4147 else if (startswith (packet, "tstop:")) 4148 { 4149 packet += strlen ("tstop:"); 4150 saved = packet; 4151 packet = strchr (packet, ';'); 4152 nbytes = (packet - saved) / 2; 4153 stopnote = (char *) xmalloc (nbytes + 1); 4154 nbytes = hex2bin (saved, (gdb_byte *) stopnote, nbytes); 4155 stopnote[nbytes] = '\0'; 4156 ++packet; /* skip the semicolon */ 4157 trace_debug ("tstop note is '%s'", stopnote); 4158 xfree (tracing_stop_note); 4159 tracing_stop_note = stopnote; 4160 } 4161 else 4162 break; 4163 } 4164 4165 write_ok (own_buf); 4166 } 4167 4168 int 4169 handle_tracepoint_general_set (char *packet) 4170 { 4171 if (strcmp ("QTinit", packet) == 0) 4172 { 4173 cmd_qtinit (packet); 4174 return 1; 4175 } 4176 else if (startswith (packet, "QTDP:")) 4177 { 4178 cmd_qtdp (packet); 4179 return 1; 4180 } 4181 else if (startswith (packet, "QTDPsrc:")) 4182 { 4183 cmd_qtdpsrc (packet); 4184 return 1; 4185 } 4186 else if (startswith (packet, "QTEnable:")) 4187 { 4188 cmd_qtenable_disable (packet, 1); 4189 return 1; 4190 } 4191 else if (startswith (packet, "QTDisable:")) 4192 { 4193 cmd_qtenable_disable (packet, 0); 4194 return 1; 4195 } 4196 else if (startswith (packet, "QTDV:")) 4197 { 4198 cmd_qtdv (packet); 4199 return 1; 4200 } 4201 else if (startswith (packet, "QTro:")) 4202 { 4203 cmd_qtro (packet); 4204 return 1; 4205 } 4206 else if (strcmp ("QTStart", packet) == 0) 4207 { 4208 cmd_qtstart (packet); 4209 return 1; 4210 } 4211 else if (strcmp ("QTStop", packet) == 0) 4212 { 4213 cmd_qtstop (packet); 4214 return 1; 4215 } 4216 else if (startswith (packet, "QTDisconnected:")) 4217 { 4218 cmd_qtdisconnected (packet); 4219 return 1; 4220 } 4221 else if (startswith (packet, "QTFrame:")) 4222 { 4223 cmd_qtframe (packet); 4224 return 1; 4225 } 4226 else if (startswith (packet, "QTBuffer:circular:")) 4227 { 4228 cmd_bigqtbuffer_circular (packet); 4229 return 1; 4230 } 4231 else if (startswith (packet, "QTBuffer:size:")) 4232 { 4233 cmd_bigqtbuffer_size (packet); 4234 return 1; 4235 } 4236 else if (startswith (packet, "QTNotes:")) 4237 { 4238 cmd_qtnotes (packet); 4239 return 1; 4240 } 4241 4242 return 0; 4243 } 4244 4245 int 4246 handle_tracepoint_query (char *packet) 4247 { 4248 if (strcmp ("qTStatus", packet) == 0) 4249 { 4250 cmd_qtstatus (packet); 4251 return 1; 4252 } 4253 else if (startswith (packet, "qTP:")) 4254 { 4255 cmd_qtp (packet); 4256 return 1; 4257 } 4258 else if (strcmp ("qTfP", packet) == 0) 4259 { 4260 cmd_qtfp (packet); 4261 return 1; 4262 } 4263 else if (strcmp ("qTsP", packet) == 0) 4264 { 4265 cmd_qtsp (packet); 4266 return 1; 4267 } 4268 else if (strcmp ("qTfV", packet) == 0) 4269 { 4270 cmd_qtfv (packet); 4271 return 1; 4272 } 4273 else if (strcmp ("qTsV", packet) == 0) 4274 { 4275 cmd_qtsv (packet); 4276 return 1; 4277 } 4278 else if (startswith (packet, "qTV:")) 4279 { 4280 cmd_qtv (packet); 4281 return 1; 4282 } 4283 else if (startswith (packet, "qTBuffer:")) 4284 { 4285 cmd_qtbuffer (packet); 4286 return 1; 4287 } 4288 else if (strcmp ("qTfSTM", packet) == 0) 4289 { 4290 cmd_qtfstm (packet); 4291 return 1; 4292 } 4293 else if (strcmp ("qTsSTM", packet) == 0) 4294 { 4295 cmd_qtsstm (packet); 4296 return 1; 4297 } 4298 else if (startswith (packet, "qTSTMat:")) 4299 { 4300 cmd_qtstmat (packet); 4301 return 1; 4302 } 4303 else if (strcmp ("qTMinFTPILen", packet) == 0) 4304 { 4305 cmd_qtminftpilen (packet); 4306 return 1; 4307 } 4308 4309 return 0; 4310 } 4311 4312 #endif 4313 #ifndef IN_PROCESS_AGENT 4314 4315 /* Call this when thread TINFO has hit the tracepoint defined by 4316 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping 4317 action. This adds a while-stepping collecting state item to the 4318 threads' collecting state list, so that we can keep track of 4319 multiple simultaneous while-stepping actions being collected by the 4320 same thread. This can happen in cases like: 4321 4322 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs 4323 ff0002 INSN2 4324 ff0003 INSN3 <-- TP2, collect $regs 4325 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs 4326 ff0005 INSN5 4327 4328 Notice that when instruction INSN5 is reached, the while-stepping 4329 actions of both TP1 and TP3 are still being collected, and that TP2 4330 had been collected meanwhile. The whole range of ff0001-ff0005 4331 should be single-stepped, due to at least TP1's while-stepping 4332 action covering the whole range. */ 4333 4334 static void 4335 add_while_stepping_state (struct thread_info *tinfo, 4336 int tp_number, CORE_ADDR tp_address) 4337 { 4338 struct wstep_state *wstep = XNEW (struct wstep_state); 4339 4340 wstep->next = tinfo->while_stepping; 4341 4342 wstep->tp_number = tp_number; 4343 wstep->tp_address = tp_address; 4344 wstep->current_step = 0; 4345 4346 tinfo->while_stepping = wstep; 4347 } 4348 4349 /* Release the while-stepping collecting state WSTEP. */ 4350 4351 static void 4352 release_while_stepping_state (struct wstep_state *wstep) 4353 { 4354 free (wstep); 4355 } 4356 4357 /* Release all while-stepping collecting states currently associated 4358 with thread TINFO. */ 4359 4360 void 4361 release_while_stepping_state_list (struct thread_info *tinfo) 4362 { 4363 struct wstep_state *head; 4364 4365 while (tinfo->while_stepping) 4366 { 4367 head = tinfo->while_stepping; 4368 tinfo->while_stepping = head->next; 4369 release_while_stepping_state (head); 4370 } 4371 } 4372 4373 /* If TINFO was handling a 'while-stepping' action, the step has 4374 finished, so collect any step data needed, and check if any more 4375 steps are required. Return true if the thread was indeed 4376 collecting tracepoint data, false otherwise. */ 4377 4378 int 4379 tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc) 4380 { 4381 struct tracepoint *tpoint; 4382 struct wstep_state *wstep; 4383 struct wstep_state **wstep_link; 4384 struct trap_tracepoint_ctx ctx; 4385 4386 /* Pull in fast tracepoint trace frames from the inferior lib buffer into 4387 our buffer. */ 4388 if (agent_loaded_p ()) 4389 upload_fast_traceframes (); 4390 4391 /* Check if we were indeed collecting data for one of more 4392 tracepoints with a 'while-stepping' count. */ 4393 if (tinfo->while_stepping == NULL) 4394 return 0; 4395 4396 if (!tracing) 4397 { 4398 /* We're not even tracing anymore. Stop this thread from 4399 collecting. */ 4400 release_while_stepping_state_list (tinfo); 4401 4402 /* The thread had stopped due to a single-step request indeed 4403 explained by a tracepoint. */ 4404 return 1; 4405 } 4406 4407 wstep = tinfo->while_stepping; 4408 wstep_link = &tinfo->while_stepping; 4409 4410 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s", 4411 target_pid_to_str (tinfo->id), 4412 wstep->tp_number, paddress (wstep->tp_address)); 4413 4414 ctx.base.type = trap_tracepoint; 4415 ctx.regcache = get_thread_regcache (tinfo, 1); 4416 4417 while (wstep != NULL) 4418 { 4419 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address); 4420 if (tpoint == NULL) 4421 { 4422 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!", 4423 wstep->tp_number, paddress (wstep->tp_address), 4424 target_pid_to_str (tinfo->id)); 4425 4426 /* Unlink. */ 4427 *wstep_link = wstep->next; 4428 release_while_stepping_state (wstep); 4429 wstep = *wstep_link; 4430 continue; 4431 } 4432 4433 /* We've just finished one step. */ 4434 ++wstep->current_step; 4435 4436 /* Collect data. */ 4437 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx, 4438 stop_pc, tpoint, wstep->current_step); 4439 4440 if (wstep->current_step >= tpoint->step_count) 4441 { 4442 /* The requested numbers of steps have occurred. */ 4443 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s", 4444 target_pid_to_str (tinfo->id), 4445 wstep->tp_number, paddress (wstep->tp_address)); 4446 4447 /* Unlink the wstep. */ 4448 *wstep_link = wstep->next; 4449 release_while_stepping_state (wstep); 4450 wstep = *wstep_link; 4451 4452 /* Only check the hit count now, which ensure that we do all 4453 our stepping before stopping the run. */ 4454 if (tpoint->pass_count > 0 4455 && tpoint->hit_count >= tpoint->pass_count 4456 && stopping_tracepoint == NULL) 4457 stopping_tracepoint = tpoint; 4458 } 4459 else 4460 { 4461 /* Keep single-stepping until the requested numbers of steps 4462 have occurred. */ 4463 wstep_link = &wstep->next; 4464 wstep = *wstep_link; 4465 } 4466 4467 if (stopping_tracepoint 4468 || trace_buffer_is_full 4469 || expr_eval_result != expr_eval_no_error) 4470 { 4471 stop_tracing (); 4472 break; 4473 } 4474 } 4475 4476 return 1; 4477 } 4478 4479 /* Handle any internal tracing control breakpoint hits. That means, 4480 pull traceframes from the IPA to our buffer, and syncing both 4481 tracing agents when the IPA's tracing stops for some reason. */ 4482 4483 int 4484 handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc) 4485 { 4486 /* Pull in fast tracepoint trace frames from the inferior in-process 4487 agent's buffer into our buffer. */ 4488 4489 if (!agent_loaded_p ()) 4490 return 0; 4491 4492 upload_fast_traceframes (); 4493 4494 /* Check if the in-process agent had decided we should stop 4495 tracing. */ 4496 if (stop_pc == ipa_sym_addrs.addr_stop_tracing) 4497 { 4498 int ipa_trace_buffer_is_full; 4499 CORE_ADDR ipa_stopping_tracepoint; 4500 int ipa_expr_eval_result; 4501 CORE_ADDR ipa_error_tracepoint; 4502 4503 trace_debug ("lib stopped at stop_tracing"); 4504 4505 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 4506 &ipa_trace_buffer_is_full); 4507 4508 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 4509 &ipa_stopping_tracepoint); 4510 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0); 4511 4512 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 4513 &ipa_error_tracepoint); 4514 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0); 4515 4516 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 4517 &ipa_expr_eval_result); 4518 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0); 4519 4520 trace_debug ("lib: trace_buffer_is_full: %d, " 4521 "stopping_tracepoint: %s, " 4522 "ipa_expr_eval_result: %d, " 4523 "error_tracepoint: %s, ", 4524 ipa_trace_buffer_is_full, 4525 paddress (ipa_stopping_tracepoint), 4526 ipa_expr_eval_result, 4527 paddress (ipa_error_tracepoint)); 4528 4529 if (debug_threads) 4530 { 4531 if (ipa_trace_buffer_is_full) 4532 trace_debug ("lib stopped due to full buffer."); 4533 if (ipa_stopping_tracepoint) 4534 trace_debug ("lib stopped due to tpoint"); 4535 if (ipa_error_tracepoint) 4536 trace_debug ("lib stopped due to error"); 4537 } 4538 4539 if (ipa_stopping_tracepoint != 0) 4540 { 4541 stopping_tracepoint 4542 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint); 4543 } 4544 else if (ipa_expr_eval_result != expr_eval_no_error) 4545 { 4546 expr_eval_result = ipa_expr_eval_result; 4547 error_tracepoint 4548 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint); 4549 } 4550 stop_tracing (); 4551 return 1; 4552 } 4553 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer) 4554 { 4555 trace_debug ("lib stopped at flush_trace_buffer"); 4556 return 1; 4557 } 4558 4559 return 0; 4560 } 4561 4562 /* Return true if TINFO just hit a tracepoint. Collect data if 4563 so. */ 4564 4565 int 4566 tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc) 4567 { 4568 struct tracepoint *tpoint; 4569 int ret = 0; 4570 struct trap_tracepoint_ctx ctx; 4571 4572 /* Not tracing, don't handle. */ 4573 if (!tracing) 4574 return 0; 4575 4576 ctx.base.type = trap_tracepoint; 4577 ctx.regcache = get_thread_regcache (tinfo, 1); 4578 4579 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) 4580 { 4581 /* Note that we collect fast tracepoints here as well. We'll 4582 step over the fast tracepoint jump later, which avoids the 4583 double collect. However, we don't collect for static 4584 tracepoints here, because UST markers are compiled in program, 4585 and probes will be executed in program. So static tracepoints 4586 are collected there. */ 4587 if (tpoint->enabled && stop_pc == tpoint->address 4588 && tpoint->type != static_tracepoint) 4589 { 4590 trace_debug ("Thread %s at address of tracepoint %d at 0x%s", 4591 target_pid_to_str (tinfo->id), 4592 tpoint->number, paddress (tpoint->address)); 4593 4594 /* Test the condition if present, and collect if true. */ 4595 if (!tpoint->cond 4596 || (condition_true_at_tracepoint 4597 ((struct tracepoint_hit_ctx *) &ctx, tpoint))) 4598 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx, 4599 stop_pc, tpoint); 4600 4601 if (stopping_tracepoint 4602 || trace_buffer_is_full 4603 || expr_eval_result != expr_eval_no_error) 4604 { 4605 stop_tracing (); 4606 } 4607 /* If the tracepoint had a 'while-stepping' action, then set 4608 the thread to collect this tracepoint on the following 4609 single-steps. */ 4610 else if (tpoint->step_count > 0) 4611 { 4612 add_while_stepping_state (tinfo, 4613 tpoint->number, tpoint->address); 4614 } 4615 4616 ret = 1; 4617 } 4618 } 4619 4620 return ret; 4621 } 4622 4623 #endif 4624 4625 #if defined IN_PROCESS_AGENT && defined HAVE_UST 4626 struct ust_marker_data; 4627 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, 4628 struct traceframe *tframe); 4629 #endif 4630 4631 /* Create a trace frame for the hit of the given tracepoint in the 4632 given thread. */ 4633 4634 static void 4635 collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc, 4636 struct tracepoint *tpoint) 4637 { 4638 struct traceframe *tframe; 4639 int acti; 4640 4641 /* Only count it as a hit when we actually collect data. */ 4642 tpoint->hit_count++; 4643 4644 /* If we've exceeded a defined pass count, record the event for 4645 later, and finish the collection for this hit. This test is only 4646 for nonstepping tracepoints, stepping tracepoints test at the end 4647 of their while-stepping loop. */ 4648 if (tpoint->pass_count > 0 4649 && tpoint->hit_count >= tpoint->pass_count 4650 && tpoint->step_count == 0 4651 && stopping_tracepoint == NULL) 4652 stopping_tracepoint = tpoint; 4653 4654 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64, 4655 tpoint->number, paddress (tpoint->address), tpoint->hit_count); 4656 4657 tframe = add_traceframe (tpoint); 4658 4659 if (tframe) 4660 { 4661 for (acti = 0; acti < tpoint->numactions; ++acti) 4662 { 4663 #ifndef IN_PROCESS_AGENT 4664 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'", 4665 tpoint->number, paddress (tpoint->address), 4666 tpoint->actions_str[acti]); 4667 #endif 4668 4669 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe, 4670 tpoint->actions[acti]); 4671 } 4672 4673 finish_traceframe (tframe); 4674 } 4675 4676 if (tframe == NULL && tracing) 4677 trace_buffer_is_full = 1; 4678 } 4679 4680 #ifndef IN_PROCESS_AGENT 4681 4682 static void 4683 collect_data_at_step (struct tracepoint_hit_ctx *ctx, 4684 CORE_ADDR stop_pc, 4685 struct tracepoint *tpoint, int current_step) 4686 { 4687 struct traceframe *tframe; 4688 int acti; 4689 4690 trace_debug ("Making new step traceframe for " 4691 "tracepoint %d at 0x%s, step %d of %" PRIu64 ", hit %" PRIu64, 4692 tpoint->number, paddress (tpoint->address), 4693 current_step, tpoint->step_count, 4694 tpoint->hit_count); 4695 4696 tframe = add_traceframe (tpoint); 4697 4698 if (tframe) 4699 { 4700 for (acti = 0; acti < tpoint->num_step_actions; ++acti) 4701 { 4702 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'", 4703 tpoint->number, paddress (tpoint->address), 4704 tpoint->step_actions_str[acti]); 4705 4706 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe, 4707 tpoint->step_actions[acti]); 4708 } 4709 4710 finish_traceframe (tframe); 4711 } 4712 4713 if (tframe == NULL && tracing) 4714 trace_buffer_is_full = 1; 4715 } 4716 4717 #endif 4718 4719 #ifdef IN_PROCESS_AGENT 4720 /* The target description index for IPA. Passed from gdbserver, used 4721 to select ipa_tdesc. */ 4722 EXTERN_C_PUSH 4723 IP_AGENT_EXPORT_VAR int ipa_tdesc_idx; 4724 EXTERN_C_POP 4725 #endif 4726 4727 static struct regcache * 4728 get_context_regcache (struct tracepoint_hit_ctx *ctx) 4729 { 4730 struct regcache *regcache = NULL; 4731 #ifdef IN_PROCESS_AGENT 4732 const struct target_desc *ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx); 4733 4734 if (ctx->type == fast_tracepoint) 4735 { 4736 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx; 4737 if (!fctx->regcache_initted) 4738 { 4739 fctx->regcache_initted = 1; 4740 init_register_cache (&fctx->regcache, ipa_tdesc, fctx->regspace); 4741 supply_regblock (&fctx->regcache, NULL); 4742 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs); 4743 } 4744 regcache = &fctx->regcache; 4745 } 4746 #ifdef HAVE_UST 4747 if (ctx->type == static_tracepoint) 4748 { 4749 struct static_tracepoint_ctx *sctx 4750 = (struct static_tracepoint_ctx *) ctx; 4751 4752 if (!sctx->regcache_initted) 4753 { 4754 sctx->regcache_initted = 1; 4755 init_register_cache (&sctx->regcache, ipa_tdesc, sctx->regspace); 4756 supply_regblock (&sctx->regcache, NULL); 4757 /* Pass down the tracepoint address, because REGS doesn't 4758 include the PC, but we know what it must have been. */ 4759 supply_static_tracepoint_registers (&sctx->regcache, 4760 (const unsigned char *) 4761 sctx->regs, 4762 sctx->tpoint->address); 4763 } 4764 regcache = &sctx->regcache; 4765 } 4766 #endif 4767 #else 4768 if (ctx->type == trap_tracepoint) 4769 { 4770 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx; 4771 regcache = tctx->regcache; 4772 } 4773 #endif 4774 4775 gdb_assert (regcache != NULL); 4776 4777 return regcache; 4778 } 4779 4780 static void 4781 do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx, 4782 CORE_ADDR stop_pc, 4783 struct tracepoint *tpoint, 4784 struct traceframe *tframe, 4785 struct tracepoint_action *taction) 4786 { 4787 enum eval_result_type err; 4788 4789 switch (taction->type) 4790 { 4791 case 'M': 4792 { 4793 struct collect_memory_action *maction; 4794 struct eval_agent_expr_context ax_ctx; 4795 4796 maction = (struct collect_memory_action *) taction; 4797 ax_ctx.regcache = NULL; 4798 ax_ctx.tframe = tframe; 4799 ax_ctx.tpoint = tpoint; 4800 4801 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)", 4802 pulongest (maction->len), 4803 paddress (maction->addr), maction->basereg); 4804 /* (should use basereg) */ 4805 agent_mem_read (&ax_ctx, NULL, (CORE_ADDR) maction->addr, 4806 maction->len); 4807 break; 4808 } 4809 case 'R': 4810 { 4811 unsigned char *regspace; 4812 struct regcache tregcache; 4813 struct regcache *context_regcache; 4814 int regcache_size; 4815 4816 trace_debug ("Want to collect registers"); 4817 4818 context_regcache = get_context_regcache (ctx); 4819 regcache_size = register_cache_size (context_regcache->tdesc); 4820 4821 /* Collect all registers for now. */ 4822 regspace = add_traceframe_block (tframe, tpoint, 1 + regcache_size); 4823 if (regspace == NULL) 4824 { 4825 trace_debug ("Trace buffer block allocation failed, skipping"); 4826 break; 4827 } 4828 /* Identify a register block. */ 4829 *regspace = 'R'; 4830 4831 /* Wrap the regblock in a register cache (in the stack, we 4832 don't want to malloc here). */ 4833 init_register_cache (&tregcache, context_regcache->tdesc, 4834 regspace + 1); 4835 4836 /* Copy the register data to the regblock. */ 4837 regcache_cpy (&tregcache, context_regcache); 4838 4839 #ifndef IN_PROCESS_AGENT 4840 /* On some platforms, trap-based tracepoints will have the PC 4841 pointing to the next instruction after the trap, but we 4842 don't want the user or GDB trying to guess whether the 4843 saved PC needs adjusting; so always record the adjusted 4844 stop_pc. Note that we can't use tpoint->address instead, 4845 since it will be wrong for while-stepping actions. This 4846 adjustment is a nop for fast tracepoints collected from the 4847 in-process lib (but not if GDBserver is collecting one 4848 preemptively), since the PC had already been adjusted to 4849 contain the tracepoint's address by the jump pad. */ 4850 trace_debug ("Storing stop pc (0x%s) in regblock", 4851 paddress (stop_pc)); 4852 4853 /* This changes the regblock, not the thread's 4854 regcache. */ 4855 regcache_write_pc (&tregcache, stop_pc); 4856 #endif 4857 } 4858 break; 4859 case 'X': 4860 { 4861 struct eval_expr_action *eaction; 4862 struct eval_agent_expr_context ax_ctx; 4863 4864 eaction = (struct eval_expr_action *) taction; 4865 ax_ctx.regcache = get_context_regcache (ctx); 4866 ax_ctx.tframe = tframe; 4867 ax_ctx.tpoint = tpoint; 4868 4869 trace_debug ("Want to evaluate expression"); 4870 4871 err = gdb_eval_agent_expr (&ax_ctx, eaction->expr, NULL); 4872 4873 if (err != expr_eval_no_error) 4874 { 4875 record_tracepoint_error (tpoint, "action expression", err); 4876 return; 4877 } 4878 } 4879 break; 4880 case 'L': 4881 { 4882 #if defined IN_PROCESS_AGENT && defined HAVE_UST 4883 trace_debug ("Want to collect static trace data"); 4884 collect_ust_data_at_tracepoint (ctx, tframe); 4885 #else 4886 trace_debug ("warning: collecting static trace data, " 4887 "but static tracepoints are not supported"); 4888 #endif 4889 } 4890 break; 4891 default: 4892 trace_debug ("unknown trace action '%c', ignoring", taction->type); 4893 break; 4894 } 4895 } 4896 4897 static int 4898 condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx, 4899 struct tracepoint *tpoint) 4900 { 4901 ULONGEST value = 0; 4902 enum eval_result_type err; 4903 4904 /* Presently, gdbserver doesn't run compiled conditions, only the 4905 IPA does. If the program stops at a fast tracepoint's address 4906 (e.g., due to a breakpoint, trap tracepoint, or stepping), 4907 gdbserver preemptively collect the fast tracepoint. Later, on 4908 resume, gdbserver steps over the fast tracepoint like it steps 4909 over breakpoints, so that the IPA doesn't see that fast 4910 tracepoint. This avoids double collects of fast tracepoints in 4911 that stopping scenario. Having gdbserver itself handle the fast 4912 tracepoint gives the user a consistent view of when fast or trap 4913 tracepoints are collected, compared to an alternative where only 4914 trap tracepoints are collected on stop, and fast tracepoints on 4915 resume. When a fast tracepoint is being processed by gdbserver, 4916 it is always the non-compiled condition expression that is 4917 used. */ 4918 #ifdef IN_PROCESS_AGENT 4919 if (tpoint->compiled_cond) 4920 { 4921 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx; 4922 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (fctx->regs, &value); 4923 } 4924 else 4925 #endif 4926 { 4927 struct eval_agent_expr_context ax_ctx; 4928 4929 ax_ctx.regcache = get_context_regcache (ctx); 4930 ax_ctx.tframe = NULL; 4931 ax_ctx.tpoint = tpoint; 4932 4933 err = gdb_eval_agent_expr (&ax_ctx, tpoint->cond, &value); 4934 } 4935 if (err != expr_eval_no_error) 4936 { 4937 record_tracepoint_error (tpoint, "condition", err); 4938 /* The error case must return false. */ 4939 return 0; 4940 } 4941 4942 trace_debug ("Tracepoint %d at 0x%s condition evals to %s", 4943 tpoint->number, paddress (tpoint->address), 4944 pulongest (value)); 4945 return (value ? 1 : 0); 4946 } 4947 4948 /* Do memory copies for bytecodes. */ 4949 /* Do the recording of memory blocks for actions and bytecodes. */ 4950 4951 int 4952 agent_mem_read (struct eval_agent_expr_context *ctx, 4953 unsigned char *to, CORE_ADDR from, ULONGEST len) 4954 { 4955 unsigned char *mspace; 4956 ULONGEST remaining = len; 4957 unsigned short blocklen; 4958 4959 /* If a 'to' buffer is specified, use it. */ 4960 if (to != NULL) 4961 { 4962 read_inferior_memory (from, to, len); 4963 return 0; 4964 } 4965 4966 /* Otherwise, create a new memory block in the trace buffer. */ 4967 while (remaining > 0) 4968 { 4969 size_t sp; 4970 4971 blocklen = (remaining > 65535 ? 65535 : remaining); 4972 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen; 4973 mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp); 4974 if (mspace == NULL) 4975 return 1; 4976 /* Identify block as a memory block. */ 4977 *mspace = 'M'; 4978 ++mspace; 4979 /* Record address and size. */ 4980 memcpy (mspace, &from, sizeof (from)); 4981 mspace += sizeof (from); 4982 memcpy (mspace, &blocklen, sizeof (blocklen)); 4983 mspace += sizeof (blocklen); 4984 /* Record the memory block proper. */ 4985 read_inferior_memory (from, mspace, blocklen); 4986 trace_debug ("%d bytes recorded", blocklen); 4987 remaining -= blocklen; 4988 from += blocklen; 4989 } 4990 return 0; 4991 } 4992 4993 int 4994 agent_mem_read_string (struct eval_agent_expr_context *ctx, 4995 unsigned char *to, CORE_ADDR from, ULONGEST len) 4996 { 4997 unsigned char *buf, *mspace; 4998 ULONGEST remaining = len; 4999 unsigned short blocklen, i; 5000 5001 /* To save a bit of space, block lengths are 16-bit, so break large 5002 requests into multiple blocks. Bordering on overkill for strings, 5003 but it could happen that someone specifies a large max length. */ 5004 while (remaining > 0) 5005 { 5006 size_t sp; 5007 5008 blocklen = (remaining > 65535 ? 65535 : remaining); 5009 /* We want working space to accumulate nonzero bytes, since 5010 traceframes must have a predecided size (otherwise it gets 5011 harder to wrap correctly for the circular case, etc). */ 5012 buf = (unsigned char *) xmalloc (blocklen + 1); 5013 for (i = 0; i < blocklen; ++i) 5014 { 5015 /* Read the string one byte at a time, in case the string is 5016 at the end of a valid memory area - we don't want a 5017 correctly-terminated string to engender segvio 5018 complaints. */ 5019 read_inferior_memory (from + i, buf + i, 1); 5020 5021 if (buf[i] == '\0') 5022 { 5023 blocklen = i + 1; 5024 /* Make sure outer loop stops now too. */ 5025 remaining = blocklen; 5026 break; 5027 } 5028 } 5029 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen; 5030 mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp); 5031 if (mspace == NULL) 5032 { 5033 xfree (buf); 5034 return 1; 5035 } 5036 /* Identify block as a memory block. */ 5037 *mspace = 'M'; 5038 ++mspace; 5039 /* Record address and size. */ 5040 memcpy ((void *) mspace, (void *) &from, sizeof (from)); 5041 mspace += sizeof (from); 5042 memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen)); 5043 mspace += sizeof (blocklen); 5044 /* Copy the string contents. */ 5045 memcpy ((void *) mspace, (void *) buf, blocklen); 5046 remaining -= blocklen; 5047 from += blocklen; 5048 xfree (buf); 5049 } 5050 return 0; 5051 } 5052 5053 /* Record the value of a trace state variable. */ 5054 5055 int 5056 agent_tsv_read (struct eval_agent_expr_context *ctx, int n) 5057 { 5058 unsigned char *vspace; 5059 LONGEST val; 5060 5061 vspace = add_traceframe_block (ctx->tframe, ctx->tpoint, 5062 1 + sizeof (n) + sizeof (LONGEST)); 5063 if (vspace == NULL) 5064 return 1; 5065 /* Identify block as a variable. */ 5066 *vspace = 'V'; 5067 /* Record variable's number and value. */ 5068 memcpy (vspace + 1, &n, sizeof (n)); 5069 val = get_trace_state_variable_value (n); 5070 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val)); 5071 trace_debug ("Variable %d recorded", n); 5072 return 0; 5073 } 5074 5075 #ifndef IN_PROCESS_AGENT 5076 5077 /* Callback for traceframe_walk_blocks, used to find a given block 5078 type in a traceframe. */ 5079 5080 static int 5081 match_blocktype (char blocktype, unsigned char *dataptr, void *data) 5082 { 5083 char *wantedp = (char *) data; 5084 5085 if (*wantedp == blocktype) 5086 return 1; 5087 5088 return 0; 5089 } 5090 5091 /* Walk over all traceframe blocks of the traceframe buffer starting 5092 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each 5093 block found, passing in DATA unmodified. If CALLBACK returns true, 5094 this returns a pointer to where the block is found. Returns NULL 5095 if no callback call returned true, indicating that all blocks have 5096 been walked. */ 5097 5098 static unsigned char * 5099 traceframe_walk_blocks (unsigned char *database, unsigned int datasize, 5100 int tfnum, 5101 int (*callback) (char blocktype, 5102 unsigned char *dataptr, 5103 void *data), 5104 void *data) 5105 { 5106 unsigned char *dataptr; 5107 5108 if (datasize == 0) 5109 { 5110 trace_debug ("traceframe %d has no data", tfnum); 5111 return NULL; 5112 } 5113 5114 /* Iterate through a traceframe's blocks, looking for a block of the 5115 requested type. */ 5116 for (dataptr = database; 5117 dataptr < database + datasize; 5118 /* nothing */) 5119 { 5120 char blocktype; 5121 unsigned short mlen; 5122 5123 if (dataptr == trace_buffer_wrap) 5124 { 5125 /* Adjust to reflect wrapping part of the frame around to 5126 the beginning. */ 5127 datasize = dataptr - database; 5128 dataptr = database = trace_buffer_lo; 5129 } 5130 5131 blocktype = *dataptr++; 5132 5133 if ((*callback) (blocktype, dataptr, data)) 5134 return dataptr; 5135 5136 switch (blocktype) 5137 { 5138 case 'R': 5139 /* Skip over the registers block. */ 5140 dataptr += current_target_desc ()->registers_size; 5141 break; 5142 case 'M': 5143 /* Skip over the memory block. */ 5144 dataptr += sizeof (CORE_ADDR); 5145 memcpy (&mlen, dataptr, sizeof (mlen)); 5146 dataptr += (sizeof (mlen) + mlen); 5147 break; 5148 case 'V': 5149 /* Skip over the TSV block. */ 5150 dataptr += (sizeof (int) + sizeof (LONGEST)); 5151 break; 5152 case 'S': 5153 /* Skip over the static trace data block. */ 5154 memcpy (&mlen, dataptr, sizeof (mlen)); 5155 dataptr += (sizeof (mlen) + mlen); 5156 break; 5157 default: 5158 trace_debug ("traceframe %d has unknown block type 0x%x", 5159 tfnum, blocktype); 5160 return NULL; 5161 } 5162 } 5163 5164 return NULL; 5165 } 5166 5167 /* Look for the block of type TYPE_WANTED in the traceframe starting 5168 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe 5169 number. */ 5170 5171 static unsigned char * 5172 traceframe_find_block_type (unsigned char *database, unsigned int datasize, 5173 int tfnum, char type_wanted) 5174 { 5175 return traceframe_walk_blocks (database, datasize, tfnum, 5176 match_blocktype, &type_wanted); 5177 } 5178 5179 static unsigned char * 5180 traceframe_find_regblock (struct traceframe *tframe, int tfnum) 5181 { 5182 unsigned char *regblock; 5183 5184 regblock = traceframe_find_block_type (tframe->data, 5185 tframe->data_size, 5186 tfnum, 'R'); 5187 5188 if (regblock == NULL) 5189 trace_debug ("traceframe %d has no register data", tfnum); 5190 5191 return regblock; 5192 } 5193 5194 /* Get registers from a traceframe. */ 5195 5196 int 5197 fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum) 5198 { 5199 unsigned char *dataptr; 5200 struct tracepoint *tpoint; 5201 struct traceframe *tframe; 5202 5203 tframe = find_traceframe (tfnum); 5204 5205 if (tframe == NULL) 5206 { 5207 trace_debug ("traceframe %d not found", tfnum); 5208 return 1; 5209 } 5210 5211 dataptr = traceframe_find_regblock (tframe, tfnum); 5212 if (dataptr == NULL) 5213 { 5214 /* Mark registers unavailable. */ 5215 supply_regblock (regcache, NULL); 5216 5217 /* We can generally guess at a PC, although this will be 5218 misleading for while-stepping frames and multi-location 5219 tracepoints. */ 5220 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum); 5221 if (tpoint != NULL) 5222 regcache_write_pc (regcache, tpoint->address); 5223 } 5224 else 5225 supply_regblock (regcache, dataptr); 5226 5227 return 0; 5228 } 5229 5230 static CORE_ADDR 5231 traceframe_get_pc (struct traceframe *tframe) 5232 { 5233 struct regcache regcache; 5234 unsigned char *dataptr; 5235 const struct target_desc *tdesc = current_target_desc (); 5236 5237 dataptr = traceframe_find_regblock (tframe, -1); 5238 if (dataptr == NULL) 5239 return 0; 5240 5241 init_register_cache (®cache, tdesc, dataptr); 5242 return regcache_read_pc (®cache); 5243 } 5244 5245 /* Read a requested block of memory from a trace frame. */ 5246 5247 int 5248 traceframe_read_mem (int tfnum, CORE_ADDR addr, 5249 unsigned char *buf, ULONGEST length, 5250 ULONGEST *nbytes) 5251 { 5252 struct traceframe *tframe; 5253 unsigned char *database, *dataptr; 5254 unsigned int datasize; 5255 CORE_ADDR maddr; 5256 unsigned short mlen; 5257 5258 trace_debug ("traceframe_read_mem"); 5259 5260 tframe = find_traceframe (tfnum); 5261 5262 if (!tframe) 5263 { 5264 trace_debug ("traceframe %d not found", tfnum); 5265 return 1; 5266 } 5267 5268 datasize = tframe->data_size; 5269 database = dataptr = &tframe->data[0]; 5270 5271 /* Iterate through a traceframe's blocks, looking for memory. */ 5272 while ((dataptr = traceframe_find_block_type (dataptr, 5273 datasize 5274 - (dataptr - database), 5275 tfnum, 'M')) != NULL) 5276 { 5277 memcpy (&maddr, dataptr, sizeof (maddr)); 5278 dataptr += sizeof (maddr); 5279 memcpy (&mlen, dataptr, sizeof (mlen)); 5280 dataptr += sizeof (mlen); 5281 trace_debug ("traceframe %d has %d bytes at %s", 5282 tfnum, mlen, paddress (maddr)); 5283 5284 /* If the block includes the first part of the desired range, 5285 return as much it has; GDB will re-request the remainder, 5286 which might be in a different block of this trace frame. */ 5287 if (maddr <= addr && addr < (maddr + mlen)) 5288 { 5289 ULONGEST amt = (maddr + mlen) - addr; 5290 if (amt > length) 5291 amt = length; 5292 5293 memcpy (buf, dataptr + (addr - maddr), amt); 5294 *nbytes = amt; 5295 return 0; 5296 } 5297 5298 /* Skip over this block. */ 5299 dataptr += mlen; 5300 } 5301 5302 trace_debug ("traceframe %d has no memory data for the desired region", 5303 tfnum); 5304 5305 *nbytes = 0; 5306 return 0; 5307 } 5308 5309 static int 5310 traceframe_read_tsv (int tsvnum, LONGEST *val) 5311 { 5312 client_state &cs = get_client_state (); 5313 int tfnum; 5314 struct traceframe *tframe; 5315 unsigned char *database, *dataptr; 5316 unsigned int datasize; 5317 int vnum; 5318 int found = 0; 5319 5320 trace_debug ("traceframe_read_tsv"); 5321 5322 tfnum = cs.current_traceframe; 5323 5324 if (tfnum < 0) 5325 { 5326 trace_debug ("no current traceframe"); 5327 return 1; 5328 } 5329 5330 tframe = find_traceframe (tfnum); 5331 5332 if (tframe == NULL) 5333 { 5334 trace_debug ("traceframe %d not found", tfnum); 5335 return 1; 5336 } 5337 5338 datasize = tframe->data_size; 5339 database = dataptr = &tframe->data[0]; 5340 5341 /* Iterate through a traceframe's blocks, looking for the last 5342 matched tsv. */ 5343 while ((dataptr = traceframe_find_block_type (dataptr, 5344 datasize 5345 - (dataptr - database), 5346 tfnum, 'V')) != NULL) 5347 { 5348 memcpy (&vnum, dataptr, sizeof (vnum)); 5349 dataptr += sizeof (vnum); 5350 5351 trace_debug ("traceframe %d has variable %d", tfnum, vnum); 5352 5353 /* Check that this is the variable we want. */ 5354 if (tsvnum == vnum) 5355 { 5356 memcpy (val, dataptr, sizeof (*val)); 5357 found = 1; 5358 } 5359 5360 /* Skip over this block. */ 5361 dataptr += sizeof (LONGEST); 5362 } 5363 5364 if (!found) 5365 trace_debug ("traceframe %d has no data for variable %d", 5366 tfnum, tsvnum); 5367 return !found; 5368 } 5369 5370 /* Read a requested block of static tracepoint data from a trace 5371 frame. */ 5372 5373 int 5374 traceframe_read_sdata (int tfnum, ULONGEST offset, 5375 unsigned char *buf, ULONGEST length, 5376 ULONGEST *nbytes) 5377 { 5378 struct traceframe *tframe; 5379 unsigned char *database, *dataptr; 5380 unsigned int datasize; 5381 unsigned short mlen; 5382 5383 trace_debug ("traceframe_read_sdata"); 5384 5385 tframe = find_traceframe (tfnum); 5386 5387 if (!tframe) 5388 { 5389 trace_debug ("traceframe %d not found", tfnum); 5390 return 1; 5391 } 5392 5393 datasize = tframe->data_size; 5394 database = &tframe->data[0]; 5395 5396 /* Iterate through a traceframe's blocks, looking for static 5397 tracepoint data. */ 5398 dataptr = traceframe_find_block_type (database, datasize, 5399 tfnum, 'S'); 5400 if (dataptr != NULL) 5401 { 5402 memcpy (&mlen, dataptr, sizeof (mlen)); 5403 dataptr += sizeof (mlen); 5404 if (offset < mlen) 5405 { 5406 if (offset + length > mlen) 5407 length = mlen - offset; 5408 5409 memcpy (buf, dataptr, length); 5410 *nbytes = length; 5411 } 5412 else 5413 *nbytes = 0; 5414 return 0; 5415 } 5416 5417 trace_debug ("traceframe %d has no static trace data", tfnum); 5418 5419 *nbytes = 0; 5420 return 0; 5421 } 5422 5423 /* Callback for traceframe_walk_blocks. Builds a traceframe-info 5424 object. DATA is pointer to a struct buffer holding the 5425 traceframe-info object being built. */ 5426 5427 static int 5428 build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data) 5429 { 5430 struct buffer *buffer = (struct buffer *) data; 5431 5432 switch (blocktype) 5433 { 5434 case 'M': 5435 { 5436 unsigned short mlen; 5437 CORE_ADDR maddr; 5438 5439 memcpy (&maddr, dataptr, sizeof (maddr)); 5440 dataptr += sizeof (maddr); 5441 memcpy (&mlen, dataptr, sizeof (mlen)); 5442 dataptr += sizeof (mlen); 5443 buffer_xml_printf (buffer, 5444 "<memory start=\"0x%s\" length=\"0x%s\"/>\n", 5445 paddress (maddr), phex_nz (mlen, sizeof (mlen))); 5446 break; 5447 } 5448 case 'V': 5449 { 5450 int vnum; 5451 5452 memcpy (&vnum, dataptr, sizeof (vnum)); 5453 buffer_xml_printf (buffer, "<tvar id=\"%d\"/>\n", vnum); 5454 break; 5455 } 5456 case 'R': 5457 case 'S': 5458 { 5459 break; 5460 } 5461 default: 5462 warning ("Unhandled trace block type (%d) '%c ' " 5463 "while building trace frame info.", 5464 blocktype, blocktype); 5465 break; 5466 } 5467 5468 return 0; 5469 } 5470 5471 /* Build a traceframe-info object for traceframe number TFNUM into 5472 BUFFER. */ 5473 5474 int 5475 traceframe_read_info (int tfnum, struct buffer *buffer) 5476 { 5477 struct traceframe *tframe; 5478 5479 trace_debug ("traceframe_read_info"); 5480 5481 tframe = find_traceframe (tfnum); 5482 5483 if (!tframe) 5484 { 5485 trace_debug ("traceframe %d not found", tfnum); 5486 return 1; 5487 } 5488 5489 buffer_grow_str (buffer, "<traceframe-info>\n"); 5490 traceframe_walk_blocks (tframe->data, tframe->data_size, 5491 tfnum, build_traceframe_info_xml, buffer); 5492 buffer_grow_str0 (buffer, "</traceframe-info>\n"); 5493 return 0; 5494 } 5495 5496 /* Return the first fast tracepoint whose jump pad contains PC. */ 5497 5498 static struct tracepoint * 5499 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc) 5500 { 5501 struct tracepoint *tpoint; 5502 5503 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) 5504 if (tpoint->type == fast_tracepoint) 5505 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end) 5506 return tpoint; 5507 5508 return NULL; 5509 } 5510 5511 /* Return the first fast tracepoint whose trampoline contains PC. */ 5512 5513 static struct tracepoint * 5514 fast_tracepoint_from_trampoline_address (CORE_ADDR pc) 5515 { 5516 struct tracepoint *tpoint; 5517 5518 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) 5519 { 5520 if (tpoint->type == fast_tracepoint 5521 && tpoint->trampoline <= pc && pc < tpoint->trampoline_end) 5522 return tpoint; 5523 } 5524 5525 return NULL; 5526 } 5527 5528 /* Return GDBserver's tracepoint that matches the IP Agent's 5529 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's 5530 address space. */ 5531 5532 static struct tracepoint * 5533 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj) 5534 { 5535 struct tracepoint *tpoint; 5536 5537 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) 5538 if (tpoint->type == fast_tracepoint) 5539 if (tpoint->obj_addr_on_target == ipa_tpoint_obj) 5540 return tpoint; 5541 5542 return NULL; 5543 } 5544 5545 #endif 5546 5547 /* The type of the object that is used to synchronize fast tracepoint 5548 collection. */ 5549 5550 typedef struct collecting_t 5551 { 5552 /* The fast tracepoint number currently collecting. */ 5553 uintptr_t tpoint; 5554 5555 /* A number that GDBserver can use to identify the thread that is 5556 presently holding the collect lock. This need not (and usually 5557 is not) the thread id, as getting the current thread ID usually 5558 requires a system call, which we want to avoid like the plague. 5559 Usually this is thread's TCB, found in the TLS (pseudo-) 5560 register, which is readable with a single insn on several 5561 architectures. */ 5562 uintptr_t thread_area; 5563 } collecting_t; 5564 5565 #ifndef IN_PROCESS_AGENT 5566 5567 void 5568 force_unlock_trace_buffer (void) 5569 { 5570 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0); 5571 } 5572 5573 /* Check if the thread identified by THREAD_AREA which is stopped at 5574 STOP_PC, is presently locking the fast tracepoint collection, and 5575 if so, gather some status of said collection. Returns 0 if the 5576 thread isn't collecting or in the jump pad at all. 1, if in the 5577 jump pad (or within gdb_collect) and hasn't executed the adjusted 5578 original insn yet (can set a breakpoint there and run to it). 2, 5579 if presently executing the adjusted original insn --- in which 5580 case, if we want to move the thread out of the jump pad, we need to 5581 single-step it until this function returns 0. */ 5582 5583 fast_tpoint_collect_result 5584 fast_tracepoint_collecting (CORE_ADDR thread_area, 5585 CORE_ADDR stop_pc, 5586 struct fast_tpoint_collect_status *status) 5587 { 5588 CORE_ADDR ipa_collecting; 5589 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end; 5590 CORE_ADDR ipa_gdb_trampoline_buffer; 5591 CORE_ADDR ipa_gdb_trampoline_buffer_end; 5592 struct tracepoint *tpoint; 5593 int needs_breakpoint; 5594 5595 /* The thread THREAD_AREA is either: 5596 5597 0. not collecting at all, not within the jump pad, or within 5598 gdb_collect or one of its callees. 5599 5600 1. in the jump pad and haven't reached gdb_collect 5601 5602 2. within gdb_collect (out of the jump pad) (collect is set) 5603 5604 3. we're in the jump pad, after gdb_collect having returned, 5605 possibly executing the adjusted insns. 5606 5607 For cases 1 and 3, `collecting' may or not be set. The jump pad 5608 doesn't have any complicated jump logic, so we can tell if the 5609 thread is executing the adjust original insn or not by just 5610 matching STOP_PC with known jump pad addresses. If we it isn't 5611 yet executing the original insn, set a breakpoint there, and let 5612 the thread run to it, so to quickly step over a possible (many 5613 insns) gdb_collect call. Otherwise, or when the breakpoint is 5614 hit, only a few (small number of) insns are left to be executed 5615 in the jump pad. Single-step the thread until it leaves the 5616 jump pad. */ 5617 5618 again: 5619 tpoint = NULL; 5620 needs_breakpoint = 0; 5621 trace_debug ("fast_tracepoint_collecting"); 5622 5623 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer, 5624 &ipa_gdb_jump_pad_buffer)) 5625 { 5626 internal_error (__FILE__, __LINE__, 5627 "error extracting `gdb_jump_pad_buffer'"); 5628 } 5629 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end, 5630 &ipa_gdb_jump_pad_buffer_end)) 5631 { 5632 internal_error (__FILE__, __LINE__, 5633 "error extracting `gdb_jump_pad_buffer_end'"); 5634 } 5635 5636 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer, 5637 &ipa_gdb_trampoline_buffer)) 5638 { 5639 internal_error (__FILE__, __LINE__, 5640 "error extracting `gdb_trampoline_buffer'"); 5641 } 5642 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end, 5643 &ipa_gdb_trampoline_buffer_end)) 5644 { 5645 internal_error (__FILE__, __LINE__, 5646 "error extracting `gdb_trampoline_buffer_end'"); 5647 } 5648 5649 if (ipa_gdb_jump_pad_buffer <= stop_pc 5650 && stop_pc < ipa_gdb_jump_pad_buffer_end) 5651 { 5652 /* We can tell which tracepoint(s) the thread is collecting by 5653 matching the jump pad address back to the tracepoint. */ 5654 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc); 5655 if (tpoint == NULL) 5656 { 5657 warning ("in jump pad, but no matching tpoint?"); 5658 return fast_tpoint_collect_result::not_collecting; 5659 } 5660 else 5661 { 5662 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); " 5663 "adj_insn(%s, %s)", 5664 tpoint->number, paddress (tpoint->address), 5665 paddress (tpoint->jump_pad), 5666 paddress (tpoint->jump_pad_end), 5667 paddress (tpoint->adjusted_insn_addr), 5668 paddress (tpoint->adjusted_insn_addr_end)); 5669 } 5670 5671 /* Definitely in the jump pad. May or may not need 5672 fast-exit-jump-pad breakpoint. */ 5673 if (tpoint->jump_pad <= stop_pc 5674 && stop_pc < tpoint->adjusted_insn_addr) 5675 needs_breakpoint = 1; 5676 } 5677 else if (ipa_gdb_trampoline_buffer <= stop_pc 5678 && stop_pc < ipa_gdb_trampoline_buffer_end) 5679 { 5680 /* We can tell which tracepoint(s) the thread is collecting by 5681 matching the trampoline address back to the tracepoint. */ 5682 tpoint = fast_tracepoint_from_trampoline_address (stop_pc); 5683 if (tpoint == NULL) 5684 { 5685 warning ("in trampoline, but no matching tpoint?"); 5686 return fast_tpoint_collect_result::not_collecting; 5687 } 5688 else 5689 { 5690 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)", 5691 tpoint->number, paddress (tpoint->address), 5692 paddress (tpoint->trampoline), 5693 paddress (tpoint->trampoline_end)); 5694 } 5695 5696 /* Have not reached jump pad yet, but treat the trampoline as a 5697 part of the jump pad that is before the adjusted original 5698 instruction. */ 5699 needs_breakpoint = 1; 5700 } 5701 else 5702 { 5703 collecting_t ipa_collecting_obj; 5704 5705 /* If `collecting' is set/locked, then the THREAD_AREA thread 5706 may or not be the one holding the lock. We have to read the 5707 lock to find out. */ 5708 5709 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 5710 &ipa_collecting)) 5711 { 5712 trace_debug ("fast_tracepoint_collecting:" 5713 " failed reading 'collecting' in the inferior"); 5714 return fast_tpoint_collect_result::not_collecting; 5715 } 5716 5717 if (!ipa_collecting) 5718 { 5719 trace_debug ("fast_tracepoint_collecting: not collecting" 5720 " (and nobody is)."); 5721 return fast_tpoint_collect_result::not_collecting; 5722 } 5723 5724 /* Some thread is collecting. Check which. */ 5725 if (read_inferior_memory (ipa_collecting, 5726 (unsigned char *) &ipa_collecting_obj, 5727 sizeof (ipa_collecting_obj)) != 0) 5728 goto again; 5729 5730 if (ipa_collecting_obj.thread_area != thread_area) 5731 { 5732 trace_debug ("fast_tracepoint_collecting: not collecting " 5733 "(another thread is)"); 5734 return fast_tpoint_collect_result::not_collecting; 5735 } 5736 5737 tpoint 5738 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint); 5739 if (tpoint == NULL) 5740 { 5741 warning ("fast_tracepoint_collecting: collecting, " 5742 "but tpoint %s not found?", 5743 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint)); 5744 return fast_tpoint_collect_result::not_collecting; 5745 } 5746 5747 /* The thread is within `gdb_collect', skip over the rest of 5748 fast tracepoint collection quickly using a breakpoint. */ 5749 needs_breakpoint = 1; 5750 } 5751 5752 /* The caller wants a bit of status detail. */ 5753 if (status != NULL) 5754 { 5755 status->tpoint_num = tpoint->number; 5756 status->tpoint_addr = tpoint->address; 5757 status->adjusted_insn_addr = tpoint->adjusted_insn_addr; 5758 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end; 5759 } 5760 5761 if (needs_breakpoint) 5762 { 5763 /* Hasn't executed the original instruction yet. Set breakpoint 5764 there, and wait till it's hit, then single-step until exiting 5765 the jump pad. */ 5766 5767 trace_debug ("\ 5768 fast_tracepoint_collecting, returning continue-until-break at %s", 5769 paddress (tpoint->adjusted_insn_addr)); 5770 5771 return fast_tpoint_collect_result::before_insn; /* continue */ 5772 } 5773 else 5774 { 5775 /* Just single-step until exiting the jump pad. */ 5776 5777 trace_debug ("fast_tracepoint_collecting, returning " 5778 "need-single-step (%s-%s)", 5779 paddress (tpoint->adjusted_insn_addr), 5780 paddress (tpoint->adjusted_insn_addr_end)); 5781 5782 return fast_tpoint_collect_result::at_insn; /* single-step */ 5783 } 5784 } 5785 5786 #endif 5787 5788 #ifdef IN_PROCESS_AGENT 5789 5790 /* The global fast tracepoint collect lock. Points to a collecting_t 5791 object built on the stack by the jump pad, if presently locked; 5792 NULL if it isn't locked. Note that this lock *must* be set while 5793 executing any *function other than the jump pad. See 5794 fast_tracepoint_collecting. */ 5795 EXTERN_C_PUSH 5796 IP_AGENT_EXPORT_VAR collecting_t *collecting; 5797 EXTERN_C_POP 5798 5799 /* This is needed for -Wmissing-declarations. */ 5800 IP_AGENT_EXPORT_FUNC void gdb_collect (struct tracepoint *tpoint, 5801 unsigned char *regs); 5802 5803 /* This routine, called from the jump pad (in asm) is designed to be 5804 called from the jump pads of fast tracepoints, thus it is on the 5805 critical path. */ 5806 5807 IP_AGENT_EXPORT_FUNC void 5808 gdb_collect (struct tracepoint *tpoint, unsigned char *regs) 5809 { 5810 struct fast_tracepoint_ctx ctx; 5811 const struct target_desc *ipa_tdesc; 5812 5813 /* Don't do anything until the trace run is completely set up. */ 5814 if (!tracing) 5815 return; 5816 5817 ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx); 5818 ctx.base.type = fast_tracepoint; 5819 ctx.regs = regs; 5820 ctx.regcache_initted = 0; 5821 /* Wrap the regblock in a register cache (in the stack, we don't 5822 want to malloc here). */ 5823 ctx.regspace = (unsigned char *) alloca (ipa_tdesc->registers_size); 5824 if (ctx.regspace == NULL) 5825 { 5826 trace_debug ("Trace buffer block allocation failed, skipping"); 5827 return; 5828 } 5829 5830 for (ctx.tpoint = tpoint; 5831 ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address; 5832 ctx.tpoint = ctx.tpoint->next) 5833 { 5834 if (!ctx.tpoint->enabled) 5835 continue; 5836 5837 /* Multiple tracepoints of different types, such as fast tracepoint and 5838 static tracepoint, can be set at the same address. */ 5839 if (ctx.tpoint->type != tpoint->type) 5840 continue; 5841 5842 /* Test the condition if present, and collect if true. */ 5843 if (ctx.tpoint->cond == NULL 5844 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx, 5845 ctx.tpoint)) 5846 { 5847 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx, 5848 ctx.tpoint->address, ctx.tpoint); 5849 5850 /* Note that this will cause original insns to be written back 5851 to where we jumped from, but that's OK because we're jumping 5852 back to the next whole instruction. This will go badly if 5853 instruction restoration is not atomic though. */ 5854 if (stopping_tracepoint 5855 || trace_buffer_is_full 5856 || expr_eval_result != expr_eval_no_error) 5857 { 5858 stop_tracing (); 5859 break; 5860 } 5861 } 5862 else 5863 { 5864 /* If there was a condition and it evaluated to false, the only 5865 way we would stop tracing is if there was an error during 5866 condition expression evaluation. */ 5867 if (expr_eval_result != expr_eval_no_error) 5868 { 5869 stop_tracing (); 5870 break; 5871 } 5872 } 5873 } 5874 } 5875 5876 /* These global variables points to the corresponding functions. This is 5877 necessary on powerpc64, where asking for function symbol address from gdb 5878 results in returning the actual code pointer, instead of the descriptor 5879 pointer. */ 5880 5881 typedef void (*gdb_collect_ptr_type) (struct tracepoint *, unsigned char *); 5882 typedef ULONGEST (*get_raw_reg_ptr_type) (const unsigned char *, int); 5883 typedef LONGEST (*get_trace_state_variable_value_ptr_type) (int); 5884 typedef void (*set_trace_state_variable_value_ptr_type) (int, LONGEST); 5885 5886 EXTERN_C_PUSH 5887 IP_AGENT_EXPORT_VAR gdb_collect_ptr_type gdb_collect_ptr = gdb_collect; 5888 IP_AGENT_EXPORT_VAR get_raw_reg_ptr_type get_raw_reg_ptr = get_raw_reg; 5889 IP_AGENT_EXPORT_VAR get_trace_state_variable_value_ptr_type 5890 get_trace_state_variable_value_ptr = get_trace_state_variable_value; 5891 IP_AGENT_EXPORT_VAR set_trace_state_variable_value_ptr_type 5892 set_trace_state_variable_value_ptr = set_trace_state_variable_value; 5893 EXTERN_C_POP 5894 5895 #endif 5896 5897 #ifndef IN_PROCESS_AGENT 5898 5899 CORE_ADDR 5900 get_raw_reg_func_addr (void) 5901 { 5902 CORE_ADDR res; 5903 if (read_inferior_data_pointer (ipa_sym_addrs.addr_get_raw_reg_ptr, &res)) 5904 { 5905 error ("error extracting get_raw_reg_ptr"); 5906 return 0; 5907 } 5908 return res; 5909 } 5910 5911 CORE_ADDR 5912 get_get_tsv_func_addr (void) 5913 { 5914 CORE_ADDR res; 5915 if (read_inferior_data_pointer ( 5916 ipa_sym_addrs.addr_get_trace_state_variable_value_ptr, &res)) 5917 { 5918 error ("error extracting get_trace_state_variable_value_ptr"); 5919 return 0; 5920 } 5921 return res; 5922 } 5923 5924 CORE_ADDR 5925 get_set_tsv_func_addr (void) 5926 { 5927 CORE_ADDR res; 5928 if (read_inferior_data_pointer ( 5929 ipa_sym_addrs.addr_set_trace_state_variable_value_ptr, &res)) 5930 { 5931 error ("error extracting set_trace_state_variable_value_ptr"); 5932 return 0; 5933 } 5934 return res; 5935 } 5936 5937 static void 5938 compile_tracepoint_condition (struct tracepoint *tpoint, 5939 CORE_ADDR *jump_entry) 5940 { 5941 CORE_ADDR entry_point = *jump_entry; 5942 enum eval_result_type err; 5943 5944 trace_debug ("Starting condition compilation for tracepoint %d\n", 5945 tpoint->number); 5946 5947 /* Initialize the global pointer to the code being built. */ 5948 current_insn_ptr = *jump_entry; 5949 5950 emit_prologue (); 5951 5952 err = compile_bytecodes (tpoint->cond); 5953 5954 if (err == expr_eval_no_error) 5955 { 5956 emit_epilogue (); 5957 5958 /* Record the beginning of the compiled code. */ 5959 tpoint->compiled_cond = entry_point; 5960 5961 trace_debug ("Condition compilation for tracepoint %d complete\n", 5962 tpoint->number); 5963 } 5964 else 5965 { 5966 /* Leave the unfinished code in situ, but don't point to it. */ 5967 5968 tpoint->compiled_cond = 0; 5969 5970 trace_debug ("Condition compilation for tracepoint %d failed, " 5971 "error code %d", 5972 tpoint->number, err); 5973 } 5974 5975 /* Update the code pointer passed in. Note that we do this even if 5976 the compile fails, so that we can look at the partial results 5977 instead of letting them be overwritten. */ 5978 *jump_entry = current_insn_ptr; 5979 5980 /* Leave a gap, to aid dump decipherment. */ 5981 *jump_entry += 16; 5982 } 5983 5984 /* The base pointer of the IPA's heap. This is the only memory the 5985 IPA is allowed to use. The IPA should _not_ call the inferior's 5986 `malloc' during operation. That'd be slow, and, most importantly, 5987 it may not be safe. We may be collecting a tracepoint in a signal 5988 handler, for example. */ 5989 static CORE_ADDR target_tp_heap; 5990 5991 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned 5992 to 8 bytes. */ 5993 5994 static CORE_ADDR 5995 target_malloc (ULONGEST size) 5996 { 5997 CORE_ADDR ptr; 5998 5999 if (target_tp_heap == 0) 6000 { 6001 /* We have the pointer *address*, need what it points to. */ 6002 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer, 6003 &target_tp_heap)) 6004 { 6005 internal_error (__FILE__, __LINE__, 6006 "couldn't get target heap head pointer"); 6007 } 6008 } 6009 6010 ptr = target_tp_heap; 6011 target_tp_heap += size; 6012 6013 /* Pad to 8-byte alignment. */ 6014 target_tp_heap = ((target_tp_heap + 7) & ~0x7); 6015 6016 return ptr; 6017 } 6018 6019 static CORE_ADDR 6020 download_agent_expr (struct agent_expr *expr) 6021 { 6022 CORE_ADDR expr_addr; 6023 CORE_ADDR expr_bytes; 6024 6025 expr_addr = target_malloc (sizeof (*expr)); 6026 target_write_memory (expr_addr, (unsigned char *) expr, sizeof (*expr)); 6027 6028 expr_bytes = target_malloc (expr->length); 6029 write_inferior_data_pointer (expr_addr + offsetof (struct agent_expr, bytes), 6030 expr_bytes); 6031 target_write_memory (expr_bytes, expr->bytes, expr->length); 6032 6033 return expr_addr; 6034 } 6035 6036 /* Align V up to N bits. */ 6037 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1)) 6038 6039 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */ 6040 6041 static void 6042 download_tracepoint_1 (struct tracepoint *tpoint) 6043 { 6044 struct tracepoint target_tracepoint; 6045 CORE_ADDR tpptr = 0; 6046 6047 gdb_assert (tpoint->type == fast_tracepoint 6048 || tpoint->type == static_tracepoint); 6049 6050 if (tpoint->cond != NULL && target_emit_ops () != NULL) 6051 { 6052 CORE_ADDR jentry, jump_entry; 6053 6054 jentry = jump_entry = get_jump_space_head (); 6055 6056 if (tpoint->cond != NULL) 6057 { 6058 /* Pad to 8-byte alignment. (needed?) */ 6059 /* Actually this should be left for the target to 6060 decide. */ 6061 jentry = UALIGN (jentry, 8); 6062 6063 compile_tracepoint_condition (tpoint, &jentry); 6064 } 6065 6066 /* Pad to 8-byte alignment. */ 6067 jentry = UALIGN (jentry, 8); 6068 claim_jump_space (jentry - jump_entry); 6069 } 6070 6071 target_tracepoint = *tpoint; 6072 6073 tpptr = target_malloc (sizeof (*tpoint)); 6074 tpoint->obj_addr_on_target = tpptr; 6075 6076 /* Write the whole object. We'll fix up its pointers in a bit. 6077 Assume no next for now. This is fixed up above on the next 6078 iteration, if there's any. */ 6079 target_tracepoint.next = NULL; 6080 /* Need to clear this here too, since we're downloading the 6081 tracepoints before clearing our own copy. */ 6082 target_tracepoint.hit_count = 0; 6083 6084 target_write_memory (tpptr, (unsigned char *) &target_tracepoint, 6085 sizeof (target_tracepoint)); 6086 6087 if (tpoint->cond) 6088 write_inferior_data_pointer (tpptr 6089 + offsetof (struct tracepoint, cond), 6090 download_agent_expr (tpoint->cond)); 6091 6092 if (tpoint->numactions) 6093 { 6094 int i; 6095 CORE_ADDR actions_array; 6096 6097 /* The pointers array. */ 6098 actions_array 6099 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions); 6100 write_inferior_data_pointer (tpptr + offsetof (struct tracepoint, 6101 actions), 6102 actions_array); 6103 6104 /* Now for each pointer, download the action. */ 6105 for (i = 0; i < tpoint->numactions; i++) 6106 { 6107 struct tracepoint_action *action = tpoint->actions[i]; 6108 CORE_ADDR ipa_action = tracepoint_action_download (action); 6109 6110 if (ipa_action != 0) 6111 write_inferior_data_pointer (actions_array 6112 + i * sizeof (*tpoint->actions), 6113 ipa_action); 6114 } 6115 } 6116 } 6117 6118 #define IPA_PROTO_FAST_TRACE_FLAG 0 6119 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2 6120 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10 6121 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18 6122 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22 6123 6124 /* Send a command to agent to download and install tracepoint TPOINT. */ 6125 6126 static int 6127 tracepoint_send_agent (struct tracepoint *tpoint) 6128 { 6129 char buf[IPA_CMD_BUF_SIZE]; 6130 char *p; 6131 int i, ret; 6132 6133 p = buf; 6134 strcpy (p, "FastTrace:"); 6135 p += 10; 6136 6137 COPY_FIELD_TO_BUF (p, tpoint, number); 6138 COPY_FIELD_TO_BUF (p, tpoint, address); 6139 COPY_FIELD_TO_BUF (p, tpoint, type); 6140 COPY_FIELD_TO_BUF (p, tpoint, enabled); 6141 COPY_FIELD_TO_BUF (p, tpoint, step_count); 6142 COPY_FIELD_TO_BUF (p, tpoint, pass_count); 6143 COPY_FIELD_TO_BUF (p, tpoint, numactions); 6144 COPY_FIELD_TO_BUF (p, tpoint, hit_count); 6145 COPY_FIELD_TO_BUF (p, tpoint, traceframe_usage); 6146 COPY_FIELD_TO_BUF (p, tpoint, compiled_cond); 6147 COPY_FIELD_TO_BUF (p, tpoint, orig_size); 6148 6149 /* condition */ 6150 p = agent_expr_send (p, tpoint->cond); 6151 6152 /* tracepoint_action */ 6153 for (i = 0; i < tpoint->numactions; i++) 6154 { 6155 struct tracepoint_action *action = tpoint->actions[i]; 6156 6157 p[0] = action->type; 6158 p = tracepoint_action_send (&p[1], action); 6159 } 6160 6161 get_jump_space_head (); 6162 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that 6163 agent can use jump pad from it. */ 6164 if (tpoint->type == fast_tracepoint) 6165 { 6166 memcpy (p, &gdb_jump_pad_head, 8); 6167 p += 8; 6168 } 6169 6170 ret = run_inferior_command (buf, (int) (ptrdiff_t) (p - buf)); 6171 if (ret) 6172 return ret; 6173 6174 if (!startswith (buf, "OK")) 6175 return 1; 6176 6177 /* The value of tracepoint's target address is stored in BUF. */ 6178 memcpy (&tpoint->obj_addr_on_target, 6179 &buf[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET], 8); 6180 6181 if (tpoint->type == fast_tracepoint) 6182 { 6183 unsigned char *insn 6184 = (unsigned char *) &buf[IPA_PROTO_FAST_TRACE_FJUMP_INSN]; 6185 int fjump_size; 6186 6187 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n", 6188 (unsigned int) tpoint->obj_addr_on_target, 6189 (unsigned int) gdb_jump_pad_head); 6190 6191 memcpy (&gdb_jump_pad_head, &buf[IPA_PROTO_FAST_TRACE_JUMP_PAD], 8); 6192 6193 /* This has been done in agent. We should also set up record for it. */ 6194 memcpy (&fjump_size, &buf[IPA_PROTO_FAST_TRACE_FJUMP_SIZE], 4); 6195 /* Wire it in. */ 6196 tpoint->handle 6197 = set_fast_tracepoint_jump (tpoint->address, insn, fjump_size); 6198 } 6199 6200 return 0; 6201 } 6202 6203 static void 6204 download_tracepoint (struct tracepoint *tpoint) 6205 { 6206 struct tracepoint *tp, *tp_prev; 6207 6208 if (tpoint->type != fast_tracepoint 6209 && tpoint->type != static_tracepoint) 6210 return; 6211 6212 download_tracepoint_1 (tpoint); 6213 6214 /* Find the previous entry of TPOINT, which is fast tracepoint or 6215 static tracepoint. */ 6216 tp_prev = NULL; 6217 for (tp = tracepoints; tp != tpoint; tp = tp->next) 6218 { 6219 if (tp->type == fast_tracepoint || tp->type == static_tracepoint) 6220 tp_prev = tp; 6221 } 6222 6223 if (tp_prev) 6224 { 6225 CORE_ADDR tp_prev_target_next_addr; 6226 6227 /* Insert TPOINT after TP_PREV in IPA. */ 6228 if (read_inferior_data_pointer (tp_prev->obj_addr_on_target 6229 + offsetof (struct tracepoint, next), 6230 &tp_prev_target_next_addr)) 6231 { 6232 internal_error (__FILE__, __LINE__, 6233 "error reading `tp_prev->next'"); 6234 } 6235 6236 /* tpoint->next = tp_prev->next */ 6237 write_inferior_data_pointer (tpoint->obj_addr_on_target 6238 + offsetof (struct tracepoint, next), 6239 tp_prev_target_next_addr); 6240 /* tp_prev->next = tpoint */ 6241 write_inferior_data_pointer (tp_prev->obj_addr_on_target 6242 + offsetof (struct tracepoint, next), 6243 tpoint->obj_addr_on_target); 6244 } 6245 else 6246 /* First object in list, set the head pointer in the 6247 inferior. */ 6248 write_inferior_data_pointer (ipa_sym_addrs.addr_tracepoints, 6249 tpoint->obj_addr_on_target); 6250 6251 } 6252 6253 static void 6254 download_trace_state_variables (void) 6255 { 6256 CORE_ADDR ptr = 0, prev_ptr = 0; 6257 struct trace_state_variable *tsv; 6258 6259 /* Start out empty. */ 6260 write_inferior_data_pointer (ipa_sym_addrs.addr_trace_state_variables, 0); 6261 6262 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next) 6263 { 6264 struct trace_state_variable target_tsv; 6265 6266 /* TSV's with a getter have been initialized equally in both the 6267 inferior and GDBserver. Skip them. */ 6268 if (tsv->getter != NULL) 6269 continue; 6270 6271 target_tsv = *tsv; 6272 6273 prev_ptr = ptr; 6274 ptr = target_malloc (sizeof (*tsv)); 6275 6276 if (tsv == trace_state_variables) 6277 { 6278 /* First object in list, set the head pointer in the 6279 inferior. */ 6280 6281 write_inferior_data_pointer (ipa_sym_addrs.addr_trace_state_variables, 6282 ptr); 6283 } 6284 else 6285 { 6286 write_inferior_data_pointer (prev_ptr 6287 + offsetof (struct trace_state_variable, 6288 next), 6289 ptr); 6290 } 6291 6292 /* Write the whole object. We'll fix up its pointers in a bit. 6293 Assume no next, fixup when needed. */ 6294 target_tsv.next = NULL; 6295 6296 target_write_memory (ptr, (unsigned char *) &target_tsv, 6297 sizeof (target_tsv)); 6298 6299 if (tsv->name != NULL) 6300 { 6301 size_t size = strlen (tsv->name) + 1; 6302 CORE_ADDR name_addr = target_malloc (size); 6303 target_write_memory (name_addr, 6304 (unsigned char *) tsv->name, size); 6305 write_inferior_data_pointer (ptr 6306 + offsetof (struct trace_state_variable, 6307 name), 6308 name_addr); 6309 } 6310 6311 gdb_assert (tsv->getter == NULL); 6312 } 6313 6314 if (prev_ptr != 0) 6315 { 6316 /* Fixup the next pointer in the last item in the list. */ 6317 write_inferior_data_pointer (prev_ptr 6318 + offsetof (struct trace_state_variable, 6319 next), 0); 6320 } 6321 } 6322 6323 /* Upload complete trace frames out of the IP Agent's trace buffer 6324 into GDBserver's trace buffer. This always uploads either all or 6325 no trace frames. This is the counter part of 6326 `trace_alloc_trace_buffer'. See its description of the atomic 6327 syncing mechanism. */ 6328 6329 static void 6330 upload_fast_traceframes (void) 6331 { 6332 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count; 6333 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy; 6334 CORE_ADDR tf; 6335 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl; 6336 unsigned int curr_tbctrl_idx; 6337 unsigned int ipa_trace_buffer_ctrl_curr; 6338 unsigned int ipa_trace_buffer_ctrl_curr_old; 6339 CORE_ADDR ipa_trace_buffer_ctrl_addr; 6340 struct breakpoint *about_to_request_buffer_space_bkpt; 6341 CORE_ADDR ipa_trace_buffer_lo; 6342 CORE_ADDR ipa_trace_buffer_hi; 6343 6344 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 6345 &ipa_traceframe_read_count_racy)) 6346 { 6347 /* This will happen in most targets if the current thread is 6348 running. */ 6349 return; 6350 } 6351 6352 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 6353 &ipa_traceframe_write_count_racy)) 6354 return; 6355 6356 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)", 6357 ipa_traceframe_write_count_racy 6358 - ipa_traceframe_read_count_racy, 6359 ipa_traceframe_write_count_racy, 6360 ipa_traceframe_read_count_racy); 6361 6362 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy) 6363 return; 6364 6365 about_to_request_buffer_space_bkpt 6366 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space, 6367 NULL); 6368 6369 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 6370 &ipa_trace_buffer_ctrl_curr)) 6371 return; 6372 6373 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr; 6374 6375 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK; 6376 6377 { 6378 unsigned int prev, counter; 6379 6380 /* Update the token, with new counters, and the GDBserver stamp 6381 bit. Alway reuse the current TBC index. */ 6382 prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR; 6383 counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR; 6384 6385 ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT 6386 | (prev << 12) 6387 | counter 6388 | curr_tbctrl_idx); 6389 } 6390 6391 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 6392 ipa_trace_buffer_ctrl_curr)) 6393 return; 6394 6395 trace_debug ("Lib: Committed %08x -> %08x", 6396 ipa_trace_buffer_ctrl_curr_old, 6397 ipa_trace_buffer_ctrl_curr); 6398 6399 /* Re-read these, now that we've installed the 6400 `about_to_request_buffer_space' breakpoint/lock. A thread could 6401 have finished a traceframe between the last read of these 6402 counters and setting the breakpoint above. If we start 6403 uploading, we never want to leave this function with 6404 traceframe_read_count != 0, otherwise, GDBserver could end up 6405 incrementing the counter tokens more than once (due to event loop 6406 nesting), which would break the IP agent's "effective" detection 6407 (see trace_alloc_trace_buffer). */ 6408 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 6409 &ipa_traceframe_read_count)) 6410 return; 6411 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 6412 &ipa_traceframe_write_count)) 6413 return; 6414 6415 if (debug_threads) 6416 { 6417 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)", 6418 ipa_traceframe_write_count - ipa_traceframe_read_count, 6419 ipa_traceframe_write_count, ipa_traceframe_read_count); 6420 6421 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy 6422 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy) 6423 trace_debug ("note that ipa_traceframe_count's parts changed"); 6424 } 6425 6426 /* Get the address of the current TBC object (the IP agent has an 6427 array of 3 such objects). The index is stored in the TBC 6428 token. */ 6429 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl; 6430 ipa_trace_buffer_ctrl_addr 6431 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx; 6432 6433 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr, 6434 (unsigned char *) &ipa_trace_buffer_ctrl, 6435 sizeof (struct ipa_trace_buffer_control))) 6436 return; 6437 6438 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo, 6439 &ipa_trace_buffer_lo)) 6440 return; 6441 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi, 6442 &ipa_trace_buffer_hi)) 6443 return; 6444 6445 /* Offsets are easier to grok for debugging than raw addresses, 6446 especially for the small trace buffer sizes that are useful for 6447 testing. */ 6448 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d " 6449 "endfree=%d wrap=%d hi=%d", 6450 curr_tbctrl_idx, 6451 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo), 6452 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo), 6453 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo), 6454 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo), 6455 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo)); 6456 6457 /* Note that the IPA's buffer is always circular. */ 6458 6459 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start) 6460 6461 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \ 6462 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size) 6463 6464 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \ 6465 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \ 6466 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \ 6467 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \ 6468 : 0)) 6469 6470 tf = IPA_FIRST_TRACEFRAME (); 6471 6472 while (ipa_traceframe_write_count - ipa_traceframe_read_count) 6473 { 6474 struct tracepoint *tpoint; 6475 struct traceframe *tframe; 6476 unsigned char *block; 6477 struct traceframe ipa_tframe; 6478 6479 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe, 6480 offsetof (struct traceframe, data))) 6481 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf)); 6482 6483 if (ipa_tframe.tpnum == 0) 6484 { 6485 internal_error (__FILE__, __LINE__, 6486 "Uploading: No (more) fast traceframes, but" 6487 " ipa_traceframe_count == %u??\n", 6488 ipa_traceframe_write_count 6489 - ipa_traceframe_read_count); 6490 } 6491 6492 /* Note that this will be incorrect for multi-location 6493 tracepoints... */ 6494 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum); 6495 6496 tframe = add_traceframe (tpoint); 6497 if (tframe == NULL) 6498 { 6499 trace_buffer_is_full = 1; 6500 trace_debug ("Uploading: trace buffer is full"); 6501 } 6502 else 6503 { 6504 /* Copy the whole set of blocks in one go for now. FIXME: 6505 split this in smaller blocks. */ 6506 block = add_traceframe_block (tframe, tpoint, 6507 ipa_tframe.data_size); 6508 if (block != NULL) 6509 { 6510 if (read_inferior_memory (tf 6511 + offsetof (struct traceframe, data), 6512 block, ipa_tframe.data_size)) 6513 error ("Uploading: Couldn't read traceframe data at %s\n", 6514 paddress (tf + offsetof (struct traceframe, data))); 6515 } 6516 6517 trace_debug ("Uploading: traceframe didn't fit"); 6518 finish_traceframe (tframe); 6519 } 6520 6521 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe); 6522 6523 /* If we freed the traceframe that wrapped around, go back 6524 to the non-wrap case. */ 6525 if (tf < ipa_trace_buffer_ctrl.start) 6526 { 6527 trace_debug ("Lib: Discarding past the wraparound"); 6528 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi; 6529 } 6530 ipa_trace_buffer_ctrl.start = tf; 6531 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start; 6532 ++ipa_traceframe_read_count; 6533 6534 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free 6535 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free) 6536 { 6537 trace_debug ("Lib: buffer is fully empty. " 6538 "Trace buffer [%d] start=%d free=%d endfree=%d", 6539 curr_tbctrl_idx, 6540 (int) (ipa_trace_buffer_ctrl.start 6541 - ipa_trace_buffer_lo), 6542 (int) (ipa_trace_buffer_ctrl.free 6543 - ipa_trace_buffer_lo), 6544 (int) (ipa_trace_buffer_ctrl.end_free 6545 - ipa_trace_buffer_lo)); 6546 6547 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo; 6548 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo; 6549 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi; 6550 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi; 6551 } 6552 6553 trace_debug ("Uploaded a traceframe\n" 6554 "Lib: Trace buffer [%d] start=%d free=%d " 6555 "endfree=%d wrap=%d hi=%d", 6556 curr_tbctrl_idx, 6557 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo), 6558 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo), 6559 (int) (ipa_trace_buffer_ctrl.end_free 6560 - ipa_trace_buffer_lo), 6561 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo), 6562 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo)); 6563 } 6564 6565 if (target_write_memory (ipa_trace_buffer_ctrl_addr, 6566 (unsigned char *) &ipa_trace_buffer_ctrl, 6567 sizeof (struct ipa_trace_buffer_control))) 6568 return; 6569 6570 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count, 6571 ipa_traceframe_read_count); 6572 6573 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx); 6574 6575 target_pause_all (true); 6576 6577 delete_breakpoint (about_to_request_buffer_space_bkpt); 6578 about_to_request_buffer_space_bkpt = NULL; 6579 6580 target_unpause_all (true); 6581 6582 if (trace_buffer_is_full) 6583 stop_tracing (); 6584 } 6585 #endif 6586 6587 #ifdef IN_PROCESS_AGENT 6588 6589 IP_AGENT_EXPORT_VAR int ust_loaded; 6590 IP_AGENT_EXPORT_VAR char cmd_buf[IPA_CMD_BUF_SIZE]; 6591 6592 #ifdef HAVE_UST 6593 6594 /* Static tracepoints. */ 6595 6596 /* UST puts a "struct tracepoint" in the global namespace, which 6597 conflicts with our tracepoint. Arguably, being a library, it 6598 shouldn't take ownership of such a generic name. We work around it 6599 here. */ 6600 #define tracepoint ust_tracepoint 6601 #include <ust/ust.h> 6602 #undef tracepoint 6603 6604 extern int serialize_to_text (char *outbuf, int bufsize, 6605 const char *fmt, va_list ap); 6606 6607 #define GDB_PROBE_NAME "gdb" 6608 6609 /* We dynamically search for the UST symbols instead of linking them 6610 in. This lets the user decide if the application uses static 6611 tracepoints, instead of always pulling libust.so in. This vector 6612 holds pointers to all functions we care about. */ 6613 6614 static struct 6615 { 6616 int (*serialize_to_text) (char *outbuf, int bufsize, 6617 const char *fmt, va_list ap); 6618 6619 int (*ltt_probe_register) (struct ltt_available_probe *pdata); 6620 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata); 6621 6622 int (*ltt_marker_connect) (const char *channel, const char *mname, 6623 const char *pname); 6624 int (*ltt_marker_disconnect) (const char *channel, const char *mname, 6625 const char *pname); 6626 6627 void (*marker_iter_start) (struct marker_iter *iter); 6628 void (*marker_iter_next) (struct marker_iter *iter); 6629 void (*marker_iter_stop) (struct marker_iter *iter); 6630 void (*marker_iter_reset) (struct marker_iter *iter); 6631 } ust_ops; 6632 6633 #include <dlfcn.h> 6634 6635 /* Cast through typeof to catch incompatible API changes. Since UST 6636 only builds with gcc, we can freely use gcc extensions here 6637 too. */ 6638 #define GET_UST_SYM(SYM) \ 6639 do \ 6640 { \ 6641 if (ust_ops.SYM == NULL) \ 6642 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \ 6643 if (ust_ops.SYM == NULL) \ 6644 return 0; \ 6645 } while (0) 6646 6647 #define USTF(SYM) ust_ops.SYM 6648 6649 /* Get pointers to all libust.so functions we care about. */ 6650 6651 static int 6652 dlsym_ust (void) 6653 { 6654 GET_UST_SYM (serialize_to_text); 6655 6656 GET_UST_SYM (ltt_probe_register); 6657 GET_UST_SYM (ltt_probe_unregister); 6658 GET_UST_SYM (ltt_marker_connect); 6659 GET_UST_SYM (ltt_marker_disconnect); 6660 6661 GET_UST_SYM (marker_iter_start); 6662 GET_UST_SYM (marker_iter_next); 6663 GET_UST_SYM (marker_iter_stop); 6664 GET_UST_SYM (marker_iter_reset); 6665 6666 ust_loaded = 1; 6667 return 1; 6668 } 6669 6670 /* Given an UST marker, return the matching gdb static tracepoint. 6671 The match is done by address. */ 6672 6673 static struct tracepoint * 6674 ust_marker_to_static_tracepoint (const struct marker *mdata) 6675 { 6676 struct tracepoint *tpoint; 6677 6678 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next) 6679 { 6680 if (tpoint->type != static_tracepoint) 6681 continue; 6682 6683 if (tpoint->address == (uintptr_t) mdata->location) 6684 return tpoint; 6685 } 6686 6687 return NULL; 6688 } 6689 6690 /* The probe function we install on lttng/ust markers. Whenever a 6691 probed ust marker is hit, this function is called. This is similar 6692 to gdb_collect, only for static tracepoints, instead of fast 6693 tracepoints. */ 6694 6695 static void 6696 gdb_probe (const struct marker *mdata, void *probe_private, 6697 struct registers *regs, void *call_private, 6698 const char *fmt, va_list *args) 6699 { 6700 struct tracepoint *tpoint; 6701 struct static_tracepoint_ctx ctx; 6702 const struct target_desc *ipa_tdesc; 6703 6704 /* Don't do anything until the trace run is completely set up. */ 6705 if (!tracing) 6706 { 6707 trace_debug ("gdb_probe: not tracing\n"); 6708 return; 6709 } 6710 6711 ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx); 6712 ctx.base.type = static_tracepoint; 6713 ctx.regcache_initted = 0; 6714 ctx.regs = regs; 6715 ctx.fmt = fmt; 6716 ctx.args = args; 6717 6718 /* Wrap the regblock in a register cache (in the stack, we don't 6719 want to malloc here). */ 6720 ctx.regspace = alloca (ipa_tdesc->registers_size); 6721 if (ctx.regspace == NULL) 6722 { 6723 trace_debug ("Trace buffer block allocation failed, skipping"); 6724 return; 6725 } 6726 6727 tpoint = ust_marker_to_static_tracepoint (mdata); 6728 if (tpoint == NULL) 6729 { 6730 trace_debug ("gdb_probe: marker not known: " 6731 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"", 6732 mdata->location, mdata->channel, 6733 mdata->name, mdata->format); 6734 return; 6735 } 6736 6737 if (!tpoint->enabled) 6738 { 6739 trace_debug ("gdb_probe: tracepoint disabled"); 6740 return; 6741 } 6742 6743 ctx.tpoint = tpoint; 6744 6745 trace_debug ("gdb_probe: collecting marker: " 6746 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"", 6747 mdata->location, mdata->channel, 6748 mdata->name, mdata->format); 6749 6750 /* Test the condition if present, and collect if true. */ 6751 if (tpoint->cond == NULL 6752 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx, 6753 tpoint)) 6754 { 6755 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx, 6756 tpoint->address, tpoint); 6757 6758 if (stopping_tracepoint 6759 || trace_buffer_is_full 6760 || expr_eval_result != expr_eval_no_error) 6761 stop_tracing (); 6762 } 6763 else 6764 { 6765 /* If there was a condition and it evaluated to false, the only 6766 way we would stop tracing is if there was an error during 6767 condition expression evaluation. */ 6768 if (expr_eval_result != expr_eval_no_error) 6769 stop_tracing (); 6770 } 6771 } 6772 6773 /* Called if the gdb static tracepoint requested collecting "$_sdata", 6774 static tracepoint string data. This is a string passed to the 6775 tracing library by the user, at the time of the tracepoint marker 6776 call. E.g., in the UST marker call: 6777 6778 trace_mark (ust, bar33, "str %s", "FOOBAZ"); 6779 6780 the collected data is "str FOOBAZ". 6781 */ 6782 6783 static void 6784 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, 6785 struct traceframe *tframe) 6786 { 6787 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx; 6788 unsigned char *bufspace; 6789 int size; 6790 va_list copy; 6791 unsigned short blocklen; 6792 6793 if (umd == NULL) 6794 { 6795 trace_debug ("Wanted to collect static trace data, " 6796 "but there's no static trace data"); 6797 return; 6798 } 6799 6800 va_copy (copy, *umd->args); 6801 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy); 6802 va_end (copy); 6803 6804 trace_debug ("Want to collect ust data"); 6805 6806 /* 'S' + size + string */ 6807 bufspace = add_traceframe_block (tframe, umd->tpoint, 6808 1 + sizeof (blocklen) + size + 1); 6809 if (bufspace == NULL) 6810 { 6811 trace_debug ("Trace buffer block allocation failed, skipping"); 6812 return; 6813 } 6814 6815 /* Identify a static trace data block. */ 6816 *bufspace = 'S'; 6817 6818 blocklen = size + 1; 6819 memcpy (bufspace + 1, &blocklen, sizeof (blocklen)); 6820 6821 va_copy (copy, *umd->args); 6822 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen), 6823 size + 1, umd->fmt, copy); 6824 va_end (copy); 6825 6826 trace_debug ("Storing static tracepoint data in regblock: %s", 6827 bufspace + 1 + sizeof (blocklen)); 6828 } 6829 6830 /* The probe to register with lttng/ust. */ 6831 static struct ltt_available_probe gdb_ust_probe = 6832 { 6833 GDB_PROBE_NAME, 6834 NULL, 6835 gdb_probe, 6836 }; 6837 6838 #endif /* HAVE_UST */ 6839 #endif /* IN_PROCESS_AGENT */ 6840 6841 #ifndef IN_PROCESS_AGENT 6842 6843 /* Ask the in-process agent to run a command. Since we don't want to 6844 have to handle the IPA hitting breakpoints while running the 6845 command, we pause all threads, remove all breakpoints, and then set 6846 the helper thread re-running. We communicate with the helper 6847 thread by means of direct memory xfering, and a socket for 6848 synchronization. */ 6849 6850 static int 6851 run_inferior_command (char *cmd, int len) 6852 { 6853 int err = -1; 6854 int pid = current_ptid.pid (); 6855 6856 trace_debug ("run_inferior_command: running: %s", cmd); 6857 6858 target_pause_all (false); 6859 uninsert_all_breakpoints (); 6860 6861 err = agent_run_command (pid, (const char *) cmd, len); 6862 6863 reinsert_all_breakpoints (); 6864 target_unpause_all (false); 6865 6866 return err; 6867 } 6868 6869 #else /* !IN_PROCESS_AGENT */ 6870 6871 #include <sys/socket.h> 6872 #include <sys/un.h> 6873 6874 #ifndef UNIX_PATH_MAX 6875 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path) 6876 #endif 6877 6878 /* Where we put the socked used for synchronization. */ 6879 #define SOCK_DIR P_tmpdir 6880 6881 /* Thread ID of the helper thread. GDBserver reads this to know which 6882 is the help thread. This is an LWP id on Linux. */ 6883 EXTERN_C_PUSH 6884 IP_AGENT_EXPORT_VAR int helper_thread_id; 6885 EXTERN_C_POP 6886 6887 static int 6888 init_named_socket (const char *name) 6889 { 6890 int result, fd; 6891 struct sockaddr_un addr; 6892 6893 result = fd = socket (PF_UNIX, SOCK_STREAM, 0); 6894 if (result == -1) 6895 { 6896 warning ("socket creation failed: %s", safe_strerror (errno)); 6897 return -1; 6898 } 6899 6900 addr.sun_family = AF_UNIX; 6901 6902 strncpy (addr.sun_path, name, UNIX_PATH_MAX); 6903 addr.sun_path[UNIX_PATH_MAX - 1] = '\0'; 6904 6905 result = access (name, F_OK); 6906 if (result == 0) 6907 { 6908 /* File exists. */ 6909 result = unlink (name); 6910 if (result == -1) 6911 { 6912 warning ("unlink failed: %s", safe_strerror (errno)); 6913 close (fd); 6914 return -1; 6915 } 6916 warning ("socket %s already exists; overwriting", name); 6917 } 6918 6919 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr)); 6920 if (result == -1) 6921 { 6922 warning ("bind failed: %s", safe_strerror (errno)); 6923 close (fd); 6924 return -1; 6925 } 6926 6927 result = listen (fd, 1); 6928 if (result == -1) 6929 { 6930 warning ("listen: %s", safe_strerror (errno)); 6931 close (fd); 6932 return -1; 6933 } 6934 6935 return fd; 6936 } 6937 6938 static char agent_socket_name[UNIX_PATH_MAX]; 6939 6940 static int 6941 gdb_agent_socket_init (void) 6942 { 6943 int result, fd; 6944 6945 result = xsnprintf (agent_socket_name, UNIX_PATH_MAX, "%s/gdb_ust%d", 6946 SOCK_DIR, getpid ()); 6947 if (result >= UNIX_PATH_MAX) 6948 { 6949 trace_debug ("string overflow allocating socket name"); 6950 return -1; 6951 } 6952 6953 fd = init_named_socket (agent_socket_name); 6954 if (fd < 0) 6955 warning ("Error initializing named socket (%s) for communication with the " 6956 "ust helper thread. Check that directory exists and that it " 6957 "is writable.", agent_socket_name); 6958 6959 return fd; 6960 } 6961 6962 #ifdef HAVE_UST 6963 6964 /* The next marker to be returned on a qTsSTM command. */ 6965 static const struct marker *next_st; 6966 6967 /* Returns the first known marker. */ 6968 6969 struct marker * 6970 first_marker (void) 6971 { 6972 struct marker_iter iter; 6973 6974 USTF(marker_iter_reset) (&iter); 6975 USTF(marker_iter_start) (&iter); 6976 6977 return iter.marker; 6978 } 6979 6980 /* Returns the marker following M. */ 6981 6982 const struct marker * 6983 next_marker (const struct marker *m) 6984 { 6985 struct marker_iter iter; 6986 6987 USTF(marker_iter_reset) (&iter); 6988 USTF(marker_iter_start) (&iter); 6989 6990 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter)) 6991 { 6992 if (iter.marker == m) 6993 { 6994 USTF(marker_iter_next) (&iter); 6995 return iter.marker; 6996 } 6997 } 6998 6999 return NULL; 7000 } 7001 7002 /* Return an hexstr version of the STR C string, fit for sending to 7003 GDB. */ 7004 7005 static char * 7006 cstr_to_hexstr (const char *str) 7007 { 7008 int len = strlen (str); 7009 char *hexstr = xmalloc (len * 2 + 1); 7010 bin2hex ((gdb_byte *) str, hexstr, len); 7011 return hexstr; 7012 } 7013 7014 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat 7015 packets. */ 7016 7017 static void 7018 response_ust_marker (char *packet, const struct marker *st) 7019 { 7020 char *strid, *format, *tmp; 7021 7022 next_st = next_marker (st); 7023 7024 tmp = xmalloc (strlen (st->channel) + 1 + 7025 strlen (st->name) + 1); 7026 sprintf (tmp, "%s/%s", st->channel, st->name); 7027 7028 strid = cstr_to_hexstr (tmp); 7029 free (tmp); 7030 7031 format = cstr_to_hexstr (st->format); 7032 7033 sprintf (packet, "m%s:%s:%s", 7034 paddress ((uintptr_t) st->location), 7035 strid, 7036 format); 7037 7038 free (strid); 7039 free (format); 7040 } 7041 7042 /* Return the first static tracepoint, and initialize the state 7043 machine that will iterate through all the static tracepoints. */ 7044 7045 static void 7046 cmd_qtfstm (char *packet) 7047 { 7048 trace_debug ("Returning first trace state variable definition"); 7049 7050 if (first_marker ()) 7051 response_ust_marker (packet, first_marker ()); 7052 else 7053 strcpy (packet, "l"); 7054 } 7055 7056 /* Return additional trace state variable definitions. */ 7057 7058 static void 7059 cmd_qtsstm (char *packet) 7060 { 7061 trace_debug ("Returning static tracepoint"); 7062 7063 if (next_st) 7064 response_ust_marker (packet, next_st); 7065 else 7066 strcpy (packet, "l"); 7067 } 7068 7069 /* Disconnect the GDB probe from a marker at a given address. */ 7070 7071 static void 7072 unprobe_marker_at (char *packet) 7073 { 7074 char *p = packet; 7075 ULONGEST address; 7076 struct marker_iter iter; 7077 7078 p += sizeof ("unprobe_marker_at:") - 1; 7079 7080 p = unpack_varlen_hex (p, &address); 7081 7082 USTF(marker_iter_reset) (&iter); 7083 USTF(marker_iter_start) (&iter); 7084 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter)) 7085 if ((uintptr_t ) iter.marker->location == address) 7086 { 7087 int result; 7088 7089 result = USTF(ltt_marker_disconnect) (iter.marker->channel, 7090 iter.marker->name, 7091 GDB_PROBE_NAME); 7092 if (result < 0) 7093 warning ("could not disable marker %s/%s", 7094 iter.marker->channel, iter.marker->name); 7095 break; 7096 } 7097 } 7098 7099 /* Connect the GDB probe to a marker at a given address. */ 7100 7101 static int 7102 probe_marker_at (char *packet) 7103 { 7104 char *p = packet; 7105 ULONGEST address; 7106 struct marker_iter iter; 7107 struct marker *m; 7108 7109 p += sizeof ("probe_marker_at:") - 1; 7110 7111 p = unpack_varlen_hex (p, &address); 7112 7113 USTF(marker_iter_reset) (&iter); 7114 7115 for (USTF(marker_iter_start) (&iter), m = iter.marker; 7116 m != NULL; 7117 USTF(marker_iter_next) (&iter), m = iter.marker) 7118 if ((uintptr_t ) m->location == address) 7119 { 7120 int result; 7121 7122 trace_debug ("found marker for address. " 7123 "ltt_marker_connect (marker = %s/%s)", 7124 m->channel, m->name); 7125 7126 result = USTF(ltt_marker_connect) (m->channel, m->name, 7127 GDB_PROBE_NAME); 7128 if (result && result != -EEXIST) 7129 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)", 7130 m->channel, m->name, -result); 7131 7132 if (result < 0) 7133 { 7134 sprintf (packet, "E.could not connect marker: channel=%s, name=%s", 7135 m->channel, m->name); 7136 return -1; 7137 } 7138 7139 strcpy (packet, "OK"); 7140 return 0; 7141 } 7142 7143 sprintf (packet, "E.no marker found at 0x%s", paddress (address)); 7144 return -1; 7145 } 7146 7147 static int 7148 cmd_qtstmat (char *packet) 7149 { 7150 char *p = packet; 7151 ULONGEST address; 7152 struct marker_iter iter; 7153 struct marker *m; 7154 7155 p += sizeof ("qTSTMat:") - 1; 7156 7157 p = unpack_varlen_hex (p, &address); 7158 7159 USTF(marker_iter_reset) (&iter); 7160 7161 for (USTF(marker_iter_start) (&iter), m = iter.marker; 7162 m != NULL; 7163 USTF(marker_iter_next) (&iter), m = iter.marker) 7164 if ((uintptr_t ) m->location == address) 7165 { 7166 response_ust_marker (packet, m); 7167 return 0; 7168 } 7169 7170 strcpy (packet, "l"); 7171 return -1; 7172 } 7173 7174 static void 7175 gdb_ust_init (void) 7176 { 7177 if (!dlsym_ust ()) 7178 return; 7179 7180 USTF(ltt_probe_register) (&gdb_ust_probe); 7181 } 7182 7183 #endif /* HAVE_UST */ 7184 7185 #include <sys/syscall.h> 7186 7187 static void 7188 gdb_agent_remove_socket (void) 7189 { 7190 unlink (agent_socket_name); 7191 } 7192 7193 /* Helper thread of agent. */ 7194 7195 static void * 7196 gdb_agent_helper_thread (void *arg) 7197 { 7198 int listen_fd; 7199 7200 atexit (gdb_agent_remove_socket); 7201 7202 while (1) 7203 { 7204 listen_fd = gdb_agent_socket_init (); 7205 7206 if (helper_thread_id == 0) 7207 helper_thread_id = syscall (SYS_gettid); 7208 7209 if (listen_fd == -1) 7210 { 7211 warning ("could not create sync socket"); 7212 break; 7213 } 7214 7215 while (1) 7216 { 7217 socklen_t tmp; 7218 struct sockaddr_un sockaddr; 7219 int fd; 7220 char buf[1]; 7221 int ret; 7222 int stop_loop = 0; 7223 7224 tmp = sizeof (sockaddr); 7225 7226 do 7227 { 7228 fd = accept (listen_fd, (struct sockaddr *) &sockaddr, &tmp); 7229 } 7230 /* It seems an ERESTARTSYS can escape out of accept. */ 7231 while (fd == -512 || (fd == -1 && errno == EINTR)); 7232 7233 if (fd < 0) 7234 { 7235 warning ("Accept returned %d, error: %s", 7236 fd, safe_strerror (errno)); 7237 break; 7238 } 7239 7240 do 7241 { 7242 ret = read (fd, buf, 1); 7243 } while (ret == -1 && errno == EINTR); 7244 7245 if (ret == -1) 7246 { 7247 warning ("reading socket (fd=%d) failed with %s", 7248 fd, safe_strerror (errno)); 7249 close (fd); 7250 break; 7251 } 7252 7253 if (cmd_buf[0]) 7254 { 7255 if (startswith (cmd_buf, "close")) 7256 { 7257 stop_loop = 1; 7258 } 7259 #ifdef HAVE_UST 7260 else if (strcmp ("qTfSTM", cmd_buf) == 0) 7261 { 7262 cmd_qtfstm (cmd_buf); 7263 } 7264 else if (strcmp ("qTsSTM", cmd_buf) == 0) 7265 { 7266 cmd_qtsstm (cmd_buf); 7267 } 7268 else if (startswith (cmd_buf, "unprobe_marker_at:")) 7269 { 7270 unprobe_marker_at (cmd_buf); 7271 } 7272 else if (startswith (cmd_buf, "probe_marker_at:")) 7273 { 7274 probe_marker_at (cmd_buf); 7275 } 7276 else if (startswith (cmd_buf, "qTSTMat:")) 7277 { 7278 cmd_qtstmat (cmd_buf); 7279 } 7280 #endif /* HAVE_UST */ 7281 } 7282 7283 /* Fix compiler's warning: ignoring return value of 'write'. */ 7284 ret = write (fd, buf, 1); 7285 close (fd); 7286 7287 if (stop_loop) 7288 { 7289 close (listen_fd); 7290 unlink (agent_socket_name); 7291 7292 /* Sleep endlessly to wait the whole inferior stops. This 7293 thread can not exit because GDB or GDBserver may still need 7294 'current_thread' (representing this thread) to access 7295 inferior memory. Otherwise, this thread exits earlier than 7296 other threads, and 'current_thread' is set to NULL. */ 7297 while (1) 7298 sleep (10); 7299 } 7300 } 7301 } 7302 7303 return NULL; 7304 } 7305 7306 #include <signal.h> 7307 #include <pthread.h> 7308 7309 EXTERN_C_PUSH 7310 IP_AGENT_EXPORT_VAR int gdb_agent_capability = AGENT_CAPA_STATIC_TRACE; 7311 EXTERN_C_POP 7312 7313 static void 7314 gdb_agent_init (void) 7315 { 7316 int res; 7317 pthread_t thread; 7318 sigset_t new_mask; 7319 sigset_t orig_mask; 7320 7321 /* We want the helper thread to be as transparent as possible, so 7322 have it inherit an all-signals-blocked mask. */ 7323 7324 sigfillset (&new_mask); 7325 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask); 7326 if (res) 7327 perror_with_name ("pthread_sigmask (1)"); 7328 7329 res = pthread_create (&thread, 7330 NULL, 7331 gdb_agent_helper_thread, 7332 NULL); 7333 7334 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL); 7335 if (res) 7336 perror_with_name ("pthread_sigmask (2)"); 7337 7338 while (helper_thread_id == 0) 7339 usleep (1); 7340 7341 #ifdef HAVE_UST 7342 gdb_ust_init (); 7343 #endif 7344 } 7345 7346 #include <sys/mman.h> 7347 7348 IP_AGENT_EXPORT_VAR char *gdb_tp_heap_buffer; 7349 IP_AGENT_EXPORT_VAR char *gdb_jump_pad_buffer; 7350 IP_AGENT_EXPORT_VAR char *gdb_jump_pad_buffer_end; 7351 IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer; 7352 IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer_end; 7353 IP_AGENT_EXPORT_VAR char *gdb_trampoline_buffer_error; 7354 7355 /* Record the result of getting buffer space for fast tracepoint 7356 trampolines. Any error message is copied, since caller may not be 7357 using persistent storage. */ 7358 7359 void 7360 set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg) 7361 { 7362 gdb_trampoline_buffer = (char *) (uintptr_t) begin; 7363 gdb_trampoline_buffer_end = (char *) (uintptr_t) end; 7364 if (errmsg) 7365 strncpy (gdb_trampoline_buffer_error, errmsg, 99); 7366 else 7367 strcpy (gdb_trampoline_buffer_error, "no buffer passed"); 7368 } 7369 7370 static void __attribute__ ((constructor)) 7371 initialize_tracepoint_ftlib (void) 7372 { 7373 initialize_tracepoint (); 7374 7375 gdb_agent_init (); 7376 } 7377 7378 #ifndef HAVE_GETAUXVAL 7379 /* Retrieve the value of TYPE from the auxiliary vector. If TYPE is not 7380 found, 0 is returned. This function is provided if glibc is too old. */ 7381 7382 unsigned long 7383 getauxval (unsigned long type) 7384 { 7385 unsigned long data[2]; 7386 FILE *f = fopen ("/proc/self/auxv", "r"); 7387 unsigned long value = 0; 7388 7389 if (f == NULL) 7390 return 0; 7391 7392 while (fread (data, sizeof (data), 1, f) > 0) 7393 { 7394 if (data[0] == type) 7395 { 7396 value = data[1]; 7397 break; 7398 } 7399 } 7400 7401 fclose (f); 7402 return value; 7403 } 7404 #endif 7405 7406 #endif /* IN_PROCESS_AGENT */ 7407 7408 /* Return a timestamp, expressed as microseconds of the usual Unix 7409 time. (As the result is a 64-bit number, it will not overflow any 7410 time soon.) */ 7411 7412 static LONGEST 7413 get_timestamp (void) 7414 { 7415 using namespace std::chrono; 7416 7417 steady_clock::time_point now = steady_clock::now (); 7418 return duration_cast<microseconds> (now.time_since_epoch ()).count (); 7419 } 7420 7421 void 7422 initialize_tracepoint (void) 7423 { 7424 /* Start with the default size. */ 7425 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE); 7426 7427 /* Wire trace state variable 1 to be the timestamp. This will be 7428 uploaded to GDB upon connection and become one of its trace state 7429 variables. (In case you're wondering, if GDB already has a trace 7430 variable numbered 1, it will be renumbered.) */ 7431 create_trace_state_variable (1, 0); 7432 set_trace_state_variable_name (1, "trace_timestamp"); 7433 set_trace_state_variable_getter (1, get_timestamp); 7434 7435 #ifdef IN_PROCESS_AGENT 7436 { 7437 int pagesize; 7438 size_t jump_pad_size; 7439 7440 pagesize = sysconf (_SC_PAGE_SIZE); 7441 if (pagesize == -1) 7442 perror_with_name ("sysconf"); 7443 7444 #define SCRATCH_BUFFER_NPAGES 20 7445 7446 jump_pad_size = pagesize * SCRATCH_BUFFER_NPAGES; 7447 7448 gdb_tp_heap_buffer = (char *) xmalloc (5 * 1024 * 1024); 7449 gdb_jump_pad_buffer = (char *) alloc_jump_pad_buffer (jump_pad_size); 7450 if (gdb_jump_pad_buffer == NULL) 7451 perror_with_name ("mmap"); 7452 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + jump_pad_size; 7453 } 7454 7455 gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0; 7456 7457 /* It's not a fatal error for something to go wrong with trampoline 7458 buffer setup, but it can be mysterious, so create a channel to 7459 report back on what went wrong, using a fixed size since we may 7460 not be able to allocate space later when the problem occurs. */ 7461 gdb_trampoline_buffer_error = (char *) xmalloc (IPA_BUFSIZ); 7462 7463 strcpy (gdb_trampoline_buffer_error, "No errors reported"); 7464 7465 initialize_low_tracepoint (); 7466 #endif 7467 } 7468