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