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