Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mpers.awk 7.6KB

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