pcprofiledump.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Dump information generated by PC profiling.
  2. Copyright (C) 1999-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* This is mainly an example. It shows how programs which want to use
  16. the information should read the file. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <argp.h>
  21. #include <byteswap.h>
  22. #include <errno.h>
  23. #include <error.h>
  24. #include <fcntl.h>
  25. #include <inttypes.h>
  26. #include <libintl.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <stdint.h>
  31. #include "../version.h"
  32. #define PACKAGE _libc_intl_domainname
  33. #ifndef _
  34. # define _(Str) gettext (Str)
  35. #endif
  36. #ifndef N_
  37. # define N_(Str) Str
  38. #endif
  39. /* Definitions of arguments for argp functions. */
  40. static const struct argp_option options[] =
  41. {
  42. { "unbuffered", 'u', NULL, 0, N_("Don't buffer output") },
  43. { NULL, 0, NULL, 0, NULL }
  44. };
  45. /* Short description of program. */
  46. static const char doc[] = N_("Dump information generated by PC profiling.");
  47. /* Strings for arguments in help texts. */
  48. static const char args_doc[] = N_("[FILE]");
  49. /* Function to print some extra text in the help message. */
  50. static char *more_help (int key, const char *text, void *input);
  51. /* Prototype for option handler. */
  52. static error_t parse_opt (int key, char *arg, struct argp_state *state);
  53. /* Name and version of program. */
  54. static void print_version (FILE *stream, struct argp_state *state);
  55. void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
  56. /* Data structure to communicate with argp functions. */
  57. static struct argp argp =
  58. {
  59. options, parse_opt, args_doc, doc, NULL, more_help
  60. };
  61. /* Try to read SIZE bytes from FD and store them on BUF. Terminate
  62. the process upon read error. Also terminate the process if less
  63. than SIZE bytes are remaining in the file. If !IN_HEADER, do not
  64. terminate the process if the end of the file is encountered
  65. immediately, before any bytes are read.
  66. Returns true if SIZE bytes have been read, and false if no bytes
  67. have been read due to an end-of-file condition. */
  68. static bool
  69. read_exactly (int fd, void *buffer, size_t size, bool in_header)
  70. {
  71. char *p = buffer;
  72. char *end = p + size;
  73. while (p < end)
  74. {
  75. ssize_t ret = TEMP_FAILURE_RETRY (read (fd, p, end - p));
  76. if (ret < 0)
  77. {
  78. if (in_header)
  79. error (EXIT_FAILURE, errno, _("cannot read header"));
  80. else
  81. error (EXIT_FAILURE, errno, _("cannot read pointer pair"));
  82. }
  83. if (ret == 0)
  84. {
  85. if (p == buffer && !in_header)
  86. /* Nothing has been read. */
  87. return false;
  88. if (in_header)
  89. error (EXIT_FAILURE, 0, _("unexpected end of file in header"));
  90. else
  91. error (EXIT_FAILURE, 0,
  92. _("unexpected end of file in pointer pair"));
  93. }
  94. p += ret;
  95. }
  96. return true;
  97. }
  98. int
  99. main (int argc, char *argv[])
  100. {
  101. /* Set locale via LC_ALL. */
  102. setlocale (LC_ALL, "");
  103. /* Set the text message domain. */
  104. textdomain (PACKAGE);
  105. /* Parse and process arguments. */
  106. int remaining;
  107. argp_parse (&argp, argc, argv, 0, &remaining, NULL);
  108. int fd;
  109. if (remaining == argc)
  110. fd = STDIN_FILENO;
  111. else if (remaining + 1 != argc)
  112. {
  113. argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
  114. program_invocation_short_name);
  115. exit (1);
  116. }
  117. else
  118. {
  119. /* Open the given file. */
  120. fd = open (argv[remaining], O_RDONLY);
  121. if (fd == -1)
  122. error (EXIT_FAILURE, errno, _("cannot open input file"));
  123. }
  124. /* Read the first 4-byte word. It contains the information about
  125. the word size and the endianness. */
  126. uint32_t word;
  127. read_exactly (fd, &word, sizeof (word), true);
  128. /* Check whether we have to swap the byte order. */
  129. int must_swap = (word & 0x0fffffff) == bswap_32 (0xdeb00000);
  130. if (must_swap)
  131. word = bswap_32 (word);
  132. /* We have two loops, one for 32 bit pointers, one for 64 bit pointers. */
  133. if (word == 0xdeb00004)
  134. {
  135. uint32_t ptrs[2];
  136. while (1)
  137. {
  138. if (!read_exactly (fd, ptrs, sizeof (ptrs), false))
  139. break;
  140. printf ("this = %#010" PRIx32 ", caller = %#010" PRIx32 "\n",
  141. must_swap ? bswap_32 (ptrs[0]) : ptrs[0],
  142. must_swap ? bswap_32 (ptrs[1]) : ptrs[1]);
  143. }
  144. }
  145. else if (word == 0xdeb00008)
  146. {
  147. uint64_t ptrs[2];
  148. while (1)
  149. {
  150. if (!read_exactly (fd, ptrs, sizeof (ptrs), false))
  151. break;
  152. printf ("this = %#018" PRIx64 ", caller = %#018" PRIx64 "\n",
  153. must_swap ? bswap_64 (ptrs[0]) : ptrs[0],
  154. must_swap ? bswap_64 (ptrs[1]) : ptrs[1]);
  155. }
  156. }
  157. else
  158. /* This should not happen. */
  159. error (EXIT_FAILURE, 0, _("invalid pointer size"));
  160. /* Clean up. */
  161. close (fd);
  162. return 0;
  163. }
  164. static error_t
  165. parse_opt (int key, char *arg, struct argp_state *state)
  166. {
  167. switch (key)
  168. {
  169. case 'u':
  170. setbuf (stdout, NULL);
  171. break;
  172. default:
  173. return ARGP_ERR_UNKNOWN;
  174. }
  175. return 0;
  176. }
  177. static char *
  178. more_help (int key, const char *text, void *input)
  179. {
  180. char *tp = NULL;
  181. switch (key)
  182. {
  183. case ARGP_KEY_HELP_EXTRA:
  184. /* We print some extra information. */
  185. if (asprintf (&tp, gettext ("\
  186. For bug reporting instructions, please see:\n\
  187. %s.\n"), REPORT_BUGS_TO) < 0)
  188. return NULL;
  189. return tp;
  190. default:
  191. break;
  192. }
  193. return (char *) text;
  194. }
  195. /* Print the version information. */
  196. static void
  197. print_version (FILE *stream, struct argp_state *state)
  198. {
  199. fprintf (stream, "pcprofiledump %s%s\n", PKGVERSION, VERSION);
  200. fprintf (stream, gettext ("\
  201. Copyright (C) %s Free Software Foundation, Inc.\n\
  202. This is free software; see the source for copying conditions. There is NO\n\
  203. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
  204. "), "2024");
  205. fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
  206. }