Commit | Line | Data |
---|---|---|
d0353e76 YQ |
1 | /* CTF format support. |
2 | ||
3 | Copyright (C) 2012-2013 Free Software Foundation, Inc. | |
4 | Contributed by Hui Zhu <hui_zhu@mentor.com> | |
5 | Contributed by Yao Qi <yao@codesourcery.com> | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | #include "defs.h" | |
23 | #include "ctf.h" | |
24 | #include "tracepoint.h" | |
25 | #include "regcache.h" | |
af307d6a | 26 | #include "gdb_stat.h" |
393fd4c3 | 27 | #include "exec.h" |
da9160e4 | 28 | #include "completer.h" |
d0353e76 YQ |
29 | |
30 | #include <ctype.h> | |
31 | ||
32 | /* GDB saves trace buffers and other information (such as trace | |
33 | status) got from the remote target into Common Trace Format (CTF). | |
34 | The following types of information are expected to save in CTF: | |
35 | ||
36 | 1. The length (in bytes) of register cache. Event "register" will | |
37 | be defined in metadata, which includes the length. | |
38 | ||
393fd4c3 YQ |
39 | 2. Trace status. Event "status" is defined in metadata, which |
40 | includes all aspects of trace status. | |
d0353e76 | 41 | |
393fd4c3 YQ |
42 | 3. Uploaded trace variables. Event "tsv_def" is defined in |
43 | metadata, which is about all aspects of a uploaded trace variable. | |
44 | Uploaded tracepoints. Event "tp_def" is defined in meta, which | |
45 | is about all aspects of an uploaded tracepoint. Note that the | |
46 | "sequence" (a CTF type, which is a dynamically-sized array.) is | |
47 | used for "actions" "step_actions" and "cmd_strings". | |
d0353e76 YQ |
48 | |
49 | 4. Trace frames. Each trace frame is composed by several blocks | |
50 | of different types ('R', 'M', 'V'). One trace frame is saved in | |
51 | one CTF packet and the blocks of this frame are saved as events. | |
52 | 4.1: The trace frame related information (such as the number of | |
53 | tracepoint associated with this frame) is saved in the packet | |
54 | context. | |
55 | 4.2: The block 'M', 'R' and 'V' are saved in event "memory", | |
56 | "register" and "tsv" respectively. | |
57 | 4.3: When iterating over events, babeltrace can't tell iterator | |
58 | goes to a new packet, so we need a marker or anchor to tell GDB | |
59 | that iterator goes into a new packet or frame. We define event | |
60 | "frame". */ | |
61 | ||
62 | #define CTF_MAGIC 0xC1FC1FC1 | |
63 | #define CTF_SAVE_MAJOR 1 | |
64 | #define CTF_SAVE_MINOR 8 | |
65 | ||
66 | #define CTF_METADATA_NAME "metadata" | |
67 | #define CTF_DATASTREAM_NAME "datastream" | |
68 | ||
69 | /* Reserved event id. */ | |
70 | ||
71 | #define CTF_EVENT_ID_REGISTER 0 | |
72 | #define CTF_EVENT_ID_TSV 1 | |
73 | #define CTF_EVENT_ID_MEMORY 2 | |
74 | #define CTF_EVENT_ID_FRAME 3 | |
393fd4c3 YQ |
75 | #define CTF_EVENT_ID_STATUS 4 |
76 | #define CTF_EVENT_ID_TSV_DEF 5 | |
77 | #define CTF_EVENT_ID_TP_DEF 6 | |
d0353e76 YQ |
78 | |
79 | /* The state kept while writing the CTF datastream file. */ | |
80 | ||
81 | struct trace_write_handler | |
82 | { | |
83 | /* File descriptor of metadata. */ | |
84 | FILE *metadata_fd; | |
85 | /* File descriptor of traceframes. */ | |
86 | FILE *datastream_fd; | |
87 | ||
88 | /* This is the content size of the current packet. */ | |
89 | size_t content_size; | |
90 | ||
91 | /* This is the start offset of current packet. */ | |
92 | long packet_start; | |
93 | }; | |
94 | ||
95 | /* Write metadata in FORMAT. */ | |
96 | ||
97 | static void | |
98 | ctf_save_write_metadata (struct trace_write_handler *handler, | |
99 | const char *format, ...) | |
100 | { | |
101 | va_list args; | |
102 | ||
103 | va_start (args, format); | |
104 | if (vfprintf (handler->metadata_fd, format, args) < 0) | |
105 | error (_("Unable to write metadata file (%s)"), | |
106 | safe_strerror (errno)); | |
107 | va_end (args); | |
108 | } | |
109 | ||
110 | /* Write BUF of length SIZE to datastream file represented by | |
111 | HANDLER. */ | |
112 | ||
113 | static int | |
114 | ctf_save_write (struct trace_write_handler *handler, | |
115 | const gdb_byte *buf, size_t size) | |
116 | { | |
117 | if (fwrite (buf, size, 1, handler->datastream_fd) != 1) | |
118 | error (_("Unable to write file for saving trace data (%s)"), | |
119 | safe_strerror (errno)); | |
120 | ||
121 | handler->content_size += size; | |
122 | ||
123 | return 0; | |
124 | } | |
125 | ||
126 | /* Write a unsigned 32-bit integer to datastream file represented by | |
127 | HANDLER. */ | |
128 | ||
129 | #define ctf_save_write_uint32(HANDLER, U32) \ | |
130 | ctf_save_write (HANDLER, (gdb_byte *) &U32, 4) | |
131 | ||
393fd4c3 YQ |
132 | /* Write a signed 32-bit integer to datastream file represented by |
133 | HANDLER. */ | |
134 | ||
135 | #define ctf_save_write_int32(HANDLER, INT32) \ | |
136 | ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4) | |
137 | ||
d0353e76 YQ |
138 | /* Set datastream file position. Update HANDLER->content_size |
139 | if WHENCE is SEEK_CUR. */ | |
140 | ||
141 | static int | |
142 | ctf_save_fseek (struct trace_write_handler *handler, long offset, | |
143 | int whence) | |
144 | { | |
145 | gdb_assert (whence != SEEK_END); | |
146 | gdb_assert (whence != SEEK_SET | |
147 | || offset <= handler->content_size + handler->packet_start); | |
148 | ||
149 | if (fseek (handler->datastream_fd, offset, whence)) | |
150 | error (_("Unable to seek file for saving trace data (%s)"), | |
151 | safe_strerror (errno)); | |
152 | ||
153 | if (whence == SEEK_CUR) | |
154 | handler->content_size += offset; | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | /* Change the datastream file position to align on ALIGN_SIZE, | |
160 | and write BUF to datastream file. The size of BUF is SIZE. */ | |
161 | ||
162 | static int | |
163 | ctf_save_align_write (struct trace_write_handler *handler, | |
164 | const gdb_byte *buf, | |
165 | size_t size, size_t align_size) | |
166 | { | |
167 | long offset | |
168 | = (align_up (handler->content_size, align_size) | |
169 | - handler->content_size); | |
170 | ||
171 | if (ctf_save_fseek (handler, offset, SEEK_CUR)) | |
172 | return -1; | |
173 | ||
174 | if (ctf_save_write (handler, buf, size)) | |
175 | return -1; | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
180 | /* Write events to next new packet. */ | |
181 | ||
182 | static void | |
183 | ctf_save_next_packet (struct trace_write_handler *handler) | |
184 | { | |
185 | handler->packet_start += (handler->content_size + 4); | |
186 | ctf_save_fseek (handler, handler->packet_start, SEEK_SET); | |
187 | handler->content_size = 0; | |
188 | } | |
189 | ||
190 | /* Write the CTF metadata header. */ | |
191 | ||
192 | static void | |
193 | ctf_save_metadata_header (struct trace_write_handler *handler) | |
194 | { | |
195 | const char metadata_fmt[] = | |
196 | "\ntrace {\n" | |
197 | " major = %u;\n" | |
198 | " minor = %u;\n" | |
199 | " byte_order = %s;\n" /* be or le */ | |
200 | " packet.header := struct {\n" | |
201 | " uint32_t magic;\n" | |
202 | " };\n" | |
203 | "};\n" | |
204 | "\n" | |
205 | "stream {\n" | |
206 | " packet.context := struct {\n" | |
207 | " uint32_t content_size;\n" | |
208 | " uint32_t packet_size;\n" | |
209 | " uint16_t tpnum;\n" | |
210 | " };\n" | |
211 | " event.header := struct {\n" | |
212 | " uint32_t id;\n" | |
213 | " };\n" | |
214 | "};\n"; | |
215 | ||
216 | ctf_save_write_metadata (handler, "/* CTF %d.%d */\n", | |
217 | CTF_SAVE_MAJOR, CTF_SAVE_MINOR); | |
218 | ctf_save_write_metadata (handler, | |
219 | "typealias integer { size = 8; align = 8; " | |
220 | "signed = false; encoding = ascii;}" | |
221 | " := ascii;\n"); | |
222 | ctf_save_write_metadata (handler, | |
223 | "typealias integer { size = 8; align = 8; " | |
224 | "signed = false; }" | |
225 | " := uint8_t;\n"); | |
226 | ctf_save_write_metadata (handler, | |
227 | "typealias integer { size = 16; align = 16;" | |
228 | "signed = false; } := uint16_t;\n"); | |
229 | ctf_save_write_metadata (handler, | |
230 | "typealias integer { size = 32; align = 32;" | |
231 | "signed = false; } := uint32_t;\n"); | |
232 | ctf_save_write_metadata (handler, | |
233 | "typealias integer { size = 64; align = 64;" | |
234 | "signed = false; base = hex;}" | |
235 | " := uint64_t;\n"); | |
393fd4c3 YQ |
236 | ctf_save_write_metadata (handler, |
237 | "typealias integer { size = 32; align = 32;" | |
238 | "signed = true; } := int32_t;\n"); | |
239 | ctf_save_write_metadata (handler, | |
240 | "typealias integer { size = 64; align = 64;" | |
241 | "signed = true; } := int64_t;\n"); | |
242 | ctf_save_write_metadata (handler, | |
243 | "typealias string { encoding = ascii;" | |
244 | " } := chars;\n"); | |
d0353e76 YQ |
245 | ctf_save_write_metadata (handler, "\n"); |
246 | ||
8249a5a9 YQ |
247 | /* Get the byte order of the host and write CTF data in this byte |
248 | order. */ | |
249 | #if WORDS_BIGENDIAN | |
250 | #define HOST_ENDIANNESS "be" | |
251 | #else | |
252 | #define HOST_ENDIANNESS "le" | |
253 | #endif | |
254 | ||
d0353e76 YQ |
255 | ctf_save_write_metadata (handler, metadata_fmt, |
256 | CTF_SAVE_MAJOR, CTF_SAVE_MINOR, | |
8249a5a9 | 257 | HOST_ENDIANNESS); |
d0353e76 YQ |
258 | ctf_save_write_metadata (handler, "\n"); |
259 | } | |
260 | ||
261 | /* CTF trace writer. */ | |
262 | ||
263 | struct ctf_trace_file_writer | |
264 | { | |
265 | struct trace_file_writer base; | |
266 | ||
267 | /* States related to writing CTF trace file. */ | |
268 | struct trace_write_handler tcs; | |
269 | }; | |
270 | ||
271 | /* This is the implementation of trace_file_write_ops method | |
272 | dtor. */ | |
273 | ||
274 | static void | |
275 | ctf_dtor (struct trace_file_writer *self) | |
276 | { | |
277 | struct ctf_trace_file_writer *writer | |
278 | = (struct ctf_trace_file_writer *) self; | |
279 | ||
280 | if (writer->tcs.metadata_fd != NULL) | |
281 | fclose (writer->tcs.metadata_fd); | |
282 | ||
283 | if (writer->tcs.datastream_fd != NULL) | |
284 | fclose (writer->tcs.datastream_fd); | |
285 | ||
286 | } | |
287 | ||
288 | /* This is the implementation of trace_file_write_ops method | |
289 | target_save. */ | |
290 | ||
291 | static int | |
292 | ctf_target_save (struct trace_file_writer *self, | |
293 | const char *dirname) | |
294 | { | |
295 | /* Don't support save trace file to CTF format in the target. */ | |
296 | return 0; | |
297 | } | |
298 | ||
af307d6a | 299 | #ifdef USE_WIN32API |
a4341769 YQ |
300 | #undef mkdir |
301 | #define mkdir(pathname, mode) mkdir (pathname) | |
af307d6a YQ |
302 | #endif |
303 | ||
d0353e76 YQ |
304 | /* This is the implementation of trace_file_write_ops method |
305 | start. It creates the directory DIRNAME, metadata and datastream | |
306 | in the directory. */ | |
307 | ||
308 | static void | |
309 | ctf_start (struct trace_file_writer *self, const char *dirname) | |
310 | { | |
311 | char *file_name; | |
312 | struct cleanup *old_chain; | |
313 | struct ctf_trace_file_writer *writer | |
314 | = (struct ctf_trace_file_writer *) self; | |
315 | int i; | |
af307d6a YQ |
316 | mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR |
317 | #ifdef S_IRGRP | |
318 | | S_IRGRP | |
319 | #endif | |
320 | #ifdef S_IXGRP | |
321 | | S_IXGRP | |
322 | #endif | |
323 | | S_IROTH /* Defined in common/gdb_stat.h if not defined. */ | |
324 | #ifdef S_IXOTH | |
325 | | S_IXOTH | |
326 | #endif | |
327 | ; | |
d0353e76 YQ |
328 | |
329 | /* Create DIRNAME. */ | |
af307d6a | 330 | if (mkdir (dirname, hmode) && errno != EEXIST) |
d0353e76 YQ |
331 | error (_("Unable to open directory '%s' for saving trace data (%s)"), |
332 | dirname, safe_strerror (errno)); | |
333 | ||
334 | memset (&writer->tcs, '\0', sizeof (writer->tcs)); | |
335 | ||
336 | file_name = xstrprintf ("%s/%s", dirname, CTF_METADATA_NAME); | |
337 | old_chain = make_cleanup (xfree, file_name); | |
338 | ||
339 | writer->tcs.metadata_fd = fopen (file_name, "w"); | |
340 | if (writer->tcs.metadata_fd == NULL) | |
341 | error (_("Unable to open file '%s' for saving trace data (%s)"), | |
342 | file_name, safe_strerror (errno)); | |
343 | do_cleanups (old_chain); | |
344 | ||
345 | ctf_save_metadata_header (&writer->tcs); | |
346 | ||
347 | file_name = xstrprintf ("%s/%s", dirname, CTF_DATASTREAM_NAME); | |
348 | old_chain = make_cleanup (xfree, file_name); | |
349 | writer->tcs.datastream_fd = fopen (file_name, "w"); | |
350 | if (writer->tcs.datastream_fd == NULL) | |
351 | error (_("Unable to open file '%s' for saving trace data (%s)"), | |
352 | file_name, safe_strerror (errno)); | |
353 | do_cleanups (old_chain); | |
354 | } | |
355 | ||
356 | /* This is the implementation of trace_file_write_ops method | |
357 | write_header. Write the types of events on trace variable and | |
358 | frame. */ | |
359 | ||
360 | static void | |
361 | ctf_write_header (struct trace_file_writer *self) | |
362 | { | |
363 | struct ctf_trace_file_writer *writer | |
364 | = (struct ctf_trace_file_writer *) self; | |
365 | ||
366 | ||
367 | ctf_save_write_metadata (&writer->tcs, "\n"); | |
368 | ctf_save_write_metadata (&writer->tcs, | |
369 | "event {\n\tname = \"memory\";\n\tid = %u;\n" | |
370 | "\tfields := struct { \n" | |
371 | "\t\tuint64_t address;\n" | |
372 | "\t\tuint16_t length;\n" | |
373 | "\t\tuint8_t contents[length];\n" | |
374 | "\t};\n" | |
375 | "};\n", CTF_EVENT_ID_MEMORY); | |
376 | ||
377 | ctf_save_write_metadata (&writer->tcs, "\n"); | |
378 | ctf_save_write_metadata (&writer->tcs, | |
379 | "event {\n\tname = \"tsv\";\n\tid = %u;\n" | |
380 | "\tfields := struct { \n" | |
381 | "\t\tuint64_t val;\n" | |
382 | "\t\tuint32_t num;\n" | |
383 | "\t};\n" | |
384 | "};\n", CTF_EVENT_ID_TSV); | |
385 | ||
386 | ctf_save_write_metadata (&writer->tcs, "\n"); | |
387 | ctf_save_write_metadata (&writer->tcs, | |
388 | "event {\n\tname = \"frame\";\n\tid = %u;\n" | |
389 | "\tfields := struct { \n" | |
390 | "\t};\n" | |
391 | "};\n", CTF_EVENT_ID_FRAME); | |
392 | ||
393fd4c3 YQ |
393 | ctf_save_write_metadata (&writer->tcs, "\n"); |
394 | ctf_save_write_metadata (&writer->tcs, | |
395 | "event {\n\tname = \"tsv_def\";\n" | |
396 | "\tid = %u;\n\tfields := struct { \n" | |
397 | "\t\tint64_t initial_value;\n" | |
398 | "\t\tint32_t number;\n" | |
399 | "\t\tint32_t builtin;\n" | |
400 | "\t\tchars name;\n" | |
401 | "\t};\n" | |
402 | "};\n", CTF_EVENT_ID_TSV_DEF); | |
403 | ||
404 | ctf_save_write_metadata (&writer->tcs, "\n"); | |
405 | ctf_save_write_metadata (&writer->tcs, | |
406 | "event {\n\tname = \"tp_def\";\n" | |
407 | "\tid = %u;\n\tfields := struct { \n" | |
408 | "\t\tuint64_t addr;\n" | |
409 | "\t\tuint64_t traceframe_usage;\n" | |
410 | "\t\tint32_t number;\n" | |
411 | "\t\tint32_t enabled;\n" | |
412 | "\t\tint32_t step;\n" | |
413 | "\t\tint32_t pass;\n" | |
414 | "\t\tint32_t hit_count;\n" | |
415 | "\t\tint32_t type;\n" | |
416 | "\t\tchars cond;\n" | |
417 | ||
418 | "\t\tuint32_t action_num;\n" | |
419 | "\t\tchars actions[action_num];\n" | |
420 | ||
421 | "\t\tuint32_t step_action_num;\n" | |
422 | "\t\tchars step_actions[step_action_num];\n" | |
423 | ||
424 | "\t\tchars at_string;\n" | |
425 | "\t\tchars cond_string;\n" | |
426 | ||
427 | "\t\tuint32_t cmd_num;\n" | |
428 | "\t\tchars cmd_strings[cmd_num];\n" | |
429 | "\t};\n" | |
430 | "};\n", CTF_EVENT_ID_TP_DEF); | |
431 | ||
d0353e76 YQ |
432 | gdb_assert (writer->tcs.content_size == 0); |
433 | gdb_assert (writer->tcs.packet_start == 0); | |
393fd4c3 YQ |
434 | |
435 | /* Create a new packet to contain this event. */ | |
436 | self->ops->frame_ops->start (self, 0); | |
d0353e76 YQ |
437 | } |
438 | ||
439 | /* This is the implementation of trace_file_write_ops method | |
440 | write_regblock_type. Write the type of register event in | |
441 | metadata. */ | |
442 | ||
443 | static void | |
444 | ctf_write_regblock_type (struct trace_file_writer *self, int size) | |
445 | { | |
446 | struct ctf_trace_file_writer *writer | |
447 | = (struct ctf_trace_file_writer *) self; | |
448 | ||
449 | ctf_save_write_metadata (&writer->tcs, "\n"); | |
450 | ||
451 | ctf_save_write_metadata (&writer->tcs, | |
452 | "event {\n\tname = \"register\";\n\tid = %u;\n" | |
453 | "\tfields := struct { \n" | |
454 | "\t\tascii contents[%d];\n" | |
455 | "\t};\n" | |
456 | "};\n", | |
457 | CTF_EVENT_ID_REGISTER, size); | |
458 | } | |
459 | ||
460 | /* This is the implementation of trace_file_write_ops method | |
461 | write_status. */ | |
462 | ||
463 | static void | |
464 | ctf_write_status (struct trace_file_writer *self, | |
465 | struct trace_status *ts) | |
466 | { | |
393fd4c3 YQ |
467 | struct ctf_trace_file_writer *writer |
468 | = (struct ctf_trace_file_writer *) self; | |
469 | uint32_t id; | |
470 | int32_t int32; | |
471 | ||
472 | ctf_save_write_metadata (&writer->tcs, "\n"); | |
473 | ctf_save_write_metadata (&writer->tcs, | |
474 | "event {\n\tname = \"status\";\n\tid = %u;\n" | |
475 | "\tfields := struct { \n" | |
476 | "\t\tint32_t stop_reason;\n" | |
477 | "\t\tint32_t stopping_tracepoint;\n" | |
478 | "\t\tint32_t traceframe_count;\n" | |
479 | "\t\tint32_t traceframes_created;\n" | |
480 | "\t\tint32_t buffer_free;\n" | |
481 | "\t\tint32_t buffer_size;\n" | |
482 | "\t\tint32_t disconnected_tracing;\n" | |
483 | "\t\tint32_t circular_buffer;\n" | |
484 | "\t};\n" | |
485 | "};\n", | |
486 | CTF_EVENT_ID_STATUS); | |
487 | ||
488 | id = CTF_EVENT_ID_STATUS; | |
489 | /* Event Id. */ | |
490 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4); | |
491 | ||
492 | ctf_save_write_int32 (&writer->tcs, ts->stop_reason); | |
493 | ctf_save_write_int32 (&writer->tcs, ts->stopping_tracepoint); | |
494 | ctf_save_write_int32 (&writer->tcs, ts->traceframe_count); | |
495 | ctf_save_write_int32 (&writer->tcs, ts->traceframes_created); | |
496 | ctf_save_write_int32 (&writer->tcs, ts->buffer_free); | |
497 | ctf_save_write_int32 (&writer->tcs, ts->buffer_size); | |
498 | ctf_save_write_int32 (&writer->tcs, ts->disconnected_tracing); | |
499 | ctf_save_write_int32 (&writer->tcs, ts->circular_buffer); | |
d0353e76 YQ |
500 | } |
501 | ||
502 | /* This is the implementation of trace_file_write_ops method | |
503 | write_uploaded_tsv. */ | |
504 | ||
505 | static void | |
506 | ctf_write_uploaded_tsv (struct trace_file_writer *self, | |
507 | struct uploaded_tsv *tsv) | |
508 | { | |
393fd4c3 YQ |
509 | struct ctf_trace_file_writer *writer |
510 | = (struct ctf_trace_file_writer *) self; | |
511 | int32_t int32; | |
512 | int64_t int64; | |
513 | unsigned int len; | |
514 | const gdb_byte zero = 0; | |
515 | ||
516 | /* Event Id. */ | |
517 | int32 = CTF_EVENT_ID_TSV_DEF; | |
518 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4); | |
519 | ||
520 | /* initial_value */ | |
521 | int64 = tsv->initial_value; | |
522 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8); | |
523 | ||
524 | /* number */ | |
525 | ctf_save_write_int32 (&writer->tcs, tsv->number); | |
526 | ||
527 | /* builtin */ | |
528 | ctf_save_write_int32 (&writer->tcs, tsv->builtin); | |
529 | ||
530 | /* name */ | |
531 | if (tsv->name != NULL) | |
a398505b PA |
532 | ctf_save_write (&writer->tcs, (gdb_byte *) tsv->name, |
533 | strlen (tsv->name)); | |
393fd4c3 | 534 | ctf_save_write (&writer->tcs, &zero, 1); |
d0353e76 YQ |
535 | } |
536 | ||
537 | /* This is the implementation of trace_file_write_ops method | |
538 | write_uploaded_tp. */ | |
539 | ||
540 | static void | |
541 | ctf_write_uploaded_tp (struct trace_file_writer *self, | |
542 | struct uploaded_tp *tp) | |
543 | { | |
393fd4c3 YQ |
544 | struct ctf_trace_file_writer *writer |
545 | = (struct ctf_trace_file_writer *) self; | |
546 | int32_t int32; | |
547 | int64_t int64; | |
548 | uint32_t u32; | |
549 | const gdb_byte zero = 0; | |
550 | int a; | |
551 | char *act; | |
552 | ||
553 | /* Event Id. */ | |
554 | int32 = CTF_EVENT_ID_TP_DEF; | |
555 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4); | |
556 | ||
557 | /* address */ | |
558 | int64 = tp->addr; | |
559 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8); | |
560 | ||
561 | /* traceframe_usage */ | |
562 | int64 = tp->traceframe_usage; | |
563 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8); | |
564 | ||
565 | /* number */ | |
566 | ctf_save_write_int32 (&writer->tcs, tp->number); | |
567 | ||
568 | /* enabled */ | |
569 | ctf_save_write_int32 (&writer->tcs, tp->enabled); | |
570 | ||
571 | /* step */ | |
572 | ctf_save_write_int32 (&writer->tcs, tp->step); | |
573 | ||
574 | /* pass */ | |
575 | ctf_save_write_int32 (&writer->tcs, tp->pass); | |
576 | ||
577 | /* hit_count */ | |
578 | ctf_save_write_int32 (&writer->tcs, tp->hit_count); | |
579 | ||
580 | /* type */ | |
581 | ctf_save_write_int32 (&writer->tcs, tp->type); | |
582 | ||
583 | /* condition */ | |
584 | if (tp->cond != NULL) | |
a398505b | 585 | ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond, strlen (tp->cond)); |
393fd4c3 YQ |
586 | ctf_save_write (&writer->tcs, &zero, 1); |
587 | ||
588 | /* actions */ | |
589 | u32 = VEC_length (char_ptr, tp->actions); | |
590 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4); | |
591 | for (a = 0; VEC_iterate (char_ptr, tp->actions, a, act); ++a) | |
a398505b | 592 | ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1); |
393fd4c3 YQ |
593 | |
594 | /* step_actions */ | |
595 | u32 = VEC_length (char_ptr, tp->step_actions); | |
596 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4); | |
597 | for (a = 0; VEC_iterate (char_ptr, tp->step_actions, a, act); ++a) | |
a398505b | 598 | ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1); |
393fd4c3 YQ |
599 | |
600 | /* at_string */ | |
601 | if (tp->at_string != NULL) | |
a398505b | 602 | ctf_save_write (&writer->tcs, (gdb_byte *) tp->at_string, |
393fd4c3 YQ |
603 | strlen (tp->at_string)); |
604 | ctf_save_write (&writer->tcs, &zero, 1); | |
605 | ||
606 | /* cond_string */ | |
607 | if (tp->cond_string != NULL) | |
a398505b | 608 | ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond_string, |
393fd4c3 YQ |
609 | strlen (tp->cond_string)); |
610 | ctf_save_write (&writer->tcs, &zero, 1); | |
611 | ||
612 | /* cmd_strings */ | |
613 | u32 = VEC_length (char_ptr, tp->cmd_strings); | |
614 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4); | |
615 | for (a = 0; VEC_iterate (char_ptr, tp->cmd_strings, a, act); ++a) | |
a398505b | 616 | ctf_save_write (&writer->tcs, (gdb_byte *) act, strlen (act) + 1); |
393fd4c3 | 617 | |
d0353e76 YQ |
618 | } |
619 | ||
620 | /* This is the implementation of trace_file_write_ops method | |
621 | write_definition_end. */ | |
622 | ||
623 | static void | |
624 | ctf_write_definition_end (struct trace_file_writer *self) | |
625 | { | |
393fd4c3 YQ |
626 | struct ctf_trace_file_writer *writer |
627 | = (struct ctf_trace_file_writer *) self; | |
628 | ||
629 | self->ops->frame_ops->end (self); | |
d0353e76 YQ |
630 | } |
631 | ||
632 | /* The minimal file size of data stream. It is required by | |
633 | babeltrace. */ | |
634 | ||
635 | #define CTF_FILE_MIN_SIZE 4096 | |
636 | ||
637 | /* This is the implementation of trace_file_write_ops method | |
638 | end. */ | |
639 | ||
640 | static void | |
641 | ctf_end (struct trace_file_writer *self) | |
642 | { | |
643 | struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self; | |
644 | ||
645 | gdb_assert (writer->tcs.content_size == 0); | |
646 | /* The babeltrace requires or assumes that the size of datastream | |
647 | file is greater than 4096 bytes. If we don't generate enough | |
648 | packets and events, create a fake packet which has zero event, | |
649 | to use up the space. */ | |
650 | if (writer->tcs.packet_start < CTF_FILE_MIN_SIZE) | |
651 | { | |
652 | uint32_t u32; | |
653 | ||
654 | /* magic. */ | |
655 | u32 = CTF_MAGIC; | |
656 | ctf_save_write_uint32 (&writer->tcs, u32); | |
657 | ||
658 | /* content_size. */ | |
659 | u32 = 0; | |
660 | ctf_save_write_uint32 (&writer->tcs, u32); | |
661 | ||
662 | /* packet_size. */ | |
663 | u32 = 12; | |
664 | if (writer->tcs.packet_start + u32 < CTF_FILE_MIN_SIZE) | |
665 | u32 = CTF_FILE_MIN_SIZE - writer->tcs.packet_start; | |
666 | ||
667 | u32 *= TARGET_CHAR_BIT; | |
668 | ctf_save_write_uint32 (&writer->tcs, u32); | |
669 | ||
670 | /* tpnum. */ | |
671 | u32 = 0; | |
672 | ctf_save_write (&writer->tcs, (gdb_byte *) &u32, 2); | |
673 | ||
674 | /* Enlarge the file to CTF_FILE_MIN_SIZE is it is still less | |
675 | than that. */ | |
676 | if (CTF_FILE_MIN_SIZE | |
677 | > (writer->tcs.packet_start + writer->tcs.content_size)) | |
678 | { | |
679 | gdb_byte b = 0; | |
680 | ||
681 | /* Fake the content size to avoid assertion failure in | |
682 | ctf_save_fseek. */ | |
683 | writer->tcs.content_size = (CTF_FILE_MIN_SIZE | |
684 | - 1 - writer->tcs.packet_start); | |
685 | ctf_save_fseek (&writer->tcs, CTF_FILE_MIN_SIZE - 1, | |
686 | SEEK_SET); | |
687 | ctf_save_write (&writer->tcs, &b, 1); | |
688 | } | |
689 | } | |
690 | } | |
691 | ||
692 | /* This is the implementation of trace_frame_write_ops method | |
693 | start. */ | |
694 | ||
695 | static void | |
696 | ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum) | |
697 | { | |
698 | struct ctf_trace_file_writer *writer | |
699 | = (struct ctf_trace_file_writer *) self; | |
700 | uint32_t id = CTF_EVENT_ID_FRAME; | |
701 | uint32_t u32; | |
702 | ||
703 | /* Step 1: Write packet context. */ | |
704 | /* magic. */ | |
705 | u32 = CTF_MAGIC; | |
706 | ctf_save_write_uint32 (&writer->tcs, u32); | |
707 | /* content_size and packet_size.. We still don't know the value, | |
708 | write it later. */ | |
709 | ctf_save_fseek (&writer->tcs, 4, SEEK_CUR); | |
710 | ctf_save_fseek (&writer->tcs, 4, SEEK_CUR); | |
711 | /* Tracepoint number. */ | |
712 | ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2); | |
713 | ||
714 | /* Step 2: Write event "frame". */ | |
715 | /* Event Id. */ | |
716 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4); | |
717 | } | |
718 | ||
719 | /* This is the implementation of trace_frame_write_ops method | |
720 | write_r_block. */ | |
721 | ||
722 | static void | |
723 | ctf_write_frame_r_block (struct trace_file_writer *self, | |
724 | gdb_byte *buf, int32_t size) | |
725 | { | |
726 | struct ctf_trace_file_writer *writer | |
727 | = (struct ctf_trace_file_writer *) self; | |
728 | uint32_t id = CTF_EVENT_ID_REGISTER; | |
729 | ||
730 | /* Event Id. */ | |
731 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4); | |
732 | ||
733 | /* array contents. */ | |
734 | ctf_save_align_write (&writer->tcs, buf, size, 1); | |
735 | } | |
736 | ||
737 | /* This is the implementation of trace_frame_write_ops method | |
738 | write_m_block_header. */ | |
739 | ||
740 | static void | |
741 | ctf_write_frame_m_block_header (struct trace_file_writer *self, | |
742 | uint64_t addr, uint16_t length) | |
743 | { | |
744 | struct ctf_trace_file_writer *writer | |
745 | = (struct ctf_trace_file_writer *) self; | |
746 | uint32_t event_id = CTF_EVENT_ID_MEMORY; | |
747 | ||
748 | /* Event Id. */ | |
749 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4); | |
750 | ||
751 | /* Address. */ | |
752 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8); | |
753 | ||
754 | /* Length. */ | |
755 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2); | |
756 | } | |
757 | ||
758 | /* This is the implementation of trace_frame_write_ops method | |
759 | write_m_block_memory. */ | |
760 | ||
761 | static void | |
762 | ctf_write_frame_m_block_memory (struct trace_file_writer *self, | |
763 | gdb_byte *buf, uint16_t length) | |
764 | { | |
765 | struct ctf_trace_file_writer *writer | |
766 | = (struct ctf_trace_file_writer *) self; | |
767 | ||
768 | /* Contents. */ | |
769 | ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1); | |
770 | } | |
771 | ||
772 | /* This is the implementation of trace_frame_write_ops method | |
773 | write_v_block. */ | |
774 | ||
775 | static void | |
776 | ctf_write_frame_v_block (struct trace_file_writer *self, | |
777 | int32_t num, uint64_t val) | |
778 | { | |
779 | struct ctf_trace_file_writer *writer | |
780 | = (struct ctf_trace_file_writer *) self; | |
781 | uint32_t id = CTF_EVENT_ID_TSV; | |
782 | ||
783 | /* Event Id. */ | |
784 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4); | |
785 | ||
786 | /* val. */ | |
787 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8); | |
788 | /* num. */ | |
789 | ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4); | |
790 | } | |
791 | ||
792 | /* This is the implementation of trace_frame_write_ops method | |
793 | end. */ | |
794 | ||
795 | static void | |
796 | ctf_write_frame_end (struct trace_file_writer *self) | |
797 | { | |
798 | struct ctf_trace_file_writer *writer | |
799 | = (struct ctf_trace_file_writer *) self; | |
800 | uint32_t u32; | |
801 | uint32_t t; | |
802 | ||
803 | /* Write the content size to packet header. */ | |
804 | ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4, | |
805 | SEEK_SET); | |
806 | u32 = writer->tcs.content_size * TARGET_CHAR_BIT; | |
807 | ||
808 | t = writer->tcs.content_size; | |
809 | ctf_save_write_uint32 (&writer->tcs, u32); | |
810 | ||
811 | /* Write the packet size. */ | |
812 | u32 += 4 * TARGET_CHAR_BIT; | |
813 | ctf_save_write_uint32 (&writer->tcs, u32); | |
814 | ||
815 | writer->tcs.content_size = t; | |
816 | ||
817 | /* Write zero at the end of the packet. */ | |
818 | ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t, | |
819 | SEEK_SET); | |
820 | u32 = 0; | |
821 | ctf_save_write_uint32 (&writer->tcs, u32); | |
822 | writer->tcs.content_size = t; | |
823 | ||
824 | ctf_save_next_packet (&writer->tcs); | |
825 | } | |
826 | ||
827 | /* Operations to write various types of trace frames into CTF | |
828 | format. */ | |
829 | ||
830 | static const struct trace_frame_write_ops ctf_write_frame_ops = | |
831 | { | |
832 | ctf_write_frame_start, | |
833 | ctf_write_frame_r_block, | |
834 | ctf_write_frame_m_block_header, | |
835 | ctf_write_frame_m_block_memory, | |
836 | ctf_write_frame_v_block, | |
837 | ctf_write_frame_end, | |
838 | }; | |
839 | ||
840 | /* Operations to write trace buffers into CTF format. */ | |
841 | ||
842 | static const struct trace_file_write_ops ctf_write_ops = | |
843 | { | |
844 | ctf_dtor, | |
845 | ctf_target_save, | |
846 | ctf_start, | |
847 | ctf_write_header, | |
848 | ctf_write_regblock_type, | |
849 | ctf_write_status, | |
850 | ctf_write_uploaded_tsv, | |
851 | ctf_write_uploaded_tp, | |
852 | ctf_write_definition_end, | |
853 | NULL, | |
854 | &ctf_write_frame_ops, | |
855 | ctf_end, | |
856 | }; | |
857 | ||
858 | /* Return a trace writer for CTF format. */ | |
859 | ||
860 | struct trace_file_writer * | |
861 | ctf_trace_file_writer_new (void) | |
862 | { | |
863 | struct ctf_trace_file_writer *writer | |
864 | = xmalloc (sizeof (struct ctf_trace_file_writer)); | |
865 | ||
866 | writer->base.ops = &ctf_write_ops; | |
867 | ||
868 | return (struct trace_file_writer *) writer; | |
869 | } | |
393fd4c3 YQ |
870 | |
871 | #if HAVE_LIBBABELTRACE | |
872 | /* Use libbabeltrace to read CTF data. The libbabeltrace provides | |
873 | iterator to iterate over each event in CTF data and APIs to get | |
874 | details of event and packet, so it is very convenient to use | |
875 | libbabeltrace to access events in CTF. */ | |
876 | ||
877 | #include <babeltrace/babeltrace.h> | |
878 | #include <babeltrace/ctf/events.h> | |
879 | #include <babeltrace/ctf/iterator.h> | |
880 | ||
881 | /* The struct pointer for current CTF directory. */ | |
882 | static struct bt_context *ctx = NULL; | |
883 | static struct bt_ctf_iter *ctf_iter = NULL; | |
884 | /* The position of the first packet containing trace frame. */ | |
885 | static struct bt_iter_pos *start_pos; | |
886 | ||
887 | /* The name of CTF directory. */ | |
888 | static char *trace_dirname; | |
889 | ||
890 | static struct target_ops ctf_ops; | |
891 | ||
892 | /* Destroy ctf iterator and context. */ | |
893 | ||
894 | static void | |
895 | ctf_destroy (void) | |
896 | { | |
897 | if (ctf_iter != NULL) | |
898 | { | |
899 | bt_ctf_iter_destroy (ctf_iter); | |
900 | ctf_iter = NULL; | |
901 | } | |
902 | if (ctx != NULL) | |
903 | { | |
904 | bt_context_put (ctx); | |
905 | ctx = NULL; | |
906 | } | |
907 | } | |
908 | ||
909 | /* Open CTF trace data in DIRNAME. */ | |
910 | ||
911 | static void | |
912 | ctf_open_dir (char *dirname) | |
913 | { | |
914 | int ret; | |
915 | struct bt_iter_pos begin_pos; | |
916 | struct bt_iter_pos *pos; | |
917 | ||
918 | ctx = bt_context_create (); | |
919 | if (ctx == NULL) | |
920 | error (_("Unable to create bt_context")); | |
921 | ret = bt_context_add_trace (ctx, dirname, "ctf", NULL, NULL, NULL); | |
922 | if (ret < 0) | |
923 | { | |
924 | ctf_destroy (); | |
925 | error (_("Unable to use libbabeltrace on directory \"%s\""), | |
926 | dirname); | |
927 | } | |
928 | ||
929 | begin_pos.type = BT_SEEK_BEGIN; | |
930 | ctf_iter = bt_ctf_iter_create (ctx, &begin_pos, NULL); | |
931 | if (ctf_iter == NULL) | |
932 | { | |
933 | ctf_destroy (); | |
934 | error (_("Unable to create bt_iterator")); | |
935 | } | |
936 | ||
937 | /* Iterate over events, and look for an event for register block | |
938 | to set trace_regblock_size. */ | |
939 | ||
940 | /* Save the current position. */ | |
941 | pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); | |
942 | gdb_assert (pos->type == BT_SEEK_RESTORE); | |
943 | ||
944 | while (1) | |
945 | { | |
946 | const char *name; | |
947 | struct bt_ctf_event *event; | |
948 | ||
949 | event = bt_ctf_iter_read_event (ctf_iter); | |
950 | ||
951 | name = bt_ctf_event_name (event); | |
952 | ||
953 | if (name == NULL) | |
954 | break; | |
955 | else if (strcmp (name, "register") == 0) | |
956 | { | |
957 | const struct bt_definition *scope | |
958 | = bt_ctf_get_top_level_scope (event, | |
959 | BT_EVENT_FIELDS); | |
960 | const struct bt_definition *array | |
961 | = bt_ctf_get_field (event, scope, "contents"); | |
962 | ||
963 | trace_regblock_size | |
964 | = bt_ctf_get_array_len (bt_ctf_get_decl_from_def (array)); | |
965 | } | |
966 | ||
967 | if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) | |
968 | break; | |
969 | } | |
970 | ||
971 | /* Restore the position. */ | |
972 | bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); | |
973 | } | |
974 | ||
975 | #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \ | |
976 | (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \ | |
977 | (SCOPE), \ | |
978 | #FIELD)) | |
979 | ||
980 | /* EVENT is the "status" event and TS is filled in. */ | |
981 | ||
982 | static void | |
983 | ctf_read_status (struct bt_ctf_event *event, struct trace_status *ts) | |
984 | { | |
985 | const struct bt_definition *scope | |
986 | = bt_ctf_get_top_level_scope (event, BT_EVENT_FIELDS); | |
987 | ||
988 | SET_INT32_FIELD (event, scope, ts, stop_reason); | |
989 | SET_INT32_FIELD (event, scope, ts, stopping_tracepoint); | |
990 | SET_INT32_FIELD (event, scope, ts, traceframe_count); | |
991 | SET_INT32_FIELD (event, scope, ts, traceframes_created); | |
992 | SET_INT32_FIELD (event, scope, ts, buffer_free); | |
993 | SET_INT32_FIELD (event, scope, ts, buffer_size); | |
994 | SET_INT32_FIELD (event, scope, ts, disconnected_tracing); | |
995 | SET_INT32_FIELD (event, scope, ts, circular_buffer); | |
996 | ||
997 | bt_iter_next (bt_ctf_get_iter (ctf_iter)); | |
998 | } | |
999 | ||
1000 | /* Read the events "tsv_def" one by one, extract its contents and fill | |
1001 | in the list UPLOADED_TSVS. */ | |
1002 | ||
1003 | static void | |
1004 | ctf_read_tsv (struct uploaded_tsv **uploaded_tsvs) | |
1005 | { | |
1006 | gdb_assert (ctf_iter != NULL); | |
1007 | ||
1008 | while (1) | |
1009 | { | |
1010 | struct bt_ctf_event *event; | |
1011 | const struct bt_definition *scope; | |
1012 | const struct bt_definition *def; | |
1013 | uint32_t event_id; | |
1014 | struct uploaded_tsv *utsv = NULL; | |
1015 | ||
1016 | event = bt_ctf_iter_read_event (ctf_iter); | |
1017 | scope = bt_ctf_get_top_level_scope (event, | |
1018 | BT_STREAM_EVENT_HEADER); | |
1019 | event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, | |
1020 | "id")); | |
1021 | if (event_id != CTF_EVENT_ID_TSV_DEF) | |
1022 | break; | |
1023 | ||
1024 | scope = bt_ctf_get_top_level_scope (event, | |
1025 | BT_EVENT_FIELDS); | |
1026 | ||
1027 | def = bt_ctf_get_field (event, scope, "number"); | |
1028 | utsv = get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def), | |
1029 | uploaded_tsvs); | |
1030 | ||
1031 | def = bt_ctf_get_field (event, scope, "builtin"); | |
1032 | utsv->builtin = (int32_t) bt_ctf_get_int64 (def); | |
1033 | def = bt_ctf_get_field (event, scope, "initial_value"); | |
1034 | utsv->initial_value = bt_ctf_get_int64 (def); | |
1035 | ||
1036 | def = bt_ctf_get_field (event, scope, "name"); | |
1037 | utsv->name = xstrdup (bt_ctf_get_string (def)); | |
1038 | ||
1039 | if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) | |
1040 | break; | |
1041 | } | |
1042 | ||
1043 | } | |
1044 | ||
1045 | /* Read the value of element whose index is NUM from CTF and write it | |
1046 | to the corresponding VAR->ARRAY. */ | |
1047 | ||
1048 | #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \ | |
1049 | do \ | |
1050 | { \ | |
1051 | uint32_t u32, i; \ | |
1052 | const struct bt_definition *def; \ | |
1053 | \ | |
1054 | u32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \ | |
1055 | (SCOPE), \ | |
1056 | #NUM)); \ | |
1057 | def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \ | |
1058 | for (i = 0; i < u32; i++) \ | |
1059 | { \ | |
1060 | const struct bt_definition *element \ | |
1061 | = bt_ctf_get_index ((EVENT), def, i); \ | |
1062 | \ | |
1063 | VEC_safe_push (char_ptr, (VAR)->ARRAY, \ | |
1064 | xstrdup (bt_ctf_get_string (element))); \ | |
1065 | } \ | |
1066 | } \ | |
1067 | while (0) | |
1068 | ||
1069 | /* Read a string from CTF and set VAR->FIELD. If the length of string | |
1070 | is zero, set VAR->FIELD to NULL. */ | |
1071 | ||
1072 | #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \ | |
1073 | do \ | |
1074 | { \ | |
1075 | const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \ | |
1076 | (SCOPE), \ | |
1077 | #FIELD)); \ | |
1078 | \ | |
1079 | if (strlen (p) > 0) \ | |
1080 | (VAR)->FIELD = xstrdup (p); \ | |
1081 | else \ | |
1082 | (VAR)->FIELD = NULL; \ | |
1083 | } \ | |
1084 | while (0) | |
1085 | ||
1086 | /* Read the events "tp_def" one by one, extract its contents and fill | |
1087 | in the list UPLOADED_TPS. */ | |
1088 | ||
1089 | static void | |
1090 | ctf_read_tp (struct uploaded_tp **uploaded_tps) | |
1091 | { | |
1092 | gdb_assert (ctf_iter != NULL); | |
1093 | ||
1094 | while (1) | |
1095 | { | |
1096 | struct bt_ctf_event *event; | |
1097 | const struct bt_definition *scope; | |
1098 | uint32_t u32; | |
1099 | int32_t int32; | |
1100 | uint64_t u64; | |
1101 | struct uploaded_tp *utp = NULL; | |
1102 | ||
1103 | event = bt_ctf_iter_read_event (ctf_iter); | |
1104 | scope = bt_ctf_get_top_level_scope (event, | |
1105 | BT_STREAM_EVENT_HEADER); | |
1106 | u32 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, | |
1107 | "id")); | |
1108 | if (u32 != CTF_EVENT_ID_TP_DEF) | |
1109 | break; | |
1110 | ||
1111 | scope = bt_ctf_get_top_level_scope (event, | |
1112 | BT_EVENT_FIELDS); | |
1113 | int32 = (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event, | |
1114 | scope, | |
1115 | "number")); | |
1116 | u64 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, | |
1117 | "addr")); | |
1118 | utp = get_uploaded_tp (int32, u64, uploaded_tps); | |
1119 | ||
1120 | SET_INT32_FIELD (event, scope, utp, enabled); | |
1121 | SET_INT32_FIELD (event, scope, utp, step); | |
1122 | SET_INT32_FIELD (event, scope, utp, pass); | |
1123 | SET_INT32_FIELD (event, scope, utp, hit_count); | |
1124 | SET_INT32_FIELD (event, scope, utp, type); | |
1125 | ||
1126 | /* Read 'cmd_strings'. */ | |
1127 | SET_ARRAY_FIELD (event, scope, utp, cmd_num, cmd_strings); | |
1128 | /* Read 'actions'. */ | |
1129 | SET_ARRAY_FIELD (event, scope, utp, action_num, actions); | |
1130 | /* Read 'step_actions'. */ | |
1131 | SET_ARRAY_FIELD (event, scope, utp, step_action_num, | |
1132 | step_actions); | |
1133 | ||
1134 | SET_STRING_FIELD(event, scope, utp, at_string); | |
1135 | SET_STRING_FIELD(event, scope, utp, cond_string); | |
1136 | ||
1137 | if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) | |
1138 | break; | |
1139 | } | |
1140 | } | |
1141 | ||
1142 | /* This is the implementation of target_ops method to_open. Open CTF | |
1143 | trace data, read trace status, trace state variables and tracepoint | |
1144 | definitions from the first packet. Set the start position at the | |
1145 | second packet which contains events on trace blocks. */ | |
1146 | ||
1147 | static void | |
1148 | ctf_open (char *dirname, int from_tty) | |
1149 | { | |
1150 | struct bt_ctf_event *event; | |
1151 | uint32_t event_id; | |
1152 | const struct bt_definition *scope; | |
1153 | struct uploaded_tsv *uploaded_tsvs = NULL; | |
1154 | struct uploaded_tp *uploaded_tps = NULL; | |
1155 | ||
1156 | if (!dirname) | |
1157 | error (_("No CTF directory specified.")); | |
1158 | ||
1159 | ctf_open_dir (dirname); | |
1160 | ||
1161 | target_preopen (from_tty); | |
1162 | ||
1163 | /* Skip the first packet which about the trace status. The first | |
1164 | event is "frame". */ | |
1165 | event = bt_ctf_iter_read_event (ctf_iter); | |
1166 | scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER); | |
1167 | event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id")); | |
1168 | if (event_id != CTF_EVENT_ID_FRAME) | |
1169 | error (_("Wrong event id of the first event")); | |
1170 | /* The second event is "status". */ | |
1171 | bt_iter_next (bt_ctf_get_iter (ctf_iter)); | |
1172 | event = bt_ctf_iter_read_event (ctf_iter); | |
1173 | scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER); | |
1174 | event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id")); | |
1175 | if (event_id != CTF_EVENT_ID_STATUS) | |
1176 | error (_("Wrong event id of the second event")); | |
1177 | ctf_read_status (event, current_trace_status ()); | |
1178 | ||
1179 | ctf_read_tsv (&uploaded_tsvs); | |
1180 | ||
1181 | ctf_read_tp (&uploaded_tps); | |
1182 | ||
1183 | event = bt_ctf_iter_read_event (ctf_iter); | |
1184 | /* EVENT can be NULL if we've already gone to the end of stream of | |
1185 | events. */ | |
1186 | if (event != NULL) | |
1187 | { | |
1188 | scope = bt_ctf_get_top_level_scope (event, | |
1189 | BT_STREAM_EVENT_HEADER); | |
1190 | event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, | |
1191 | scope, "id")); | |
1192 | if (event_id != CTF_EVENT_ID_FRAME) | |
1193 | error (_("Wrong event id of the first event of the second packet")); | |
1194 | } | |
1195 | ||
1196 | start_pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); | |
1197 | gdb_assert (start_pos->type == BT_SEEK_RESTORE); | |
1198 | ||
1199 | trace_dirname = xstrdup (dirname); | |
1200 | push_target (&ctf_ops); | |
1201 | ||
1202 | merge_uploaded_trace_state_variables (&uploaded_tsvs); | |
1203 | merge_uploaded_tracepoints (&uploaded_tps); | |
1204 | } | |
1205 | ||
1206 | /* This is the implementation of target_ops method to_close. Destroy | |
1207 | CTF iterator and context. */ | |
1208 | ||
1209 | static void | |
1210 | ctf_close (void) | |
1211 | { | |
1212 | ctf_destroy (); | |
1213 | xfree (trace_dirname); | |
1214 | trace_dirname = NULL; | |
aef525cb YQ |
1215 | |
1216 | trace_reset_local_state (); | |
393fd4c3 YQ |
1217 | } |
1218 | ||
1219 | /* This is the implementation of target_ops method to_files_info. | |
1220 | Print the directory name of CTF trace data. */ | |
1221 | ||
1222 | static void | |
1223 | ctf_files_info (struct target_ops *t) | |
1224 | { | |
1225 | printf_filtered ("\t`%s'\n", trace_dirname); | |
1226 | } | |
1227 | ||
1228 | /* This is the implementation of target_ops method to_fetch_registers. | |
1229 | Iterate over events whose name is "register" in current frame, | |
1230 | extract contents from events, and set REGCACHE with the contents. | |
1231 | If no matched events are found, mark registers unavailable. */ | |
1232 | ||
1233 | static void | |
1234 | ctf_fetch_registers (struct target_ops *ops, | |
1235 | struct regcache *regcache, int regno) | |
1236 | { | |
1237 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
1238 | int offset, regn, regsize, pc_regno; | |
8b4833e3 | 1239 | gdb_byte *regs = NULL; |
393fd4c3 YQ |
1240 | struct bt_ctf_event *event = NULL; |
1241 | struct bt_iter_pos *pos; | |
1242 | ||
1243 | /* An uninitialized reg size says we're not going to be | |
1244 | successful at getting register blocks. */ | |
1245 | if (trace_regblock_size == 0) | |
1246 | return; | |
1247 | ||
1248 | gdb_assert (ctf_iter != NULL); | |
1249 | /* Save the current position. */ | |
1250 | pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); | |
1251 | gdb_assert (pos->type == BT_SEEK_RESTORE); | |
1252 | ||
1253 | while (1) | |
1254 | { | |
1255 | const char *name; | |
1256 | struct bt_ctf_event *event1; | |
1257 | ||
1258 | event1 = bt_ctf_iter_read_event (ctf_iter); | |
1259 | ||
1260 | name = bt_ctf_event_name (event1); | |
1261 | ||
1262 | if (name == NULL || strcmp (name, "frame") == 0) | |
1263 | break; | |
1264 | else if (strcmp (name, "register") == 0) | |
1265 | { | |
1266 | event = event1; | |
1267 | break; | |
1268 | } | |
1269 | ||
1270 | if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) | |
1271 | break; | |
1272 | } | |
1273 | ||
1274 | /* Restore the position. */ | |
1275 | bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); | |
1276 | ||
1277 | if (event != NULL) | |
1278 | { | |
1279 | const struct bt_definition *scope | |
1280 | = bt_ctf_get_top_level_scope (event, | |
1281 | BT_EVENT_FIELDS); | |
1282 | const struct bt_definition *array | |
1283 | = bt_ctf_get_field (event, scope, "contents"); | |
1284 | ||
8b4833e3 | 1285 | regs = (gdb_byte *) bt_ctf_get_char_array (array); |
393fd4c3 YQ |
1286 | /* Assume the block is laid out in GDB register number order, |
1287 | each register with the size that it has in GDB. */ | |
1288 | offset = 0; | |
1289 | for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++) | |
1290 | { | |
1291 | regsize = register_size (gdbarch, regn); | |
1292 | /* Make sure we stay within block bounds. */ | |
1293 | if (offset + regsize >= trace_regblock_size) | |
1294 | break; | |
1295 | if (regcache_register_status (regcache, regn) == REG_UNKNOWN) | |
1296 | { | |
1297 | if (regno == regn) | |
1298 | { | |
1299 | regcache_raw_supply (regcache, regno, regs + offset); | |
1300 | break; | |
1301 | } | |
1302 | else if (regno == -1) | |
1303 | { | |
1304 | regcache_raw_supply (regcache, regn, regs + offset); | |
1305 | } | |
1306 | } | |
1307 | offset += regsize; | |
1308 | } | |
1309 | return; | |
1310 | } | |
1311 | ||
1312 | regs = alloca (trace_regblock_size); | |
1313 | ||
1314 | /* We get here if no register data has been found. Mark registers | |
1315 | as unavailable. */ | |
1316 | for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++) | |
1317 | regcache_raw_supply (regcache, regn, NULL); | |
1318 | ||
1319 | /* We can often usefully guess that the PC is going to be the same | |
1320 | as the address of the tracepoint. */ | |
1321 | pc_regno = gdbarch_pc_regnum (gdbarch); | |
1322 | if (pc_regno >= 0 && (regno == -1 || regno == pc_regno)) | |
1323 | { | |
1324 | struct tracepoint *tp = get_tracepoint (get_tracepoint_number ()); | |
1325 | ||
1326 | if (tp != NULL && tp->base.loc) | |
1327 | { | |
1328 | /* But don't try to guess if tracepoint is multi-location... */ | |
1329 | if (tp->base.loc->next != NULL) | |
1330 | { | |
1331 | warning (_("Tracepoint %d has multiple " | |
1332 | "locations, cannot infer $pc"), | |
1333 | tp->base.number); | |
1334 | return; | |
1335 | } | |
1336 | /* ... or does while-stepping. */ | |
1337 | if (tp->step_count > 0) | |
1338 | { | |
1339 | warning (_("Tracepoint %d does while-stepping, " | |
1340 | "cannot infer $pc"), | |
1341 | tp->base.number); | |
1342 | return; | |
1343 | } | |
1344 | ||
1345 | store_unsigned_integer (regs, register_size (gdbarch, pc_regno), | |
1346 | gdbarch_byte_order (gdbarch), | |
1347 | tp->base.loc->address); | |
1348 | regcache_raw_supply (regcache, pc_regno, regs); | |
1349 | } | |
1350 | } | |
1351 | } | |
1352 | ||
1353 | /* This is the implementation of target_ops method to_xfer_partial. | |
1354 | Iterate over events whose name is "memory" in | |
1355 | current frame, extract the address and length from events. If | |
1356 | OFFSET is within the range, read the contents from events to | |
1357 | READBUF. */ | |
1358 | ||
1359 | static LONGEST | |
1360 | ctf_xfer_partial (struct target_ops *ops, enum target_object object, | |
1361 | const char *annex, gdb_byte *readbuf, | |
1362 | const gdb_byte *writebuf, ULONGEST offset, | |
1363 | LONGEST len) | |
1364 | { | |
1365 | /* We're only doing regular memory for now. */ | |
1366 | if (object != TARGET_OBJECT_MEMORY) | |
1367 | return -1; | |
1368 | ||
1369 | if (readbuf == NULL) | |
1370 | error (_("ctf_xfer_partial: trace file is read-only")); | |
1371 | ||
1372 | if (get_traceframe_number () != -1) | |
1373 | { | |
1374 | struct bt_iter_pos *pos; | |
1375 | int i = 0; | |
1376 | ||
1377 | gdb_assert (ctf_iter != NULL); | |
1378 | /* Save the current position. */ | |
1379 | pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); | |
1380 | gdb_assert (pos->type == BT_SEEK_RESTORE); | |
1381 | ||
1382 | /* Iterate through the traceframe's blocks, looking for | |
1383 | memory. */ | |
1384 | while (1) | |
1385 | { | |
1386 | ULONGEST amt; | |
1387 | uint64_t maddr; | |
1388 | uint16_t mlen; | |
1389 | enum bfd_endian byte_order | |
1390 | = gdbarch_byte_order (target_gdbarch ()); | |
1391 | const struct bt_definition *scope; | |
1392 | const struct bt_definition *def; | |
1393 | struct bt_ctf_event *event | |
1394 | = bt_ctf_iter_read_event (ctf_iter); | |
1395 | const char *name = bt_ctf_event_name (event); | |
1396 | ||
1397 | if (strcmp (name, "frame") == 0) | |
1398 | break; | |
1399 | else if (strcmp (name, "memory") != 0) | |
1400 | { | |
1401 | if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) | |
1402 | break; | |
1403 | ||
1404 | continue; | |
1405 | } | |
1406 | ||
1407 | scope = bt_ctf_get_top_level_scope (event, | |
1408 | BT_EVENT_FIELDS); | |
1409 | ||
1410 | def = bt_ctf_get_field (event, scope, "address"); | |
1411 | maddr = bt_ctf_get_uint64 (def); | |
1412 | def = bt_ctf_get_field (event, scope, "length"); | |
1413 | mlen = (uint16_t) bt_ctf_get_uint64 (def); | |
1414 | ||
1415 | /* If the block includes the first part of the desired | |
1416 | range, return as much it has; GDB will re-request the | |
1417 | remainder, which might be in a different block of this | |
1418 | trace frame. */ | |
1419 | if (maddr <= offset && offset < (maddr + mlen)) | |
1420 | { | |
1421 | const struct bt_definition *array | |
1422 | = bt_ctf_get_field (event, scope, "contents"); | |
1423 | const struct bt_declaration *decl | |
1424 | = bt_ctf_get_decl_from_def (array); | |
1425 | gdb_byte *contents; | |
1426 | int k; | |
1427 | ||
1428 | contents = xmalloc (mlen); | |
1429 | ||
1430 | for (k = 0; k < mlen; k++) | |
1431 | { | |
1432 | const struct bt_definition *element | |
1433 | = bt_ctf_get_index (event, array, k); | |
1434 | ||
1435 | contents[k] = (gdb_byte) bt_ctf_get_uint64 (element); | |
1436 | } | |
1437 | ||
1438 | amt = (maddr + mlen) - offset; | |
1439 | if (amt > len) | |
1440 | amt = len; | |
1441 | ||
1442 | memcpy (readbuf, &contents[offset - maddr], amt); | |
1443 | ||
1444 | xfree (contents); | |
1445 | ||
1446 | /* Restore the position. */ | |
1447 | bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); | |
1448 | ||
1449 | return amt; | |
1450 | } | |
1451 | ||
1452 | if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) | |
1453 | break; | |
1454 | } | |
1455 | ||
1456 | /* Restore the position. */ | |
1457 | bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); | |
1458 | } | |
1459 | ||
1460 | /* It's unduly pedantic to refuse to look at the executable for | |
1461 | read-only pieces; so do the equivalent of readonly regions aka | |
1462 | QTro packet. */ | |
1463 | if (exec_bfd != NULL) | |
1464 | { | |
1465 | asection *s; | |
1466 | bfd_size_type size; | |
1467 | bfd_vma vma; | |
1468 | ||
1469 | for (s = exec_bfd->sections; s; s = s->next) | |
1470 | { | |
1471 | if ((s->flags & SEC_LOAD) == 0 | |
1472 | || (s->flags & SEC_READONLY) == 0) | |
1473 | continue; | |
1474 | ||
1475 | vma = s->vma; | |
1476 | size = bfd_get_section_size (s); | |
1477 | if (vma <= offset && offset < (vma + size)) | |
1478 | { | |
1479 | ULONGEST amt; | |
1480 | ||
1481 | amt = (vma + size) - offset; | |
1482 | if (amt > len) | |
1483 | amt = len; | |
1484 | ||
1485 | amt = bfd_get_section_contents (exec_bfd, s, | |
1486 | readbuf, offset - vma, amt); | |
1487 | return amt; | |
1488 | } | |
1489 | } | |
1490 | } | |
1491 | ||
1492 | /* Indicate failure to find the requested memory block. */ | |
1493 | return -1; | |
1494 | } | |
1495 | ||
1496 | /* This is the implementation of target_ops method | |
1497 | to_get_trace_state_variable_value. | |
1498 | Iterate over events whose name is "tsv" in current frame. When the | |
1499 | trace variable is found, set the value of it to *VAL and return | |
1500 | true, otherwise return false. */ | |
1501 | ||
1502 | static int | |
1503 | ctf_get_trace_state_variable_value (int tsvnum, LONGEST *val) | |
1504 | { | |
1505 | struct bt_iter_pos *pos; | |
1506 | int found = 0; | |
1507 | ||
1508 | gdb_assert (ctf_iter != NULL); | |
1509 | /* Save the current position. */ | |
1510 | pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); | |
1511 | gdb_assert (pos->type == BT_SEEK_RESTORE); | |
1512 | ||
1513 | /* Iterate through the traceframe's blocks, looking for 'V' | |
1514 | block. */ | |
1515 | while (1) | |
1516 | { | |
1517 | struct bt_ctf_event *event | |
1518 | = bt_ctf_iter_read_event (ctf_iter); | |
1519 | const char *name = bt_ctf_event_name (event); | |
1520 | ||
1521 | if (name == NULL || strcmp (name, "frame") == 0) | |
1522 | break; | |
1523 | else if (strcmp (name, "tsv") == 0) | |
1524 | { | |
1525 | const struct bt_definition *scope; | |
1526 | const struct bt_definition *def; | |
1527 | ||
1528 | scope = bt_ctf_get_top_level_scope (event, | |
1529 | BT_EVENT_FIELDS); | |
1530 | ||
1531 | def = bt_ctf_get_field (event, scope, "num"); | |
1532 | if (tsvnum == (int32_t) bt_ctf_get_uint64 (def)) | |
1533 | { | |
1534 | def = bt_ctf_get_field (event, scope, "val"); | |
1535 | *val = bt_ctf_get_uint64 (def); | |
1536 | ||
1537 | found = 1; | |
1538 | } | |
1539 | } | |
1540 | ||
1541 | if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) | |
1542 | break; | |
1543 | } | |
1544 | ||
1545 | /* Restore the position. */ | |
1546 | bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); | |
1547 | ||
1548 | return found; | |
1549 | } | |
1550 | ||
1551 | /* Return the tracepoint number in "frame" event. */ | |
1552 | ||
1553 | static int | |
1554 | ctf_get_tpnum_from_frame_event (struct bt_ctf_event *event) | |
1555 | { | |
1556 | /* The packet context of events has a field "tpnum". */ | |
1557 | const struct bt_definition *scope | |
1558 | = bt_ctf_get_top_level_scope (event, BT_STREAM_PACKET_CONTEXT); | |
1559 | uint64_t tpnum | |
1560 | = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "tpnum")); | |
1561 | ||
1562 | return (int) tpnum; | |
1563 | } | |
1564 | ||
1565 | /* Return the address at which the current frame was collected. */ | |
1566 | ||
1567 | static CORE_ADDR | |
1568 | ctf_get_traceframe_address (void) | |
1569 | { | |
1570 | struct bt_ctf_event *event = NULL; | |
1571 | struct bt_iter_pos *pos; | |
1572 | CORE_ADDR addr = 0; | |
1573 | ||
1574 | gdb_assert (ctf_iter != NULL); | |
1575 | pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); | |
1576 | gdb_assert (pos->type == BT_SEEK_RESTORE); | |
1577 | ||
1578 | while (1) | |
1579 | { | |
1580 | const char *name; | |
1581 | struct bt_ctf_event *event1; | |
1582 | ||
1583 | event1 = bt_ctf_iter_read_event (ctf_iter); | |
1584 | ||
1585 | name = bt_ctf_event_name (event1); | |
1586 | ||
1587 | if (name == NULL) | |
1588 | break; | |
1589 | else if (strcmp (name, "frame") == 0) | |
1590 | { | |
1591 | event = event1; | |
1592 | break; | |
1593 | } | |
1594 | ||
1595 | if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) | |
1596 | break; | |
1597 | } | |
1598 | ||
1599 | if (event != NULL) | |
1600 | { | |
1601 | int tpnum = ctf_get_tpnum_from_frame_event (event); | |
1602 | struct tracepoint *tp | |
1603 | = get_tracepoint_by_number_on_target (tpnum); | |
1604 | ||
1605 | if (tp && tp->base.loc) | |
1606 | addr = tp->base.loc->address; | |
1607 | } | |
1608 | ||
1609 | /* Restore the position. */ | |
1610 | bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); | |
1611 | ||
1612 | return addr; | |
1613 | } | |
1614 | ||
1615 | /* This is the implementation of target_ops method to_trace_find. | |
1616 | Iterate the events whose name is "frame", extract the tracepoint | |
1617 | number in it. Return traceframe number when matched. */ | |
1618 | ||
1619 | static int | |
1620 | ctf_trace_find (enum trace_find_type type, int num, | |
1621 | CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) | |
1622 | { | |
1623 | int ret = -1; | |
1624 | int tfnum = 0; | |
1625 | int found = 0; | |
1626 | struct bt_iter_pos pos; | |
1627 | ||
1628 | if (num == -1) | |
1629 | { | |
1630 | if (tpp != NULL) | |
1631 | *tpp = -1; | |
1632 | return -1; | |
1633 | } | |
1634 | ||
1635 | gdb_assert (ctf_iter != NULL); | |
1636 | /* Set iterator back to the start. */ | |
1637 | bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), start_pos); | |
1638 | ||
1639 | while (1) | |
1640 | { | |
1641 | int id; | |
1642 | struct bt_ctf_event *event; | |
1643 | const char *name; | |
1644 | ||
1645 | event = bt_ctf_iter_read_event (ctf_iter); | |
1646 | ||
1647 | name = bt_ctf_event_name (event); | |
1648 | ||
1649 | if (event == NULL || name == NULL) | |
1650 | break; | |
1651 | ||
1652 | if (strcmp (name, "frame") == 0) | |
1653 | { | |
1654 | CORE_ADDR tfaddr; | |
1655 | ||
1656 | if (type == tfind_number) | |
1657 | { | |
1658 | /* Looking for a specific trace frame. */ | |
1659 | if (tfnum == num) | |
1660 | found = 1; | |
1661 | } | |
1662 | else | |
1663 | { | |
1664 | /* Start from the _next_ trace frame. */ | |
1665 | if (tfnum > get_traceframe_number ()) | |
1666 | { | |
1667 | switch (type) | |
1668 | { | |
1669 | case tfind_tp: | |
1670 | { | |
1671 | struct tracepoint *tp = get_tracepoint (num); | |
1672 | ||
1673 | if (tp != NULL | |
1674 | && (tp->number_on_target | |
1675 | == ctf_get_tpnum_from_frame_event (event))) | |
1676 | found = 1; | |
1677 | break; | |
1678 | } | |
1679 | case tfind_pc: | |
1680 | tfaddr = ctf_get_traceframe_address (); | |
1681 | if (tfaddr == addr1) | |
1682 | found = 1; | |
1683 | break; | |
1684 | case tfind_range: | |
1685 | tfaddr = ctf_get_traceframe_address (); | |
1686 | if (addr1 <= tfaddr && tfaddr <= addr2) | |
1687 | found = 1; | |
1688 | break; | |
1689 | case tfind_outside: | |
1690 | tfaddr = ctf_get_traceframe_address (); | |
1691 | if (!(addr1 <= tfaddr && tfaddr <= addr2)) | |
1692 | found = 1; | |
1693 | break; | |
1694 | default: | |
1695 | internal_error (__FILE__, __LINE__, _("unknown tfind type")); | |
1696 | } | |
1697 | } | |
1698 | } | |
1699 | if (found) | |
1700 | { | |
1701 | if (tpp != NULL) | |
1702 | *tpp = ctf_get_tpnum_from_frame_event (event); | |
1703 | ||
1704 | /* Skip the event "frame". */ | |
1705 | bt_iter_next (bt_ctf_get_iter (ctf_iter)); | |
1706 | ||
1707 | return tfnum; | |
1708 | } | |
1709 | tfnum++; | |
1710 | } | |
1711 | ||
1712 | if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) | |
1713 | break; | |
1714 | } | |
1715 | ||
1716 | return -1; | |
1717 | } | |
1718 | ||
1719 | /* This is the implementation of target_ops method to_has_stack. | |
1720 | The target has a stack when GDB has already selected one trace | |
1721 | frame. */ | |
1722 | ||
1723 | static int | |
1724 | ctf_has_stack (struct target_ops *ops) | |
1725 | { | |
1726 | return get_traceframe_number () != -1; | |
1727 | } | |
1728 | ||
1729 | /* This is the implementation of target_ops method to_has_registers. | |
1730 | The target has registers when GDB has already selected one trace | |
1731 | frame. */ | |
1732 | ||
1733 | static int | |
1734 | ctf_has_registers (struct target_ops *ops) | |
1735 | { | |
1736 | return get_traceframe_number () != -1; | |
1737 | } | |
1738 | ||
1739 | /* This is the implementation of target_ops method to_traceframe_info. | |
1740 | Iterate the events whose name is "memory", in current | |
1741 | frame, extract memory range information, and return them in | |
1742 | traceframe_info. */ | |
1743 | ||
1744 | static struct traceframe_info * | |
1745 | ctf_traceframe_info (void) | |
1746 | { | |
1747 | struct traceframe_info *info = XCNEW (struct traceframe_info); | |
1748 | const char *name; | |
1749 | struct bt_iter_pos *pos; | |
1750 | ||
1751 | gdb_assert (ctf_iter != NULL); | |
1752 | /* Save the current position. */ | |
1753 | pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter)); | |
1754 | gdb_assert (pos->type == BT_SEEK_RESTORE); | |
1755 | ||
1756 | do | |
1757 | { | |
1758 | struct bt_ctf_event *event | |
1759 | = bt_ctf_iter_read_event (ctf_iter); | |
1760 | ||
1761 | name = bt_ctf_event_name (event); | |
1762 | ||
1763 | if (name == NULL || strcmp (name, "register") == 0 | |
1764 | || strcmp (name, "frame") == 0) | |
1765 | ; | |
1766 | else if (strcmp (name, "memory") == 0) | |
1767 | { | |
1768 | const struct bt_definition *scope | |
1769 | = bt_ctf_get_top_level_scope (event, | |
1770 | BT_EVENT_FIELDS); | |
1771 | const struct bt_definition *def; | |
1772 | struct mem_range *r; | |
1773 | ||
1774 | r = VEC_safe_push (mem_range_s, info->memory, NULL); | |
1775 | def = bt_ctf_get_field (event, scope, "address"); | |
1776 | r->start = bt_ctf_get_uint64 (def); | |
1777 | ||
1778 | def = bt_ctf_get_field (event, scope, "length"); | |
1779 | r->length = (uint16_t) bt_ctf_get_uint64 (def); | |
1780 | } | |
28a93511 YQ |
1781 | else if (strcmp (name, "tsv") == 0) |
1782 | { | |
1783 | int vnum; | |
1784 | const struct bt_definition *scope | |
1785 | = bt_ctf_get_top_level_scope (event, | |
1786 | BT_EVENT_FIELDS); | |
1787 | const struct bt_definition *def; | |
1788 | ||
1789 | def = bt_ctf_get_field (event, scope, "num"); | |
1790 | vnum = (int) bt_ctf_get_int64 (def); | |
1791 | VEC_safe_push (int, info->tvars, vnum); | |
1792 | } | |
393fd4c3 YQ |
1793 | else |
1794 | { | |
1795 | warning (_("Unhandled trace block type (%s) " | |
1796 | "while building trace frame info."), | |
1797 | name); | |
1798 | } | |
1799 | ||
1800 | if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0) | |
1801 | break; | |
1802 | } | |
1803 | while (name != NULL && strcmp (name, "frame") != 0); | |
1804 | ||
1805 | /* Restore the position. */ | |
1806 | bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos); | |
1807 | ||
1808 | return info; | |
1809 | } | |
1810 | ||
1811 | /* This is the implementation of target_ops method to_get_trace_status. | |
1812 | The trace status for a file is that tracing can never be run. */ | |
1813 | ||
1814 | static int | |
1815 | ctf_get_trace_status (struct trace_status *ts) | |
1816 | { | |
1817 | /* Other bits of trace status were collected as part of opening the | |
1818 | trace files, so nothing to do here. */ | |
1819 | ||
1820 | return -1; | |
1821 | } | |
1822 | ||
1823 | static void | |
1824 | init_ctf_ops (void) | |
1825 | { | |
1826 | memset (&ctf_ops, 0, sizeof (ctf_ops)); | |
1827 | ||
1828 | ctf_ops.to_shortname = "ctf"; | |
1829 | ctf_ops.to_longname = "CTF file"; | |
1830 | ctf_ops.to_doc = "Use a CTF directory as a target.\n\ | |
1831 | Specify the filename of the CTF directory."; | |
1832 | ctf_ops.to_open = ctf_open; | |
1833 | ctf_ops.to_close = ctf_close; | |
1834 | ctf_ops.to_fetch_registers = ctf_fetch_registers; | |
1835 | ctf_ops.to_xfer_partial = ctf_xfer_partial; | |
1836 | ctf_ops.to_files_info = ctf_files_info; | |
1837 | ctf_ops.to_get_trace_status = ctf_get_trace_status; | |
1838 | ctf_ops.to_trace_find = ctf_trace_find; | |
1839 | ctf_ops.to_get_trace_state_variable_value | |
1840 | = ctf_get_trace_state_variable_value; | |
1841 | ctf_ops.to_stratum = process_stratum; | |
1842 | ctf_ops.to_has_stack = ctf_has_stack; | |
1843 | ctf_ops.to_has_registers = ctf_has_registers; | |
1844 | ctf_ops.to_traceframe_info = ctf_traceframe_info; | |
1845 | ctf_ops.to_magic = OPS_MAGIC; | |
1846 | } | |
1847 | ||
1848 | #endif | |
1849 | ||
1850 | /* -Wmissing-prototypes */ | |
1851 | ||
1852 | extern initialize_file_ftype _initialize_ctf; | |
1853 | ||
1854 | /* module initialization */ | |
1855 | ||
1856 | void | |
1857 | _initialize_ctf (void) | |
1858 | { | |
1859 | #if HAVE_LIBBABELTRACE | |
1860 | init_ctf_ops (); | |
1861 | ||
da9160e4 | 1862 | add_target_with_completer (&ctf_ops, filename_completer); |
393fd4c3 YQ |
1863 | #endif |
1864 | } |