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