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 6.3KB

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