tracectf.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /* CTF format support.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. Contributed by Hui Zhu <hui_zhu@mentor.com>
  4. Contributed by Yao Qi <yao@codesourcery.com>
  5. This file is part of GDB.
  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. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "defs.h"
  17. #include "tracectf.h"
  18. #include "tracepoint.h"
  19. #include "regcache.h"
  20. #include <sys/stat.h>
  21. #include "exec.h"
  22. #include "completer.h"
  23. #include "inferior.h"
  24. #include "gdbthread.h"
  25. #include "tracefile.h"
  26. #include <ctype.h>
  27. #include <algorithm>
  28. #include "gdbsupport/filestuff.h"
  29. #include "gdbarch.h"
  30. /* The CTF target. */
  31. static const target_info ctf_target_info = {
  32. "ctf",
  33. N_("CTF file"),
  34. N_("(Use a CTF directory as a target.\n\
  35. Specify the filename of the CTF directory.")
  36. };
  37. class ctf_target final : public tracefile_target
  38. {
  39. public:
  40. const target_info &info () const override
  41. { return ctf_target_info; }
  42. void close () override;
  43. void fetch_registers (struct regcache *, int) override;
  44. enum target_xfer_status xfer_partial (enum target_object object,
  45. const char *annex,
  46. gdb_byte *readbuf,
  47. const gdb_byte *writebuf,
  48. ULONGEST offset, ULONGEST len,
  49. ULONGEST *xfered_len) override;
  50. void files_info () override;
  51. int trace_find (enum trace_find_type type, int num,
  52. CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) override;
  53. bool get_trace_state_variable_value (int tsv, LONGEST *val) override;
  54. traceframe_info_up traceframe_info () override;
  55. };
  56. /* GDB saves trace buffers and other information (such as trace
  57. status) got from the remote target into Common Trace Format (CTF).
  58. The following types of information are expected to save in CTF:
  59. 1. The length (in bytes) of register cache. Event "register" will
  60. be defined in metadata, which includes the length.
  61. 2. Trace status. Event "status" is defined in metadata, which
  62. includes all aspects of trace status.
  63. 3. Uploaded trace variables. Event "tsv_def" is defined in
  64. metadata, which is about all aspects of a uploaded trace variable.
  65. Uploaded tracepoints. Event "tp_def" is defined in meta, which
  66. is about all aspects of an uploaded tracepoint. Note that the
  67. "sequence" (a CTF type, which is a dynamically-sized array.) is
  68. used for "actions" "step_actions" and "cmd_strings".
  69. 4. Trace frames. Each trace frame is composed by several blocks
  70. of different types ('R', 'M', 'V'). One trace frame is saved in
  71. one CTF packet and the blocks of this frame are saved as events.
  72. 4.1: The trace frame related information (such as the number of
  73. tracepoint associated with this frame) is saved in the packet
  74. context.
  75. 4.2: The block 'M', 'R' and 'V' are saved in event "memory",
  76. "register" and "tsv" respectively.
  77. 4.3: When iterating over events, babeltrace can't tell iterator
  78. goes to a new packet, so we need a marker or anchor to tell GDB
  79. that iterator goes into a new packet or frame. We define event
  80. "frame". */
  81. #define CTF_MAGIC 0xC1FC1FC1
  82. #define CTF_SAVE_MAJOR 1
  83. #define CTF_SAVE_MINOR 8
  84. #define CTF_METADATA_NAME "metadata"
  85. #define CTF_DATASTREAM_NAME "datastream"
  86. /* Reserved event id. */
  87. #define CTF_EVENT_ID_REGISTER 0
  88. #define CTF_EVENT_ID_TSV 1
  89. #define CTF_EVENT_ID_MEMORY 2
  90. #define CTF_EVENT_ID_FRAME 3
  91. #define CTF_EVENT_ID_STATUS 4
  92. #define CTF_EVENT_ID_TSV_DEF 5
  93. #define CTF_EVENT_ID_TP_DEF 6
  94. #define CTF_PID (2)
  95. /* The state kept while writing the CTF datastream file. */
  96. struct trace_write_handler
  97. {
  98. /* File descriptor of metadata. */
  99. FILE *metadata_fd;
  100. /* File descriptor of traceframes. */
  101. FILE *datastream_fd;
  102. /* This is the content size of the current packet. */
  103. size_t content_size;
  104. /* This is the start offset of current packet. */
  105. long packet_start;
  106. };
  107. /* Write metadata in FORMAT. */
  108. static void
  109. ctf_save_write_metadata (struct trace_write_handler *handler,
  110. const char *format, ...)
  111. ATTRIBUTE_PRINTF (2, 3);
  112. static void
  113. ctf_save_write_metadata (struct trace_write_handler *handler,
  114. const char *format, ...)
  115. {
  116. va_list args;
  117. va_start (args, format);
  118. if (vfprintf (handler->metadata_fd, format, args) < 0)
  119. error (_("Unable to write metadata file (%s)"),
  120. safe_strerror (errno));
  121. va_end (args);
  122. }
  123. /* Write BUF of length SIZE to datastream file represented by
  124. HANDLER. */
  125. static int
  126. ctf_save_write (struct trace_write_handler *handler,
  127. const gdb_byte *buf, size_t size)
  128. {
  129. if (fwrite (buf, size, 1, handler->datastream_fd) != 1)
  130. error (_("Unable to write file for saving trace data (%s)"),
  131. safe_strerror (errno));
  132. handler->content_size += size;
  133. return 0;
  134. }
  135. /* Write a unsigned 32-bit integer to datastream file represented by
  136. HANDLER. */
  137. #define ctf_save_write_uint32(HANDLER, U32) \
  138. ctf_save_write (HANDLER, (gdb_byte *) &U32, 4)
  139. /* Write a signed 32-bit integer to datastream file represented by
  140. HANDLER. */
  141. #define ctf_save_write_int32(HANDLER, INT32) \
  142. ctf_save_write ((HANDLER), (gdb_byte *) &(INT32), 4)
  143. /* Set datastream file position. Update HANDLER->content_size
  144. if WHENCE is SEEK_CUR. */
  145. static int
  146. ctf_save_fseek (struct trace_write_handler *handler, long offset,
  147. int whence)
  148. {
  149. gdb_assert (whence != SEEK_END);
  150. gdb_assert (whence != SEEK_SET
  151. || offset <= handler->content_size + handler->packet_start);
  152. if (fseek (handler->datastream_fd, offset, whence))
  153. error (_("Unable to seek file for saving trace data (%s)"),
  154. safe_strerror (errno));
  155. if (whence == SEEK_CUR)
  156. handler->content_size += offset;
  157. return 0;
  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. static int
  162. ctf_save_align_write (struct trace_write_handler *handler,
  163. const gdb_byte *buf,
  164. size_t size, size_t align_size)
  165. {
  166. long offset
  167. = (align_up (handler->content_size, align_size)
  168. - handler->content_size);
  169. if (ctf_save_fseek (handler, offset, SEEK_CUR))
  170. return -1;
  171. if (ctf_save_write (handler, buf, size))
  172. return -1;
  173. return 0;
  174. }
  175. /* Write events to next new packet. */
  176. static void
  177. ctf_save_next_packet (struct trace_write_handler *handler)
  178. {
  179. handler->packet_start += (handler->content_size + 4);
  180. ctf_save_fseek (handler, handler->packet_start, SEEK_SET);
  181. handler->content_size = 0;
  182. }
  183. /* Write the CTF metadata header. */
  184. static void
  185. ctf_save_metadata_header (struct trace_write_handler *handler)
  186. {
  187. ctf_save_write_metadata (handler, "/* CTF %d.%d */\n",
  188. CTF_SAVE_MAJOR, CTF_SAVE_MINOR);
  189. ctf_save_write_metadata (handler,
  190. "typealias integer { size = 8; align = 8; "
  191. "signed = false; encoding = ascii;}"
  192. " := ascii;\n");
  193. ctf_save_write_metadata (handler,
  194. "typealias integer { size = 8; align = 8; "
  195. "signed = false; }"
  196. " := uint8_t;\n");
  197. ctf_save_write_metadata (handler,
  198. "typealias integer { size = 16; align = 16;"
  199. "signed = false; } := uint16_t;\n");
  200. ctf_save_write_metadata (handler,
  201. "typealias integer { size = 32; align = 32;"
  202. "signed = false; } := uint32_t;\n");
  203. ctf_save_write_metadata (handler,
  204. "typealias integer { size = 64; align = 64;"
  205. "signed = false; base = hex;}"
  206. " := uint64_t;\n");
  207. ctf_save_write_metadata (handler,
  208. "typealias integer { size = 32; align = 32;"
  209. "signed = true; } := int32_t;\n");
  210. ctf_save_write_metadata (handler,
  211. "typealias integer { size = 64; align = 64;"
  212. "signed = true; } := int64_t;\n");
  213. ctf_save_write_metadata (handler,
  214. "typealias string { encoding = ascii;"
  215. " } := chars;\n");
  216. ctf_save_write_metadata (handler, "\n");
  217. /* Get the byte order of the host and write CTF data in this byte
  218. order. */
  219. #if WORDS_BIGENDIAN
  220. #define HOST_ENDIANNESS "be"
  221. #else
  222. #define HOST_ENDIANNESS "le"
  223. #endif
  224. ctf_save_write_metadata (handler,
  225. "\ntrace {\n"
  226. " major = %u;\n"
  227. " minor = %u;\n"
  228. " byte_order = %s;\n"
  229. " packet.header := struct {\n"
  230. " uint32_t magic;\n"
  231. " };\n"
  232. "};\n"
  233. "\n"
  234. "stream {\n"
  235. " packet.context := struct {\n"
  236. " uint32_t content_size;\n"
  237. " uint32_t packet_size;\n"
  238. " uint16_t tpnum;\n"
  239. " };\n"
  240. " event.header := struct {\n"
  241. " uint32_t id;\n"
  242. " };\n"
  243. "};\n",
  244. CTF_SAVE_MAJOR, CTF_SAVE_MINOR,
  245. HOST_ENDIANNESS);
  246. ctf_save_write_metadata (handler, "\n");
  247. }
  248. /* CTF trace writer. */
  249. struct ctf_trace_file_writer
  250. {
  251. struct trace_file_writer base;
  252. /* States related to writing CTF trace file. */
  253. struct trace_write_handler tcs;
  254. };
  255. /* This is the implementation of trace_file_write_ops method
  256. dtor. */
  257. static void
  258. ctf_dtor (struct trace_file_writer *self)
  259. {
  260. struct ctf_trace_file_writer *writer
  261. = (struct ctf_trace_file_writer *) self;
  262. if (writer->tcs.metadata_fd != NULL)
  263. fclose (writer->tcs.metadata_fd);
  264. if (writer->tcs.datastream_fd != NULL)
  265. fclose (writer->tcs.datastream_fd);
  266. }
  267. /* This is the implementation of trace_file_write_ops method
  268. target_save. */
  269. static int
  270. ctf_target_save (struct trace_file_writer *self,
  271. const char *dirname)
  272. {
  273. /* Don't support save trace file to CTF format in the target. */
  274. return 0;
  275. }
  276. /* This is the implementation of trace_file_write_ops method
  277. start. It creates the directory DIRNAME, metadata and datastream
  278. in the directory. */
  279. static void
  280. ctf_start (struct trace_file_writer *self, const char *dirname)
  281. {
  282. struct ctf_trace_file_writer *writer
  283. = (struct ctf_trace_file_writer *) self;
  284. mode_t hmode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH;
  285. /* Create DIRNAME. */
  286. if (mkdir (dirname, hmode) && errno != EEXIST)
  287. error (_("Unable to open directory '%s' for saving trace data (%s)"),
  288. dirname, safe_strerror (errno));
  289. memset (&writer->tcs, '\0', sizeof (writer->tcs));
  290. std::string file_name = string_printf ("%s/%s", dirname, CTF_METADATA_NAME);
  291. writer->tcs.metadata_fd
  292. = gdb_fopen_cloexec (file_name.c_str (), "w").release ();
  293. if (writer->tcs.metadata_fd == NULL)
  294. error (_("Unable to open file '%s' for saving trace data (%s)"),
  295. file_name.c_str (), safe_strerror (errno));
  296. ctf_save_metadata_header (&writer->tcs);
  297. file_name = string_printf ("%s/%s", dirname, CTF_DATASTREAM_NAME);
  298. writer->tcs.datastream_fd
  299. = gdb_fopen_cloexec (file_name.c_str (), "w").release ();
  300. if (writer->tcs.datastream_fd == NULL)
  301. error (_("Unable to open file '%s' for saving trace data (%s)"),
  302. file_name.c_str (), safe_strerror (errno));
  303. }
  304. /* This is the implementation of trace_file_write_ops method
  305. write_header. Write the types of events on trace variable and
  306. frame. */
  307. static void
  308. ctf_write_header (struct trace_file_writer *self)
  309. {
  310. struct ctf_trace_file_writer *writer
  311. = (struct ctf_trace_file_writer *) self;
  312. ctf_save_write_metadata (&writer->tcs, "\n");
  313. ctf_save_write_metadata (&writer->tcs,
  314. "event {\n\tname = \"memory\";\n\tid = %u;\n"
  315. "\tfields := struct { \n"
  316. "\t\tuint64_t address;\n"
  317. "\t\tuint16_t length;\n"
  318. "\t\tuint8_t contents[length];\n"
  319. "\t};\n"
  320. "};\n", CTF_EVENT_ID_MEMORY);
  321. ctf_save_write_metadata (&writer->tcs, "\n");
  322. ctf_save_write_metadata (&writer->tcs,
  323. "event {\n\tname = \"tsv\";\n\tid = %u;\n"
  324. "\tfields := struct { \n"
  325. "\t\tuint64_t val;\n"
  326. "\t\tuint32_t num;\n"
  327. "\t};\n"
  328. "};\n", CTF_EVENT_ID_TSV);
  329. ctf_save_write_metadata (&writer->tcs, "\n");
  330. ctf_save_write_metadata (&writer->tcs,
  331. "event {\n\tname = \"frame\";\n\tid = %u;\n"
  332. "\tfields := struct { \n"
  333. "\t};\n"
  334. "};\n", CTF_EVENT_ID_FRAME);
  335. ctf_save_write_metadata (&writer->tcs, "\n");
  336. ctf_save_write_metadata (&writer->tcs,
  337. "event {\n\tname = \"tsv_def\";\n"
  338. "\tid = %u;\n\tfields := struct { \n"
  339. "\t\tint64_t initial_value;\n"
  340. "\t\tint32_t number;\n"
  341. "\t\tint32_t builtin;\n"
  342. "\t\tchars name;\n"
  343. "\t};\n"
  344. "};\n", CTF_EVENT_ID_TSV_DEF);
  345. ctf_save_write_metadata (&writer->tcs, "\n");
  346. ctf_save_write_metadata (&writer->tcs,
  347. "event {\n\tname = \"tp_def\";\n"
  348. "\tid = %u;\n\tfields := struct { \n"
  349. "\t\tuint64_t addr;\n"
  350. "\t\tuint64_t traceframe_usage;\n"
  351. "\t\tint32_t number;\n"
  352. "\t\tint32_t enabled;\n"
  353. "\t\tint32_t step;\n"
  354. "\t\tint32_t pass;\n"
  355. "\t\tint32_t hit_count;\n"
  356. "\t\tint32_t type;\n"
  357. "\t\tchars cond;\n"
  358. "\t\tuint32_t action_num;\n"
  359. "\t\tchars actions[action_num];\n"
  360. "\t\tuint32_t step_action_num;\n"
  361. "\t\tchars step_actions[step_action_num];\n"
  362. "\t\tchars at_string;\n"
  363. "\t\tchars cond_string;\n"
  364. "\t\tuint32_t cmd_num;\n"
  365. "\t\tchars cmd_strings[cmd_num];\n"
  366. "\t};\n"
  367. "};\n", CTF_EVENT_ID_TP_DEF);
  368. gdb_assert (writer->tcs.content_size == 0);
  369. gdb_assert (writer->tcs.packet_start == 0);
  370. /* Create a new packet to contain this event. */
  371. self->ops->frame_ops->start (self, 0);
  372. }
  373. /* This is the implementation of trace_file_write_ops method
  374. write_regblock_type. Write the type of register event in
  375. metadata. */
  376. static void
  377. ctf_write_regblock_type (struct trace_file_writer *self, int size)
  378. {
  379. struct ctf_trace_file_writer *writer
  380. = (struct ctf_trace_file_writer *) self;
  381. ctf_save_write_metadata (&writer->tcs, "\n");
  382. ctf_save_write_metadata (&writer->tcs,
  383. "event {\n\tname = \"register\";\n\tid = %u;\n"
  384. "\tfields := struct { \n"
  385. "\t\tascii contents[%d];\n"
  386. "\t};\n"
  387. "};\n",
  388. CTF_EVENT_ID_REGISTER, size);
  389. }
  390. /* This is the implementation of trace_file_write_ops method
  391. write_status. */
  392. static void
  393. ctf_write_status (struct trace_file_writer *self,
  394. struct trace_status *ts)
  395. {
  396. struct ctf_trace_file_writer *writer
  397. = (struct ctf_trace_file_writer *) self;
  398. uint32_t id;
  399. ctf_save_write_metadata (&writer->tcs, "\n");
  400. ctf_save_write_metadata (&writer->tcs,
  401. "event {\n\tname = \"status\";\n\tid = %u;\n"
  402. "\tfields := struct { \n"
  403. "\t\tint32_t stop_reason;\n"
  404. "\t\tint32_t stopping_tracepoint;\n"
  405. "\t\tint32_t traceframe_count;\n"
  406. "\t\tint32_t traceframes_created;\n"
  407. "\t\tint32_t buffer_free;\n"
  408. "\t\tint32_t buffer_size;\n"
  409. "\t\tint32_t disconnected_tracing;\n"
  410. "\t\tint32_t circular_buffer;\n"
  411. "\t};\n"
  412. "};\n",
  413. CTF_EVENT_ID_STATUS);
  414. id = CTF_EVENT_ID_STATUS;
  415. /* Event Id. */
  416. ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
  417. ctf_save_write_int32 (&writer->tcs, ts->stop_reason);
  418. ctf_save_write_int32 (&writer->tcs, ts->stopping_tracepoint);
  419. ctf_save_write_int32 (&writer->tcs, ts->traceframe_count);
  420. ctf_save_write_int32 (&writer->tcs, ts->traceframes_created);
  421. ctf_save_write_int32 (&writer->tcs, ts->buffer_free);
  422. ctf_save_write_int32 (&writer->tcs, ts->buffer_size);
  423. ctf_save_write_int32 (&writer->tcs, ts->disconnected_tracing);
  424. ctf_save_write_int32 (&writer->tcs, ts->circular_buffer);
  425. }
  426. /* This is the implementation of trace_file_write_ops method
  427. write_uploaded_tsv. */
  428. static void
  429. ctf_write_uploaded_tsv (struct trace_file_writer *self,
  430. struct uploaded_tsv *tsv)
  431. {
  432. struct ctf_trace_file_writer *writer
  433. = (struct ctf_trace_file_writer *) self;
  434. int32_t int32;
  435. int64_t int64;
  436. const gdb_byte zero = 0;
  437. /* Event Id. */
  438. int32 = CTF_EVENT_ID_TSV_DEF;
  439. ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
  440. /* initial_value */
  441. int64 = tsv->initial_value;
  442. ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
  443. /* number */
  444. ctf_save_write_int32 (&writer->tcs, tsv->number);
  445. /* builtin */
  446. ctf_save_write_int32 (&writer->tcs, tsv->builtin);
  447. /* name */
  448. if (tsv->name != NULL)
  449. ctf_save_write (&writer->tcs, (gdb_byte *) tsv->name,
  450. strlen (tsv->name));
  451. ctf_save_write (&writer->tcs, &zero, 1);
  452. }
  453. /* This is the implementation of trace_file_write_ops method
  454. write_uploaded_tp. */
  455. static void
  456. ctf_write_uploaded_tp (struct trace_file_writer *self,
  457. struct uploaded_tp *tp)
  458. {
  459. struct ctf_trace_file_writer *writer
  460. = (struct ctf_trace_file_writer *) self;
  461. int32_t int32;
  462. int64_t int64;
  463. uint32_t u32;
  464. const gdb_byte zero = 0;
  465. /* Event Id. */
  466. int32 = CTF_EVENT_ID_TP_DEF;
  467. ctf_save_align_write (&writer->tcs, (gdb_byte *) &int32, 4, 4);
  468. /* address */
  469. int64 = tp->addr;
  470. ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
  471. /* traceframe_usage */
  472. int64 = tp->traceframe_usage;
  473. ctf_save_align_write (&writer->tcs, (gdb_byte *) &int64, 8, 8);
  474. /* number */
  475. ctf_save_write_int32 (&writer->tcs, tp->number);
  476. /* enabled */
  477. ctf_save_write_int32 (&writer->tcs, tp->enabled);
  478. /* step */
  479. ctf_save_write_int32 (&writer->tcs, tp->step);
  480. /* pass */
  481. ctf_save_write_int32 (&writer->tcs, tp->pass);
  482. /* hit_count */
  483. ctf_save_write_int32 (&writer->tcs, tp->hit_count);
  484. /* type */
  485. ctf_save_write_int32 (&writer->tcs, tp->type);
  486. /* condition */
  487. if (tp->cond != NULL)
  488. ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond.get (),
  489. strlen (tp->cond.get ()));
  490. ctf_save_write (&writer->tcs, &zero, 1);
  491. /* actions */
  492. u32 = tp->actions.size ();
  493. ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
  494. for (const auto &act : tp->actions)
  495. ctf_save_write (&writer->tcs, (gdb_byte *) act.get (),
  496. strlen (act.get ()) + 1);
  497. /* step_actions */
  498. u32 = tp->step_actions.size ();
  499. ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
  500. for (const auto &act : tp->step_actions)
  501. ctf_save_write (&writer->tcs, (gdb_byte *) act.get (),
  502. strlen (act.get ()) + 1);
  503. /* at_string */
  504. if (tp->at_string != NULL)
  505. ctf_save_write (&writer->tcs, (gdb_byte *) tp->at_string.get (),
  506. strlen (tp->at_string.get ()));
  507. ctf_save_write (&writer->tcs, &zero, 1);
  508. /* cond_string */
  509. if (tp->cond_string != NULL)
  510. ctf_save_write (&writer->tcs, (gdb_byte *) tp->cond_string.get (),
  511. strlen (tp->cond_string.get ()));
  512. ctf_save_write (&writer->tcs, &zero, 1);
  513. /* cmd_strings */
  514. u32 = tp->cmd_strings.size ();
  515. ctf_save_align_write (&writer->tcs, (gdb_byte *) &u32, 4, 4);
  516. for (const auto &act : tp->cmd_strings)
  517. ctf_save_write (&writer->tcs, (gdb_byte *) act.get (),
  518. strlen (act.get ()) + 1);
  519. }
  520. /* This is the implementation of trace_file_write_ops method
  521. write_tdesc. */
  522. static void
  523. ctf_write_tdesc (struct trace_file_writer *self)
  524. {
  525. /* Nothing so far. */
  526. }
  527. /* This is the implementation of trace_file_write_ops method
  528. write_definition_end. */
  529. static void
  530. ctf_write_definition_end (struct trace_file_writer *self)
  531. {
  532. self->ops->frame_ops->end (self);
  533. }
  534. /* This is the implementation of trace_file_write_ops method
  535. end. */
  536. static void
  537. ctf_end (struct trace_file_writer *self)
  538. {
  539. struct ctf_trace_file_writer *writer = (struct ctf_trace_file_writer *) self;
  540. gdb_assert (writer->tcs.content_size == 0);
  541. }
  542. /* This is the implementation of trace_frame_write_ops method
  543. start. */
  544. static void
  545. ctf_write_frame_start (struct trace_file_writer *self, uint16_t tpnum)
  546. {
  547. struct ctf_trace_file_writer *writer
  548. = (struct ctf_trace_file_writer *) self;
  549. uint32_t id = CTF_EVENT_ID_FRAME;
  550. uint32_t u32;
  551. /* Step 1: Write packet context. */
  552. /* magic. */
  553. u32 = CTF_MAGIC;
  554. ctf_save_write_uint32 (&writer->tcs, u32);
  555. /* content_size and packet_size.. We still don't know the value,
  556. write it later. */
  557. ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
  558. ctf_save_fseek (&writer->tcs, 4, SEEK_CUR);
  559. /* Tracepoint number. */
  560. ctf_save_write (&writer->tcs, (gdb_byte *) &tpnum, 2);
  561. /* Step 2: Write event "frame". */
  562. /* Event Id. */
  563. ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
  564. }
  565. /* This is the implementation of trace_frame_write_ops method
  566. write_r_block. */
  567. static void
  568. ctf_write_frame_r_block (struct trace_file_writer *self,
  569. gdb_byte *buf, int32_t size)
  570. {
  571. struct ctf_trace_file_writer *writer
  572. = (struct ctf_trace_file_writer *) self;
  573. uint32_t id = CTF_EVENT_ID_REGISTER;
  574. /* Event Id. */
  575. ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
  576. /* array contents. */
  577. ctf_save_align_write (&writer->tcs, buf, size, 1);
  578. }
  579. /* This is the implementation of trace_frame_write_ops method
  580. write_m_block_header. */
  581. static void
  582. ctf_write_frame_m_block_header (struct trace_file_writer *self,
  583. uint64_t addr, uint16_t length)
  584. {
  585. struct ctf_trace_file_writer *writer
  586. = (struct ctf_trace_file_writer *) self;
  587. uint32_t event_id = CTF_EVENT_ID_MEMORY;
  588. /* Event Id. */
  589. ctf_save_align_write (&writer->tcs, (gdb_byte *) &event_id, 4, 4);
  590. /* Address. */
  591. ctf_save_align_write (&writer->tcs, (gdb_byte *) &addr, 8, 8);
  592. /* Length. */
  593. ctf_save_align_write (&writer->tcs, (gdb_byte *) &length, 2, 2);
  594. }
  595. /* This is the implementation of trace_frame_write_ops method
  596. write_m_block_memory. */
  597. static void
  598. ctf_write_frame_m_block_memory (struct trace_file_writer *self,
  599. gdb_byte *buf, uint16_t length)
  600. {
  601. struct ctf_trace_file_writer *writer
  602. = (struct ctf_trace_file_writer *) self;
  603. /* Contents. */
  604. ctf_save_align_write (&writer->tcs, (gdb_byte *) buf, length, 1);
  605. }
  606. /* This is the implementation of trace_frame_write_ops method
  607. write_v_block. */
  608. static void
  609. ctf_write_frame_v_block (struct trace_file_writer *self,
  610. int32_t num, uint64_t val)
  611. {
  612. struct ctf_trace_file_writer *writer
  613. = (struct ctf_trace_file_writer *) self;
  614. uint32_t id = CTF_EVENT_ID_TSV;
  615. /* Event Id. */
  616. ctf_save_align_write (&writer->tcs, (gdb_byte *) &id, 4, 4);
  617. /* val. */
  618. ctf_save_align_write (&writer->tcs, (gdb_byte *) &val, 8, 8);
  619. /* num. */
  620. ctf_save_align_write (&writer->tcs, (gdb_byte *) &num, 4, 4);
  621. }
  622. /* This is the implementation of trace_frame_write_ops method
  623. end. */
  624. static void
  625. ctf_write_frame_end (struct trace_file_writer *self)
  626. {
  627. struct ctf_trace_file_writer *writer
  628. = (struct ctf_trace_file_writer *) self;
  629. uint32_t u32;
  630. uint32_t t;
  631. /* Write the content size to packet header. */
  632. ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + 4,
  633. SEEK_SET);
  634. u32 = writer->tcs.content_size * TARGET_CHAR_BIT;
  635. t = writer->tcs.content_size;
  636. ctf_save_write_uint32 (&writer->tcs, u32);
  637. /* Write the packet size. */
  638. u32 += 4 * TARGET_CHAR_BIT;
  639. ctf_save_write_uint32 (&writer->tcs, u32);
  640. writer->tcs.content_size = t;
  641. /* Write zero at the end of the packet. */
  642. ctf_save_fseek (&writer->tcs, writer->tcs.packet_start + t,
  643. SEEK_SET);
  644. u32 = 0;
  645. ctf_save_write_uint32 (&writer->tcs, u32);
  646. writer->tcs.content_size = t;
  647. ctf_save_next_packet (&writer->tcs);
  648. }
  649. /* Operations to write various types of trace frames into CTF
  650. format. */
  651. static const struct trace_frame_write_ops ctf_write_frame_ops =
  652. {
  653. ctf_write_frame_start,
  654. ctf_write_frame_r_block,
  655. ctf_write_frame_m_block_header,
  656. ctf_write_frame_m_block_memory,
  657. ctf_write_frame_v_block,
  658. ctf_write_frame_end,
  659. };
  660. /* Operations to write trace buffers into CTF format. */
  661. static const struct trace_file_write_ops ctf_write_ops =
  662. {
  663. ctf_dtor,
  664. ctf_target_save,
  665. ctf_start,
  666. ctf_write_header,
  667. ctf_write_regblock_type,
  668. ctf_write_status,
  669. ctf_write_uploaded_tsv,
  670. ctf_write_uploaded_tp,
  671. ctf_write_tdesc,
  672. ctf_write_definition_end,
  673. NULL,
  674. &ctf_write_frame_ops,
  675. ctf_end,
  676. };
  677. /* Return a trace writer for CTF format. */
  678. struct trace_file_writer *
  679. ctf_trace_file_writer_new (void)
  680. {
  681. struct ctf_trace_file_writer *writer = XNEW (struct ctf_trace_file_writer);
  682. writer->base.ops = &ctf_write_ops;
  683. return (struct trace_file_writer *) writer;
  684. }
  685. #if HAVE_LIBBABELTRACE
  686. /* Use libbabeltrace to read CTF data. The libbabeltrace provides
  687. iterator to iterate over each event in CTF data and APIs to get
  688. details of event and packet, so it is very convenient to use
  689. libbabeltrace to access events in CTF. */
  690. #include <babeltrace/babeltrace.h>
  691. #include <babeltrace/ctf/events.h>
  692. #include <babeltrace/ctf/iterator.h>
  693. /* The struct pointer for current CTF directory. */
  694. static int handle_id = -1;
  695. static struct bt_context *ctx = NULL;
  696. static struct bt_ctf_iter *ctf_iter = NULL;
  697. /* The position of the first packet containing trace frame. */
  698. static struct bt_iter_pos *start_pos;
  699. /* The name of CTF directory. */
  700. static char *trace_dirname;
  701. static ctf_target ctf_ops;
  702. /* Destroy ctf iterator and context. */
  703. static void
  704. ctf_destroy (void)
  705. {
  706. if (ctf_iter != NULL)
  707. {
  708. bt_ctf_iter_destroy (ctf_iter);
  709. ctf_iter = NULL;
  710. }
  711. if (ctx != NULL)
  712. {
  713. bt_context_put (ctx);
  714. ctx = NULL;
  715. }
  716. }
  717. /* Open CTF trace data in DIRNAME. */
  718. static void
  719. ctf_open_dir (const char *dirname)
  720. {
  721. struct bt_iter_pos begin_pos;
  722. unsigned int count, i;
  723. struct bt_ctf_event_decl * const *list;
  724. ctx = bt_context_create ();
  725. if (ctx == NULL)
  726. error (_("Unable to create bt_context"));
  727. handle_id = bt_context_add_trace (ctx, dirname, "ctf", NULL, NULL, NULL);
  728. if (handle_id < 0)
  729. {
  730. ctf_destroy ();
  731. error (_("Unable to use libbabeltrace on directory \"%s\""),
  732. dirname);
  733. }
  734. begin_pos.type = BT_SEEK_BEGIN;
  735. ctf_iter = bt_ctf_iter_create (ctx, &begin_pos, NULL);
  736. if (ctf_iter == NULL)
  737. {
  738. ctf_destroy ();
  739. error (_("Unable to create bt_iterator"));
  740. }
  741. /* Look for the declaration of register block. Get the length of
  742. array "contents" to set trace_regblock_size. */
  743. bt_ctf_get_event_decl_list (handle_id, ctx, &list, &count);
  744. for (i = 0; i < count; i++)
  745. if (strcmp ("register", bt_ctf_get_decl_event_name (list[i])) == 0)
  746. {
  747. const struct bt_ctf_field_decl * const *field_list;
  748. const struct bt_declaration *decl;
  749. bt_ctf_get_decl_fields (list[i], BT_EVENT_FIELDS, &field_list,
  750. &count);
  751. gdb_assert (count == 1);
  752. gdb_assert (0 == strcmp ("contents",
  753. bt_ctf_get_decl_field_name (field_list[0])));
  754. decl = bt_ctf_get_decl_from_field_decl (field_list[0]);
  755. trace_regblock_size = bt_ctf_get_array_len (decl);
  756. break;
  757. }
  758. }
  759. #define SET_INT32_FIELD(EVENT, SCOPE, VAR, FIELD) \
  760. (VAR)->FIELD = (int) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
  761. (SCOPE), \
  762. #FIELD))
  763. #define SET_ENUM_FIELD(EVENT, SCOPE, VAR, TYPE, FIELD) \
  764. (VAR)->FIELD = (TYPE) bt_ctf_get_int64 (bt_ctf_get_field ((EVENT), \
  765. (SCOPE), \
  766. #FIELD))
  767. /* EVENT is the "status" event and TS is filled in. */
  768. static void
  769. ctf_read_status (struct bt_ctf_event *event, struct trace_status *ts)
  770. {
  771. const struct bt_definition *scope
  772. = bt_ctf_get_top_level_scope (event, BT_EVENT_FIELDS);
  773. SET_ENUM_FIELD (event, scope, ts, enum trace_stop_reason, stop_reason);
  774. SET_INT32_FIELD (event, scope, ts, stopping_tracepoint);
  775. SET_INT32_FIELD (event, scope, ts, traceframe_count);
  776. SET_INT32_FIELD (event, scope, ts, traceframes_created);
  777. SET_INT32_FIELD (event, scope, ts, buffer_free);
  778. SET_INT32_FIELD (event, scope, ts, buffer_size);
  779. SET_INT32_FIELD (event, scope, ts, disconnected_tracing);
  780. SET_INT32_FIELD (event, scope, ts, circular_buffer);
  781. bt_iter_next (bt_ctf_get_iter (ctf_iter));
  782. }
  783. /* Read the events "tsv_def" one by one, extract its contents and fill
  784. in the list UPLOADED_TSVS. */
  785. static void
  786. ctf_read_tsv (struct uploaded_tsv **uploaded_tsvs)
  787. {
  788. gdb_assert (ctf_iter != NULL);
  789. while (1)
  790. {
  791. struct bt_ctf_event *event;
  792. const struct bt_definition *scope;
  793. const struct bt_definition *def;
  794. uint32_t event_id;
  795. struct uploaded_tsv *utsv = NULL;
  796. event = bt_ctf_iter_read_event (ctf_iter);
  797. scope = bt_ctf_get_top_level_scope (event,
  798. BT_STREAM_EVENT_HEADER);
  799. event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
  800. "id"));
  801. if (event_id != CTF_EVENT_ID_TSV_DEF)
  802. break;
  803. scope = bt_ctf_get_top_level_scope (event,
  804. BT_EVENT_FIELDS);
  805. def = bt_ctf_get_field (event, scope, "number");
  806. utsv = get_uploaded_tsv ((int32_t) bt_ctf_get_int64 (def),
  807. uploaded_tsvs);
  808. def = bt_ctf_get_field (event, scope, "builtin");
  809. utsv->builtin = (int32_t) bt_ctf_get_int64 (def);
  810. def = bt_ctf_get_field (event, scope, "initial_value");
  811. utsv->initial_value = bt_ctf_get_int64 (def);
  812. def = bt_ctf_get_field (event, scope, "name");
  813. utsv->name = xstrdup (bt_ctf_get_string (def));
  814. if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
  815. break;
  816. }
  817. }
  818. /* Read the value of element whose index is NUM from CTF and write it
  819. to the corresponding VAR->ARRAY. */
  820. #define SET_ARRAY_FIELD(EVENT, SCOPE, VAR, NUM, ARRAY) \
  821. do \
  822. { \
  823. uint32_t lu32, i; \
  824. const struct bt_definition *def; \
  825. \
  826. lu32 = (uint32_t) bt_ctf_get_uint64 (bt_ctf_get_field ((EVENT), \
  827. (SCOPE), \
  828. #NUM)); \
  829. def = bt_ctf_get_field ((EVENT), (SCOPE), #ARRAY); \
  830. for (i = 0; i < lu32; i++) \
  831. { \
  832. const struct bt_definition *element \
  833. = bt_ctf_get_index ((EVENT), def, i); \
  834. \
  835. (VAR)->ARRAY.emplace_back \
  836. (xstrdup (bt_ctf_get_string (element))); \
  837. } \
  838. } \
  839. while (0)
  840. /* Read a string from CTF and set VAR->FIELD. If the length of string
  841. is zero, set VAR->FIELD to NULL. */
  842. #define SET_STRING_FIELD(EVENT, SCOPE, VAR, FIELD) \
  843. do \
  844. { \
  845. const char *p = bt_ctf_get_string (bt_ctf_get_field ((EVENT), \
  846. (SCOPE), \
  847. #FIELD)); \
  848. \
  849. if (strlen (p) > 0) \
  850. (VAR)->FIELD.reset (xstrdup (p)); \
  851. else \
  852. (VAR)->FIELD = NULL; \
  853. } \
  854. while (0)
  855. /* Read the events "tp_def" one by one, extract its contents and fill
  856. in the list UPLOADED_TPS. */
  857. static void
  858. ctf_read_tp (struct uploaded_tp **uploaded_tps)
  859. {
  860. gdb_assert (ctf_iter != NULL);
  861. while (1)
  862. {
  863. struct bt_ctf_event *event;
  864. const struct bt_definition *scope;
  865. uint32_t u32;
  866. int32_t int32;
  867. uint64_t u64;
  868. struct uploaded_tp *utp = NULL;
  869. event = bt_ctf_iter_read_event (ctf_iter);
  870. scope = bt_ctf_get_top_level_scope (event,
  871. BT_STREAM_EVENT_HEADER);
  872. u32 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
  873. "id"));
  874. if (u32 != CTF_EVENT_ID_TP_DEF)
  875. break;
  876. scope = bt_ctf_get_top_level_scope (event,
  877. BT_EVENT_FIELDS);
  878. int32 = (int32_t) bt_ctf_get_int64 (bt_ctf_get_field (event,
  879. scope,
  880. "number"));
  881. u64 = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope,
  882. "addr"));
  883. utp = get_uploaded_tp (int32, u64, uploaded_tps);
  884. SET_INT32_FIELD (event, scope, utp, enabled);
  885. SET_INT32_FIELD (event, scope, utp, step);
  886. SET_INT32_FIELD (event, scope, utp, pass);
  887. SET_INT32_FIELD (event, scope, utp, hit_count);
  888. SET_ENUM_FIELD (event, scope, utp, enum bptype, type);
  889. /* Read 'cmd_strings'. */
  890. SET_ARRAY_FIELD (event, scope, utp, cmd_num, cmd_strings);
  891. /* Read 'actions'. */
  892. SET_ARRAY_FIELD (event, scope, utp, action_num, actions);
  893. /* Read 'step_actions'. */
  894. SET_ARRAY_FIELD (event, scope, utp, step_action_num,
  895. step_actions);
  896. SET_STRING_FIELD(event, scope, utp, at_string);
  897. SET_STRING_FIELD(event, scope, utp, cond_string);
  898. if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
  899. break;
  900. }
  901. }
  902. /* This is the implementation of target_ops method to_open. Open CTF
  903. trace data, read trace status, trace state variables and tracepoint
  904. definitions from the first packet. Set the start position at the
  905. second packet which contains events on trace blocks. */
  906. static void
  907. ctf_target_open (const char *dirname, int from_tty)
  908. {
  909. struct bt_ctf_event *event;
  910. uint32_t event_id;
  911. const struct bt_definition *scope;
  912. struct uploaded_tsv *uploaded_tsvs = NULL;
  913. struct uploaded_tp *uploaded_tps = NULL;
  914. if (!dirname)
  915. error (_("No CTF directory specified."));
  916. ctf_open_dir (dirname);
  917. target_preopen (from_tty);
  918. /* Skip the first packet which about the trace status. The first
  919. event is "frame". */
  920. event = bt_ctf_iter_read_event (ctf_iter);
  921. scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
  922. event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
  923. if (event_id != CTF_EVENT_ID_FRAME)
  924. error (_("Wrong event id of the first event"));
  925. /* The second event is "status". */
  926. bt_iter_next (bt_ctf_get_iter (ctf_iter));
  927. event = bt_ctf_iter_read_event (ctf_iter);
  928. scope = bt_ctf_get_top_level_scope (event, BT_STREAM_EVENT_HEADER);
  929. event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "id"));
  930. if (event_id != CTF_EVENT_ID_STATUS)
  931. error (_("Wrong event id of the second event"));
  932. ctf_read_status (event, current_trace_status ());
  933. ctf_read_tsv (&uploaded_tsvs);
  934. ctf_read_tp (&uploaded_tps);
  935. event = bt_ctf_iter_read_event (ctf_iter);
  936. /* EVENT can be NULL if we've already gone to the end of stream of
  937. events. */
  938. if (event != NULL)
  939. {
  940. scope = bt_ctf_get_top_level_scope (event,
  941. BT_STREAM_EVENT_HEADER);
  942. event_id = bt_ctf_get_uint64 (bt_ctf_get_field (event,
  943. scope, "id"));
  944. if (event_id != CTF_EVENT_ID_FRAME)
  945. error (_("Wrong event id of the first event of the second packet"));
  946. }
  947. start_pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
  948. gdb_assert (start_pos->type == BT_SEEK_RESTORE);
  949. trace_dirname = xstrdup (dirname);
  950. current_inferior ()->push_target (&ctf_ops);
  951. inferior_appeared (current_inferior (), CTF_PID);
  952. thread_info *thr = add_thread_silent (&ctf_ops, ptid_t (CTF_PID));
  953. switch_to_thread (thr);
  954. merge_uploaded_trace_state_variables (&uploaded_tsvs);
  955. merge_uploaded_tracepoints (&uploaded_tps);
  956. post_create_inferior (from_tty);
  957. }
  958. /* This is the implementation of target_ops method to_close. Destroy
  959. CTF iterator and context. */
  960. void
  961. ctf_target::close ()
  962. {
  963. ctf_destroy ();
  964. xfree (trace_dirname);
  965. trace_dirname = NULL;
  966. switch_to_no_thread (); /* Avoid confusion from thread stuff. */
  967. exit_inferior_silent (current_inferior ());
  968. trace_reset_local_state ();
  969. }
  970. /* This is the implementation of target_ops method to_files_info.
  971. Print the directory name of CTF trace data. */
  972. void
  973. ctf_target::files_info ()
  974. {
  975. gdb_printf ("\t`%s'\n", trace_dirname);
  976. }
  977. /* This is the implementation of target_ops method to_fetch_registers.
  978. Iterate over events whose name is "register" in current frame,
  979. extract contents from events, and set REGCACHE with the contents.
  980. If no matched events are found, mark registers unavailable. */
  981. void
  982. ctf_target::fetch_registers (struct regcache *regcache, int regno)
  983. {
  984. struct gdbarch *gdbarch = regcache->arch ();
  985. struct bt_ctf_event *event = NULL;
  986. struct bt_iter_pos *pos;
  987. /* An uninitialized reg size says we're not going to be
  988. successful at getting register blocks. */
  989. if (trace_regblock_size == 0)
  990. return;
  991. gdb_assert (ctf_iter != NULL);
  992. /* Save the current position. */
  993. pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
  994. gdb_assert (pos->type == BT_SEEK_RESTORE);
  995. while (1)
  996. {
  997. const char *name;
  998. struct bt_ctf_event *event1;
  999. event1 = bt_ctf_iter_read_event (ctf_iter);
  1000. name = bt_ctf_event_name (event1);
  1001. if (name == NULL || strcmp (name, "frame") == 0)
  1002. break;
  1003. else if (strcmp (name, "register") == 0)
  1004. {
  1005. event = event1;
  1006. break;
  1007. }
  1008. if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
  1009. break;
  1010. }
  1011. /* Restore the position. */
  1012. bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
  1013. if (event != NULL)
  1014. {
  1015. int offset, regsize, regn;
  1016. const struct bt_definition *scope
  1017. = bt_ctf_get_top_level_scope (event,
  1018. BT_EVENT_FIELDS);
  1019. const struct bt_definition *array
  1020. = bt_ctf_get_field (event, scope, "contents");
  1021. gdb_byte *regs = (gdb_byte *) bt_ctf_get_char_array (array);
  1022. /* Assume the block is laid out in GDB register number order,
  1023. each register with the size that it has in GDB. */
  1024. offset = 0;
  1025. for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
  1026. {
  1027. regsize = register_size (gdbarch, regn);
  1028. /* Make sure we stay within block bounds. */
  1029. if (offset + regsize >= trace_regblock_size)
  1030. break;
  1031. if (regcache->get_register_status (regn) == REG_UNKNOWN)
  1032. {
  1033. if (regno == regn)
  1034. {
  1035. regcache->raw_supply (regno, regs + offset);
  1036. break;
  1037. }
  1038. else if (regno == -1)
  1039. {
  1040. regcache->raw_supply (regn, regs + offset);
  1041. }
  1042. }
  1043. offset += regsize;
  1044. }
  1045. }
  1046. else
  1047. tracefile_fetch_registers (regcache, regno);
  1048. }
  1049. /* This is the implementation of target_ops method to_xfer_partial.
  1050. Iterate over events whose name is "memory" in
  1051. current frame, extract the address and length from events. If
  1052. OFFSET is within the range, read the contents from events to
  1053. READBUF. */
  1054. enum target_xfer_status
  1055. ctf_target::xfer_partial (enum target_object object,
  1056. const char *annex, gdb_byte *readbuf,
  1057. const gdb_byte *writebuf, ULONGEST offset,
  1058. ULONGEST len, ULONGEST *xfered_len)
  1059. {
  1060. /* We're only doing regular memory for now. */
  1061. if (object != TARGET_OBJECT_MEMORY)
  1062. return TARGET_XFER_E_IO;
  1063. if (readbuf == NULL)
  1064. error (_("ctf_xfer_partial: trace file is read-only"));
  1065. if (get_traceframe_number () != -1)
  1066. {
  1067. struct bt_iter_pos *pos;
  1068. enum target_xfer_status res;
  1069. /* Records the lowest available address of all blocks that
  1070. intersects the requested range. */
  1071. ULONGEST low_addr_available = 0;
  1072. gdb_assert (ctf_iter != NULL);
  1073. /* Save the current position. */
  1074. pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
  1075. gdb_assert (pos->type == BT_SEEK_RESTORE);
  1076. /* Iterate through the traceframe's blocks, looking for
  1077. memory. */
  1078. while (1)
  1079. {
  1080. ULONGEST amt;
  1081. uint64_t maddr;
  1082. uint16_t mlen;
  1083. const struct bt_definition *scope;
  1084. const struct bt_definition *def;
  1085. struct bt_ctf_event *event
  1086. = bt_ctf_iter_read_event (ctf_iter);
  1087. const char *name = bt_ctf_event_name (event);
  1088. if (name == NULL || strcmp (name, "frame") == 0)
  1089. break;
  1090. else if (strcmp (name, "memory") != 0)
  1091. {
  1092. if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
  1093. break;
  1094. continue;
  1095. }
  1096. scope = bt_ctf_get_top_level_scope (event,
  1097. BT_EVENT_FIELDS);
  1098. def = bt_ctf_get_field (event, scope, "address");
  1099. maddr = bt_ctf_get_uint64 (def);
  1100. def = bt_ctf_get_field (event, scope, "length");
  1101. mlen = (uint16_t) bt_ctf_get_uint64 (def);
  1102. /* If the block includes the first part of the desired
  1103. range, return as much it has; GDB will re-request the
  1104. remainder, which might be in a different block of this
  1105. trace frame. */
  1106. if (maddr <= offset && offset < (maddr + mlen))
  1107. {
  1108. const struct bt_definition *array
  1109. = bt_ctf_get_field (event, scope, "contents");
  1110. gdb_byte *contents;
  1111. int k;
  1112. contents = (gdb_byte *) xmalloc (mlen);
  1113. for (k = 0; k < mlen; k++)
  1114. {
  1115. const struct bt_definition *element
  1116. = bt_ctf_get_index (event, array, k);
  1117. contents[k] = (gdb_byte) bt_ctf_get_uint64 (element);
  1118. }
  1119. amt = (maddr + mlen) - offset;
  1120. if (amt > len)
  1121. amt = len;
  1122. memcpy (readbuf, &contents[offset - maddr], amt);
  1123. xfree (contents);
  1124. /* Restore the position. */
  1125. bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
  1126. if (amt == 0)
  1127. return TARGET_XFER_EOF;
  1128. else
  1129. {
  1130. *xfered_len = amt;
  1131. return TARGET_XFER_OK;
  1132. }
  1133. }
  1134. if (offset < maddr && maddr < (offset + len))
  1135. if (low_addr_available == 0 || low_addr_available > maddr)
  1136. low_addr_available = maddr;
  1137. if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
  1138. break;
  1139. }
  1140. /* Restore the position. */
  1141. bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
  1142. /* Requested memory is unavailable in the context of traceframes,
  1143. and this address falls within a read-only section, fallback
  1144. to reading from executable, up to LOW_ADDR_AVAILABLE */
  1145. if (offset < low_addr_available)
  1146. len = std::min (len, low_addr_available - offset);
  1147. res = exec_read_partial_read_only (readbuf, offset, len, xfered_len);
  1148. if (res == TARGET_XFER_OK)
  1149. return TARGET_XFER_OK;
  1150. else
  1151. {
  1152. /* No use trying further, we know some memory starting
  1153. at MEMADDR isn't available. */
  1154. *xfered_len = len;
  1155. return TARGET_XFER_UNAVAILABLE;
  1156. }
  1157. }
  1158. else
  1159. {
  1160. /* Fallback to reading from read-only sections. */
  1161. return section_table_read_available_memory (readbuf, offset, len, xfered_len);
  1162. }
  1163. }
  1164. /* This is the implementation of target_ops method
  1165. to_get_trace_state_variable_value.
  1166. Iterate over events whose name is "tsv" in current frame. When the
  1167. trace variable is found, set the value of it to *VAL and return
  1168. true, otherwise return false. */
  1169. bool
  1170. ctf_target::get_trace_state_variable_value (int tsvnum, LONGEST *val)
  1171. {
  1172. struct bt_iter_pos *pos;
  1173. bool found = false;
  1174. gdb_assert (ctf_iter != NULL);
  1175. /* Save the current position. */
  1176. pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
  1177. gdb_assert (pos->type == BT_SEEK_RESTORE);
  1178. /* Iterate through the traceframe's blocks, looking for 'V'
  1179. block. */
  1180. while (1)
  1181. {
  1182. struct bt_ctf_event *event
  1183. = bt_ctf_iter_read_event (ctf_iter);
  1184. const char *name = bt_ctf_event_name (event);
  1185. if (name == NULL || strcmp (name, "frame") == 0)
  1186. break;
  1187. else if (strcmp (name, "tsv") == 0)
  1188. {
  1189. const struct bt_definition *scope;
  1190. const struct bt_definition *def;
  1191. scope = bt_ctf_get_top_level_scope (event,
  1192. BT_EVENT_FIELDS);
  1193. def = bt_ctf_get_field (event, scope, "num");
  1194. if (tsvnum == (int32_t) bt_ctf_get_uint64 (def))
  1195. {
  1196. def = bt_ctf_get_field (event, scope, "val");
  1197. *val = bt_ctf_get_uint64 (def);
  1198. found = true;
  1199. }
  1200. }
  1201. if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
  1202. break;
  1203. }
  1204. /* Restore the position. */
  1205. bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
  1206. return found;
  1207. }
  1208. /* Return the tracepoint number in "frame" event. */
  1209. static int
  1210. ctf_get_tpnum_from_frame_event (struct bt_ctf_event *event)
  1211. {
  1212. /* The packet context of events has a field "tpnum". */
  1213. const struct bt_definition *scope
  1214. = bt_ctf_get_top_level_scope (event, BT_STREAM_PACKET_CONTEXT);
  1215. uint64_t tpnum
  1216. = bt_ctf_get_uint64 (bt_ctf_get_field (event, scope, "tpnum"));
  1217. return (int) tpnum;
  1218. }
  1219. /* Return the address at which the current frame was collected. */
  1220. static CORE_ADDR
  1221. ctf_get_traceframe_address (void)
  1222. {
  1223. struct bt_ctf_event *event = NULL;
  1224. struct bt_iter_pos *pos;
  1225. CORE_ADDR addr = 0;
  1226. gdb_assert (ctf_iter != NULL);
  1227. pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
  1228. gdb_assert (pos->type == BT_SEEK_RESTORE);
  1229. while (1)
  1230. {
  1231. const char *name;
  1232. struct bt_ctf_event *event1;
  1233. event1 = bt_ctf_iter_read_event (ctf_iter);
  1234. name = bt_ctf_event_name (event1);
  1235. if (name == NULL)
  1236. break;
  1237. else if (strcmp (name, "frame") == 0)
  1238. {
  1239. event = event1;
  1240. break;
  1241. }
  1242. if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
  1243. break;
  1244. }
  1245. if (event != NULL)
  1246. {
  1247. int tpnum = ctf_get_tpnum_from_frame_event (event);
  1248. struct tracepoint *tp
  1249. = get_tracepoint_by_number_on_target (tpnum);
  1250. if (tp && tp->loc)
  1251. addr = tp->loc->address;
  1252. }
  1253. /* Restore the position. */
  1254. bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
  1255. return addr;
  1256. }
  1257. /* This is the implementation of target_ops method to_trace_find.
  1258. Iterate the events whose name is "frame", extract the tracepoint
  1259. number in it. Return traceframe number when matched. */
  1260. int
  1261. ctf_target::trace_find (enum trace_find_type type, int num,
  1262. CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
  1263. {
  1264. int tfnum = 0;
  1265. int found = 0;
  1266. if (num == -1)
  1267. {
  1268. if (tpp != NULL)
  1269. *tpp = -1;
  1270. return -1;
  1271. }
  1272. gdb_assert (ctf_iter != NULL);
  1273. /* Set iterator back to the start. */
  1274. bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), start_pos);
  1275. while (1)
  1276. {
  1277. struct bt_ctf_event *event;
  1278. const char *name;
  1279. event = bt_ctf_iter_read_event (ctf_iter);
  1280. name = bt_ctf_event_name (event);
  1281. if (event == NULL || name == NULL)
  1282. break;
  1283. if (strcmp (name, "frame") == 0)
  1284. {
  1285. CORE_ADDR tfaddr;
  1286. if (type == tfind_number)
  1287. {
  1288. /* Looking for a specific trace frame. */
  1289. if (tfnum == num)
  1290. found = 1;
  1291. }
  1292. else
  1293. {
  1294. /* Start from the _next_ trace frame. */
  1295. if (tfnum > get_traceframe_number ())
  1296. {
  1297. switch (type)
  1298. {
  1299. case tfind_tp:
  1300. {
  1301. struct tracepoint *tp = get_tracepoint (num);
  1302. if (tp != NULL
  1303. && (tp->number_on_target
  1304. == ctf_get_tpnum_from_frame_event (event)))
  1305. found = 1;
  1306. break;
  1307. }
  1308. case tfind_pc:
  1309. tfaddr = ctf_get_traceframe_address ();
  1310. if (tfaddr == addr1)
  1311. found = 1;
  1312. break;
  1313. case tfind_range:
  1314. tfaddr = ctf_get_traceframe_address ();
  1315. if (addr1 <= tfaddr && tfaddr <= addr2)
  1316. found = 1;
  1317. break;
  1318. case tfind_outside:
  1319. tfaddr = ctf_get_traceframe_address ();
  1320. if (!(addr1 <= tfaddr && tfaddr <= addr2))
  1321. found = 1;
  1322. break;
  1323. default:
  1324. internal_error (__FILE__, __LINE__, _("unknown tfind type"));
  1325. }
  1326. }
  1327. }
  1328. if (found)
  1329. {
  1330. if (tpp != NULL)
  1331. *tpp = ctf_get_tpnum_from_frame_event (event);
  1332. /* Skip the event "frame". */
  1333. bt_iter_next (bt_ctf_get_iter (ctf_iter));
  1334. return tfnum;
  1335. }
  1336. tfnum++;
  1337. }
  1338. if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
  1339. break;
  1340. }
  1341. return -1;
  1342. }
  1343. /* This is the implementation of target_ops method to_traceframe_info.
  1344. Iterate the events whose name is "memory", in current
  1345. frame, extract memory range information, and return them in
  1346. traceframe_info. */
  1347. traceframe_info_up
  1348. ctf_target::traceframe_info ()
  1349. {
  1350. traceframe_info_up info (new struct traceframe_info);
  1351. const char *name;
  1352. struct bt_iter_pos *pos;
  1353. gdb_assert (ctf_iter != NULL);
  1354. /* Save the current position. */
  1355. pos = bt_iter_get_pos (bt_ctf_get_iter (ctf_iter));
  1356. gdb_assert (pos->type == BT_SEEK_RESTORE);
  1357. do
  1358. {
  1359. struct bt_ctf_event *event
  1360. = bt_ctf_iter_read_event (ctf_iter);
  1361. name = bt_ctf_event_name (event);
  1362. if (name == NULL || strcmp (name, "register") == 0
  1363. || strcmp (name, "frame") == 0)
  1364. ;
  1365. else if (strcmp (name, "memory") == 0)
  1366. {
  1367. const struct bt_definition *scope
  1368. = bt_ctf_get_top_level_scope (event,
  1369. BT_EVENT_FIELDS);
  1370. const struct bt_definition *def;
  1371. def = bt_ctf_get_field (event, scope, "address");
  1372. CORE_ADDR start = bt_ctf_get_uint64 (def);
  1373. def = bt_ctf_get_field (event, scope, "length");
  1374. int length = (uint16_t) bt_ctf_get_uint64 (def);
  1375. info->memory.emplace_back (start, length);
  1376. }
  1377. else if (strcmp (name, "tsv") == 0)
  1378. {
  1379. int vnum;
  1380. const struct bt_definition *scope
  1381. = bt_ctf_get_top_level_scope (event,
  1382. BT_EVENT_FIELDS);
  1383. const struct bt_definition *def;
  1384. def = bt_ctf_get_field (event, scope, "num");
  1385. vnum = (int) bt_ctf_get_uint64 (def);
  1386. info->tvars.push_back (vnum);
  1387. }
  1388. else
  1389. {
  1390. warning (_("Unhandled trace block type (%s) "
  1391. "while building trace frame info."),
  1392. name);
  1393. }
  1394. if (bt_iter_next (bt_ctf_get_iter (ctf_iter)) < 0)
  1395. break;
  1396. }
  1397. while (name != NULL && strcmp (name, "frame") != 0);
  1398. /* Restore the position. */
  1399. bt_iter_set_pos (bt_ctf_get_iter (ctf_iter), pos);
  1400. return info;
  1401. }
  1402. #endif
  1403. /* module initialization */
  1404. void _initialize_ctf ();
  1405. void
  1406. _initialize_ctf ()
  1407. {
  1408. #if HAVE_LIBBABELTRACE
  1409. add_target (ctf_target_info, ctf_target_open, filename_completer);
  1410. #endif
  1411. }