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