]> granicus.if.org Git - strace/blob - mpers.awk
rtnl_neightbl: always decode struct ndt_config and struct ndt_stats
[strace] / mpers.awk
1 #!/bin/gawk
2 #
3 # Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
4 # Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5 # Copyright (c) 2015-2018 The strace developers.
6 # All rights reserved.
7 #
8 # SPDX-License-Identifier: LGPL-2.1-or-later
9
10 function array_get(array_idx, array_member, \
11                    array_return)
12 {
13         array_return = array[array_idx, array_member]
14         if ("" == array_return) {
15                 printf("%s: index [%s] without %s\n",
16                        FILENAME, array_idx, array_member) > "/dev/stderr"
17                 exit 1
18         }
19         return array_return
20 }
21 function norm_idx(idx)
22 {
23         return sprintf("%016s", idx)
24 }
25 function array_seq(array_idx)
26 {
27         if ((array_idx, "seq") in array)
28                 return array[array_idx, "seq"]
29         index_seq++
30         array[array_idx, "seq"] = index_seq
31         return index_seq
32 }
33 function enter(array_idx,
34                item)
35 {
36         if (array_idx in called) {
37                 printf("%s: index loop detected:", FILENAME) > "/dev/stderr"
38                 for (item in called)
39                         printf(" %s", item) > "/dev/stderr"
40                 print "" > "/dev/stderr"
41                 exit 1
42         }
43         called[array_idx] = 1
44 }
45 function leave(array_idx, to_return)
46 {
47         delete called[array_idx]
48         return to_return
49 }
50 function update_upper_bound(idx, val, \
51                             count)
52 {
53         count = array[idx, "count"]
54         if (count == "")
55                 count = 1
56         array[idx, "count"] = count * val
57         array[idx, "upper_bound"] = array[idx, "upper_bound"] "[" val "]"
58 }
59 function what_is(what_idx, \
60                  item, loc_diff, location, prev_location, prev_returned_size, \
61                  special, to_return, type_idx, enc, i)
62 {
63         enter(what_idx)
64         special = array_get(what_idx, "special")
65         if (special == "base_type") {
66                 enc = array_get(what_idx, "encoding")
67                 if (enc == 5) { # signed
68                         printf("int%s_t ",
69                                8 * array_get(what_idx, "byte_size"))
70                 } else if (enc == 7) { # unsigned
71                         printf("uint%s_t ",
72                                8 * array_get(what_idx, "byte_size"))
73                 } else { # float, signed/unsigned char
74                         printf("%s ", array_get(what_idx, "name"))
75                 }
76                 returned_size = array_get(what_idx, "byte_size")
77         } else if (special == "enumeration_type") {
78                 returned_size = array_get(what_idx, "byte_size")
79                 printf("uint%s_t ", 8 * returned_size)
80         } else if (special == "pointer_type") {
81                 printf("mpers_ptr_t ")
82                 returned_size = array_get(what_idx, "byte_size")
83         } else if (special == "array_type") {
84                 type_idx = array_get(what_idx, "type")
85                 what_is(type_idx)
86                 to_return = array[what_idx, "upper_bound"]
87                 if ("" == to_return)
88                         to_return = "[0]"
89                 returned_size = array[what_idx, "count"] * returned_size
90                 return leave(what_idx, to_return)
91         } else if (special == "structure_type") {
92                 print "struct {"
93                 prev_location = 0
94                 location = 0
95                 returned_size = 0
96                 prev_returned_size = 0
97                 for (i = 1; i <= parents_cnt; i += 1) {
98                         if (array_parents[aparents_keys[i]] == what_idx) {
99                                 location = array_get(aparents_keys[i], "location")
100                                 loc_diff = location - prev_location - \
101                                         prev_returned_size
102                                 if (loc_diff != 0) {
103                                         printf("unsigned char mpers_%s_%s[%s];\n",
104                                                "filler", array_seq(aparents_keys[i]), loc_diff)
105                                 }
106                                 prev_location = location
107                                 returned = what_is(aparents_keys[i])
108                                 prev_returned_size = returned_size
109                                 printf("%s%s;\n", array[aparents_keys[i], "name"], returned)
110                         }
111                 }
112                 returned_size = array_get(what_idx, "byte_size")
113                 loc_diff = returned_size - prev_location - prev_returned_size
114                 if (loc_diff != 0) {
115                         printf("unsigned char mpers_%s_%s[%s];\n",
116                                "end_filler", array_seq(item), loc_diff)
117                 }
118                 printf("} ATTRIBUTE_PACKED ")
119         } else if (special == "union_type") {
120                 print "union {"
121                 for (i = 1; i <= parents_cnt; i += 1) {
122                         if (array_parents[aparents_keys[i]] == what_idx) {
123                                 returned = what_is(aparents_keys[i])
124                                 printf("%s%s;\n", array[aparents_keys[i], "name"], returned)
125                         }
126                 }
127                 printf("} ")
128                 returned_size = array_get(what_idx, "byte_size")
129         } else if (special == "typedef") {
130                 type_idx = array_get(what_idx, "type")
131                 return leave(what_idx, what_is(type_idx))
132         } else if (special == "member") {
133                 type_idx = array_get(what_idx, "type")
134                 return leave(what_idx, what_is(type_idx))
135         } else {
136                 type_idx = array_get(what_idx, "type")
137                 what_is(type_idx)
138         }
139         return leave(what_idx, "")
140 }
141 BEGIN {
142         match(ARCH_FLAG, /[[:digit:]]+/, temparray)
143         default_pointer_size = temparray[0] / 8
144         print "#include <stdint.h>"
145 }
146 /^<[[:xdigit:]]+>/ {
147         match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches)
148         level = matches[1]
149         idx = norm_idx(matches[2])
150         array[idx, "idx"] = idx
151         parent[level] = idx
152 }
153 /^DW_AT_data_member_location/ {
154         if (!match($0, /\(DW_OP_plus_uconst:[[:space:]]+([[:digit:]]+)\)/, temparray))
155                 match($0, /([[:digit:]]+)/, temparray)
156         array[idx, "location"] = temparray[1]
157 }
158 /^DW_AT_name/ {
159         match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \
160                 temparray)
161         array_names[idx] = 1
162         array[idx, "name"] = temparray[1]
163 }
164 /^DW_AT_byte_size/ {
165         match($0, /[[:digit:]]+/, temparray)
166         array[idx, "byte_size"] = temparray[0]
167 }
168 /^DW_AT_encoding/ {
169         match($0, /[[:digit:]]+/, temparray)
170         array[idx, "encoding"] = temparray[0]
171 }
172 /^DW_AT_type/ {
173         match($0, /:[[:space:]]+<0x([[:xdigit:]]*)>$/, temparray)
174         array[idx, "type"] = norm_idx(temparray[1])
175 }
176 /^DW_AT_upper_bound/ {
177         match($0, /[[:digit:]]+/, temparray)
178         update_upper_bound(parent[level - 1], temparray[0] + 1)
179 }
180 /^DW_AT_count/ {
181         match($0, /[[:digit:]]+/, temparray)
182         update_upper_bound(parent[level - 1], temparray[0])
183 }
184 /^Abbrev Number:[^(]+\(DW_TAG_/ {
185         if (match($0, /typedef|union_type|structure_type|pointer_type\
186 |enumeration_type|array_type|base_type|member/, temparray)) {
187                 array_special[idx] = temparray[0]
188                 array[idx, "special"] = temparray[0]
189                 if ("pointer_type" == temparray[0])
190                         array[idx, "byte_size"] = default_pointer_size
191                 if (level > 1 && "member" == temparray[0])
192                         array_parents[idx] = parent[level-1]
193         }
194 }
195 END {
196         parents_cnt = asorti(array_parents, aparents_keys)
197
198         for (item in array_special) {
199                 if (array[item, "special"] == "pointer_type") {
200                         mpers_ptr_t = \
201                                 "uint" 8 * array_get(item, "byte_size") "_t"
202                         print "#ifndef mpers_ptr_t_is_" mpers_ptr_t
203                         print "typedef " mpers_ptr_t " mpers_ptr_t;"
204                         print "#define mpers_ptr_t_is_" mpers_ptr_t
205                         print "#endif"
206                         break
207                 }
208         }
209         for (item in array_names) {
210                 if (array[item, "name"] == VAR_NAME) {
211                         type = array_get(item, "type")
212                         print "typedef"
213                         what_is(type)
214                         name = array_get(type, "name")
215                         print ARCH_FLAG "_" name ";"
216                         print "#define MPERS_" \
217                                 ARCH_FLAG "_" name " " \
218                                 ARCH_FLAG "_" name
219                         break
220                 }
221         }
222 }