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