]> granicus.if.org Git - strace/blob - mpers.awk
Remove linux/ptp_clock.h
[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, c2)
32 {
33         c1 = strtonum(sprintf("%s", i1))
34         c2 = strtonum(sprintf("%s", i2))
35         if (c1 < c2)
36                 return -1
37         return (c1 != c2)
38 }
39 function array_get(array_idx, array_member, \
40                    array_return)
41 {
42         array_return = array[array_idx][array_member]
43         if ("" == array_return) {
44                 printf("%s: index [%s] without %s\n",
45                        FILENAME, array_idx, array_member) > "/dev/stderr"
46                 exit 1
47         }
48         return array_return
49 }
50 function array_seq(array_idx)
51 {
52         if ("seq" in array[array_idx])
53                 return array[array_idx]["seq"]
54         index_seq++
55         array[array_idx]["seq"] = index_seq
56         return index_seq
57 }
58 function enter(array_idx,
59                item)
60 {
61         if (array_idx in called) {
62                 printf("%s: index loop detected:", FILENAME) > "/dev/stderr"
63                 for (item in called)
64                         printf(" %s", item) > "/dev/stderr"
65                 print "" > "/dev/stderr"
66                 exit 1
67         }
68         called[array_idx] = 1
69 }
70 function leave(array_idx, to_return)
71 {
72         delete called[array_idx]
73         return to_return
74 }
75 function update_upper_bound(idx, val, \
76                             count)
77 {
78         count = array[idx]["count"]
79         if (count == "")
80                 count = 1
81         array[idx]["count"] = count * val
82         array[idx]["upper_bound"] = array[idx]["upper_bound"] "[" val "]"
83 }
84 function what_is(what_idx, \
85                  item, loc_diff, location, prev_location, prev_returned_size, \
86                  special, to_return, type_idx)
87 {
88         enter(what_idx)
89         special = array_get(what_idx, "special")
90         switch (special) {
91         case "base_type":
92                 switch (array_get(what_idx, "encoding")) {
93                 case 5: # signed
94                         printf("int%s_t ",
95                                8 * array_get(what_idx, "byte_size"))
96                         break
97                 case 7: # unsigned
98                         printf("uint%s_t ",
99                                8 * array_get(what_idx, "byte_size"))
100                         break
101                 default: # float, signed/unsigned char
102                         printf("%s ", array_get(what_idx, "name"))
103                         break
104                 }
105                 returned_size = array_get(what_idx, "byte_size")
106                 break
107         case "enumeration_type":
108                 returned_size = array_get(what_idx, "byte_size")
109                 printf("uint%s_t ", 8 * returned_size)
110                 break
111         case "pointer_type":
112                 printf("mpers_ptr_t ")
113                 returned_size = array_get(what_idx, "byte_size")
114                 break
115         case "array_type":
116                 type_idx = array_get(what_idx, "type")
117                 what_is(type_idx)
118                 to_return = array[what_idx]["upper_bound"]
119                 if ("" == to_return)
120                         to_return = "[0]"
121                 returned_size = array[what_idx]["count"] * returned_size
122                 return leave(what_idx, to_return)
123                 break
124         case "structure_type":
125                 print "struct {"
126                 prev_location = 0
127                 location = 0
128                 returned_size = 0
129                 prev_returned_size = 0
130                 for (item in array) {
131                         if ("parent" in array[item] && \
132                                 array_get(item, "parent") == what_idx) {
133                                 location = array_get(item, "location")
134                                 loc_diff = location - prev_location - \
135                                         prev_returned_size
136                                 if (loc_diff != 0) {
137                                         printf("unsigned char mpers_%s_%s[%s];\n",
138                                                "filler", array_seq(item), loc_diff)
139                                 }
140                                 prev_location = location
141                                 returned = what_is(item)
142                                 prev_returned_size = returned_size
143                                 printf("%s%s;\n", array[item]["name"], returned)
144                         }
145                 }
146                 returned_size = array_get(what_idx, "byte_size")
147                 loc_diff = returned_size - prev_location - prev_returned_size
148                 if (loc_diff != 0) {
149                         printf("unsigned char mpers_%s_%s[%s];\n",
150                                "end_filler", array_seq(item), loc_diff)
151                 }
152                 printf("} ATTRIBUTE_PACKED ")
153                 break
154         case "union_type":
155                 print "union {"
156                 for (item in array) {
157                         if ("parent" in array[item] && \
158                                 array_get(item, "parent") == what_idx) {
159                                 returned = what_is(item)
160                                 printf("%s%s;\n", array[item]["name"], returned)
161                         }
162                 }
163                 printf("} ")
164                 returned_size = array_get(what_idx, "byte_size")
165                 break
166         case "typedef":
167                 type_idx = array_get(what_idx, "type")
168                 return leave(what_idx, what_is(type_idx))
169                 break
170         case "member":
171                 type_idx = array_get(what_idx, "type")
172                 return leave(what_idx, what_is(type_idx))
173                 break
174         default:
175                 type_idx = array_get(what_idx, "type")
176                 what_is(type_idx)
177                 break
178         }
179         return leave(what_idx, "")
180 }
181 BEGIN {
182         match(ARCH_FLAG, /[[:digit:]]+/, temparray)
183         default_pointer_size = temparray[0] / 8
184         print "#include <inttypes.h>"
185 }
186 /^<[[:xdigit:]]+>/ {
187         match($0, /([[:alnum:]]+)><([[:alnum:]]+)/, matches)
188         level = matches[1]
189         idx = "0x" matches[2]
190         array[idx]["idx"] = idx
191         parent[level] = idx
192 }
193 /^DW_AT_data_member_location/ {
194         if (!match($0, /\(DW_OP_plus_uconst:[[:space:]]+([[:digit:]]+)\)/, temparray))
195                 match($0, /([[:digit:]]+)/, temparray)
196         array[idx]["location"] = temparray[1]
197 }
198 /^DW_AT_name/ {
199         match($0, /:[[:space:]]+([[:alpha:]_][[:alnum:]_[:space:]]*)/, \
200                 temparray)
201         array[idx]["name"] = temparray[1]
202 }
203 /^DW_AT_byte_size/ {
204         match($0, /[[:digit:]]+/, temparray)
205         array[idx]["byte_size"] = temparray[0]
206 }
207 /^DW_AT_encoding/ {
208         match($0, /[[:digit:]]+/, temparray)
209         array[idx]["encoding"] = temparray[0]
210 }
211 /^DW_AT_type/ {
212         match($0, /:[[:space:]]+<(0x[[:xdigit:]]*)>$/, temparray)
213         array[idx]["type"] = temparray[1]
214 }
215 /^DW_AT_upper_bound/ {
216         match($0, /[[:digit:]]+/, temparray)
217         update_upper_bound(parent[level - 1], temparray[0] + 1)
218 }
219 /^DW_AT_count/ {
220         match($0, /[[:digit:]]+/, temparray)
221         update_upper_bound(parent[level - 1], temparray[0])
222 }
223 /^Abbrev Number:[^(]+\(DW_TAG_/ {
224         if (match($0, /typedef|union_type|structure_type|pointer_type\
225 |enumeration_type|array_type|base_type|member/, temparray)) {
226                 array[idx]["special"] = temparray[0]
227                 if ("pointer_type" == temparray[0])
228                         array[idx]["byte_size"] = default_pointer_size
229                 if (level > 1 && "member" == temparray[0])
230                         array[idx]["parent"] = parent[level-1]
231         }
232 }
233 END {
234         PROCINFO["sorted_in"] = "compare_indices"
235         for (item in array) {
236                 if (array[item]["special"] == "pointer_type") {
237                         print "typedef uint" \
238                                 8 * array_get(item, "byte_size") "_t mpers_ptr_t;"
239                         break
240                 }
241         }
242         for (item in array) {
243                 if (array[item]["name"] == VAR_NAME) {
244                         type = array_get(item, "type")
245                         print "typedef"
246                         what_is(type)
247                         name = array_get(type, "name")
248                         print ARCH_FLAG "_" name ";"
249                         print "#define MPERS_" \
250                                 ARCH_FLAG "_" name " " \
251                                 ARCH_FLAG "_" name
252                         break
253                 }
254         }
255 }