]> granicus.if.org Git - strace/blob - mpers.awk
mpers.awk: separate local variables from real parameters by newline
[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-2017 The strace developers.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 #    notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 #    notice, this list of conditions and the following disclaimer in the
15 #    documentation and/or other materials provided with the distribution.
16 # 3. The name of the author may not be used to endorse or promote products
17 #    derived from this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 function compare_indices(i1, v1, i2, v2) {
31         c1 = strtonum(sprintf("%s", i1))
32         c2 = strtonum(sprintf("%s", i2))
33         if (c1 < c2)
34                 return -1
35         return (c1 != c2)
36 }
37 function array_get(array_idx, array_member, \
38                    array_return)
39 {
40         array_return = array[array_idx][array_member]
41         if ("" == array_return) {
42                 printf("%s: index [%s] without %s\n",
43                        FILENAME, array_idx, array_member) > "/dev/stderr"
44                 exit 1
45         }
46         return array_return
47 }
48 function array_seq(array_idx)
49 {
50         if ("seq" in array[array_idx])
51                 return array[array_idx]["seq"]
52         index_seq++
53         array[array_idx]["seq"] = index_seq
54         return index_seq
55 }
56 function enter(array_idx)
57 {
58         if (array_idx in called) {
59                 printf("%s: index loop detected:", FILENAME) > "/dev/stderr"
60                 for (item in called)
61                         printf(" %s", item) > "/dev/stderr"
62                 print "" > "/dev/stderr"
63                 exit 1
64         }
65         called[array_idx] = 1
66 }
67 function leave(array_idx, to_return)
68 {
69         delete called[array_idx]
70         return to_return
71 }
72 function update_upper_bound(idx, val, \
73                             count)
74 {
75         count = array[idx]["count"]
76         if (count == "")
77                 count = 1
78         array[idx]["count"] = count * val
79         array[idx]["upper_bound"] = array[idx]["upper_bound"] "[" val "]"
80 }
81 function what_is(what_idx, type_idx, special, item, \
82                  location, prev_location, prev_returned_size)
83 {
84         enter(what_idx)
85         special = array_get(what_idx, "special")
86         switch (special) {
87         case "base_type":
88                 switch (array_get(what_idx, "encoding")) {
89                 case 5: # signed
90                         printf("int%s_t ",
91                                8 * array_get(what_idx, "byte_size"))
92                         break
93                 case 7: # unsigned
94                         printf("uint%s_t ",
95                                8 * array_get(what_idx, "byte_size"))
96                         break
97                 default: # float, signed/unsigned char
98                         printf("%s ", array_get(what_idx, "name"))
99                         break
100                 }
101                 returned_size = array_get(what_idx, "byte_size")
102                 break
103         case "enumeration_type":
104                 returned_size = array_get(what_idx, "byte_size")
105                 printf("uint%s_t ", 8 * returned_size)
106                 break
107         case "pointer_type":
108                 printf("mpers_ptr_t ")
109                 returned_size = array_get(what_idx, "byte_size")
110                 break
111         case "array_type":
112                 type_idx = array_get(what_idx, "type")
113                 what_is(type_idx)
114                 to_return = array[what_idx]["upper_bound"]
115                 if ("" == to_return)
116                         to_return = "[0]"
117                 returned_size = array[what_idx]["count"] * returned_size
118                 return leave(what_idx, to_return)
119                 break
120         case "structure_type":
121                 print "struct {"
122                 prev_location = 0
123                 location = 0
124                 returned_size = 0
125                 prev_returned_size = 0
126                 for (item in array) {
127                         if ("parent" in array[item] && \
128                                 array_get(item, "parent") == what_idx) {
129                                 location = array_get(item, "location")
130                                 loc_diff = location - prev_location - \
131                                         prev_returned_size
132                                 if (loc_diff != 0) {
133                                         printf("unsigned char mpers_%s_%s[%s];\n",
134                                                "filler", array_seq(item), loc_diff)
135                                 }
136                                 prev_location = location
137                                 returned = what_is(item)
138                                 prev_returned_size = returned_size
139                                 printf("%s%s;\n", array[item]["name"], returned)
140                         }
141                 }
142                 returned_size = array_get(what_idx, "byte_size")
143                 loc_diff = returned_size - prev_location - prev_returned_size
144                 if (loc_diff != 0) {
145                         printf("unsigned char mpers_%s_%s[%s];\n",
146                                "end_filler", array_seq(item), loc_diff)
147                 }
148                 printf("} ATTRIBUTE_PACKED ")
149                 break
150         case "union_type":
151                 print "union {"
152                 for (item in array) {
153                         if ("parent" in array[item] && \
154                                 array_get(item, "parent") == what_idx) {
155                                 returned = what_is(item)
156                                 printf("%s%s;\n", array[item]["name"], returned)
157                         }
158                 }
159                 printf("} ")
160                 returned_size = array_get(what_idx, "byte_size")
161                 break
162         case "typedef":
163                 type_idx = array_get(what_idx, "type")
164                 return leave(what_idx, what_is(type_idx))
165                 break
166         case "member":
167                 type_idx = array_get(what_idx, "type")
168                 return leave(what_idx, what_is(type_idx))
169                 break
170         default:
171                 type_idx = array_get(what_idx, "type")
172                 what_is(type_idx)
173                 break
174         }
175         return leave(what_idx, "")
176 }
177 BEGIN {
178         match(ARCH_FLAG, /[[:digit:]]+/, temparray)
179         default_pointer_size = temparray[0] / 8
180         print "#include <inttypes.h>"
181 }
182 /^<[[:xdigit:]]+>/ {
183         match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches)
184         level = matches[1]
185         idx = "0x" matches[2]
186         array[idx]["idx"] = idx
187         parent[level] = idx
188 }
189 /^DW_AT_data_member_location/ {
190         if (!match($0, /\(DW_OP_plus_uconst:[[:space:]]+([[:digit:]]+)\)/, temparray))
191                 match($0, /([[:digit:]]+)/, temparray)
192         array[idx]["location"] = temparray[1]
193 }
194 /^DW_AT_name/ {
195         match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \
196                 temparray)
197         array[idx]["name"] = temparray[1]
198 }
199 /^DW_AT_byte_size/ {
200         match($0, /[[:digit:]]+/, temparray)
201         array[idx]["byte_size"] = temparray[0]
202 }
203 /^DW_AT_encoding/ {
204         match($0, /[[:digit:]]+/, temparray)
205         array[idx]["encoding"] = temparray[0]
206 }
207 /^DW_AT_type/ {
208         match($0, /:[[:space:]]+<(0x[[:xdigit:]]*)>$/, temparray)
209         array[idx]["type"] = temparray[1]
210 }
211 /^DW_AT_upper_bound/ {
212         match($0, /[[:digit:]]+/, temparray)
213         update_upper_bound(parent[level - 1], temparray[0] + 1)
214 }
215 /^DW_AT_count/ {
216         match($0, /[[:digit:]]+/, temparray)
217         update_upper_bound(parent[level - 1], temparray[0])
218 }
219 /^Abbrev Number:[^(]+\(DW_TAG_/ {
220         if (match($0, /typedef|union_type|structure_type|pointer_type\
221 |enumeration_type|array_type|base_type|member/, temparray)) {
222                 array[idx]["special"] = temparray[0]
223                 if ("pointer_type" == temparray[0])
224                         array[idx]["byte_size"] = default_pointer_size
225                 if (level > 1 && "member" == temparray[0])
226                         array[idx]["parent"] = parent[level-1]
227         }
228 }
229 END {
230         PROCINFO["sorted_in"] = "compare_indices"
231         for (item in array) {
232                 if (array[item]["special"] == "pointer_type") {
233                         print "typedef uint" \
234                                 8 * array_get(item, "byte_size") "_t mpers_ptr_t;"
235                         break
236                 }
237         }
238         for (item in array) {
239                 if (array[item]["name"] == VAR_NAME) {
240                         type = array_get(item, "type")
241                         print "typedef"
242                         what_is(type)
243                         name = array_get(type, "name")
244                         print ARCH_FLAG "_" name ";"
245                         print "#define MPERS_" \
246                                 ARCH_FLAG "_" name " " \
247                                 ARCH_FLAG "_" name
248                         break
249                 }
250         }
251 }