Debian Codegraph @

src/bin/dwarfexample/showsectiongroups.c

raw
1 /* David Anderson. 2022. This small program is hereby
2 placed into the public domain to be copied or
3 used by anyone for any purpose.
4
5 The C source is src/bin/dwarfexample/showsectiongroups.c
6 */
7 /*! @file showsectiongroups.c
8 @defgroup showsecgroups A simple report on section groups.
9 @brief Section groups are for Split DWARF
10
11 @code
12
13 The C source is src/bin/dwarfexample/showsectiongroups.c
14 */
15
16 #include <config.h>
17
18 #include <stdio.h> /* printf() */
19 #include <stdlib.h> /* calloc() exit() free() */
20 #include <string.h> /* strcmp() */
21
22 #include "dwarf.h"
23 #include "libdwarf.h"
24 #define FALSE 0
25
26 char trueoutpath[2000];
27
28 static int
29 one_file_show_groups(char *path_in,
30 char *shortpath,
31 int chosengroup)
32 {
33 int res = 0;
34 Dwarf_Debug dbg = 0;
35 Dwarf_Error error = 0;
36 char * path = 0;
37 Dwarf_Unsigned section_count = 0;
38 Dwarf_Unsigned group_count = 0;
39 Dwarf_Unsigned selected_group = 0;
40 Dwarf_Unsigned map_entry_count = 0;
41 Dwarf_Unsigned * group_numbers_array = 0;
42 Dwarf_Unsigned * sec_numbers_array = 0;
43 const char ** sec_names_array = 0;
44 Dwarf_Unsigned i = 0;
45 const char *grpname = 0;
46
47 switch(chosengroup) {
48 case DW_GROUPNUMBER_ANY:
49 grpname="DW_GROUPNUMBER_ANY";
50 break;
51 case DW_GROUPNUMBER_BASE:
52 grpname="DW_GROUPNUMBER_BASE";
53 break;
54 case DW_GROUPNUMBER_DWO:
55 grpname="DW_GROUPNUMBER_DWO";
56 break;
57 default:
58 grpname = "";
59 }
60 path = path_in;
61 res = dwarf_init_path(path,
62 0,0,
63 chosengroup,
64 0,0, &dbg, &error);
65 if (res == DW_DLV_ERROR) {
66 printf("Error from libdwarf opening \"%s\": %s\n",
67 shortpath, dwarf_errmsg(error));
68 dwarf_dealloc_error(dbg,error);
69 error = 0;
70 return res;
71 }
72 if (res == DW_DLV_NO_ENTRY) {
73 printf("There is no such file as \"%s\" "
74 "or the selected group %d (%s) does "
75 "not appear in the file\n",
76 shortpath,chosengroup,grpname);
77 return DW_DLV_NO_ENTRY;
78 }
79
80 res = dwarf_sec_group_sizes(dbg, &section_count,
81 &group_count, &selected_group, &map_entry_count,
82 &error);
83 if (res == DW_DLV_ERROR) {
84 printf("Error from libdwarf getting group "
85 "sizes \"%s\": %s\n",
86 shortpath, dwarf_errmsg(error));
87 dwarf_dealloc_error(dbg,error);
88 error = 0;
89 dwarf_finish(dbg);
90 return res;
91 }
92 if (res == DW_DLV_NO_ENTRY) {
93 printf("Impossible. libdwarf claims no groups from %s\n",
94 shortpath);
95 dwarf_finish(dbg);
96 return res;
97 }
98 printf("Group Map data sizes\n");
99 printf(" requested group : %4lu\n",
100 (unsigned long)chosengroup);
101 printf(" section count : %4lu\n",
102 (unsigned long)section_count);
103 printf(" group count : %4lu\n",
104 (unsigned long)group_count);
105 printf(" selected group : %4lu\n",
106 (unsigned long)selected_group);
107 printf(" map entry count : %4lu\n",
108 (unsigned long)map_entry_count);
109
110 group_numbers_array = (Dwarf_Unsigned *)calloc(map_entry_count,
111 sizeof(Dwarf_Unsigned));
112 if (!group_numbers_array) {
113 printf("Error calloc fail, group count %lu\n",
114 (unsigned long)group_count);
115 dwarf_finish(dbg);
116 return DW_DLV_ERROR;
117 }
118 sec_numbers_array = (Dwarf_Unsigned *)calloc(map_entry_count,
119 sizeof(Dwarf_Unsigned));
120 if (!sec_numbers_array) {
121 free(group_numbers_array);
122 printf("Error calloc fail sec numbers, section count %lu\n",
123 (unsigned long)section_count);
124 dwarf_finish(dbg);
125 return DW_DLV_ERROR;
126 }
127 sec_names_array = (const char **)calloc(map_entry_count,
128 sizeof(const char *));
129 if (!sec_names_array) {
130 free(sec_numbers_array);
131 free(group_numbers_array);
132 printf("Error calloc fail on names, section count %lu\n",
133 (unsigned long)section_count);
134 dwarf_finish(dbg);
135 return DW_DLV_ERROR;
136 }
137 res = dwarf_sec_group_map(dbg,map_entry_count,
138 group_numbers_array,
139 sec_numbers_array, sec_names_array,&error);
140 if (res == DW_DLV_ERROR) {
141 free(sec_names_array);
142 free(sec_numbers_array);
143 free(group_numbers_array);
144 printf("Error from libdwarf getting group detals "
145 "sizes \"%s\": %s\n",
146 shortpath, dwarf_errmsg(error));
147 dwarf_dealloc_error(dbg,error);
148 error = 0;
149 dwarf_finish(dbg);
150 return res;
151 }
152 if (res == DW_DLV_NO_ENTRY) {
153 free(sec_names_array);
154 free(sec_numbers_array);
155 free(group_numbers_array);
156 printf("Impossible. libdwarf claims details from %s\n",
157 shortpath);
158 dwarf_finish(dbg);
159 return res;
160 }
161 printf(" [index] group section \n");
162 for (i = 0; i < map_entry_count;++i) {
163 printf(" [%5lu] %4lu %4lu %s\n",
164 (unsigned long)i,
165 (unsigned long)group_numbers_array[i],
166 (unsigned long)sec_numbers_array[i],
167 sec_names_array[i]);
168 }
169 free(sec_names_array);
170 free(sec_numbers_array);
171 free(group_numbers_array);
172 dwarf_finish(dbg);
173 return DW_DLV_OK;
174 }
175
176 /* Does not return */
177 static void
178 usage(void)
179 {
180 printf("Usage: showsectiongroups [-group <n>] "
181 "<objectfile> ...\n");
182 printf("Usage: group defaults to zero (DW_GROUPNUMBER ANY)\n");
183 exit(EXIT_FAILURE);
184 }
185
186 /* This trimming of the file path makes libdwarf regression
187 testing easier by arranging baseline output
188 not show the full path. */
189 static void
190 trimpathprefix(char *out,unsigned int outlen, char *in)
191 {
192 char *cpo = out;
193 char *cpi = in;
194 char *suffix = 0;
195 unsigned int lencopied = 0;
196 for ( ; *cpi ; ++cpi) {
197 if (*cpi == '/') {
198 suffix= cpi+1;
199 }
200 }
201 if (suffix) {
202 cpi = suffix;
203 }
204 lencopied = 0;
205 for ( ; lencopied < outlen; ++cpo,++cpi)
206 {
207 *cpo = *cpi;
208 if (! *cpi) {
209 return;
210 }
211 ++lencopied;
212 }
213 printf("FAIL copy file name: not terminated \n");
214 exit(EXIT_FAILURE);
215 }
216
217 int
218 main(int argc, char **argv)
219 {
220 int res = 0;
221 int i = 1;
222 int chosengroup = DW_GROUPNUMBER_ANY;
223 static char reportingpath[16000];
224
225 if (argc < 2) {
226 usage();
227 return 0;
228 }
229 for ( ; i < argc; ++i) {
230 char *arg = argv[i];
231 if (!strcmp(arg,"-group")) {
232 i++;
233 if (i >= argc) {
234 usage();
235 }
236 arg = argv[i];
237 chosengroup = atoi(arg);
238 /* We are ignoring errors to simplify
239 this source. Use strtol, carefully,
240 in real code. */
241 continue;
242 }
243 if (!strcmp(argv[i],"--suppress-de-alloc-tree")) {
244 /* Do nothing, ignore the argument */
245 continue;
246 }
247 trimpathprefix(reportingpath,sizeof(reportingpath),arg);
248 res = one_file_show_groups(arg,
249 reportingpath,chosengroup);
250 printf("=======done with %s, status %s\n",reportingpath,
251 (res == DW_DLV_OK)?"DW_DLV_OK":
252 (res == DW_DLV_ERROR)?"DW_DLV_ERROR":
253 "DW_DLV_NO_ENTRY");
254 printf("\n");
255 }
256 return 0;
257 }
258 /*! @endcode*/