satyr.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. satyr.c
  3. Copyright (C) 2010 Red Hat, Inc.
  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 2 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 along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "gdb/stacktrace.h"
  17. #include "gdb/thread.h"
  18. #include "gdb/frame.h"
  19. #include "utils.h"
  20. #include "location.h"
  21. #include "strbuf.h"
  22. #include "cluster.h"
  23. #include "normalize.h"
  24. #include "report.h"
  25. #include "abrt.h"
  26. #include "config.h"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <argp.h>
  31. #include <sysexits.h>
  32. #include <assert.h>
  33. #include <libgen.h>
  34. static char *g_program_name;
  35. static void
  36. help()
  37. {
  38. printf("Usage: %s COMMAND [OPTION...]\n", g_program_name);
  39. printf("%s -- Automatic problem management with anonymous reports\n\n", g_program_name);
  40. puts("The following commands are available:");
  41. puts(" abrt-print-report-from-dir Create report from an ABRT directory");
  42. puts(" abrt-report-dir Create report from an ABRT directory and");
  43. puts(" send it to a server");
  44. puts(" abrt-create-core-stacktrace Create core stacktrace from an ABRT directory");
  45. puts(" debug Commands for debugging and development support");
  46. }
  47. static void
  48. short_usage_and_exit()
  49. {
  50. printf("Usage: %s COMMAND [OPTION...]\n", g_program_name);
  51. printf("Try `%s --help' or `%s --usage' for more information.\n", g_program_name, g_program_name);
  52. exit(1);
  53. }
  54. static void
  55. usage()
  56. {
  57. printf("Usage: %s --version\n", g_program_name);
  58. printf("Usage: %s abrt-print-report-from-dir DIR [OPTION...]\n", g_program_name);
  59. printf("Usage: %s abrt-report-dir DIR URL [OPTION...]\n", g_program_name);
  60. printf("Usage: %s abrt-create-core-stacktrace DIR [OPTION...]\n", g_program_name);
  61. printf("Usage: %s debug COMMAND [OPTION...]\n", g_program_name);
  62. }
  63. static void
  64. version()
  65. {
  66. printf("Satyr version %s\n", PACKAGE_VERSION);
  67. }
  68. static void
  69. abrt_print_report_from_dir(int argc, char **argv)
  70. {
  71. /* Require ABRT problem directory path. */
  72. if (argc == 0)
  73. {
  74. fprintf(stderr, "Missing ABRT problem directory path.\n");
  75. short_usage_and_exit();
  76. }
  77. char *error_message;
  78. bool success = sr_abrt_print_report_from_dir(argv[0],
  79. &error_message);
  80. if (!success)
  81. {
  82. fprintf(stderr, "%s\n", error_message);
  83. free(error_message);
  84. exit(1);
  85. }
  86. }
  87. static void
  88. abrt_report_dir(int argc, char **argv)
  89. {
  90. fprintf(stderr, "Not implemented.\n");
  91. }
  92. static void
  93. abrt_create_core_stacktrace(int argc, char **argv)
  94. {
  95. /* Require ABRT problem directory path. */
  96. if (argc == 0)
  97. {
  98. fprintf(stderr, "Missing ABRT problem directory path.\n");
  99. short_usage_and_exit();
  100. }
  101. bool hash_fingerprints = true;
  102. if (argc > 1 &&
  103. 0 == strcmp(argv[0], "-r"))
  104. {
  105. hash_fingerprints = false;
  106. argc--;
  107. argv++;
  108. }
  109. char *error_message;
  110. bool success = sr_abrt_create_core_stacktrace(argv[0],
  111. hash_fingerprints,
  112. &error_message);
  113. if (!success)
  114. {
  115. fprintf(stderr, "%s\n", error_message);
  116. free(error_message);
  117. exit(1);
  118. }
  119. }
  120. static void
  121. debug_normalize(int argc, char **argv)
  122. {
  123. /* Debug normalize requires a filename. */
  124. if (argc == 0)
  125. {
  126. fprintf(stderr, "Missing file name.\n");
  127. short_usage_and_exit();
  128. }
  129. char *error_message;
  130. char *text = sr_file_to_string(argv[0], &error_message);
  131. if (!text)
  132. {
  133. fprintf(stderr, "%s\n", error_message);
  134. exit(1);
  135. }
  136. struct sr_gdb_thread *thread = sr_gdb_thread_new();
  137. char *cur = text;
  138. /* Parse the text. */
  139. while (*cur)
  140. {
  141. struct sr_gdb_frame *frame = sr_gdb_frame_new();
  142. /* SYMBOL */
  143. /* may contain spaces! RHBZ #857475 */
  144. /* if ( character is found, we do not stop on white space, but on ) */
  145. /* parentheses may be nested, we need to consider the depth */
  146. sr_skip_whitespace(cur);
  147. char *end;
  148. for (end = cur; *end && *end != ' '; ++end)
  149. {
  150. }
  151. frame->function_name = sr_strndup(cur, end - cur);
  152. cur = end;
  153. sr_skip_whitespace(cur);
  154. for (end = cur; *end && *end != '\n'; ++end)
  155. {
  156. }
  157. frame->library_name = sr_strndup(cur, end - cur);
  158. cur = end;
  159. sr_skip_whitespace(cur);
  160. for (end = cur; *end && *end != '\n'; ++end)
  161. {
  162. }
  163. frame->source_file = sr_strndup(cur, end - cur);
  164. /* Skip the rest of the line. */
  165. while (*cur && *cur++ != '\n')
  166. {
  167. }
  168. thread->frames = sr_gdb_frame_append(thread->frames,
  169. frame);
  170. }
  171. sr_normalize_gdb_thread(thread);
  172. struct sr_strbuf *strbuf = sr_strbuf_new();
  173. sr_gdb_thread_append_to_str(thread, strbuf, false);
  174. puts(strbuf->buf);
  175. }
  176. static void
  177. debug(int argc, char **argv)
  178. {
  179. /* Debug command requires a subcommand. */
  180. if (argc == 0)
  181. {
  182. fprintf(stderr, "Missing debug command.\n");
  183. short_usage_and_exit();
  184. }
  185. if (0 == strcmp(argv[0], "normalize"))
  186. debug_normalize(argc - 1, argv + 1);
  187. else
  188. {
  189. fprintf(stderr, "Unknown debug subcommand.\n");
  190. short_usage_and_exit();
  191. exit(1);
  192. }
  193. }
  194. int
  195. main(int argc, char **argv)
  196. {
  197. g_program_name = basename(argv[0]);
  198. if (argc == 1)
  199. short_usage_and_exit();
  200. if (0 == strcmp(argv[1], "--help"))
  201. help();
  202. else if (0 == strcmp(argv[1], "--usage"))
  203. usage();
  204. else if (0 == strcmp(argv[1], "--version"))
  205. version();
  206. else if (0 == strcmp(argv[1], "abrt-print-report-from-dir"))
  207. abrt_print_report_from_dir(argc - 2, argv + 2);
  208. else if (0 == strcmp(argv[1], "abrt-report-dir"))
  209. abrt_report_dir(argc - 2, argv + 2);
  210. else if (0 == strcmp(argv[1], "abrt-create-core-stacktrace"))
  211. abrt_create_core_stacktrace(argc - 2, argv + 2);
  212. else if (0 == strcmp(argv[1], "debug"))
  213. debug(argc - 2, argv + 2);
  214. else
  215. {
  216. fprintf(stderr, "Unknown command.\n");
  217. short_usage_and_exit();
  218. return 1;
  219. }
  220. return 0;
  221. }