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.

print_fields.h 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2016-2017 Dmitry V. Levin <ldv@altlinux.org>
  3. * Copyright (c) 2017-2019 The strace developers.
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: LGPL-2.1-or-later
  7. */
  8. #ifndef STRACE_PRINT_FIELDS_H
  9. # define STRACE_PRINT_FIELDS_H
  10. /*
  11. * The printf-like function to use in header files
  12. * shared between strace and its tests.
  13. */
  14. # ifndef STRACE_PRINTF
  15. # define STRACE_PRINTF tprintf
  16. # endif
  17. # define PRINT_FIELD_D(prefix_, where_, field_) \
  18. STRACE_PRINTF("%s%s=%lld", (prefix_), #field_, \
  19. sign_extend_unsigned_to_ll((where_).field_))
  20. # define PRINT_FIELD_U(prefix_, where_, field_) \
  21. STRACE_PRINTF("%s%s=%llu", (prefix_), #field_, \
  22. zero_extend_signed_to_ull((where_).field_))
  23. # define PRINT_FIELD_U_CAST(prefix_, where_, field_, type_) \
  24. STRACE_PRINTF("%s%s=%llu", (prefix_), #field_, \
  25. zero_extend_signed_to_ull((type_) (where_).field_))
  26. # define PRINT_FIELD_X(prefix_, where_, field_) \
  27. STRACE_PRINTF("%s%s=%#llx", (prefix_), #field_, \
  28. zero_extend_signed_to_ull((where_).field_))
  29. # define PRINT_FIELD_ADDR(prefix_, where_, field_) \
  30. do { \
  31. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  32. printaddr((where_).field_); \
  33. } while (0)
  34. # define PRINT_FIELD_ADDR64(prefix_, where_, field_) \
  35. do { \
  36. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  37. printaddr64((where_).field_); \
  38. } while (0)
  39. # define PRINT_FIELD_0X(prefix_, where_, field_) \
  40. STRACE_PRINTF("%s%s=%#0*llx", (prefix_), #field_, \
  41. (int) sizeof((where_).field_) * 2, \
  42. zero_extend_signed_to_ull((where_).field_))
  43. # define PRINT_FIELD_COOKIE(prefix_, where_, field_) \
  44. STRACE_PRINTF("%s%s=[%llu, %llu]", (prefix_), #field_, \
  45. zero_extend_signed_to_ull((where_).field_[0]), \
  46. zero_extend_signed_to_ull((where_).field_[1]))
  47. # define PRINT_FIELD_FLAGS(prefix_, where_, field_, xlat_, dflt_) \
  48. do { \
  49. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  50. printflags64((xlat_), \
  51. zero_extend_signed_to_ull((where_).field_),\
  52. (dflt_)); \
  53. } while (0)
  54. # define PRINT_FIELD_XVAL(prefix_, where_, field_, xlat_, dflt_) \
  55. do { \
  56. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  57. printxval64((xlat_), \
  58. zero_extend_signed_to_ull((where_).field_), \
  59. (dflt_)); \
  60. } while (0)
  61. # define PRINT_FIELD_XVAL_U(prefix_, where_, field_, xlat_, dflt_) \
  62. do { \
  63. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  64. printxvals_ex(zero_extend_signed_to_ull((where_).field_), \
  65. (dflt_), XLAT_STYLE_FMT_U, \
  66. (xlat_), NULL); \
  67. } while (0)
  68. # define PRINT_FIELD_ERR_D(prefix_, where_, field_) \
  69. do { \
  70. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  71. print_err(sign_extend_unsigned_to_ll((where_).field_), \
  72. true); \
  73. } while (0)
  74. # define PRINT_FIELD_ERR_U(prefix_, where_, field_) \
  75. do { \
  76. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  77. print_err(zero_extend_signed_to_ull((where_).field_), \
  78. false); \
  79. } while (0)
  80. /*
  81. * Generic "ID" printing. ID is considered unsigned except for the special value
  82. * of -1.
  83. */
  84. # define PRINT_FIELD_ID(prefix_, where_, field_) \
  85. do { \
  86. if (sign_extend_unsigned_to_ll((where_).field_) == -1LL) \
  87. STRACE_PRINTF("%s%s=-1", (prefix_), #field_); \
  88. else \
  89. STRACE_PRINTF("%s%s=%llu", (prefix_), #field_, \
  90. zero_extend_signed_to_ull((where_).field_)); \
  91. } while (0)
  92. # define PRINT_FIELD_UID PRINT_FIELD_ID
  93. # define PRINT_FIELD_UUID(prefix_, where_, field_) \
  94. do { \
  95. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  96. print_uuid((const unsigned char *) ((where_).field_)); \
  97. } while (0)
  98. # define PRINT_FIELD_U64(prefix_, where_, field_) \
  99. do { \
  100. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  101. if (zero_extend_signed_to_ull((where_).field_) == UINT64_MAX) \
  102. print_xlat_u(UINT64_MAX); \
  103. else \
  104. STRACE_PRINTF("%llu", \
  105. zero_extend_signed_to_ull((where_).field_)); \
  106. } while (0)
  107. # define PRINT_FIELD_STRING(prefix_, where_, field_, len_, style_) \
  108. do { \
  109. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  110. print_quoted_string((const char *)(where_).field_, \
  111. (len_), (style_)); \
  112. } while (0)
  113. # define PRINT_FIELD_CSTRING(prefix_, where_, field_) \
  114. do { \
  115. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  116. print_quoted_cstring((const char *) (where_).field_, \
  117. sizeof((where_).field_) + \
  118. MUST_BE_ARRAY((where_).field_)); \
  119. } while (0)
  120. # define PRINT_FIELD_CSTRING_SZ(prefix_, where_, field_, size_) \
  121. do { \
  122. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  123. print_quoted_cstring((const char *) (where_).field_, \
  124. (size_)); \
  125. } while (0)
  126. # define PRINT_FIELD_HEX_ARRAY(prefix_, where_, field_) \
  127. do { \
  128. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  129. print_quoted_string((const char *)(where_).field_, \
  130. sizeof((where_).field_) + \
  131. MUST_BE_ARRAY((where_).field_), \
  132. QUOTE_FORCE_HEX); \
  133. } while (0)
  134. # define PRINT_FIELD_INET_ADDR(prefix_, where_, field_, af_) \
  135. do { \
  136. STRACE_PRINTF(prefix_); \
  137. print_inet_addr((af_), &(where_).field_, \
  138. sizeof((where_).field_), #field_); \
  139. } while (0)
  140. # define PRINT_FIELD_AX25_ADDR(prefix_, where_, field_) \
  141. do { \
  142. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  143. print_ax25_addr(&(where_).field_); \
  144. } while (0)
  145. # define PRINT_FIELD_X25_ADDR(prefix_, where_, field_) \
  146. do { \
  147. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  148. print_x25_addr(&(where_).field_); \
  149. } while (0)
  150. # define PRINT_FIELD_NET_PORT(prefix_, where_, field_) \
  151. do { \
  152. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  153. \
  154. if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV) \
  155. print_quoted_string((const char *) \
  156. &(where_).field_, \
  157. sizeof((where_).field_), \
  158. QUOTE_FORCE_HEX); \
  159. \
  160. if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW) \
  161. break; \
  162. \
  163. if (xlat_verbose(xlat_verbosity) \
  164. == XLAT_STYLE_VERBOSE) \
  165. STRACE_PRINTF(" /* "); \
  166. \
  167. STRACE_PRINTF("htons(%u)", ntohs((where_).field_)); \
  168. \
  169. if (xlat_verbose(xlat_verbosity) \
  170. == XLAT_STYLE_VERBOSE) \
  171. STRACE_PRINTF(" */"); \
  172. } while (0)
  173. # define PRINT_FIELD_IFINDEX(prefix_, where_, field_) \
  174. do { \
  175. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  176. print_ifindex((where_).field_); \
  177. } while (0)
  178. # define PRINT_FIELD_SOCKADDR(prefix_, where_, field_) \
  179. do { \
  180. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  181. print_sockaddr(&(where_).field_, \
  182. sizeof((where_).field_)); \
  183. } while (0)
  184. # define PRINT_FIELD_DEV(prefix_, where_, field_) \
  185. do { \
  186. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  187. print_dev_t((where_).field_); \
  188. } while (0)
  189. # define PRINT_FIELD_PTR(prefix_, where_, field_) \
  190. do { \
  191. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  192. printaddr((mpers_ptr_t) (where_).field_); \
  193. } while (0)
  194. # define PRINT_FIELD_FD(prefix_, where_, field_, tcp_) \
  195. do { \
  196. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  197. printfd((tcp_), (where_).field_); \
  198. } while (0)
  199. # define PRINT_FIELD_STRN(prefix_, where_, field_, len_, tcp_) \
  200. do { \
  201. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  202. printstrn((tcp_), (where_).field_, (len_)); \
  203. } while (0)
  204. # define PRINT_FIELD_STR(prefix_, where_, field_, tcp_) \
  205. do { \
  206. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  207. printstr((tcp_), (where_).field_); \
  208. } while (0)
  209. # define PRINT_FIELD_PATH(prefix_, where_, field_, tcp_) \
  210. do { \
  211. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  212. printpath((tcp_), (where_).field_); \
  213. } while (0)
  214. # define PRINT_FIELD_MAC(prefix_, where_, field_) \
  215. PRINT_FIELD_MAC_SZ((prefix_), (where_), field_, \
  216. ARRAY_SIZE((where_).field_))
  217. # define PRINT_FIELD_MAC_SZ(prefix_, where_, field_, size_) \
  218. do { \
  219. static_assert(sizeof(((where_).field_)[0]) == 1, \
  220. "MAC address is not a byte array"); \
  221. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  222. print_mac_addr("", (const uint8_t *) ((where_).field_), \
  223. (size_)); \
  224. } while (0)
  225. # define PRINT_FIELD_HWADDR_SZ(prefix_, where_, field_, size_, hwtype_) \
  226. do { \
  227. static_assert(sizeof(((where_).field_)[0]) == 1, \
  228. "hwaddress is not a byte array"); \
  229. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  230. print_hwaddr("", (const uint8_t *) ((where_).field_), \
  231. (size_), (hwtype_)); \
  232. } while (0)
  233. #endif /* !STRACE_PRINT_FIELDS_H */