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