| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#include <sysprof.h> |
| 4 |
|
| 5 |
#include "sysprof-document-private.h" |
| 6 |
|
| 7 |
static GMainLoop *main_loop; |
| 8 |
static gboolean silent; |
| 9 |
static gboolean no_bundled; |
| 10 |
static gboolean debuginfod; |
| 11 |
static char **debug_dirs; |
| 12 |
static char *kallsyms_path; |
| 13 |
static const GOptionEntry entries[] = { |
| 14 |
{ "no-bundled", 'b', 0, G_OPTION_ARG_NONE, &no_bundled, "Ignore symbols bundled in capture file" }, |
| 15 |
{ "silent", 's', 0, G_OPTION_ARG_NONE, &silent, "Do not print symbol information" }, |
| 16 |
{ "debug-dir", 'd', 0, G_OPTION_ARG_FILENAME_ARRAY, &debug_dirs, "Specify external debug directory, may be repeated" }, |
| 17 |
{ "kallsyms", 'k', 0, G_OPTION_ARG_FILENAME, &kallsyms_path, "Specify path to kallsyms for kernel symbolizing" }, |
| 18 |
{ "debuginfod", 'D', 0, G_OPTION_ARG_NONE, &debuginfod, "Use debuginfod for automatically fetching debug symbols" }, |
| 19 |
{ 0 } |
| 20 |
}; |
| 21 |
|
| 22 |
static void |
| 23 |
load_cb (GObject *object, |
| 24 |
GAsyncResult *result, |
| 25 |
gpointer user_data) |
| 26 |
{ |
| 27 |
SysprofDocumentLoader *loader = (SysprofDocumentLoader *)object; |
| 28 |
g_autoptr(SysprofDocument) document = NULL; |
| 29 |
g_autoptr(GListModel) traceables = NULL; |
| 30 |
g_autoptr(GError) error = NULL; |
| 31 |
g_autoptr(GString) str = NULL; |
| 32 |
SysprofSymbol *symbols[128]; |
| 33 |
guint n_symbols; |
| 34 |
guint n_items; |
| 35 |
|
| 36 |
g_assert (SYSPROF_IS_DOCUMENT_LOADER (loader)); |
| 37 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 38 |
|
| 39 |
if (!(document = sysprof_document_loader_load_finish (loader, result, &error))) |
| 40 |
g_error ("Failed to load document: %s", error->message); |
| 41 |
|
| 42 |
traceables = sysprof_document_list_traceables (document); |
| 43 |
n_items = g_list_model_get_n_items (traceables); |
| 44 |
|
| 45 |
if (silent) |
| 46 |
{ |
| 47 |
g_main_loop_quit (main_loop); |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
str = g_string_new (""); |
| 52 |
|
| 53 |
for (guint i = 0; i < n_items; i++) |
| 54 |
{ |
| 55 |
g_autoptr(SysprofDocumentTraceable) traceable = g_list_model_get_item (traceables, i); |
| 56 |
|
| 57 |
str->len = 0; |
| 58 |
str->str[0] = 0; |
| 59 |
|
| 60 |
g_assert (traceable != NULL); |
| 61 |
g_assert (SYSPROF_IS_DOCUMENT_TRACEABLE (traceable)); |
| 62 |
|
| 63 |
n_symbols = sysprof_document_symbolize_traceable (document, |
| 64 |
traceable, |
| 65 |
symbols, |
| 66 |
G_N_ELEMENTS (symbols), |
| 67 |
NULL); |
| 68 |
|
| 69 |
g_print ("%s depth=%u pid=%u\n", |
| 70 |
G_OBJECT_TYPE_NAME (traceable), |
| 71 |
n_symbols, |
| 72 |
sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (traceable))); |
| 73 |
|
| 74 |
for (guint j = 0; j < n_symbols; j++) |
| 75 |
{ |
| 76 |
SysprofSymbol *symbol = symbols[j]; |
| 77 |
const char *name; |
| 78 |
const char *path; |
| 79 |
const char *nick; |
| 80 |
|
| 81 |
if (symbol != NULL) |
| 82 |
{ |
| 83 |
name = sysprof_symbol_get_name (symbol); |
| 84 |
path = sysprof_symbol_get_binary_path (symbol); |
| 85 |
nick = sysprof_symbol_get_binary_nick (symbol); |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
name = path = nick = NULL; |
| 90 |
} |
| 91 |
|
| 92 |
g_string_append_printf (str, |
| 93 |
" %02d: 0x%"G_GINT64_MODIFIER"x:", |
| 94 |
j, |
| 95 |
sysprof_document_traceable_get_stack_address (traceable, j)); |
| 96 |
|
| 97 |
if (name) |
| 98 |
g_string_append_printf (str, " %s", name); |
| 99 |
|
| 100 |
if (path) |
| 101 |
g_string_append_printf (str, " [%s]", path); |
| 102 |
|
| 103 |
if (nick) |
| 104 |
g_string_append_printf (str, " (%s)", nick); |
| 105 |
|
| 106 |
g_string_append_c (str, '\n'); |
| 107 |
} |
| 108 |
|
| 109 |
g_string_append (str, " ================\n"); |
| 110 |
|
| 111 |
write (STDOUT_FILENO, str->str, str->len); |
| 112 |
} |
| 113 |
|
| 114 |
g_print ("Document symbolized\n"); |
| 115 |
|
| 116 |
g_main_loop_quit (main_loop); |
| 117 |
} |
| 118 |
|
| 119 |
int |
| 120 |
main (int argc, |
| 121 |
char *argv[]) |
| 122 |
{ |
| 123 |
g_autoptr(SysprofDocumentLoader) loader = NULL; |
| 124 |
g_autoptr(GOptionContext) context = NULL; |
| 125 |
g_autoptr(GError) error = NULL; |
| 126 |
|
| 127 |
main_loop = g_main_loop_new (NULL, FALSE); |
| 128 |
context = g_option_context_new ("- test document symbolization"); |
| 129 |
g_option_context_add_main_entries (context, entries, NULL); |
| 130 |
|
| 131 |
if (!g_option_context_parse (context, &argc, &argv, &error)) |
| 132 |
{ |
| 133 |
g_printerr ("%s\n", error->message); |
| 134 |
return 1; |
| 135 |
} |
| 136 |
|
| 137 |
if (argc != 2 || !g_file_test (argv[1], G_FILE_TEST_EXISTS)) |
| 138 |
{ |
| 139 |
g_printerr ("usage: %s CAPTURE_FILE\n", argv[0]); |
| 140 |
return 1; |
| 141 |
} |
| 142 |
|
| 143 |
loader = sysprof_document_loader_new (argv[1]); |
| 144 |
|
| 145 |
if (TRUE) |
| 146 |
{ |
| 147 |
g_autoptr(SysprofMultiSymbolizer) multi = sysprof_multi_symbolizer_new (); |
| 148 |
SysprofSymbolizer *elf = sysprof_elf_symbolizer_new (); |
| 149 |
g_autoptr(GFile) kallsyms_file = kallsyms_path ? g_file_new_for_path (kallsyms_path) : NULL; |
| 150 |
GFileInputStream *kallsyms_stream = kallsyms_file ? g_file_read (kallsyms_file, NULL, NULL) : NULL; |
| 151 |
|
| 152 |
if (debug_dirs) |
| 153 |
sysprof_elf_symbolizer_set_external_debug_dirs (SYSPROF_ELF_SYMBOLIZER (elf), |
| 154 |
(const char * const *)debug_dirs); |
| 155 |
|
| 156 |
if (!no_bundled) |
| 157 |
sysprof_multi_symbolizer_take (multi, sysprof_bundled_symbolizer_new ()); |
| 158 |
|
| 159 |
if (kallsyms_stream == NULL) |
| 160 |
sysprof_multi_symbolizer_take (multi, sysprof_kallsyms_symbolizer_new ()); |
| 161 |
else |
| 162 |
sysprof_multi_symbolizer_take (multi, sysprof_kallsyms_symbolizer_new_for_symbols (G_INPUT_STREAM (kallsyms_stream))); |
| 163 |
|
| 164 |
sysprof_multi_symbolizer_take (multi, elf); |
| 165 |
sysprof_multi_symbolizer_take (multi, sysprof_jitmap_symbolizer_new ()); |
| 166 |
|
| 167 |
#if HAVE_DEBUGINFOD |
| 168 |
if (debuginfod) |
| 169 |
{ |
| 170 |
g_autoptr(SysprofSymbolizer) debuginfod_symbolizer = NULL; |
| 171 |
g_autoptr(GError) debuginfod_error = NULL; |
| 172 |
|
| 173 |
if (!(debuginfod_symbolizer = sysprof_debuginfod_symbolizer_new (&debuginfod_error))) |
| 174 |
g_warning ("Failed to create debuginfod symbolizer: %s", debuginfod_error->message); |
| 175 |
else |
| 176 |
sysprof_multi_symbolizer_take (multi, g_steal_pointer (&debuginfod_symbolizer)); |
| 177 |
} |
| 178 |
#endif |
| 179 |
|
| 180 |
sysprof_document_loader_set_symbolizer (loader, SYSPROF_SYMBOLIZER (multi)); |
| 181 |
} |
| 182 |
|
| 183 |
sysprof_document_loader_load_async (loader, NULL, load_cb, NULL); |
| 184 |
g_main_loop_run (main_loop); |
| 185 |
|
| 186 |
return 0; |
| 187 |
} |