Replace code accessing list implementation details with API calls.
[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 = hex2bin (packet, (gdb_byte *) src, 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 = hex2bin (packet, (gdb_byte *) varname, 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 bin2hex ((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 bin2hex ((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 bin2hex ((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 /* Helper for gdb_agent_about_to_close.
3947 Return non-zero if thread ENTRY is in the same process in DATA. */
3948
3949 static int
3950 same_process_p (struct inferior_list_entry *entry, void *data)
3951 {
3952 int *pid = data;
3953
3954 return ptid_get_pid (entry->id) == *pid;
3955 }
3956
3957 /* Sent the agent a command to close it. */
3958
3959 void
3960 gdb_agent_about_to_close (int pid)
3961 {
3962 char buf[IPA_CMD_BUF_SIZE];
3963
3964 if (!maybe_write_ipa_not_loaded (buf))
3965 {
3966 struct thread_info *save_inferior;
3967
3968 save_inferior = current_inferior;
3969
3970 /* Find any thread which belongs to process PID. */
3971 current_inferior = (struct thread_info *)
3972 find_inferior (&all_threads, same_process_p, &pid);
3973
3974 strcpy (buf, "close");
3975
3976 run_inferior_command (buf, strlen (buf) + 1);
3977
3978 current_inferior = save_inferior;
3979 }
3980 }
3981
3982 /* Return the minimum instruction size needed for fast tracepoints as a
3983 hexadecimal number. */
3984
3985 static void
3986 cmd_qtminftpilen (char *packet)
3987 {
3988 if (current_inferior == NULL)
3989 {
3990 /* Indicate that the minimum length is currently unknown. */
3991 strcpy (packet, "0");
3992 return;
3993 }
3994
3995 sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
3996 }
3997
3998 /* Respond to qTBuffer packet with a block of raw data from the trace
3999 buffer. GDB may ask for a lot, but we are allowed to reply with
4000 only as much as will fit within packet limits or whatever. */
4001
4002 static void
4003 cmd_qtbuffer (char *own_buf)
4004 {
4005 ULONGEST offset, num, tot;
4006 unsigned char *tbp;
4007 char *packet = own_buf;
4008
4009 packet += strlen ("qTBuffer:");
4010
4011 packet = unpack_varlen_hex (packet, &offset);
4012 ++packet; /* skip a comma */
4013 unpack_varlen_hex (packet, &num);
4014
4015 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
4016 (int) num, phex_nz (offset, 0));
4017
4018 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
4019
4020 /* If we're right at the end, reply specially that we're done. */
4021 if (offset == tot)
4022 {
4023 strcpy (own_buf, "l");
4024 return;
4025 }
4026
4027 /* Object to any other out-of-bounds request. */
4028 if (offset > tot)
4029 {
4030 write_enn (own_buf);
4031 return;
4032 }
4033
4034 /* Compute the pointer corresponding to the given offset, accounting
4035 for wraparound. */
4036 tbp = trace_buffer_start + offset;
4037 if (tbp >= trace_buffer_wrap)
4038 tbp -= (trace_buffer_wrap - trace_buffer_lo);
4039
4040 /* Trim to the remaining bytes if we're close to the end. */
4041 if (num > tot - offset)
4042 num = tot - offset;
4043
4044 /* Trim to available packet size. */
4045 if (num >= (PBUFSIZ - 16) / 2 )
4046 num = (PBUFSIZ - 16) / 2;
4047
4048 bin2hex (tbp, own_buf, num);
4049 }
4050
4051 static void
4052 cmd_bigqtbuffer_circular (char *own_buf)
4053 {
4054 ULONGEST val;
4055 char *packet = own_buf;
4056
4057 packet += strlen ("QTBuffer:circular:");
4058
4059 unpack_varlen_hex (packet, &val);
4060 circular_trace_buffer = val;
4061 trace_debug ("Trace buffer is now %s",
4062 circular_trace_buffer ? "circular" : "linear");
4063 write_ok (own_buf);
4064 }
4065
4066 static void
4067 cmd_bigqtbuffer_size (char *own_buf)
4068 {
4069 ULONGEST val;
4070 LONGEST sval;
4071 char *packet = own_buf;
4072
4073 /* Can't change the size during a tracing run. */
4074 if (tracing)
4075 {
4076 write_enn (own_buf);
4077 return;
4078 }
4079
4080 packet += strlen ("QTBuffer:size:");
4081
4082 /* -1 is sent as literal "-1". */
4083 if (strcmp (packet, "-1") == 0)
4084 sval = DEFAULT_TRACE_BUFFER_SIZE;
4085 else
4086 {
4087 unpack_varlen_hex (packet, &val);
4088 sval = (LONGEST) val;
4089 }
4090
4091 init_trace_buffer (sval);
4092 trace_debug ("Trace buffer is now %s bytes",
4093 plongest (trace_buffer_size));
4094 write_ok (own_buf);
4095 }
4096
4097 static void
4098 cmd_qtnotes (char *own_buf)
4099 {
4100 size_t nbytes;
4101 char *saved, *user, *notes, *stopnote;
4102 char *packet = own_buf;
4103
4104 packet += strlen ("QTNotes:");
4105
4106 while (*packet)
4107 {
4108 if (strncmp ("user:", packet, strlen ("user:")) == 0)
4109 {
4110 packet += strlen ("user:");
4111 saved = packet;
4112 packet = strchr (packet, ';');
4113 nbytes = (packet - saved) / 2;
4114 user = xmalloc (nbytes + 1);
4115 nbytes = hex2bin (saved, (gdb_byte *) user, nbytes);
4116 user[nbytes] = '\0';
4117 ++packet; /* skip the semicolon */
4118 trace_debug ("User is '%s'", user);
4119 xfree (tracing_user_name);
4120 tracing_user_name = user;
4121 }
4122 else if (strncmp ("notes:", packet, strlen ("notes:")) == 0)
4123 {
4124 packet += strlen ("notes:");
4125 saved = packet;
4126 packet = strchr (packet, ';');
4127 nbytes = (packet - saved) / 2;
4128 notes = xmalloc (nbytes + 1);
4129 nbytes = hex2bin (saved, (gdb_byte *) notes, nbytes);
4130 notes[nbytes] = '\0';
4131 ++packet; /* skip the semicolon */
4132 trace_debug ("Notes is '%s'", notes);
4133 xfree (tracing_notes);
4134 tracing_notes = notes;
4135 }
4136 else if (strncmp ("tstop:", packet, strlen ("tstop:")) == 0)
4137 {
4138 packet += strlen ("tstop:");
4139 saved = packet;
4140 packet = strchr (packet, ';');
4141 nbytes = (packet - saved) / 2;
4142 stopnote = xmalloc (nbytes + 1);
4143 nbytes = hex2bin (saved, (gdb_byte *) stopnote, nbytes);
4144 stopnote[nbytes] = '\0';
4145 ++packet; /* skip the semicolon */
4146 trace_debug ("tstop note is '%s'", stopnote);
4147 xfree (tracing_stop_note);
4148 tracing_stop_note = stopnote;
4149 }
4150 else
4151 break;
4152 }
4153
4154 write_ok (own_buf);
4155 }
4156
4157 int
4158 handle_tracepoint_general_set (char *packet)
4159 {
4160 if (strcmp ("QTinit", packet) == 0)
4161 {
4162 cmd_qtinit (packet);
4163 return 1;
4164 }
4165 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
4166 {
4167 cmd_qtdp (packet);
4168 return 1;
4169 }
4170 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
4171 {
4172 cmd_qtdpsrc (packet);
4173 return 1;
4174 }
4175 else if (strncmp ("QTEnable:", packet, strlen ("QTEnable:")) == 0)
4176 {
4177 cmd_qtenable_disable (packet, 1);
4178 return 1;
4179 }
4180 else if (strncmp ("QTDisable:", packet, strlen ("QTDisable:")) == 0)
4181 {
4182 cmd_qtenable_disable (packet, 0);
4183 return 1;
4184 }
4185 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
4186 {
4187 cmd_qtdv (packet);
4188 return 1;
4189 }
4190 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
4191 {
4192 cmd_qtro (packet);
4193 return 1;
4194 }
4195 else if (strcmp ("QTStart", packet) == 0)
4196 {
4197 cmd_qtstart (packet);
4198 return 1;
4199 }
4200 else if (strcmp ("QTStop", packet) == 0)
4201 {
4202 cmd_qtstop (packet);
4203 return 1;
4204 }
4205 else if (strncmp ("QTDisconnected:", packet,
4206 strlen ("QTDisconnected:")) == 0)
4207 {
4208 cmd_qtdisconnected (packet);
4209 return 1;
4210 }
4211 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
4212 {
4213 cmd_qtframe (packet);
4214 return 1;
4215 }
4216 else if (strncmp ("QTBuffer:circular:", packet, strlen ("QTBuffer:circular:")) == 0)
4217 {
4218 cmd_bigqtbuffer_circular (packet);
4219 return 1;
4220 }
4221 else if (strncmp ("QTBuffer:size:", packet, strlen ("QTBuffer:size:")) == 0)
4222 {
4223 cmd_bigqtbuffer_size (packet);
4224 return 1;
4225 }
4226 else if (strncmp ("QTNotes:", packet, strlen ("QTNotes:")) == 0)
4227 {
4228 cmd_qtnotes (packet);
4229 return 1;
4230 }
4231
4232 return 0;
4233 }
4234
4235 int
4236 handle_tracepoint_query (char *packet)
4237 {
4238 if (strcmp ("qTStatus", packet) == 0)
4239 {
4240 cmd_qtstatus (packet);
4241 return 1;
4242 }
4243 else if (strncmp ("qTP:", packet, strlen ("qTP:")) == 0)
4244 {
4245 cmd_qtp (packet);
4246 return 1;
4247 }
4248 else if (strcmp ("qTfP", packet) == 0)
4249 {
4250 cmd_qtfp (packet);
4251 return 1;
4252 }
4253 else if (strcmp ("qTsP", packet) == 0)
4254 {
4255 cmd_qtsp (packet);
4256 return 1;
4257 }
4258 else if (strcmp ("qTfV", packet) == 0)
4259 {
4260 cmd_qtfv (packet);
4261 return 1;
4262 }
4263 else if (strcmp ("qTsV", packet) == 0)
4264 {
4265 cmd_qtsv (packet);
4266 return 1;
4267 }
4268 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
4269 {
4270 cmd_qtv (packet);
4271 return 1;
4272 }
4273 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
4274 {
4275 cmd_qtbuffer (packet);
4276 return 1;
4277 }
4278 else if (strcmp ("qTfSTM", packet) == 0)
4279 {
4280 cmd_qtfstm (packet);
4281 return 1;
4282 }
4283 else if (strcmp ("qTsSTM", packet) == 0)
4284 {
4285 cmd_qtsstm (packet);
4286 return 1;
4287 }
4288 else if (strncmp ("qTSTMat:", packet, strlen ("qTSTMat:")) == 0)
4289 {
4290 cmd_qtstmat (packet);
4291 return 1;
4292 }
4293 else if (strcmp ("qTMinFTPILen", packet) == 0)
4294 {
4295 cmd_qtminftpilen (packet);
4296 return 1;
4297 }
4298
4299 return 0;
4300 }
4301
4302 #endif
4303 #ifndef IN_PROCESS_AGENT
4304
4305 /* Call this when thread TINFO has hit the tracepoint defined by
4306 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4307 action. This adds a while-stepping collecting state item to the
4308 threads' collecting state list, so that we can keep track of
4309 multiple simultaneous while-stepping actions being collected by the
4310 same thread. This can happen in cases like:
4311
4312 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4313 ff0002 INSN2
4314 ff0003 INSN3 <-- TP2, collect $regs
4315 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4316 ff0005 INSN5
4317
4318 Notice that when instruction INSN5 is reached, the while-stepping
4319 actions of both TP1 and TP3 are still being collected, and that TP2
4320 had been collected meanwhile. The whole range of ff0001-ff0005
4321 should be single-stepped, due to at least TP1's while-stepping
4322 action covering the whole range. */
4323
4324 static void
4325 add_while_stepping_state (struct thread_info *tinfo,
4326 int tp_number, CORE_ADDR tp_address)
4327 {
4328 struct wstep_state *wstep;
4329
4330 wstep = xmalloc (sizeof (*wstep));
4331 wstep->next = tinfo->while_stepping;
4332
4333 wstep->tp_number = tp_number;
4334 wstep->tp_address = tp_address;
4335 wstep->current_step = 0;
4336
4337 tinfo->while_stepping = wstep;
4338 }
4339
4340 /* Release the while-stepping collecting state WSTEP. */
4341
4342 static void
4343 release_while_stepping_state (struct wstep_state *wstep)
4344 {
4345 free (wstep);
4346 }
4347
4348 /* Release all while-stepping collecting states currently associated
4349 with thread TINFO. */
4350
4351 void
4352 release_while_stepping_state_list (struct thread_info *tinfo)
4353 {
4354 struct wstep_state *head;
4355
4356 while (tinfo->while_stepping)
4357 {
4358 head = tinfo->while_stepping;
4359 tinfo->while_stepping = head->next;
4360 release_while_stepping_state (head);
4361 }
4362 }
4363
4364 /* If TINFO was handling a 'while-stepping' action, the step has
4365 finished, so collect any step data needed, and check if any more
4366 steps are required. Return true if the thread was indeed
4367 collecting tracepoint data, false otherwise. */
4368
4369 int
4370 tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
4371 {
4372 struct tracepoint *tpoint;
4373 struct wstep_state *wstep;
4374 struct wstep_state **wstep_link;
4375 struct trap_tracepoint_ctx ctx;
4376
4377 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4378 our buffer. */
4379 if (agent_loaded_p ())
4380 upload_fast_traceframes ();
4381
4382 /* Check if we were indeed collecting data for one of more
4383 tracepoints with a 'while-stepping' count. */
4384 if (tinfo->while_stepping == NULL)
4385 return 0;
4386
4387 if (!tracing)
4388 {
4389 /* We're not even tracing anymore. Stop this thread from
4390 collecting. */
4391 release_while_stepping_state_list (tinfo);
4392
4393 /* The thread had stopped due to a single-step request indeed
4394 explained by a tracepoint. */
4395 return 1;
4396 }
4397
4398 wstep = tinfo->while_stepping;
4399 wstep_link = &tinfo->while_stepping;
4400
4401 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4402 target_pid_to_str (tinfo->entry.id),
4403 wstep->tp_number, paddress (wstep->tp_address));
4404
4405 ctx.base.type = trap_tracepoint;
4406 ctx.regcache = get_thread_regcache (tinfo, 1);
4407
4408 while (wstep != NULL)
4409 {
4410 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
4411 if (tpoint == NULL)
4412 {
4413 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4414 wstep->tp_number, paddress (wstep->tp_address),
4415 target_pid_to_str (tinfo->entry.id));
4416
4417 /* Unlink. */
4418 *wstep_link = wstep->next;
4419 release_while_stepping_state (wstep);
4420 wstep = *wstep_link;
4421 continue;
4422 }
4423
4424 /* We've just finished one step. */
4425 ++wstep->current_step;
4426
4427 /* Collect data. */
4428 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
4429 stop_pc, tpoint, wstep->current_step);
4430
4431 if (wstep->current_step >= tpoint->step_count)
4432 {
4433 /* The requested numbers of steps have occurred. */
4434 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4435 target_pid_to_str (tinfo->entry.id),
4436 wstep->tp_number, paddress (wstep->tp_address));
4437
4438 /* Unlink the wstep. */
4439 *wstep_link = wstep->next;
4440 release_while_stepping_state (wstep);
4441 wstep = *wstep_link;
4442
4443 /* Only check the hit count now, which ensure that we do all
4444 our stepping before stopping the run. */
4445 if (tpoint->pass_count > 0
4446 && tpoint->hit_count >= tpoint->pass_count
4447 && stopping_tracepoint == NULL)
4448 stopping_tracepoint = tpoint;
4449 }
4450 else
4451 {
4452 /* Keep single-stepping until the requested numbers of steps
4453 have occurred. */
4454 wstep_link = &wstep->next;
4455 wstep = *wstep_link;
4456 }
4457
4458 if (stopping_tracepoint
4459 || trace_buffer_is_full
4460 || expr_eval_result != expr_eval_no_error)
4461 {
4462 stop_tracing ();
4463 break;
4464 }
4465 }
4466
4467 return 1;
4468 }
4469
4470 /* Handle any internal tracing control breakpoint hits. That means,
4471 pull traceframes from the IPA to our buffer, and syncing both
4472 tracing agents when the IPA's tracing stops for some reason. */
4473
4474 int
4475 handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
4476 {
4477 /* Pull in fast tracepoint trace frames from the inferior in-process
4478 agent's buffer into our buffer. */
4479
4480 if (!agent_loaded_p ())
4481 return 0;
4482
4483 upload_fast_traceframes ();
4484
4485 /* Check if the in-process agent had decided we should stop
4486 tracing. */
4487 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
4488 {
4489 int ipa_trace_buffer_is_full;
4490 CORE_ADDR ipa_stopping_tracepoint;
4491 int ipa_expr_eval_result;
4492 CORE_ADDR ipa_error_tracepoint;
4493
4494 trace_debug ("lib stopped at stop_tracing");
4495
4496 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
4497 &ipa_trace_buffer_is_full);
4498
4499 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
4500 &ipa_stopping_tracepoint);
4501 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
4502
4503 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
4504 &ipa_error_tracepoint);
4505 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
4506
4507 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
4508 &ipa_expr_eval_result);
4509 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
4510
4511 trace_debug ("lib: trace_buffer_is_full: %d, "
4512 "stopping_tracepoint: %s, "
4513 "ipa_expr_eval_result: %d, "
4514 "error_tracepoint: %s, ",
4515 ipa_trace_buffer_is_full,
4516 paddress (ipa_stopping_tracepoint),
4517 ipa_expr_eval_result,
4518 paddress (ipa_error_tracepoint));
4519
4520 if (debug_threads)
4521 {
4522 if (ipa_trace_buffer_is_full)
4523 trace_debug ("lib stopped due to full buffer.");
4524 if (ipa_stopping_tracepoint)
4525 trace_debug ("lib stopped due to tpoint");
4526 if (ipa_stopping_tracepoint)
4527 trace_debug ("lib stopped due to error");
4528 }
4529
4530 if (ipa_stopping_tracepoint != 0)
4531 {
4532 stopping_tracepoint
4533 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
4534 }
4535 else if (ipa_expr_eval_result != expr_eval_no_error)
4536 {
4537 expr_eval_result = ipa_expr_eval_result;
4538 error_tracepoint
4539 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
4540 }
4541 stop_tracing ();
4542 return 1;
4543 }
4544 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
4545 {
4546 trace_debug ("lib stopped at flush_trace_buffer");
4547 return 1;
4548 }
4549
4550 return 0;
4551 }
4552
4553 /* Return true if TINFO just hit a tracepoint. Collect data if
4554 so. */
4555
4556 int
4557 tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
4558 {
4559 struct tracepoint *tpoint;
4560 int ret = 0;
4561 struct trap_tracepoint_ctx ctx;
4562
4563 /* Not tracing, don't handle. */
4564 if (!tracing)
4565 return 0;
4566
4567 ctx.base.type = trap_tracepoint;
4568 ctx.regcache = get_thread_regcache (tinfo, 1);
4569
4570 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4571 {
4572 /* Note that we collect fast tracepoints here as well. We'll
4573 step over the fast tracepoint jump later, which avoids the
4574 double collect. However, we don't collect for static
4575 tracepoints here, because UST markers are compiled in program,
4576 and probes will be executed in program. So static tracepoints
4577 are collected there. */
4578 if (tpoint->enabled && stop_pc == tpoint->address
4579 && tpoint->type != static_tracepoint)
4580 {
4581 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4582 target_pid_to_str (tinfo->entry.id),
4583 tpoint->number, paddress (tpoint->address));
4584
4585 /* Test the condition if present, and collect if true. */
4586 if (!tpoint->cond
4587 || (condition_true_at_tracepoint
4588 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
4589 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4590 stop_pc, tpoint);
4591
4592 if (stopping_tracepoint
4593 || trace_buffer_is_full
4594 || expr_eval_result != expr_eval_no_error)
4595 {
4596 stop_tracing ();
4597 }
4598 /* If the tracepoint had a 'while-stepping' action, then set
4599 the thread to collect this tracepoint on the following
4600 single-steps. */
4601 else if (tpoint->step_count > 0)
4602 {
4603 add_while_stepping_state (tinfo,
4604 tpoint->number, tpoint->address);
4605 }
4606
4607 ret = 1;
4608 }
4609 }
4610
4611 return ret;
4612 }
4613
4614 #endif
4615
4616 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4617 struct ust_marker_data;
4618 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4619 struct traceframe *tframe);
4620 #endif
4621
4622 /* Create a trace frame for the hit of the given tracepoint in the
4623 given thread. */
4624
4625 static void
4626 collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
4627 struct tracepoint *tpoint)
4628 {
4629 struct traceframe *tframe;
4630 int acti;
4631
4632 /* Only count it as a hit when we actually collect data. */
4633 tpoint->hit_count++;
4634
4635 /* If we've exceeded a defined pass count, record the event for
4636 later, and finish the collection for this hit. This test is only
4637 for nonstepping tracepoints, stepping tracepoints test at the end
4638 of their while-stepping loop. */
4639 if (tpoint->pass_count > 0
4640 && tpoint->hit_count >= tpoint->pass_count
4641 && tpoint->step_count == 0
4642 && stopping_tracepoint == NULL)
4643 stopping_tracepoint = tpoint;
4644
4645 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64,
4646 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
4647
4648 tframe = add_traceframe (tpoint);
4649
4650 if (tframe)
4651 {
4652 for (acti = 0; acti < tpoint->numactions; ++acti)
4653 {
4654 #ifndef IN_PROCESS_AGENT
4655 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4656 tpoint->number, paddress (tpoint->address),
4657 tpoint->actions_str[acti]);
4658 #endif
4659
4660 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4661 tpoint->actions[acti]);
4662 }
4663
4664 finish_traceframe (tframe);
4665 }
4666
4667 if (tframe == NULL && tracing)
4668 trace_buffer_is_full = 1;
4669 }
4670
4671 #ifndef IN_PROCESS_AGENT
4672
4673 static void
4674 collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4675 CORE_ADDR stop_pc,
4676 struct tracepoint *tpoint, int current_step)
4677 {
4678 struct traceframe *tframe;
4679 int acti;
4680
4681 trace_debug ("Making new step traceframe for "
4682 "tracepoint %d at 0x%s, step %d of %" PRIu64 ", hit %" PRIu64,
4683 tpoint->number, paddress (tpoint->address),
4684 current_step, tpoint->step_count,
4685 tpoint->hit_count);
4686
4687 tframe = add_traceframe (tpoint);
4688
4689 if (tframe)
4690 {
4691 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4692 {
4693 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4694 tpoint->number, paddress (tpoint->address),
4695 tpoint->step_actions_str[acti]);
4696
4697 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4698 tpoint->step_actions[acti]);
4699 }
4700
4701 finish_traceframe (tframe);
4702 }
4703
4704 if (tframe == NULL && tracing)
4705 trace_buffer_is_full = 1;
4706 }
4707
4708 #endif
4709
4710 #ifdef IN_PROCESS_AGENT
4711 /* The target description used by the IPA. Given that the IPA library
4712 is built for a specific architecture that is loaded into the
4713 inferior, there only needs to be one such description per
4714 build. */
4715 const struct target_desc *ipa_tdesc;
4716 #endif
4717
4718 static struct regcache *
4719 get_context_regcache (struct tracepoint_hit_ctx *ctx)
4720 {
4721 struct regcache *regcache = NULL;
4722
4723 #ifdef IN_PROCESS_AGENT
4724 if (ctx->type == fast_tracepoint)
4725 {
4726 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4727 if (!fctx->regcache_initted)
4728 {
4729 fctx->regcache_initted = 1;
4730 init_register_cache (&fctx->regcache, ipa_tdesc, fctx->regspace);
4731 supply_regblock (&fctx->regcache, NULL);
4732 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4733 }
4734 regcache = &fctx->regcache;
4735 }
4736 #ifdef HAVE_UST
4737 if (ctx->type == static_tracepoint)
4738 {
4739 struct static_tracepoint_ctx *sctx
4740 = (struct static_tracepoint_ctx *) ctx;
4741
4742 if (!sctx->regcache_initted)
4743 {
4744 sctx->regcache_initted = 1;
4745 init_register_cache (&sctx->regcache, ipa_tdesc, sctx->regspace);
4746 supply_regblock (&sctx->regcache, NULL);
4747 /* Pass down the tracepoint address, because REGS doesn't
4748 include the PC, but we know what it must have been. */
4749 supply_static_tracepoint_registers (&sctx->regcache,
4750 (const unsigned char *)
4751 sctx->regs,
4752 sctx->tpoint->address);
4753 }
4754 regcache = &sctx->regcache;
4755 }
4756 #endif
4757 #else
4758 if (ctx->type == trap_tracepoint)
4759 {
4760 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4761 regcache = tctx->regcache;
4762 }
4763 #endif
4764
4765 gdb_assert (regcache != NULL);
4766
4767 return regcache;
4768 }
4769
4770 static void
4771 do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4772 CORE_ADDR stop_pc,
4773 struct tracepoint *tpoint,
4774 struct traceframe *tframe,
4775 struct tracepoint_action *taction)
4776 {
4777 enum eval_result_type err;
4778
4779 switch (taction->type)
4780 {
4781 case 'M':
4782 {
4783 struct collect_memory_action *maction;
4784 struct eval_agent_expr_context ax_ctx;
4785
4786 maction = (struct collect_memory_action *) taction;
4787 ax_ctx.regcache = NULL;
4788 ax_ctx.tframe = tframe;
4789 ax_ctx.tpoint = tpoint;
4790
4791 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4792 pulongest (maction->len),
4793 paddress (maction->addr), maction->basereg);
4794 /* (should use basereg) */
4795 agent_mem_read (&ax_ctx, NULL, (CORE_ADDR) maction->addr,
4796 maction->len);
4797 break;
4798 }
4799 case 'R':
4800 {
4801 unsigned char *regspace;
4802 struct regcache tregcache;
4803 struct regcache *context_regcache;
4804 int regcache_size;
4805
4806 trace_debug ("Want to collect registers");
4807
4808 context_regcache = get_context_regcache (ctx);
4809 regcache_size = register_cache_size (context_regcache->tdesc);
4810
4811 /* Collect all registers for now. */
4812 regspace = add_traceframe_block (tframe, tpoint, 1 + regcache_size);
4813 if (regspace == NULL)
4814 {
4815 trace_debug ("Trace buffer block allocation failed, skipping");
4816 break;
4817 }
4818 /* Identify a register block. */
4819 *regspace = 'R';
4820
4821 /* Wrap the regblock in a register cache (in the stack, we
4822 don't want to malloc here). */
4823 init_register_cache (&tregcache, context_regcache->tdesc,
4824 regspace + 1);
4825
4826 /* Copy the register data to the regblock. */
4827 regcache_cpy (&tregcache, context_regcache);
4828
4829 #ifndef IN_PROCESS_AGENT
4830 /* On some platforms, trap-based tracepoints will have the PC
4831 pointing to the next instruction after the trap, but we
4832 don't want the user or GDB trying to guess whether the
4833 saved PC needs adjusting; so always record the adjusted
4834 stop_pc. Note that we can't use tpoint->address instead,
4835 since it will be wrong for while-stepping actions. This
4836 adjustment is a nop for fast tracepoints collected from the
4837 in-process lib (but not if GDBserver is collecting one
4838 preemptively), since the PC had already been adjusted to
4839 contain the tracepoint's address by the jump pad. */
4840 trace_debug ("Storing stop pc (0x%s) in regblock",
4841 paddress (stop_pc));
4842
4843 /* This changes the regblock, not the thread's
4844 regcache. */
4845 regcache_write_pc (&tregcache, stop_pc);
4846 #endif
4847 }
4848 break;
4849 case 'X':
4850 {
4851 struct eval_expr_action *eaction;
4852 struct eval_agent_expr_context ax_ctx;
4853
4854 eaction = (struct eval_expr_action *) taction;
4855 ax_ctx.regcache = get_context_regcache (ctx);
4856 ax_ctx.tframe = tframe;
4857 ax_ctx.tpoint = tpoint;
4858
4859 trace_debug ("Want to evaluate expression");
4860
4861 err = gdb_eval_agent_expr (&ax_ctx, eaction->expr, NULL);
4862
4863 if (err != expr_eval_no_error)
4864 {
4865 record_tracepoint_error (tpoint, "action expression", err);
4866 return;
4867 }
4868 }
4869 break;
4870 case 'L':
4871 {
4872 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4873 trace_debug ("Want to collect static trace data");
4874 collect_ust_data_at_tracepoint (ctx, tframe);
4875 #else
4876 trace_debug ("warning: collecting static trace data, "
4877 "but static tracepoints are not supported");
4878 #endif
4879 }
4880 break;
4881 default:
4882 trace_debug ("unknown trace action '%c', ignoring", taction->type);
4883 break;
4884 }
4885 }
4886
4887 static int
4888 condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4889 struct tracepoint *tpoint)
4890 {
4891 ULONGEST value = 0;
4892 enum eval_result_type err;
4893
4894 /* Presently, gdbserver doesn't run compiled conditions, only the
4895 IPA does. If the program stops at a fast tracepoint's address
4896 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4897 gdbserver preemptively collect the fast tracepoint. Later, on
4898 resume, gdbserver steps over the fast tracepoint like it steps
4899 over breakpoints, so that the IPA doesn't see that fast
4900 tracepoint. This avoids double collects of fast tracepoints in
4901 that stopping scenario. Having gdbserver itself handle the fast
4902 tracepoint gives the user a consistent view of when fast or trap
4903 tracepoints are collected, compared to an alternative where only
4904 trap tracepoints are collected on stop, and fast tracepoints on
4905 resume. When a fast tracepoint is being processed by gdbserver,
4906 it is always the non-compiled condition expression that is
4907 used. */
4908 #ifdef IN_PROCESS_AGENT
4909 if (tpoint->compiled_cond)
4910 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
4911 else
4912 #endif
4913 {
4914 struct eval_agent_expr_context ax_ctx;
4915
4916 ax_ctx.regcache = get_context_regcache (ctx);
4917 ax_ctx.tframe = NULL;
4918 ax_ctx.tpoint = tpoint;
4919
4920 err = gdb_eval_agent_expr (&ax_ctx, tpoint->cond, &value);
4921 }
4922 if (err != expr_eval_no_error)
4923 {
4924 record_tracepoint_error (tpoint, "condition", err);
4925 /* The error case must return false. */
4926 return 0;
4927 }
4928
4929 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4930 tpoint->number, paddress (tpoint->address),
4931 pulongest (value));
4932 return (value ? 1 : 0);
4933 }
4934
4935 /* Do memory copies for bytecodes. */
4936 /* Do the recording of memory blocks for actions and bytecodes. */
4937
4938 int
4939 agent_mem_read (struct eval_agent_expr_context *ctx,
4940 unsigned char *to, CORE_ADDR from, ULONGEST len)
4941 {
4942 unsigned char *mspace;
4943 ULONGEST remaining = len;
4944 unsigned short blocklen;
4945
4946 /* If a 'to' buffer is specified, use it. */
4947 if (to != NULL)
4948 {
4949 read_inferior_memory (from, to, len);
4950 return 0;
4951 }
4952
4953 /* Otherwise, create a new memory block in the trace buffer. */
4954 while (remaining > 0)
4955 {
4956 size_t sp;
4957
4958 blocklen = (remaining > 65535 ? 65535 : remaining);
4959 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4960 mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp);
4961 if (mspace == NULL)
4962 return 1;
4963 /* Identify block as a memory block. */
4964 *mspace = 'M';
4965 ++mspace;
4966 /* Record address and size. */
4967 memcpy (mspace, &from, sizeof (from));
4968 mspace += sizeof (from);
4969 memcpy (mspace, &blocklen, sizeof (blocklen));
4970 mspace += sizeof (blocklen);
4971 /* Record the memory block proper. */
4972 read_inferior_memory (from, mspace, blocklen);
4973 trace_debug ("%d bytes recorded", blocklen);
4974 remaining -= blocklen;
4975 from += blocklen;
4976 }
4977 return 0;
4978 }
4979
4980 int
4981 agent_mem_read_string (struct eval_agent_expr_context *ctx,
4982 unsigned char *to, CORE_ADDR from, ULONGEST len)
4983 {
4984 unsigned char *buf, *mspace;
4985 ULONGEST remaining = len;
4986 unsigned short blocklen, i;
4987
4988 /* To save a bit of space, block lengths are 16-bit, so break large
4989 requests into multiple blocks. Bordering on overkill for strings,
4990 but it could happen that someone specifies a large max length. */
4991 while (remaining > 0)
4992 {
4993 size_t sp;
4994
4995 blocklen = (remaining > 65535 ? 65535 : remaining);
4996 /* We want working space to accumulate nonzero bytes, since
4997 traceframes must have a predecided size (otherwise it gets
4998 harder to wrap correctly for the circular case, etc). */
4999 buf = (unsigned char *) xmalloc (blocklen + 1);
5000 for (i = 0; i < blocklen; ++i)
5001 {
5002 /* Read the string one byte at a time, in case the string is
5003 at the end of a valid memory area - we don't want a
5004 correctly-terminated string to engender segvio
5005 complaints. */
5006 read_inferior_memory (from + i, buf + i, 1);
5007
5008 if (buf[i] == '\0')
5009 {
5010 blocklen = i + 1;
5011 /* Make sure outer loop stops now too. */
5012 remaining = blocklen;
5013 break;
5014 }
5015 }
5016 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
5017 mspace = add_traceframe_block (ctx->tframe, ctx->tpoint, sp);
5018 if (mspace == NULL)
5019 {
5020 xfree (buf);
5021 return 1;
5022 }
5023 /* Identify block as a memory block. */
5024 *mspace = 'M';
5025 ++mspace;
5026 /* Record address and size. */
5027 memcpy ((void *) mspace, (void *) &from, sizeof (from));
5028 mspace += sizeof (from);
5029 memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
5030 mspace += sizeof (blocklen);
5031 /* Copy the string contents. */
5032 memcpy ((void *) mspace, (void *) buf, blocklen);
5033 remaining -= blocklen;
5034 from += blocklen;
5035 xfree (buf);
5036 }
5037 return 0;
5038 }
5039
5040 /* Record the value of a trace state variable. */
5041
5042 int
5043 agent_tsv_read (struct eval_agent_expr_context *ctx, int n)
5044 {
5045 unsigned char *vspace;
5046 LONGEST val;
5047
5048 vspace = add_traceframe_block (ctx->tframe, ctx->tpoint,
5049 1 + sizeof (n) + sizeof (LONGEST));
5050 if (vspace == NULL)
5051 return 1;
5052 /* Identify block as a variable. */
5053 *vspace = 'V';
5054 /* Record variable's number and value. */
5055 memcpy (vspace + 1, &n, sizeof (n));
5056 val = get_trace_state_variable_value (n);
5057 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
5058 trace_debug ("Variable %d recorded", n);
5059 return 0;
5060 }
5061
5062 #ifndef IN_PROCESS_AGENT
5063
5064 /* Callback for traceframe_walk_blocks, used to find a given block
5065 type in a traceframe. */
5066
5067 static int
5068 match_blocktype (char blocktype, unsigned char *dataptr, void *data)
5069 {
5070 char *wantedp = data;
5071
5072 if (*wantedp == blocktype)
5073 return 1;
5074
5075 return 0;
5076 }
5077
5078 /* Walk over all traceframe blocks of the traceframe buffer starting
5079 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5080 block found, passing in DATA unmodified. If CALLBACK returns true,
5081 this returns a pointer to where the block is found. Returns NULL
5082 if no callback call returned true, indicating that all blocks have
5083 been walked. */
5084
5085 static unsigned char *
5086 traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
5087 int tfnum,
5088 int (*callback) (char blocktype,
5089 unsigned char *dataptr,
5090 void *data),
5091 void *data)
5092 {
5093 unsigned char *dataptr;
5094
5095 if (datasize == 0)
5096 {
5097 trace_debug ("traceframe %d has no data", tfnum);
5098 return NULL;
5099 }
5100
5101 /* Iterate through a traceframe's blocks, looking for a block of the
5102 requested type. */
5103 for (dataptr = database;
5104 dataptr < database + datasize;
5105 /* nothing */)
5106 {
5107 char blocktype;
5108 unsigned short mlen;
5109
5110 if (dataptr == trace_buffer_wrap)
5111 {
5112 /* Adjust to reflect wrapping part of the frame around to
5113 the beginning. */
5114 datasize = dataptr - database;
5115 dataptr = database = trace_buffer_lo;
5116 }
5117
5118 blocktype = *dataptr++;
5119
5120 if ((*callback) (blocktype, dataptr, data))
5121 return dataptr;
5122
5123 switch (blocktype)
5124 {
5125 case 'R':
5126 /* Skip over the registers block. */
5127 dataptr += current_target_desc ()->registers_size;
5128 break;
5129 case 'M':
5130 /* Skip over the memory block. */
5131 dataptr += sizeof (CORE_ADDR);
5132 memcpy (&mlen, dataptr, sizeof (mlen));
5133 dataptr += (sizeof (mlen) + mlen);
5134 break;
5135 case 'V':
5136 /* Skip over the TSV block. */
5137 dataptr += (sizeof (int) + sizeof (LONGEST));
5138 break;
5139 case 'S':
5140 /* Skip over the static trace data block. */
5141 memcpy (&mlen, dataptr, sizeof (mlen));
5142 dataptr += (sizeof (mlen) + mlen);
5143 break;
5144 default:
5145 trace_debug ("traceframe %d has unknown block type 0x%x",
5146 tfnum, blocktype);
5147 return NULL;
5148 }
5149 }
5150
5151 return NULL;
5152 }
5153
5154 /* Look for the block of type TYPE_WANTED in the trameframe starting
5155 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5156 number. */
5157
5158 static unsigned char *
5159 traceframe_find_block_type (unsigned char *database, unsigned int datasize,
5160 int tfnum, char type_wanted)
5161 {
5162 return traceframe_walk_blocks (database, datasize, tfnum,
5163 match_blocktype, &type_wanted);
5164 }
5165
5166 static unsigned char *
5167 traceframe_find_regblock (struct traceframe *tframe, int tfnum)
5168 {
5169 unsigned char *regblock;
5170
5171 regblock = traceframe_find_block_type (tframe->data,
5172 tframe->data_size,
5173 tfnum, 'R');
5174
5175 if (regblock == NULL)
5176 trace_debug ("traceframe %d has no register data", tfnum);
5177
5178 return regblock;
5179 }
5180
5181 /* Get registers from a traceframe. */
5182
5183 int
5184 fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
5185 {
5186 unsigned char *dataptr;
5187 struct tracepoint *tpoint;
5188 struct traceframe *tframe;
5189
5190 tframe = find_traceframe (tfnum);
5191
5192 if (tframe == NULL)
5193 {
5194 trace_debug ("traceframe %d not found", tfnum);
5195 return 1;
5196 }
5197
5198 dataptr = traceframe_find_regblock (tframe, tfnum);
5199 if (dataptr == NULL)
5200 {
5201 /* Mark registers unavailable. */
5202 supply_regblock (regcache, NULL);
5203
5204 /* We can generally guess at a PC, although this will be
5205 misleading for while-stepping frames and multi-location
5206 tracepoints. */
5207 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
5208 if (tpoint != NULL)
5209 regcache_write_pc (regcache, tpoint->address);
5210 }
5211 else
5212 supply_regblock (regcache, dataptr);
5213
5214 return 0;
5215 }
5216
5217 static CORE_ADDR
5218 traceframe_get_pc (struct traceframe *tframe)
5219 {
5220 struct regcache regcache;
5221 unsigned char *dataptr;
5222 const struct target_desc *tdesc = current_target_desc ();
5223
5224 dataptr = traceframe_find_regblock (tframe, -1);
5225 if (dataptr == NULL)
5226 return 0;
5227
5228 init_register_cache (&regcache, tdesc, dataptr);
5229 return regcache_read_pc (&regcache);
5230 }
5231
5232 /* Read a requested block of memory from a trace frame. */
5233
5234 int
5235 traceframe_read_mem (int tfnum, CORE_ADDR addr,
5236 unsigned char *buf, ULONGEST length,
5237 ULONGEST *nbytes)
5238 {
5239 struct traceframe *tframe;
5240 unsigned char *database, *dataptr;
5241 unsigned int datasize;
5242 CORE_ADDR maddr;
5243 unsigned short mlen;
5244
5245 trace_debug ("traceframe_read_mem");
5246
5247 tframe = find_traceframe (tfnum);
5248
5249 if (!tframe)
5250 {
5251 trace_debug ("traceframe %d not found", tfnum);
5252 return 1;
5253 }
5254
5255 datasize = tframe->data_size;
5256 database = dataptr = &tframe->data[0];
5257
5258 /* Iterate through a traceframe's blocks, looking for memory. */
5259 while ((dataptr = traceframe_find_block_type (dataptr,
5260 datasize
5261 - (dataptr - database),
5262 tfnum, 'M')) != NULL)
5263 {
5264 memcpy (&maddr, dataptr, sizeof (maddr));
5265 dataptr += sizeof (maddr);
5266 memcpy (&mlen, dataptr, sizeof (mlen));
5267 dataptr += sizeof (mlen);
5268 trace_debug ("traceframe %d has %d bytes at %s",
5269 tfnum, mlen, paddress (maddr));
5270
5271 /* If the block includes the first part of the desired range,
5272 return as much it has; GDB will re-request the remainder,
5273 which might be in a different block of this trace frame. */
5274 if (maddr <= addr && addr < (maddr + mlen))
5275 {
5276 ULONGEST amt = (maddr + mlen) - addr;
5277 if (amt > length)
5278 amt = length;
5279
5280 memcpy (buf, dataptr + (addr - maddr), amt);
5281 *nbytes = amt;
5282 return 0;
5283 }
5284
5285 /* Skip over this block. */
5286 dataptr += mlen;
5287 }
5288
5289 trace_debug ("traceframe %d has no memory data for the desired region",
5290 tfnum);
5291
5292 *nbytes = 0;
5293 return 0;
5294 }
5295
5296 static int
5297 traceframe_read_tsv (int tsvnum, LONGEST *val)
5298 {
5299 int tfnum;
5300 struct traceframe *tframe;
5301 unsigned char *database, *dataptr;
5302 unsigned int datasize;
5303 int vnum;
5304 int found = 0;
5305
5306 trace_debug ("traceframe_read_tsv");
5307
5308 tfnum = current_traceframe;
5309
5310 if (tfnum < 0)
5311 {
5312 trace_debug ("no current traceframe");
5313 return 1;
5314 }
5315
5316 tframe = find_traceframe (tfnum);
5317
5318 if (tframe == NULL)
5319 {
5320 trace_debug ("traceframe %d not found", tfnum);
5321 return 1;
5322 }
5323
5324 datasize = tframe->data_size;
5325 database = dataptr = &tframe->data[0];
5326
5327 /* Iterate through a traceframe's blocks, looking for the last
5328 matched tsv. */
5329 while ((dataptr = traceframe_find_block_type (dataptr,
5330 datasize
5331 - (dataptr - database),
5332 tfnum, 'V')) != NULL)
5333 {
5334 memcpy (&vnum, dataptr, sizeof (vnum));
5335 dataptr += sizeof (vnum);
5336
5337 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
5338
5339 /* Check that this is the variable we want. */
5340 if (tsvnum == vnum)
5341 {
5342 memcpy (val, dataptr, sizeof (*val));
5343 found = 1;
5344 }
5345
5346 /* Skip over this block. */
5347 dataptr += sizeof (LONGEST);
5348 }
5349
5350 if (!found)
5351 trace_debug ("traceframe %d has no data for variable %d",
5352 tfnum, tsvnum);
5353 return !found;
5354 }
5355
5356 /* Read a requested block of static tracepoint data from a trace
5357 frame. */
5358
5359 int
5360 traceframe_read_sdata (int tfnum, ULONGEST offset,
5361 unsigned char *buf, ULONGEST length,
5362 ULONGEST *nbytes)
5363 {
5364 struct traceframe *tframe;
5365 unsigned char *database, *dataptr;
5366 unsigned int datasize;
5367 unsigned short mlen;
5368
5369 trace_debug ("traceframe_read_sdata");
5370
5371 tframe = find_traceframe (tfnum);
5372
5373 if (!tframe)
5374 {
5375 trace_debug ("traceframe %d not found", tfnum);
5376 return 1;
5377 }
5378
5379 datasize = tframe->data_size;
5380 database = &tframe->data[0];
5381
5382 /* Iterate through a traceframe's blocks, looking for static
5383 tracepoint data. */
5384 dataptr = traceframe_find_block_type (database, datasize,
5385 tfnum, 'S');
5386 if (dataptr != NULL)
5387 {
5388 memcpy (&mlen, dataptr, sizeof (mlen));
5389 dataptr += sizeof (mlen);
5390 if (offset < mlen)
5391 {
5392 if (offset + length > mlen)
5393 length = mlen - offset;
5394
5395 memcpy (buf, dataptr, length);
5396 *nbytes = length;
5397 }
5398 else
5399 *nbytes = 0;
5400 return 0;
5401 }
5402
5403 trace_debug ("traceframe %d has no static trace data", tfnum);
5404
5405 *nbytes = 0;
5406 return 0;
5407 }
5408
5409 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5410 object. DATA is pointer to a struct buffer holding the
5411 traceframe-info object being built. */
5412
5413 static int
5414 build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5415 {
5416 struct buffer *buffer = data;
5417
5418 switch (blocktype)
5419 {
5420 case 'M':
5421 {
5422 unsigned short mlen;
5423 CORE_ADDR maddr;
5424
5425 memcpy (&maddr, dataptr, sizeof (maddr));
5426 dataptr += sizeof (maddr);
5427 memcpy (&mlen, dataptr, sizeof (mlen));
5428 dataptr += sizeof (mlen);
5429 buffer_xml_printf (buffer,
5430 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5431 paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5432 break;
5433 }
5434 case 'V':
5435 {
5436 int vnum;
5437
5438 memcpy (&vnum, dataptr, sizeof (vnum));
5439 buffer_xml_printf (buffer, "<tvar id=\"%d\"/>\n", vnum);
5440 break;
5441 }
5442 case 'R':
5443 case 'S':
5444 {
5445 break;
5446 }
5447 default:
5448 warning ("Unhandled trace block type (%d) '%c ' "
5449 "while building trace frame info.",
5450 blocktype, blocktype);
5451 break;
5452 }
5453
5454 return 0;
5455 }
5456
5457 /* Build a traceframe-info object for traceframe number TFNUM into
5458 BUFFER. */
5459
5460 int
5461 traceframe_read_info (int tfnum, struct buffer *buffer)
5462 {
5463 struct traceframe *tframe;
5464
5465 trace_debug ("traceframe_read_info");
5466
5467 tframe = find_traceframe (tfnum);
5468
5469 if (!tframe)
5470 {
5471 trace_debug ("traceframe %d not found", tfnum);
5472 return 1;
5473 }
5474
5475 buffer_grow_str (buffer, "<traceframe-info>\n");
5476 traceframe_walk_blocks (tframe->data, tframe->data_size,
5477 tfnum, build_traceframe_info_xml, buffer);
5478 buffer_grow_str0 (buffer, "</traceframe-info>\n");
5479 return 0;
5480 }
5481
5482 /* Return the first fast tracepoint whose jump pad contains PC. */
5483
5484 static struct tracepoint *
5485 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5486 {
5487 struct tracepoint *tpoint;
5488
5489 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5490 if (tpoint->type == fast_tracepoint)
5491 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5492 return tpoint;
5493
5494 return NULL;
5495 }
5496
5497 /* Return the first fast tracepoint whose trampoline contains PC. */
5498
5499 static struct tracepoint *
5500 fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
5501 {
5502 struct tracepoint *tpoint;
5503
5504 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5505 {
5506 if (tpoint->type == fast_tracepoint
5507 && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
5508 return tpoint;
5509 }
5510
5511 return NULL;
5512 }
5513
5514 /* Return GDBserver's tracepoint that matches the IP Agent's
5515 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5516 address space. */
5517
5518 static struct tracepoint *
5519 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5520 {
5521 struct tracepoint *tpoint;
5522
5523 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5524 if (tpoint->type == fast_tracepoint)
5525 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5526 return tpoint;
5527
5528 return NULL;
5529 }
5530
5531 #endif
5532
5533 /* The type of the object that is used to synchronize fast tracepoint
5534 collection. */
5535
5536 typedef struct collecting_t
5537 {
5538 /* The fast tracepoint number currently collecting. */
5539 uintptr_t tpoint;
5540
5541 /* A number that GDBserver can use to identify the thread that is
5542 presently holding the collect lock. This need not (and usually
5543 is not) the thread id, as getting the current thread ID usually
5544 requires a system call, which we want to avoid like the plague.
5545 Usually this is thread's TCB, found in the TLS (pseudo-)
5546 register, which is readable with a single insn on several
5547 architectures. */
5548 uintptr_t thread_area;
5549 } collecting_t;
5550
5551 #ifndef IN_PROCESS_AGENT
5552
5553 void
5554 force_unlock_trace_buffer (void)
5555 {
5556 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5557 }
5558
5559 /* Check if the thread identified by THREAD_AREA which is stopped at
5560 STOP_PC, is presently locking the fast tracepoint collection, and
5561 if so, gather some status of said collection. Returns 0 if the
5562 thread isn't collecting or in the jump pad at all. 1, if in the
5563 jump pad (or within gdb_collect) and hasn't executed the adjusted
5564 original insn yet (can set a breakpoint there and run to it). 2,
5565 if presently executing the adjusted original insn --- in which
5566 case, if we want to move the thread out of the jump pad, we need to
5567 single-step it until this function returns 0. */
5568
5569 int
5570 fast_tracepoint_collecting (CORE_ADDR thread_area,
5571 CORE_ADDR stop_pc,
5572 struct fast_tpoint_collect_status *status)
5573 {
5574 CORE_ADDR ipa_collecting;
5575 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
5576 CORE_ADDR ipa_gdb_trampoline_buffer;
5577 CORE_ADDR ipa_gdb_trampoline_buffer_end;
5578 struct tracepoint *tpoint;
5579 int needs_breakpoint;
5580
5581 /* The thread THREAD_AREA is either:
5582
5583 0. not collecting at all, not within the jump pad, or within
5584 gdb_collect or one of its callees.
5585
5586 1. in the jump pad and haven't reached gdb_collect
5587
5588 2. within gdb_collect (out of the jump pad) (collect is set)
5589
5590 3. we're in the jump pad, after gdb_collect having returned,
5591 possibly executing the adjusted insns.
5592
5593 For cases 1 and 3, `collecting' may or not be set. The jump pad
5594 doesn't have any complicated jump logic, so we can tell if the
5595 thread is executing the adjust original insn or not by just
5596 matching STOP_PC with known jump pad addresses. If we it isn't
5597 yet executing the original insn, set a breakpoint there, and let
5598 the thread run to it, so to quickly step over a possible (many
5599 insns) gdb_collect call. Otherwise, or when the breakpoint is
5600 hit, only a few (small number of) insns are left to be executed
5601 in the jump pad. Single-step the thread until it leaves the
5602 jump pad. */
5603
5604 again:
5605 tpoint = NULL;
5606 needs_breakpoint = 0;
5607 trace_debug ("fast_tracepoint_collecting");
5608
5609 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5610 &ipa_gdb_jump_pad_buffer))
5611 fatal ("error extracting `gdb_jump_pad_buffer'");
5612 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5613 &ipa_gdb_jump_pad_buffer_end))
5614 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5615
5616 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
5617 &ipa_gdb_trampoline_buffer))
5618 fatal ("error extracting `gdb_trampoline_buffer'");
5619 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
5620 &ipa_gdb_trampoline_buffer_end))
5621 fatal ("error extracting `gdb_trampoline_buffer_end'");
5622
5623 if (ipa_gdb_jump_pad_buffer <= stop_pc
5624 && stop_pc < ipa_gdb_jump_pad_buffer_end)
5625 {
5626 /* We can tell which tracepoint(s) the thread is collecting by
5627 matching the jump pad address back to the tracepoint. */
5628 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5629 if (tpoint == NULL)
5630 {
5631 warning ("in jump pad, but no matching tpoint?");
5632 return 0;
5633 }
5634 else
5635 {
5636 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5637 "adj_insn(%s, %s)",
5638 tpoint->number, paddress (tpoint->address),
5639 paddress (tpoint->jump_pad),
5640 paddress (tpoint->jump_pad_end),
5641 paddress (tpoint->adjusted_insn_addr),
5642 paddress (tpoint->adjusted_insn_addr_end));
5643 }
5644
5645 /* Definitely in the jump pad. May or may not need
5646 fast-exit-jump-pad breakpoint. */
5647 if (tpoint->jump_pad <= stop_pc
5648 && stop_pc < tpoint->adjusted_insn_addr)
5649 needs_breakpoint = 1;
5650 }
5651 else if (ipa_gdb_trampoline_buffer <= stop_pc
5652 && stop_pc < ipa_gdb_trampoline_buffer_end)
5653 {
5654 /* We can tell which tracepoint(s) the thread is collecting by
5655 matching the trampoline address back to the tracepoint. */
5656 tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
5657 if (tpoint == NULL)
5658 {
5659 warning ("in trampoline, but no matching tpoint?");
5660 return 0;
5661 }
5662 else
5663 {
5664 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5665 tpoint->number, paddress (tpoint->address),
5666 paddress (tpoint->trampoline),
5667 paddress (tpoint->trampoline_end));
5668 }
5669
5670 /* Have not reached jump pad yet, but treat the trampoline as a
5671 part of the jump pad that is before the adjusted original
5672 instruction. */
5673 needs_breakpoint = 1;
5674 }
5675 else
5676 {
5677 collecting_t ipa_collecting_obj;
5678
5679 /* If `collecting' is set/locked, then the THREAD_AREA thread
5680 may or not be the one holding the lock. We have to read the
5681 lock to find out. */
5682
5683 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5684 &ipa_collecting))
5685 {
5686 trace_debug ("fast_tracepoint_collecting:"
5687 " failed reading 'collecting' in the inferior");
5688 return 0;
5689 }
5690
5691 if (!ipa_collecting)
5692 {
5693 trace_debug ("fast_tracepoint_collecting: not collecting"
5694 " (and nobody is).");
5695 return 0;
5696 }
5697
5698 /* Some thread is collecting. Check which. */
5699 if (read_inferior_memory (ipa_collecting,
5700 (unsigned char *) &ipa_collecting_obj,
5701 sizeof (ipa_collecting_obj)) != 0)
5702 goto again;
5703
5704 if (ipa_collecting_obj.thread_area != thread_area)
5705 {
5706 trace_debug ("fast_tracepoint_collecting: not collecting "
5707 "(another thread is)");
5708 return 0;
5709 }
5710
5711 tpoint
5712 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5713 if (tpoint == NULL)
5714 {
5715 warning ("fast_tracepoint_collecting: collecting, "
5716 "but tpoint %s not found?",
5717 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5718 return 0;
5719 }
5720
5721 /* The thread is within `gdb_collect', skip over the rest of
5722 fast tracepoint collection quickly using a breakpoint. */
5723 needs_breakpoint = 1;
5724 }
5725
5726 /* The caller wants a bit of status detail. */
5727 if (status != NULL)
5728 {
5729 status->tpoint_num = tpoint->number;
5730 status->tpoint_addr = tpoint->address;
5731 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5732 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5733 }
5734
5735 if (needs_breakpoint)
5736 {
5737 /* Hasn't executed the original instruction yet. Set breakpoint
5738 there, and wait till it's hit, then single-step until exiting
5739 the jump pad. */
5740
5741 trace_debug ("\
5742 fast_tracepoint_collecting, returning continue-until-break at %s",
5743 paddress (tpoint->adjusted_insn_addr));
5744
5745 return 1; /* continue */
5746 }
5747 else
5748 {
5749 /* Just single-step until exiting the jump pad. */
5750
5751 trace_debug ("fast_tracepoint_collecting, returning "
5752 "need-single-step (%s-%s)",
5753 paddress (tpoint->adjusted_insn_addr),
5754 paddress (tpoint->adjusted_insn_addr_end));
5755
5756 return 2; /* single-step */
5757 }
5758 }
5759
5760 #endif
5761
5762 #ifdef IN_PROCESS_AGENT
5763
5764 /* The global fast tracepoint collect lock. Points to a collecting_t
5765 object built on the stack by the jump pad, if presently locked;
5766 NULL if it isn't locked. Note that this lock *must* be set while
5767 executing any *function other than the jump pad. See
5768 fast_tracepoint_collecting. */
5769 static collecting_t * ATTR_USED collecting;
5770
5771 /* This routine, called from the jump pad (in asm) is designed to be
5772 called from the jump pads of fast tracepoints, thus it is on the
5773 critical path. */
5774
5775 IP_AGENT_EXPORT void ATTR_USED
5776 gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5777 {
5778 struct fast_tracepoint_ctx ctx;
5779
5780 /* Don't do anything until the trace run is completely set up. */
5781 if (!tracing)
5782 return;
5783
5784 ctx.base.type = fast_tracepoint;
5785 ctx.regs = regs;
5786 ctx.regcache_initted = 0;
5787 /* Wrap the regblock in a register cache (in the stack, we don't
5788 want to malloc here). */
5789 ctx.regspace = alloca (ipa_tdesc->registers_size);
5790 if (ctx.regspace == NULL)
5791 {
5792 trace_debug ("Trace buffer block allocation failed, skipping");
5793 return;
5794 }
5795
5796 for (ctx.tpoint = tpoint;
5797 ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
5798 ctx.tpoint = ctx.tpoint->next)
5799 {
5800 if (!ctx.tpoint->enabled)
5801 continue;
5802
5803 /* Multiple tracepoints of different types, such as fast tracepoint and
5804 static tracepoint, can be set at the same address. */
5805 if (ctx.tpoint->type != tpoint->type)
5806 continue;
5807
5808 /* Test the condition if present, and collect if true. */
5809 if (ctx.tpoint->cond == NULL
5810 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5811 ctx.tpoint))
5812 {
5813 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5814 ctx.tpoint->address, ctx.tpoint);
5815
5816 /* Note that this will cause original insns to be written back
5817 to where we jumped from, but that's OK because we're jumping
5818 back to the next whole instruction. This will go badly if
5819 instruction restoration is not atomic though. */
5820 if (stopping_tracepoint
5821 || trace_buffer_is_full
5822 || expr_eval_result != expr_eval_no_error)
5823 {
5824 stop_tracing ();
5825 break;
5826 }
5827 }
5828 else
5829 {
5830 /* If there was a condition and it evaluated to false, the only
5831 way we would stop tracing is if there was an error during
5832 condition expression evaluation. */
5833 if (expr_eval_result != expr_eval_no_error)
5834 {
5835 stop_tracing ();
5836 break;
5837 }
5838 }
5839 }
5840 }
5841
5842 #endif
5843
5844 #ifndef IN_PROCESS_AGENT
5845
5846 CORE_ADDR
5847 get_raw_reg_func_addr (void)
5848 {
5849 return ipa_sym_addrs.addr_get_raw_reg;
5850 }
5851
5852 CORE_ADDR
5853 get_get_tsv_func_addr (void)
5854 {
5855 return ipa_sym_addrs.addr_get_trace_state_variable_value;
5856 }
5857
5858 CORE_ADDR
5859 get_set_tsv_func_addr (void)
5860 {
5861 return ipa_sym_addrs.addr_set_trace_state_variable_value;
5862 }
5863
5864 static void
5865 compile_tracepoint_condition (struct tracepoint *tpoint,
5866 CORE_ADDR *jump_entry)
5867 {
5868 CORE_ADDR entry_point = *jump_entry;
5869 enum eval_result_type err;
5870
5871 trace_debug ("Starting condition compilation for tracepoint %d\n",
5872 tpoint->number);
5873
5874 /* Initialize the global pointer to the code being built. */
5875 current_insn_ptr = *jump_entry;
5876
5877 emit_prologue ();
5878
5879 err = compile_bytecodes (tpoint->cond);
5880
5881 if (err == expr_eval_no_error)
5882 {
5883 emit_epilogue ();
5884
5885 /* Record the beginning of the compiled code. */
5886 tpoint->compiled_cond = entry_point;
5887
5888 trace_debug ("Condition compilation for tracepoint %d complete\n",
5889 tpoint->number);
5890 }
5891 else
5892 {
5893 /* Leave the unfinished code in situ, but don't point to it. */
5894
5895 tpoint->compiled_cond = 0;
5896
5897 trace_debug ("Condition compilation for tracepoint %d failed, "
5898 "error code %d",
5899 tpoint->number, err);
5900 }
5901
5902 /* Update the code pointer passed in. Note that we do this even if
5903 the compile fails, so that we can look at the partial results
5904 instead of letting them be overwritten. */
5905 *jump_entry = current_insn_ptr;
5906
5907 /* Leave a gap, to aid dump decipherment. */
5908 *jump_entry += 16;
5909 }
5910
5911 /* We'll need to adjust these when we consider bi-arch setups, and big
5912 endian machines. */
5913
5914 static int
5915 write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
5916 {
5917 return write_inferior_memory (where,
5918 (unsigned char *) &ptr, sizeof (void *));
5919 }
5920
5921 /* The base pointer of the IPA's heap. This is the only memory the
5922 IPA is allowed to use. The IPA should _not_ call the inferior's
5923 `malloc' during operation. That'd be slow, and, most importantly,
5924 it may not be safe. We may be collecting a tracepoint in a signal
5925 handler, for example. */
5926 static CORE_ADDR target_tp_heap;
5927
5928 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5929 to 8 bytes. */
5930
5931 static CORE_ADDR
5932 target_malloc (ULONGEST size)
5933 {
5934 CORE_ADDR ptr;
5935
5936 if (target_tp_heap == 0)
5937 {
5938 /* We have the pointer *address*, need what it points to. */
5939 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
5940 &target_tp_heap))
5941 fatal ("could get target heap head pointer");
5942 }
5943
5944 ptr = target_tp_heap;
5945 target_tp_heap += size;
5946
5947 /* Pad to 8-byte alignment. */
5948 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
5949
5950 return ptr;
5951 }
5952
5953 static CORE_ADDR
5954 download_agent_expr (struct agent_expr *expr)
5955 {
5956 CORE_ADDR expr_addr;
5957 CORE_ADDR expr_bytes;
5958
5959 expr_addr = target_malloc (sizeof (*expr));
5960 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
5961
5962 expr_bytes = target_malloc (expr->length);
5963 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
5964 expr_bytes);
5965 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
5966
5967 return expr_addr;
5968 }
5969
5970 /* Align V up to N bits. */
5971 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5972
5973 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5974
5975 static void
5976 download_tracepoint_1 (struct tracepoint *tpoint)
5977 {
5978 struct tracepoint target_tracepoint;
5979 CORE_ADDR tpptr = 0;
5980
5981 gdb_assert (tpoint->type == fast_tracepoint
5982 || tpoint->type == static_tracepoint);
5983
5984 if (tpoint->cond != NULL && target_emit_ops () != NULL)
5985 {
5986 CORE_ADDR jentry, jump_entry;
5987
5988 jentry = jump_entry = get_jump_space_head ();
5989
5990 if (tpoint->cond != NULL)
5991 {
5992 /* Pad to 8-byte alignment. (needed?) */
5993 /* Actually this should be left for the target to
5994 decide. */
5995 jentry = UALIGN (jentry, 8);
5996
5997 compile_tracepoint_condition (tpoint, &jentry);
5998 }
5999
6000 /* Pad to 8-byte alignment. */
6001 jentry = UALIGN (jentry, 8);
6002 claim_jump_space (jentry - jump_entry);
6003 }
6004
6005 target_tracepoint = *tpoint;
6006
6007 tpptr = target_malloc (sizeof (*tpoint));
6008 tpoint->obj_addr_on_target = tpptr;
6009
6010 /* Write the whole object. We'll fix up its pointers in a bit.
6011 Assume no next for now. This is fixed up above on the next
6012 iteration, if there's any. */
6013 target_tracepoint.next = NULL;
6014 /* Need to clear this here too, since we're downloading the
6015 tracepoints before clearing our own copy. */
6016 target_tracepoint.hit_count = 0;
6017
6018 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
6019 sizeof (target_tracepoint));
6020
6021 if (tpoint->cond)
6022 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6023 cond),
6024 download_agent_expr (tpoint->cond));
6025
6026 if (tpoint->numactions)
6027 {
6028 int i;
6029 CORE_ADDR actions_array;
6030
6031 /* The pointers array. */
6032 actions_array
6033 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
6034 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6035 actions),
6036 actions_array);
6037
6038 /* Now for each pointer, download the action. */
6039 for (i = 0; i < tpoint->numactions; i++)
6040 {
6041 struct tracepoint_action *action = tpoint->actions[i];
6042 CORE_ADDR ipa_action = action->ops->download (action);
6043
6044 if (ipa_action != 0)
6045 write_inferior_data_ptr
6046 (actions_array + i * sizeof (*tpoint->actions),
6047 ipa_action);
6048 }
6049 }
6050 }
6051
6052 #define IPA_PROTO_FAST_TRACE_FLAG 0
6053 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6054 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6055 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6056 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6057
6058 /* Send a command to agent to download and install tracepoint TPOINT. */
6059
6060 static int
6061 tracepoint_send_agent (struct tracepoint *tpoint)
6062 {
6063 char buf[IPA_CMD_BUF_SIZE];
6064 char *p;
6065 int i, ret;
6066
6067 p = buf;
6068 strcpy (p, "FastTrace:");
6069 p += 10;
6070
6071 COPY_FIELD_TO_BUF (p, tpoint, number);
6072 COPY_FIELD_TO_BUF (p, tpoint, address);
6073 COPY_FIELD_TO_BUF (p, tpoint, type);
6074 COPY_FIELD_TO_BUF (p, tpoint, enabled);
6075 COPY_FIELD_TO_BUF (p, tpoint, step_count);
6076 COPY_FIELD_TO_BUF (p, tpoint, pass_count);
6077 COPY_FIELD_TO_BUF (p, tpoint, numactions);
6078 COPY_FIELD_TO_BUF (p, tpoint, hit_count);
6079 COPY_FIELD_TO_BUF (p, tpoint, traceframe_usage);
6080 COPY_FIELD_TO_BUF (p, tpoint, compiled_cond);
6081 COPY_FIELD_TO_BUF (p, tpoint, orig_size);
6082
6083 /* condition */
6084 p = agent_expr_send (p, tpoint->cond);
6085
6086 /* tracepoint_action */
6087 for (i = 0; i < tpoint->numactions; i++)
6088 {
6089 struct tracepoint_action *action = tpoint->actions[i];
6090
6091 p[0] = action->type;
6092 p = action->ops->send (&p[1], action);
6093 }
6094
6095 get_jump_space_head ();
6096 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6097 agent can use jump pad from it. */
6098 if (tpoint->type == fast_tracepoint)
6099 {
6100 memcpy (p, &gdb_jump_pad_head, 8);
6101 p += 8;
6102 }
6103
6104 ret = run_inferior_command (buf, (int) (ptrdiff_t) (p - buf));
6105 if (ret)
6106 return ret;
6107
6108 if (strncmp (buf, "OK", 2) != 0)
6109 return 1;
6110
6111 /* The value of tracepoint's target address is stored in BUF. */
6112 memcpy (&tpoint->obj_addr_on_target,
6113 &buf[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET], 8);
6114
6115 if (tpoint->type == fast_tracepoint)
6116 {
6117 unsigned char *insn
6118 = (unsigned char *) &buf[IPA_PROTO_FAST_TRACE_FJUMP_INSN];
6119 int fjump_size;
6120
6121 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6122 (unsigned int) tpoint->obj_addr_on_target,
6123 (unsigned int) gdb_jump_pad_head);
6124
6125 memcpy (&gdb_jump_pad_head, &buf[IPA_PROTO_FAST_TRACE_JUMP_PAD], 8);
6126
6127 /* This has been done in agent. We should also set up record for it. */
6128 memcpy (&fjump_size, &buf[IPA_PROTO_FAST_TRACE_FJUMP_SIZE], 4);
6129 /* Wire it in. */
6130 tpoint->handle
6131 = set_fast_tracepoint_jump (tpoint->address, insn, fjump_size);
6132 }
6133
6134 return 0;
6135 }
6136
6137 static void
6138 download_tracepoint (struct tracepoint *tpoint)
6139 {
6140 struct tracepoint *tp, *tp_prev;
6141
6142 if (tpoint->type != fast_tracepoint
6143 && tpoint->type != static_tracepoint)
6144 return;
6145
6146 download_tracepoint_1 (tpoint);
6147
6148 /* Find the previous entry of TPOINT, which is fast tracepoint or
6149 static tracepoint. */
6150 tp_prev = NULL;
6151 for (tp = tracepoints; tp != tpoint; tp = tp->next)
6152 {
6153 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
6154 tp_prev = tp;
6155 }
6156
6157 if (tp_prev)
6158 {
6159 CORE_ADDR tp_prev_target_next_addr;
6160
6161 /* Insert TPOINT after TP_PREV in IPA. */
6162 if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
6163 + offsetof (struct tracepoint, next),
6164 &tp_prev_target_next_addr))
6165 fatal ("error reading `tp_prev->next'");
6166
6167 /* tpoint->next = tp_prev->next */
6168 write_inferior_data_ptr (tpoint->obj_addr_on_target
6169 + offsetof (struct tracepoint, next),
6170 tp_prev_target_next_addr);
6171 /* tp_prev->next = tpoint */
6172 write_inferior_data_ptr (tp_prev->obj_addr_on_target
6173 + offsetof (struct tracepoint, next),
6174 tpoint->obj_addr_on_target);
6175 }
6176 else
6177 /* First object in list, set the head pointer in the
6178 inferior. */
6179 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints,
6180 tpoint->obj_addr_on_target);
6181
6182 }
6183
6184 static void
6185 download_trace_state_variables (void)
6186 {
6187 CORE_ADDR ptr = 0, prev_ptr = 0;
6188 struct trace_state_variable *tsv;
6189
6190 /* Start out empty. */
6191 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
6192
6193 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
6194 {
6195 struct trace_state_variable target_tsv;
6196
6197 /* TSV's with a getter have been initialized equally in both the
6198 inferior and GDBserver. Skip them. */
6199 if (tsv->getter != NULL)
6200 continue;
6201
6202 target_tsv = *tsv;
6203
6204 prev_ptr = ptr;
6205 ptr = target_malloc (sizeof (*tsv));
6206
6207 if (tsv == trace_state_variables)
6208 {
6209 /* First object in list, set the head pointer in the
6210 inferior. */
6211
6212 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
6213 ptr);
6214 }
6215 else
6216 {
6217 write_inferior_data_ptr (prev_ptr
6218 + offsetof (struct trace_state_variable,
6219 next),
6220 ptr);
6221 }
6222
6223 /* Write the whole object. We'll fix up its pointers in a bit.
6224 Assume no next, fixup when needed. */
6225 target_tsv.next = NULL;
6226
6227 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
6228 sizeof (target_tsv));
6229
6230 if (tsv->name != NULL)
6231 {
6232 size_t size = strlen (tsv->name) + 1;
6233 CORE_ADDR name_addr = target_malloc (size);
6234 write_inferior_memory (name_addr,
6235 (unsigned char *) tsv->name, size);
6236 write_inferior_data_ptr (ptr
6237 + offsetof (struct trace_state_variable,
6238 name),
6239 name_addr);
6240 }
6241
6242 if (tsv->getter != NULL)
6243 {
6244 fatal ("what to do with these?");
6245 }
6246 }
6247
6248 if (prev_ptr != 0)
6249 {
6250 /* Fixup the next pointer in the last item in the list. */
6251 write_inferior_data_ptr (prev_ptr
6252 + offsetof (struct trace_state_variable,
6253 next), 0);
6254 }
6255 }
6256
6257 /* Upload complete trace frames out of the IP Agent's trace buffer
6258 into GDBserver's trace buffer. This always uploads either all or
6259 no trace frames. This is the counter part of
6260 `trace_alloc_trace_buffer'. See its description of the atomic
6261 synching mechanism. */
6262
6263 static void
6264 upload_fast_traceframes (void)
6265 {
6266 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
6267 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
6268 CORE_ADDR tf;
6269 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
6270 unsigned int curr_tbctrl_idx;
6271 unsigned int ipa_trace_buffer_ctrl_curr;
6272 unsigned int ipa_trace_buffer_ctrl_curr_old;
6273 CORE_ADDR ipa_trace_buffer_ctrl_addr;
6274 struct breakpoint *about_to_request_buffer_space_bkpt;
6275 CORE_ADDR ipa_trace_buffer_lo;
6276 CORE_ADDR ipa_trace_buffer_hi;
6277
6278 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6279 &ipa_traceframe_read_count_racy))
6280 {
6281 /* This will happen in most targets if the current thread is
6282 running. */
6283 return;
6284 }
6285
6286 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6287 &ipa_traceframe_write_count_racy))
6288 return;
6289
6290 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6291 ipa_traceframe_write_count_racy
6292 - ipa_traceframe_read_count_racy,
6293 ipa_traceframe_write_count_racy,
6294 ipa_traceframe_read_count_racy);
6295
6296 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
6297 return;
6298
6299 about_to_request_buffer_space_bkpt
6300 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
6301 NULL);
6302
6303 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6304 &ipa_trace_buffer_ctrl_curr))
6305 return;
6306
6307 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
6308
6309 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
6310
6311 {
6312 unsigned int prev, counter;
6313
6314 /* Update the token, with new counters, and the GDBserver stamp
6315 bit. Alway reuse the current TBC index. */
6316 prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR;
6317 counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR;
6318
6319 ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6320 | (prev << 12)
6321 | counter
6322 | curr_tbctrl_idx);
6323 }
6324
6325 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6326 ipa_trace_buffer_ctrl_curr))
6327 return;
6328
6329 trace_debug ("Lib: Committed %08x -> %08x",
6330 ipa_trace_buffer_ctrl_curr_old,
6331 ipa_trace_buffer_ctrl_curr);
6332
6333 /* Re-read these, now that we've installed the
6334 `about_to_request_buffer_space' breakpoint/lock. A thread could
6335 have finished a traceframe between the last read of these
6336 counters and setting the breakpoint above. If we start
6337 uploading, we never want to leave this function with
6338 traceframe_read_count != 0, otherwise, GDBserver could end up
6339 incrementing the counter tokens more than once (due to event loop
6340 nesting), which would break the IP agent's "effective" detection
6341 (see trace_alloc_trace_buffer). */
6342 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6343 &ipa_traceframe_read_count))
6344 return;
6345 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6346 &ipa_traceframe_write_count))
6347 return;
6348
6349 if (debug_threads)
6350 {
6351 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6352 ipa_traceframe_write_count - ipa_traceframe_read_count,
6353 ipa_traceframe_write_count, ipa_traceframe_read_count);
6354
6355 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
6356 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
6357 trace_debug ("note that ipa_traceframe_count's parts changed");
6358 }
6359
6360 /* Get the address of the current TBC object (the IP agent has an
6361 array of 3 such objects). The index is stored in the TBC
6362 token. */
6363 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
6364 ipa_trace_buffer_ctrl_addr
6365 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
6366
6367 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
6368 (unsigned char *) &ipa_trace_buffer_ctrl,
6369 sizeof (struct ipa_trace_buffer_control)))
6370 return;
6371
6372 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
6373 &ipa_trace_buffer_lo))
6374 return;
6375 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
6376 &ipa_trace_buffer_hi))
6377 return;
6378
6379 /* Offsets are easier to grok for debugging than raw addresses,
6380 especially for the small trace buffer sizes that are useful for
6381 testing. */
6382 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6383 "endfree=%d wrap=%d hi=%d",
6384 curr_tbctrl_idx,
6385 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6386 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6387 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6388 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6389 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6390
6391 /* Note that the IPA's buffer is always circular. */
6392
6393 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6394
6395 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6396 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6397
6398 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6399 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6400 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6401 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6402 : 0))
6403
6404 tf = IPA_FIRST_TRACEFRAME ();
6405
6406 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
6407 {
6408 struct tracepoint *tpoint;
6409 struct traceframe *tframe;
6410 unsigned char *block;
6411 struct traceframe ipa_tframe;
6412
6413 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
6414 offsetof (struct traceframe, data)))
6415 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
6416
6417 if (ipa_tframe.tpnum == 0)
6418 fatal ("Uploading: No (more) fast traceframes, but "
6419 "ipa_traceframe_count == %u??\n",
6420 ipa_traceframe_write_count - ipa_traceframe_read_count);
6421
6422 /* Note that this will be incorrect for multi-location
6423 tracepoints... */
6424 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
6425
6426 tframe = add_traceframe (tpoint);
6427 if (tframe == NULL)
6428 {
6429 trace_buffer_is_full = 1;
6430 trace_debug ("Uploading: trace buffer is full");
6431 }
6432 else
6433 {
6434 /* Copy the whole set of blocks in one go for now. FIXME:
6435 split this in smaller blocks. */
6436 block = add_traceframe_block (tframe, tpoint,
6437 ipa_tframe.data_size);
6438 if (block != NULL)
6439 {
6440 if (read_inferior_memory (tf
6441 + offsetof (struct traceframe, data),
6442 block, ipa_tframe.data_size))
6443 error ("Uploading: Couldn't read traceframe data at %s\n",
6444 paddress (tf + offsetof (struct traceframe, data)));
6445 }
6446
6447 trace_debug ("Uploading: traceframe didn't fit");
6448 finish_traceframe (tframe);
6449 }
6450
6451 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6452
6453 /* If we freed the traceframe that wrapped around, go back
6454 to the non-wrap case. */
6455 if (tf < ipa_trace_buffer_ctrl.start)
6456 {
6457 trace_debug ("Lib: Discarding past the wraparound");
6458 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6459 }
6460 ipa_trace_buffer_ctrl.start = tf;
6461 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6462 ++ipa_traceframe_read_count;
6463
6464 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6465 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6466 {
6467 trace_debug ("Lib: buffer is fully empty. "
6468 "Trace buffer [%d] start=%d free=%d endfree=%d",
6469 curr_tbctrl_idx,
6470 (int) (ipa_trace_buffer_ctrl.start
6471 - ipa_trace_buffer_lo),
6472 (int) (ipa_trace_buffer_ctrl.free
6473 - ipa_trace_buffer_lo),
6474 (int) (ipa_trace_buffer_ctrl.end_free
6475 - ipa_trace_buffer_lo));
6476
6477 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6478 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6479 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6480 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6481 }
6482
6483 trace_debug ("Uploaded a traceframe\n"
6484 "Lib: Trace buffer [%d] start=%d free=%d "
6485 "endfree=%d wrap=%d hi=%d",
6486 curr_tbctrl_idx,
6487 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6488 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6489 (int) (ipa_trace_buffer_ctrl.end_free
6490 - ipa_trace_buffer_lo),
6491 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6492 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6493 }
6494
6495 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
6496 (unsigned char *) &ipa_trace_buffer_ctrl,
6497 sizeof (struct ipa_trace_buffer_control)))
6498 return;
6499
6500 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6501 ipa_traceframe_read_count);
6502
6503 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6504
6505 pause_all (1);
6506 cancel_breakpoints ();
6507
6508 delete_breakpoint (about_to_request_buffer_space_bkpt);
6509 about_to_request_buffer_space_bkpt = NULL;
6510
6511 unpause_all (1);
6512
6513 if (trace_buffer_is_full)
6514 stop_tracing ();
6515 }
6516 #endif
6517
6518 #ifdef IN_PROCESS_AGENT
6519
6520 IP_AGENT_EXPORT int ust_loaded;
6521 IP_AGENT_EXPORT char cmd_buf[IPA_CMD_BUF_SIZE];
6522
6523 #ifdef HAVE_UST
6524
6525 /* Static tracepoints. */
6526
6527 /* UST puts a "struct tracepoint" in the global namespace, which
6528 conflicts with our tracepoint. Arguably, being a library, it
6529 shouldn't take ownership of such a generic name. We work around it
6530 here. */
6531 #define tracepoint ust_tracepoint
6532 #include <ust/ust.h>
6533 #undef tracepoint
6534
6535 extern int serialize_to_text (char *outbuf, int bufsize,
6536 const char *fmt, va_list ap);
6537
6538 #define GDB_PROBE_NAME "gdb"
6539
6540 /* We dynamically search for the UST symbols instead of linking them
6541 in. This lets the user decide if the application uses static
6542 tracepoints, instead of always pulling libust.so in. This vector
6543 holds pointers to all functions we care about. */
6544
6545 static struct
6546 {
6547 int (*serialize_to_text) (char *outbuf, int bufsize,
6548 const char *fmt, va_list ap);
6549
6550 int (*ltt_probe_register) (struct ltt_available_probe *pdata);
6551 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
6552
6553 int (*ltt_marker_connect) (const char *channel, const char *mname,
6554 const char *pname);
6555 int (*ltt_marker_disconnect) (const char *channel, const char *mname,
6556 const char *pname);
6557
6558 void (*marker_iter_start) (struct marker_iter *iter);
6559 void (*marker_iter_next) (struct marker_iter *iter);
6560 void (*marker_iter_stop) (struct marker_iter *iter);
6561 void (*marker_iter_reset) (struct marker_iter *iter);
6562 } ust_ops;
6563
6564 #include <dlfcn.h>
6565
6566 /* Cast through typeof to catch incompatible API changes. Since UST
6567 only builds with gcc, we can freely use gcc extensions here
6568 too. */
6569 #define GET_UST_SYM(SYM) \
6570 do \
6571 { \
6572 if (ust_ops.SYM == NULL) \
6573 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6574 if (ust_ops.SYM == NULL) \
6575 return 0; \
6576 } while (0)
6577
6578 #define USTF(SYM) ust_ops.SYM
6579
6580 /* Get pointers to all libust.so functions we care about. */
6581
6582 static int
6583 dlsym_ust (void)
6584 {
6585 GET_UST_SYM (serialize_to_text);
6586
6587 GET_UST_SYM (ltt_probe_register);
6588 GET_UST_SYM (ltt_probe_unregister);
6589 GET_UST_SYM (ltt_marker_connect);
6590 GET_UST_SYM (ltt_marker_disconnect);
6591
6592 GET_UST_SYM (marker_iter_start);
6593 GET_UST_SYM (marker_iter_next);
6594 GET_UST_SYM (marker_iter_stop);
6595 GET_UST_SYM (marker_iter_reset);
6596
6597 ust_loaded = 1;
6598 return 1;
6599 }
6600
6601 /* Given an UST marker, return the matching gdb static tracepoint.
6602 The match is done by address. */
6603
6604 static struct tracepoint *
6605 ust_marker_to_static_tracepoint (const struct marker *mdata)
6606 {
6607 struct tracepoint *tpoint;
6608
6609 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6610 {
6611 if (tpoint->type != static_tracepoint)
6612 continue;
6613
6614 if (tpoint->address == (uintptr_t) mdata->location)
6615 return tpoint;
6616 }
6617
6618 return NULL;
6619 }
6620
6621 /* The probe function we install on lttng/ust markers. Whenever a
6622 probed ust marker is hit, this function is called. This is similar
6623 to gdb_collect, only for static tracepoints, instead of fast
6624 tracepoints. */
6625
6626 static void
6627 gdb_probe (const struct marker *mdata, void *probe_private,
6628 struct registers *regs, void *call_private,
6629 const char *fmt, va_list *args)
6630 {
6631 struct tracepoint *tpoint;
6632 struct static_tracepoint_ctx ctx;
6633
6634 /* Don't do anything until the trace run is completely set up. */
6635 if (!tracing)
6636 {
6637 trace_debug ("gdb_probe: not tracing\n");
6638 return;
6639 }
6640
6641 ctx.base.type = static_tracepoint;
6642 ctx.regcache_initted = 0;
6643 ctx.regs = regs;
6644 ctx.fmt = fmt;
6645 ctx.args = args;
6646
6647 /* Wrap the regblock in a register cache (in the stack, we don't
6648 want to malloc here). */
6649 ctx.regspace = alloca (ipa_tdesc->registers_size);
6650 if (ctx.regspace == NULL)
6651 {
6652 trace_debug ("Trace buffer block allocation failed, skipping");
6653 return;
6654 }
6655
6656 tpoint = ust_marker_to_static_tracepoint (mdata);
6657 if (tpoint == NULL)
6658 {
6659 trace_debug ("gdb_probe: marker not known: "
6660 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6661 mdata->location, mdata->channel,
6662 mdata->name, mdata->format);
6663 return;
6664 }
6665
6666 if (!tpoint->enabled)
6667 {
6668 trace_debug ("gdb_probe: tracepoint disabled");
6669 return;
6670 }
6671
6672 ctx.tpoint = tpoint;
6673
6674 trace_debug ("gdb_probe: collecting marker: "
6675 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6676 mdata->location, mdata->channel,
6677 mdata->name, mdata->format);
6678
6679 /* Test the condition if present, and collect if true. */
6680 if (tpoint->cond == NULL
6681 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6682 tpoint))
6683 {
6684 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6685 tpoint->address, tpoint);
6686
6687 if (stopping_tracepoint
6688 || trace_buffer_is_full
6689 || expr_eval_result != expr_eval_no_error)
6690 stop_tracing ();
6691 }
6692 else
6693 {
6694 /* If there was a condition and it evaluated to false, the only
6695 way we would stop tracing is if there was an error during
6696 condition expression evaluation. */
6697 if (expr_eval_result != expr_eval_no_error)
6698 stop_tracing ();
6699 }
6700 }
6701
6702 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6703 static tracepoint string data. This is a string passed to the
6704 tracing library by the user, at the time of the tracepoint marker
6705 call. E.g., in the UST marker call:
6706
6707 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6708
6709 the collected data is "str FOOBAZ".
6710 */
6711
6712 static void
6713 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
6714 struct traceframe *tframe)
6715 {
6716 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
6717 unsigned char *bufspace;
6718 int size;
6719 va_list copy;
6720 unsigned short blocklen;
6721
6722 if (umd == NULL)
6723 {
6724 trace_debug ("Wanted to collect static trace data, "
6725 "but there's no static trace data");
6726 return;
6727 }
6728
6729 va_copy (copy, *umd->args);
6730 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
6731 va_end (copy);
6732
6733 trace_debug ("Want to collect ust data");
6734
6735 /* 'S' + size + string */
6736 bufspace = add_traceframe_block (tframe, umd->tpoint,
6737 1 + sizeof (blocklen) + size + 1);
6738 if (bufspace == NULL)
6739 {
6740 trace_debug ("Trace buffer block allocation failed, skipping");
6741 return;
6742 }
6743
6744 /* Identify a static trace data block. */
6745 *bufspace = 'S';
6746
6747 blocklen = size + 1;
6748 memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
6749
6750 va_copy (copy, *umd->args);
6751 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
6752 size + 1, umd->fmt, copy);
6753 va_end (copy);
6754
6755 trace_debug ("Storing static tracepoint data in regblock: %s",
6756 bufspace + 1 + sizeof (blocklen));
6757 }
6758
6759 /* The probe to register with lttng/ust. */
6760 static struct ltt_available_probe gdb_ust_probe =
6761 {
6762 GDB_PROBE_NAME,
6763 NULL,
6764 gdb_probe,
6765 };
6766
6767 #endif /* HAVE_UST */
6768 #endif /* IN_PROCESS_AGENT */
6769
6770 #ifndef IN_PROCESS_AGENT
6771
6772 /* Ask the in-process agent to run a command. Since we don't want to
6773 have to handle the IPA hitting breakpoints while running the
6774 command, we pause all threads, remove all breakpoints, and then set
6775 the helper thread re-running. We communicate with the helper
6776 thread by means of direct memory xfering, and a socket for
6777 synchronization. */
6778
6779 static int
6780 run_inferior_command (char *cmd, int len)
6781 {
6782 int err = -1;
6783 int pid = ptid_get_pid (current_ptid);
6784
6785 trace_debug ("run_inferior_command: running: %s", cmd);
6786
6787 pause_all (0);
6788 uninsert_all_breakpoints ();
6789
6790 err = agent_run_command (pid, (const char *) cmd, len);
6791
6792 reinsert_all_breakpoints ();
6793 unpause_all (0);
6794
6795 return err;
6796 }
6797
6798 #else /* !IN_PROCESS_AGENT */
6799
6800 #include <sys/socket.h>
6801 #include <sys/un.h>
6802
6803 #ifndef UNIX_PATH_MAX
6804 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6805 #endif
6806
6807 /* Where we put the socked used for synchronization. */
6808 #define SOCK_DIR P_tmpdir
6809
6810 /* Thread ID of the helper thread. GDBserver reads this to know which
6811 is the help thread. This is an LWP id on Linux. */
6812 int helper_thread_id;
6813
6814 static int
6815 init_named_socket (const char *name)
6816 {
6817 int result, fd;
6818 struct sockaddr_un addr;
6819
6820 result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
6821 if (result == -1)
6822 {
6823 warning ("socket creation failed: %s", strerror (errno));
6824 return -1;
6825 }
6826
6827 addr.sun_family = AF_UNIX;
6828
6829 strncpy (addr.sun_path, name, UNIX_PATH_MAX);
6830 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
6831
6832 result = access (name, F_OK);
6833 if (result == 0)
6834 {
6835 /* File exists. */
6836 result = unlink (name);
6837 if (result == -1)
6838 {
6839 warning ("unlink failed: %s", strerror (errno));
6840 close (fd);
6841 return -1;
6842 }
6843 warning ("socket %s already exists; overwriting", name);
6844 }
6845
6846 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
6847 if (result == -1)
6848 {
6849 warning ("bind failed: %s", strerror (errno));
6850 close (fd);
6851 return -1;
6852 }
6853
6854 result = listen (fd, 1);
6855 if (result == -1)
6856 {
6857 warning ("listen: %s", strerror (errno));
6858 close (fd);
6859 return -1;
6860 }
6861
6862 return fd;
6863 }
6864
6865 static char agent_socket_name[UNIX_PATH_MAX];
6866
6867 static int
6868 gdb_agent_socket_init (void)
6869 {
6870 int result, fd;
6871
6872 result = xsnprintf (agent_socket_name, UNIX_PATH_MAX, "%s/gdb_ust%d",
6873 SOCK_DIR, getpid ());
6874 if (result >= UNIX_PATH_MAX)
6875 {
6876 trace_debug ("string overflow allocating socket name");
6877 return -1;
6878 }
6879
6880 fd = init_named_socket (agent_socket_name);
6881 if (fd < 0)
6882 warning ("Error initializing named socket (%s) for communication with the "
6883 "ust helper thread. Check that directory exists and that it "
6884 "is writable.", agent_socket_name);
6885
6886 return fd;
6887 }
6888
6889 #ifdef HAVE_UST
6890
6891 /* The next marker to be returned on a qTsSTM command. */
6892 static const struct marker *next_st;
6893
6894 /* Returns the first known marker. */
6895
6896 struct marker *
6897 first_marker (void)
6898 {
6899 struct marker_iter iter;
6900
6901 USTF(marker_iter_reset) (&iter);
6902 USTF(marker_iter_start) (&iter);
6903
6904 return iter.marker;
6905 }
6906
6907 /* Returns the marker following M. */
6908
6909 const struct marker *
6910 next_marker (const struct marker *m)
6911 {
6912 struct marker_iter iter;
6913
6914 USTF(marker_iter_reset) (&iter);
6915 USTF(marker_iter_start) (&iter);
6916
6917 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6918 {
6919 if (iter.marker == m)
6920 {
6921 USTF(marker_iter_next) (&iter);
6922 return iter.marker;
6923 }
6924 }
6925
6926 return NULL;
6927 }
6928
6929 /* Return an hexstr version of the STR C string, fit for sending to
6930 GDB. */
6931
6932 static char *
6933 cstr_to_hexstr (const char *str)
6934 {
6935 int len = strlen (str);
6936 char *hexstr = xmalloc (len * 2 + 1);
6937 bin2hex ((gdb_byte *) str, hexstr, len);
6938 return hexstr;
6939 }
6940
6941 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6942 packets. */
6943
6944 static void
6945 response_ust_marker (char *packet, const struct marker *st)
6946 {
6947 char *strid, *format, *tmp;
6948
6949 next_st = next_marker (st);
6950
6951 tmp = xmalloc (strlen (st->channel) + 1 +
6952 strlen (st->name) + 1);
6953 sprintf (tmp, "%s/%s", st->channel, st->name);
6954
6955 strid = cstr_to_hexstr (tmp);
6956 free (tmp);
6957
6958 format = cstr_to_hexstr (st->format);
6959
6960 sprintf (packet, "m%s:%s:%s",
6961 paddress ((uintptr_t) st->location),
6962 strid,
6963 format);
6964
6965 free (strid);
6966 free (format);
6967 }
6968
6969 /* Return the first static tracepoint, and initialize the state
6970 machine that will iterate through all the static tracepoints. */
6971
6972 static void
6973 cmd_qtfstm (char *packet)
6974 {
6975 trace_debug ("Returning first trace state variable definition");
6976
6977 if (first_marker ())
6978 response_ust_marker (packet, first_marker ());
6979 else
6980 strcpy (packet, "l");
6981 }
6982
6983 /* Return additional trace state variable definitions. */
6984
6985 static void
6986 cmd_qtsstm (char *packet)
6987 {
6988 trace_debug ("Returning static tracepoint");
6989
6990 if (next_st)
6991 response_ust_marker (packet, next_st);
6992 else
6993 strcpy (packet, "l");
6994 }
6995
6996 /* Disconnect the GDB probe from a marker at a given address. */
6997
6998 static void
6999 unprobe_marker_at (char *packet)
7000 {
7001 char *p = packet;
7002 ULONGEST address;
7003 struct marker_iter iter;
7004
7005 p += sizeof ("unprobe_marker_at:") - 1;
7006
7007 p = unpack_varlen_hex (p, &address);
7008
7009 USTF(marker_iter_reset) (&iter);
7010 USTF(marker_iter_start) (&iter);
7011 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7012 if ((uintptr_t ) iter.marker->location == address)
7013 {
7014 int result;
7015
7016 result = USTF(ltt_marker_disconnect) (iter.marker->channel,
7017 iter.marker->name,
7018 GDB_PROBE_NAME);
7019 if (result < 0)
7020 warning ("could not disable marker %s/%s",
7021 iter.marker->channel, iter.marker->name);
7022 break;
7023 }
7024 }
7025
7026 /* Connect the GDB probe to a marker at a given address. */
7027
7028 static int
7029 probe_marker_at (char *packet)
7030 {
7031 char *p = packet;
7032 ULONGEST address;
7033 struct marker_iter iter;
7034 struct marker *m;
7035
7036 p += sizeof ("probe_marker_at:") - 1;
7037
7038 p = unpack_varlen_hex (p, &address);
7039
7040 USTF(marker_iter_reset) (&iter);
7041
7042 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7043 m != NULL;
7044 USTF(marker_iter_next) (&iter), m = iter.marker)
7045 if ((uintptr_t ) m->location == address)
7046 {
7047 int result;
7048
7049 trace_debug ("found marker for address. "
7050 "ltt_marker_connect (marker = %s/%s)",
7051 m->channel, m->name);
7052
7053 result = USTF(ltt_marker_connect) (m->channel, m->name,
7054 GDB_PROBE_NAME);
7055 if (result && result != -EEXIST)
7056 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7057 m->channel, m->name, -result);
7058
7059 if (result < 0)
7060 {
7061 sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
7062 m->channel, m->name);
7063 return -1;
7064 }
7065
7066 strcpy (packet, "OK");
7067 return 0;
7068 }
7069
7070 sprintf (packet, "E.no marker found at 0x%s", paddress (address));
7071 return -1;
7072 }
7073
7074 static int
7075 cmd_qtstmat (char *packet)
7076 {
7077 char *p = packet;
7078 ULONGEST address;
7079 struct marker_iter iter;
7080 struct marker *m;
7081
7082 p += sizeof ("qTSTMat:") - 1;
7083
7084 p = unpack_varlen_hex (p, &address);
7085
7086 USTF(marker_iter_reset) (&iter);
7087
7088 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7089 m != NULL;
7090 USTF(marker_iter_next) (&iter), m = iter.marker)
7091 if ((uintptr_t ) m->location == address)
7092 {
7093 response_ust_marker (packet, m);
7094 return 0;
7095 }
7096
7097 strcpy (packet, "l");
7098 return -1;
7099 }
7100
7101 static void
7102 gdb_ust_init (void)
7103 {
7104 if (!dlsym_ust ())
7105 return;
7106
7107 USTF(ltt_probe_register) (&gdb_ust_probe);
7108 }
7109
7110 #endif /* HAVE_UST */
7111
7112 #include <sys/syscall.h>
7113 #include <stdlib.h>
7114
7115 static void
7116 gdb_agent_remove_socket (void)
7117 {
7118 unlink (agent_socket_name);
7119 }
7120
7121 /* Helper thread of agent. */
7122
7123 static void *
7124 gdb_agent_helper_thread (void *arg)
7125 {
7126 int listen_fd;
7127
7128 atexit (gdb_agent_remove_socket);
7129
7130 while (1)
7131 {
7132 listen_fd = gdb_agent_socket_init ();
7133
7134 if (helper_thread_id == 0)
7135 helper_thread_id = syscall (SYS_gettid);
7136
7137 if (listen_fd == -1)
7138 {
7139 warning ("could not create sync socket\n");
7140 break;
7141 }
7142
7143 while (1)
7144 {
7145 socklen_t tmp;
7146 struct sockaddr_un sockaddr;
7147 int fd;
7148 char buf[1];
7149 int ret;
7150 int stop_loop = 0;
7151
7152 tmp = sizeof (sockaddr);
7153
7154 do
7155 {
7156 fd = accept (listen_fd, &sockaddr, &tmp);
7157 }
7158 /* It seems an ERESTARTSYS can escape out of accept. */
7159 while (fd == -512 || (fd == -1 && errno == EINTR));
7160
7161 if (fd < 0)
7162 {
7163 warning ("Accept returned %d, error: %s\n",
7164 fd, strerror (errno));
7165 break;
7166 }
7167
7168 do
7169 {
7170 ret = read (fd, buf, 1);
7171 } while (ret == -1 && errno == EINTR);
7172
7173 if (ret == -1)
7174 {
7175 warning ("reading socket (fd=%d) failed with %s",
7176 fd, strerror (errno));
7177 close (fd);
7178 break;
7179 }
7180
7181 if (cmd_buf[0])
7182 {
7183 if (strncmp ("close", cmd_buf, 5) == 0)
7184 {
7185 stop_loop = 1;
7186 }
7187 #ifdef HAVE_UST
7188 else if (strcmp ("qTfSTM", cmd_buf) == 0)
7189 {
7190 cmd_qtfstm (cmd_buf);
7191 }
7192 else if (strcmp ("qTsSTM", cmd_buf) == 0)
7193 {
7194 cmd_qtsstm (cmd_buf);
7195 }
7196 else if (strncmp ("unprobe_marker_at:",
7197 cmd_buf,
7198 sizeof ("unprobe_marker_at:") - 1) == 0)
7199 {
7200 unprobe_marker_at (cmd_buf);
7201 }
7202 else if (strncmp ("probe_marker_at:",
7203 cmd_buf,
7204 sizeof ("probe_marker_at:") - 1) == 0)
7205 {
7206 probe_marker_at (cmd_buf);
7207 }
7208 else if (strncmp ("qTSTMat:",
7209 cmd_buf,
7210 sizeof ("qTSTMat:") - 1) == 0)
7211 {
7212 cmd_qtstmat (cmd_buf);
7213 }
7214 #endif /* HAVE_UST */
7215 }
7216
7217 /* Fix compiler's warning: ignoring return value of 'write'. */
7218 ret = write (fd, buf, 1);
7219 close (fd);
7220
7221 if (stop_loop)
7222 {
7223 close (listen_fd);
7224 unlink (agent_socket_name);
7225
7226 /* Sleep endlessly to wait the whole inferior stops. This
7227 thread can not exit because GDB or GDBserver may still need
7228 'current_inferior' (representing this thread) to access
7229 inferior memory. Otherwise, this thread exits earlier than
7230 other threads, and 'current_inferior' is set to NULL. */
7231 while (1)
7232 sleep (10);
7233 }
7234 }
7235 }
7236
7237 return NULL;
7238 }
7239
7240 #include <signal.h>
7241 #include <pthread.h>
7242
7243 IP_AGENT_EXPORT int gdb_agent_capability = AGENT_CAPA_STATIC_TRACE;
7244
7245 static void
7246 gdb_agent_init (void)
7247 {
7248 int res;
7249 pthread_t thread;
7250 sigset_t new_mask;
7251 sigset_t orig_mask;
7252
7253 /* We want the helper thread to be as transparent as possible, so
7254 have it inherit an all-signals-blocked mask. */
7255
7256 sigfillset (&new_mask);
7257 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
7258 if (res)
7259 fatal ("pthread_sigmask (1) failed: %s", strerror (res));
7260
7261 res = pthread_create (&thread,
7262 NULL,
7263 gdb_agent_helper_thread,
7264 NULL);
7265
7266 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
7267 if (res)
7268 fatal ("pthread_sigmask (2) failed: %s", strerror (res));
7269
7270 while (helper_thread_id == 0)
7271 usleep (1);
7272
7273 #ifdef HAVE_UST
7274 gdb_ust_init ();
7275 #endif
7276 }
7277
7278 #include <sys/mman.h>
7279 #include <fcntl.h>
7280
7281 IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
7282 IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
7283 IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
7284 IP_AGENT_EXPORT char *gdb_trampoline_buffer;
7285 IP_AGENT_EXPORT char *gdb_trampoline_buffer_end;
7286 IP_AGENT_EXPORT char *gdb_trampoline_buffer_error;
7287
7288 /* Record the result of getting buffer space for fast tracepoint
7289 trampolines. Any error message is copied, since caller may not be
7290 using persistent storage. */
7291
7292 void
7293 set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
7294 {
7295 gdb_trampoline_buffer = (char *) (uintptr_t) begin;
7296 gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
7297 if (errmsg)
7298 strncpy (gdb_trampoline_buffer_error, errmsg, 99);
7299 else
7300 strcpy (gdb_trampoline_buffer_error, "no buffer passed");
7301 }
7302
7303 static void __attribute__ ((constructor))
7304 initialize_tracepoint_ftlib (void)
7305 {
7306 initialize_tracepoint ();
7307
7308 gdb_agent_init ();
7309 }
7310
7311 #endif /* IN_PROCESS_AGENT */
7312
7313 /* Return a timestamp, expressed as microseconds of the usual Unix
7314 time. (As the result is a 64-bit number, it will not overflow any
7315 time soon.) */
7316
7317 static LONGEST
7318 get_timestamp (void)
7319 {
7320 struct timeval tv;
7321
7322 if (gettimeofday (&tv, 0) != 0)
7323 return -1;
7324 else
7325 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
7326 }
7327
7328 void
7329 initialize_tracepoint (void)
7330 {
7331 /* Start with the default size. */
7332 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE);
7333
7334 /* Wire trace state variable 1 to be the timestamp. This will be
7335 uploaded to GDB upon connection and become one of its trace state
7336 variables. (In case you're wondering, if GDB already has a trace
7337 variable numbered 1, it will be renumbered.) */
7338 create_trace_state_variable (1, 0);
7339 set_trace_state_variable_name (1, "trace_timestamp");
7340 set_trace_state_variable_getter (1, get_timestamp);
7341
7342 #ifdef IN_PROCESS_AGENT
7343 {
7344 uintptr_t addr;
7345 int pagesize;
7346
7347 pagesize = sysconf (_SC_PAGE_SIZE);
7348 if (pagesize == -1)
7349 fatal ("sysconf");
7350
7351 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
7352
7353 #define SCRATCH_BUFFER_NPAGES 20
7354
7355 /* Allocate scratch buffer aligned on a page boundary, at a low
7356 address (close to the main executable's code). */
7357 for (addr = pagesize; addr != 0; addr += pagesize)
7358 {
7359 gdb_jump_pad_buffer = mmap ((void *) addr, pagesize * SCRATCH_BUFFER_NPAGES,
7360 PROT_READ | PROT_WRITE | PROT_EXEC,
7361 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
7362 -1, 0);
7363 if (gdb_jump_pad_buffer != MAP_FAILED)
7364 break;
7365 }
7366
7367 if (addr == 0)
7368 fatal ("\
7369 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7370 strerror (errno));
7371
7372 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * SCRATCH_BUFFER_NPAGES;
7373 }
7374
7375 gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
7376
7377 /* It's not a fatal error for something to go wrong with trampoline
7378 buffer setup, but it can be mysterious, so create a channel to
7379 report back on what went wrong, using a fixed size since we may
7380 not be able to allocate space later when the problem occurs. */
7381 gdb_trampoline_buffer_error = xmalloc (IPA_BUFSIZ);
7382
7383 strcpy (gdb_trampoline_buffer_error, "No errors reported");
7384
7385 initialize_low_tracepoint ();
7386 #endif
7387 }
This page took 0.263563 seconds and 4 git commands to generate.