pahole: Describe expected use of 'default' in the man page
[dwarves.git] / pdwtags.c
blob5b622181f472a25dc5796a05fa1f9c1b1d889a6f
1 /*
2 SPDX-License-Identifier: GPL-2.0-only
4 Copyright (C) 2007-2016 Arnaldo Carvalho de Melo <acme@kernel.org>
5 */
7 #include <argp.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <malloc.h>
12 #include "dwarves.h"
13 #include "dutil.h"
15 static struct conf_fprintf conf = {
16 .emit_stats = 1,
19 static void emit_tag(struct tag *tag, uint32_t tag_id, struct cu *cu)
21 printf("/* %d */\n", tag_id);
23 if (tag__is_struct(tag))
24 class__find_holes(tag__class(tag));
26 if (tag->tag == DW_TAG_base_type) {
27 char bf[64];
28 const char *name = base_type__name(tag__base_type(tag), bf, sizeof(bf));
30 if (name == NULL)
31 printf("anonymous base_type\n");
32 else
33 puts(name);
34 } else if (tag__is_pointer(tag))
35 printf(" /* pointer to %lld */\n", (unsigned long long)tag->type);
36 else
37 tag__fprintf(tag, cu, &conf, stdout);
39 printf(" /* size: %zd */\n\n", tag__size(tag, cu));
42 static int cu__emit_tags(struct cu *cu)
44 uint32_t i;
45 struct tag *tag;
47 puts("/* Types: */\n");
48 cu__for_each_type(cu, i, tag)
49 emit_tag(tag, i, cu);
51 puts("/* Functions: */\n");
52 conf.no_semicolon = true;
53 struct function *function;
54 cu__for_each_function(cu, i, function) {
55 tag__fprintf(function__tag(function), cu, &conf, stdout);
56 putchar('\n');
57 lexblock__fprintf(&function->lexblock, cu, function, 0,
58 &conf, stdout);
59 printf(" /* size: %zd */\n\n",
60 tag__size(function__tag(function), cu));
62 conf.no_semicolon = false;
64 puts("\n\n/* Variables: */\n");
65 cu__for_each_variable(cu, i, tag) {
66 tag__fprintf(tag, cu, NULL, stdout);
67 printf(" /* size: %zd */\n\n", tag__size(tag, cu));
70 puts("\n\n/* Constants: */\n");
71 cu__for_each_constant(cu, i, tag) {
72 tag__fprintf(tag, cu, NULL, stdout);
73 printf(" /* size: %zd */\n\n", tag__size(tag, cu));
76 return 0;
79 static enum load_steal_kind pdwtags_stealer(struct cu *cu,
80 struct conf_load *conf_load __maybe_unused,
81 void *thr_data __maybe_unused)
83 cu__emit_tags(cu);
84 return LSK__DELETE;
87 static struct conf_load pdwtags_conf_load = {
88 .steal = pdwtags_stealer,
89 .conf_fprintf = &conf,
92 /* Name and version of program. */
93 ARGP_PROGRAM_VERSION_HOOK_DEF = dwarves_print_version;
95 static const struct argp_option pdwtags__options[] = {
97 .name = "format_path",
98 .key = 'F',
99 .arg = "FORMAT_LIST",
100 .doc = "List of debugging formats to try"
103 .key = 'V',
104 .name = "verbose",
105 .doc = "show details",
108 .name = NULL,
112 static error_t pdwtags__options_parser(int key, char *arg __maybe_unused,
113 struct argp_state *state)
115 switch (key) {
116 case ARGP_KEY_INIT:
117 if (state->child_inputs != NULL)
118 state->child_inputs[0] = state->input;
119 break;
120 case 'F': pdwtags_conf_load.format_path = arg; break;
121 case 'V': conf.show_decl_info = 1; break;
122 default: return ARGP_ERR_UNKNOWN;
124 return 0;
127 static const char pdwtags__args_doc[] = "FILE";
129 static struct argp pdwtags__argp = {
130 .options = pdwtags__options,
131 .parser = pdwtags__options_parser,
132 .args_doc = pdwtags__args_doc,
135 int main(int argc, char *argv[])
137 int remaining, rc = EXIT_FAILURE, err;
138 struct cus *cus = cus__new();
140 if (dwarves__init() || cus == NULL) {
141 fputs("pwdtags: insufficient memory\n", stderr);
142 goto out;
145 dwarves__resolve_cacheline_size(&pdwtags_conf_load, 0);
147 if (argp_parse(&pdwtags__argp, argc, argv, 0, &remaining, NULL) ||
148 remaining == argc) {
149 argp_help(&pdwtags__argp, stderr, ARGP_HELP_SEE, argv[0]);
150 goto out;
153 err = cus__load_files(cus, &pdwtags_conf_load, argv + remaining);
154 if (err == 0) {
155 rc = EXIT_SUCCESS;
156 goto out;
159 cus__fprintf_load_files_err(cus, "pdwtags", argv + remaining, err, stderr);
160 out:
161 cus__delete(cus);
162 dwarves__exit();
163 return rc;