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