tracefile.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* Trace file support in GDB.
  2. Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "tracefile.h"
  16. #include "tracectf.h"
  17. #include "exec.h"
  18. #include "regcache.h"
  19. #include "gdbsupport/byte-vector.h"
  20. #include "gdbarch.h"
  21. #include "gdbsupport/buildargv.h"
  22. /* Helper macros. */
  23. #define TRACE_WRITE_R_BLOCK(writer, buf, size) \
  24. writer->ops->frame_ops->write_r_block ((writer), (buf), (size))
  25. #define TRACE_WRITE_M_BLOCK_HEADER(writer, addr, size) \
  26. writer->ops->frame_ops->write_m_block_header ((writer), (addr), \
  27. (size))
  28. #define TRACE_WRITE_M_BLOCK_MEMORY(writer, buf, size) \
  29. writer->ops->frame_ops->write_m_block_memory ((writer), (buf), \
  30. (size))
  31. #define TRACE_WRITE_V_BLOCK(writer, num, val) \
  32. writer->ops->frame_ops->write_v_block ((writer), (num), (val))
  33. /* A unique pointer policy class for trace_file_writer. */
  34. struct trace_file_writer_deleter
  35. {
  36. void operator() (struct trace_file_writer *writer)
  37. {
  38. writer->ops->dtor (writer);
  39. xfree (writer);
  40. }
  41. };
  42. /* A unique_ptr specialization for trace_file_writer. */
  43. typedef std::unique_ptr<trace_file_writer, trace_file_writer_deleter>
  44. trace_file_writer_up;
  45. /* Save tracepoint data to file named FILENAME through WRITER. WRITER
  46. determines the trace file format. If TARGET_DOES_SAVE is non-zero,
  47. the save is performed on the target, otherwise GDB obtains all trace
  48. data and saves it locally. */
  49. static void
  50. trace_save (const char *filename, struct trace_file_writer *writer,
  51. int target_does_save)
  52. {
  53. struct trace_status *ts = current_trace_status ();
  54. struct uploaded_tp *uploaded_tps = NULL, *utp;
  55. struct uploaded_tsv *uploaded_tsvs = NULL, *utsv;
  56. ULONGEST offset = 0;
  57. #define MAX_TRACE_UPLOAD 2000
  58. gdb::byte_vector buf (std::max (MAX_TRACE_UPLOAD, trace_regblock_size));
  59. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  60. /* If the target is to save the data to a file on its own, then just
  61. send the command and be done with it. */
  62. if (target_does_save)
  63. {
  64. if (!writer->ops->target_save (writer, filename))
  65. error (_("Target failed to save trace data to '%s'."),
  66. filename);
  67. return;
  68. }
  69. /* Get the trace status first before opening the file, so if the
  70. target is losing, we can get out without touching files. Since
  71. we're just calling this for side effects, we ignore the
  72. result. */
  73. target_get_trace_status (ts);
  74. writer->ops->start (writer, filename);
  75. writer->ops->write_header (writer);
  76. /* Write descriptive info. */
  77. /* Write out the size of a register block. */
  78. writer->ops->write_regblock_type (writer, trace_regblock_size);
  79. /* Write out the target description info. */
  80. writer->ops->write_tdesc (writer);
  81. /* Write out status of the tracing run (aka "tstatus" info). */
  82. writer->ops->write_status (writer, ts);
  83. /* Note that we want to upload tracepoints and save those, rather
  84. than simply writing out the local ones, because the user may have
  85. changed tracepoints in GDB in preparation for a future tracing
  86. run, or maybe just mass-deleted all types of breakpoints as part
  87. of cleaning up. So as not to contaminate the session, leave the
  88. data in its uploaded form, don't make into real tracepoints. */
  89. /* Get trace state variables first, they may be checked when parsing
  90. uploaded commands. */
  91. target_upload_trace_state_variables (&uploaded_tsvs);
  92. for (utsv = uploaded_tsvs; utsv; utsv = utsv->next)
  93. writer->ops->write_uploaded_tsv (writer, utsv);
  94. free_uploaded_tsvs (&uploaded_tsvs);
  95. target_upload_tracepoints (&uploaded_tps);
  96. for (utp = uploaded_tps; utp; utp = utp->next)
  97. target_get_tracepoint_status (NULL, utp);
  98. for (utp = uploaded_tps; utp; utp = utp->next)
  99. writer->ops->write_uploaded_tp (writer, utp);
  100. free_uploaded_tps (&uploaded_tps);
  101. /* Mark the end of the definition section. */
  102. writer->ops->write_definition_end (writer);
  103. /* Get and write the trace data proper. */
  104. while (1)
  105. {
  106. LONGEST gotten = 0;
  107. /* The writer supports writing the contents of trace buffer
  108. directly to trace file. Don't parse the contents of trace
  109. buffer. */
  110. if (writer->ops->write_trace_buffer != NULL)
  111. {
  112. /* We ask for big blocks, in the hopes of efficiency, but
  113. will take less if the target has packet size limitations
  114. or some such. */
  115. gotten = target_get_raw_trace_data (buf.data (), offset,
  116. MAX_TRACE_UPLOAD);
  117. if (gotten < 0)
  118. error (_("Failure to get requested trace buffer data"));
  119. /* No more data is forthcoming, we're done. */
  120. if (gotten == 0)
  121. break;
  122. writer->ops->write_trace_buffer (writer, buf.data (), gotten);
  123. offset += gotten;
  124. }
  125. else
  126. {
  127. uint16_t tp_num;
  128. uint32_t tf_size;
  129. /* Parse the trace buffers according to how data are stored
  130. in trace buffer in GDBserver. */
  131. gotten = target_get_raw_trace_data (buf.data (), offset, 6);
  132. if (gotten == 0)
  133. break;
  134. /* Read the first six bytes in, which is the tracepoint
  135. number and trace frame size. */
  136. tp_num = (uint16_t)
  137. extract_unsigned_integer (&((buf.data ())[0]), 2, byte_order);
  138. tf_size = (uint32_t)
  139. extract_unsigned_integer (&((buf.data ())[2]), 4, byte_order);
  140. writer->ops->frame_ops->start (writer, tp_num);
  141. gotten = 6;
  142. if (tf_size > 0)
  143. {
  144. unsigned int block;
  145. offset += 6;
  146. for (block = 0; block < tf_size; )
  147. {
  148. gdb_byte block_type;
  149. /* We'll fetch one block each time, in order to
  150. handle the extremely large 'M' block. We first
  151. fetch one byte to get the type of the block. */
  152. gotten = target_get_raw_trace_data (buf.data (),
  153. offset, 1);
  154. if (gotten < 1)
  155. error (_("Failure to get requested trace buffer data"));
  156. gotten = 1;
  157. block += 1;
  158. offset += 1;
  159. block_type = buf[0];
  160. switch (block_type)
  161. {
  162. case 'R':
  163. gotten
  164. = target_get_raw_trace_data (buf.data (), offset,
  165. trace_regblock_size);
  166. if (gotten < trace_regblock_size)
  167. error (_("Failure to get requested trace"
  168. " buffer data"));
  169. TRACE_WRITE_R_BLOCK (writer, buf.data (),
  170. trace_regblock_size);
  171. break;
  172. case 'M':
  173. {
  174. unsigned short mlen;
  175. ULONGEST addr;
  176. LONGEST t;
  177. int j;
  178. t = target_get_raw_trace_data (buf.data (),
  179. offset, 10);
  180. if (t < 10)
  181. error (_("Failure to get requested trace"
  182. " buffer data"));
  183. offset += 10;
  184. block += 10;
  185. gotten = 0;
  186. addr = (ULONGEST)
  187. extract_unsigned_integer (buf.data (), 8,
  188. byte_order);
  189. mlen = (unsigned short)
  190. extract_unsigned_integer (&((buf.data ())[8]), 2,
  191. byte_order);
  192. TRACE_WRITE_M_BLOCK_HEADER (writer, addr,
  193. mlen);
  194. /* The memory contents in 'M' block may be
  195. very large. Fetch the data from the target
  196. and write them into file one by one. */
  197. for (j = 0; j < mlen; )
  198. {
  199. unsigned int read_length;
  200. if (mlen - j > MAX_TRACE_UPLOAD)
  201. read_length = MAX_TRACE_UPLOAD;
  202. else
  203. read_length = mlen - j;
  204. t = target_get_raw_trace_data (buf.data (),
  205. offset + j,
  206. read_length);
  207. if (t < read_length)
  208. error (_("Failure to get requested"
  209. " trace buffer data"));
  210. TRACE_WRITE_M_BLOCK_MEMORY (writer,
  211. buf.data (),
  212. read_length);
  213. j += read_length;
  214. gotten += read_length;
  215. }
  216. break;
  217. }
  218. case 'V':
  219. {
  220. int vnum;
  221. LONGEST val;
  222. gotten
  223. = target_get_raw_trace_data (buf.data (),
  224. offset, 12);
  225. if (gotten < 12)
  226. error (_("Failure to get requested"
  227. " trace buffer data"));
  228. vnum = (int) extract_signed_integer (buf.data (),
  229. 4,
  230. byte_order);
  231. val
  232. = extract_signed_integer (&((buf.data ())[4]),
  233. 8, byte_order);
  234. TRACE_WRITE_V_BLOCK (writer, vnum, val);
  235. }
  236. break;
  237. default:
  238. error (_("Unknown block type '%c' (0x%x) in"
  239. " trace frame"),
  240. block_type, block_type);
  241. }
  242. block += gotten;
  243. offset += gotten;
  244. }
  245. }
  246. else
  247. offset += gotten;
  248. writer->ops->frame_ops->end (writer);
  249. }
  250. }
  251. writer->ops->end (writer);
  252. }
  253. static void
  254. tsave_command (const char *args, int from_tty)
  255. {
  256. int target_does_save = 0;
  257. char **argv;
  258. char *filename = NULL;
  259. int generate_ctf = 0;
  260. if (args == NULL)
  261. error_no_arg (_("file in which to save trace data"));
  262. gdb_argv built_argv (args);
  263. argv = built_argv.get ();
  264. for (; *argv; ++argv)
  265. {
  266. if (strcmp (*argv, "-r") == 0)
  267. target_does_save = 1;
  268. else if (strcmp (*argv, "-ctf") == 0)
  269. generate_ctf = 1;
  270. else if (**argv == '-')
  271. error (_("unknown option `%s'"), *argv);
  272. else
  273. filename = *argv;
  274. }
  275. if (!filename)
  276. error_no_arg (_("file in which to save trace data"));
  277. if (generate_ctf)
  278. trace_save_ctf (filename, target_does_save);
  279. else
  280. trace_save_tfile (filename, target_does_save);
  281. if (from_tty)
  282. gdb_printf (_("Trace data saved to %s '%s'.\n"),
  283. generate_ctf ? "directory" : "file", filename);
  284. }
  285. /* Save the trace data to file FILENAME of tfile format. */
  286. void
  287. trace_save_tfile (const char *filename, int target_does_save)
  288. {
  289. trace_file_writer_up writer (tfile_trace_file_writer_new ());
  290. trace_save (filename, writer.get (), target_does_save);
  291. }
  292. /* Save the trace data to dir DIRNAME of ctf format. */
  293. void
  294. trace_save_ctf (const char *dirname, int target_does_save)
  295. {
  296. trace_file_writer_up writer (ctf_trace_file_writer_new ());
  297. trace_save (dirname, writer.get (), target_does_save);
  298. }
  299. /* Fetch register data from tracefile, shared for both tfile and
  300. ctf. */
  301. void
  302. tracefile_fetch_registers (struct regcache *regcache, int regno)
  303. {
  304. struct gdbarch *gdbarch = regcache->arch ();
  305. struct tracepoint *tp = get_tracepoint (get_tracepoint_number ());
  306. int regn;
  307. /* We get here if no register data has been found. Mark registers
  308. as unavailable. */
  309. for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
  310. regcache->raw_supply (regn, NULL);
  311. /* We can often usefully guess that the PC is going to be the same
  312. as the address of the tracepoint. */
  313. if (tp == NULL || tp->loc == NULL)
  314. return;
  315. /* But don't try to guess if tracepoint is multi-location... */
  316. if (tp->loc->next)
  317. {
  318. warning (_("Tracepoint %d has multiple "
  319. "locations, cannot infer $pc"),
  320. tp->number);
  321. return;
  322. }
  323. /* ... or does while-stepping. */
  324. else if (tp->step_count > 0)
  325. {
  326. warning (_("Tracepoint %d does while-stepping, "
  327. "cannot infer $pc"),
  328. tp->number);
  329. return;
  330. }
  331. /* Guess what we can from the tracepoint location. */
  332. gdbarch_guess_tracepoint_registers (gdbarch, regcache,
  333. tp->loc->address);
  334. }
  335. /* This is the implementation of target_ops method to_has_all_memory. */
  336. bool
  337. tracefile_target::has_all_memory ()
  338. {
  339. return 1;
  340. }
  341. /* This is the implementation of target_ops method to_has_memory. */
  342. bool
  343. tracefile_target::has_memory ()
  344. {
  345. return 1;
  346. }
  347. /* This is the implementation of target_ops method to_has_stack.
  348. The target has a stack when GDB has already selected one trace
  349. frame. */
  350. bool
  351. tracefile_target::has_stack ()
  352. {
  353. return get_traceframe_number () != -1;
  354. }
  355. /* This is the implementation of target_ops method to_has_registers.
  356. The target has registers when GDB has already selected one trace
  357. frame. */
  358. bool
  359. tracefile_target::has_registers ()
  360. {
  361. return get_traceframe_number () != -1;
  362. }
  363. /* This is the implementation of target_ops method to_thread_alive.
  364. tracefile has one thread faked by GDB. */
  365. bool
  366. tracefile_target::thread_alive (ptid_t ptid)
  367. {
  368. return 1;
  369. }
  370. /* This is the implementation of target_ops method to_get_trace_status.
  371. The trace status for a file is that tracing can never be run. */
  372. int
  373. tracefile_target::get_trace_status (struct trace_status *ts)
  374. {
  375. /* Other bits of trace status were collected as part of opening the
  376. trace files, so nothing to do here. */
  377. return -1;
  378. }
  379. void _initialize_tracefile ();
  380. void
  381. _initialize_tracefile ()
  382. {
  383. add_com ("tsave", class_trace, tsave_command, _("\
  384. Save the trace data to a file.\n\
  385. Use the '-ctf' option to save the data to CTF format.\n\
  386. Use the '-r' option to direct the target to save directly to the file,\n\
  387. using its own filesystem."));
  388. }