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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2016-2017 Dmitry V. Levin <ldv@altlinux.org>
  3. * Copyright (c) 2017-2018 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_XVAL_SORTED_SIZED(prefix_, where_, field_, xlat_, \
  69. xlat_size_, dflt_) \
  70. do { \
  71. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  72. printxval_searchn((xlat_), (xlat_size_), \
  73. zero_extend_signed_to_ull((where_).field_), \
  74. (dflt_)); \
  75. } while (0)
  76. # define PRINT_FIELD_XVAL_INDEX(prefix_, where_, field_, xlat_, dflt_) \
  77. do { \
  78. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  79. printxval_index((xlat_), \
  80. zero_extend_signed_to_ull((where_).field_), \
  81. (dflt_)); \
  82. } while (0)
  83. /*
  84. * Generic "ID" printing. ID is considered unsigned except for the special value
  85. * of -1.
  86. */
  87. # define PRINT_FIELD_ID(prefix_, where_, field_) \
  88. do { \
  89. if (sign_extend_unsigned_to_ll((where_).field_) == -1LL) \
  90. STRACE_PRINTF("%s%s=-1", (prefix_), #field_); \
  91. else \
  92. STRACE_PRINTF("%s%s=%llu", (prefix_), #field_, \
  93. zero_extend_signed_to_ull((where_).field_)); \
  94. } while (0)
  95. # define PRINT_FIELD_UID PRINT_FIELD_ID
  96. # define PRINT_FIELD_UUID(prefix_, where_, field_) \
  97. do { \
  98. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  99. print_uuid((const unsigned char *) ((where_).field_)); \
  100. } while (0)
  101. # define PRINT_FIELD_U64(prefix_, where_, field_) \
  102. do { \
  103. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  104. if (zero_extend_signed_to_ull((where_).field_) == UINT64_MAX) \
  105. print_xlat_u(UINT64_MAX); \
  106. else \
  107. STRACE_PRINTF("%llu", \
  108. zero_extend_signed_to_ull((where_).field_)); \
  109. } while (0)
  110. # define PRINT_FIELD_STRING(prefix_, where_, field_, len_, style_) \
  111. do { \
  112. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  113. print_quoted_string((const char *)(where_).field_, \
  114. (len_), (style_)); \
  115. } while (0)
  116. # define PRINT_FIELD_CSTRING(prefix_, where_, field_) \
  117. do { \
  118. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  119. print_quoted_cstring((const char *) (where_).field_, \
  120. sizeof((where_).field_) + \
  121. MUST_BE_ARRAY((where_).field_)); \
  122. } while (0)
  123. # define PRINT_FIELD_CSTRING_SZ(prefix_, where_, field_, size_) \
  124. do { \
  125. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  126. print_quoted_cstring((const char *) (where_).field_, \
  127. (size_)); \
  128. } while (0)
  129. # define PRINT_FIELD_HEX_ARRAY(prefix_, where_, field_) \
  130. do { \
  131. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  132. print_quoted_string((const char *)(where_).field_, \
  133. sizeof((where_).field_) + \
  134. MUST_BE_ARRAY((where_).field_), \
  135. QUOTE_FORCE_HEX); \
  136. } while (0)
  137. # define PRINT_FIELD_INET_ADDR(prefix_, where_, field_, af_) \
  138. do { \
  139. STRACE_PRINTF(prefix_); \
  140. print_inet_addr((af_), &(where_).field_, \
  141. sizeof((where_).field_), #field_); \
  142. } while (0)
  143. # define PRINT_FIELD_AX25_ADDR(prefix_, where_, field_) \
  144. do { \
  145. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  146. print_ax25_addr(&(where_).field_); \
  147. } while (0)
  148. # define PRINT_FIELD_X25_ADDR(prefix_, where_, field_) \
  149. do { \
  150. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  151. print_x25_addr(&(where_).field_); \
  152. } while (0)
  153. #define PRINT_FIELD_NET_PORT(prefix_, where_, field_) \
  154. do { \
  155. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  156. \
  157. if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV) \
  158. print_quoted_string((const char *) \
  159. &(where_).field_, \
  160. sizeof((where_).field_), \
  161. QUOTE_FORCE_HEX); \
  162. \
  163. if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW) \
  164. break; \
  165. \
  166. if (xlat_verbose(xlat_verbosity) \
  167. == XLAT_STYLE_VERBOSE) \
  168. STRACE_PRINTF(" /* "); \
  169. \
  170. STRACE_PRINTF("htons(%u)", ntohs((where_).field_)); \
  171. \
  172. if (xlat_verbose(xlat_verbosity) \
  173. == XLAT_STYLE_VERBOSE) \
  174. STRACE_PRINTF(" */"); \
  175. } while (0)
  176. # define PRINT_FIELD_IFINDEX(prefix_, where_, field_) \
  177. do { \
  178. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  179. print_ifindex((where_).field_); \
  180. } while (0)
  181. # define PRINT_FIELD_SOCKADDR(prefix_, where_, field_) \
  182. do { \
  183. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  184. print_sockaddr(&(where_).field_, \
  185. sizeof((where_).field_)); \
  186. } while (0)
  187. # define PRINT_FIELD_DEV(prefix_, where_, field_) \
  188. do { \
  189. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  190. print_dev_t((where_).field_); \
  191. } while (0)
  192. # define PRINT_FIELD_PTR(prefix_, where_, field_) \
  193. do { \
  194. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  195. printaddr((mpers_ptr_t) (where_).field_); \
  196. } while (0)
  197. # define PRINT_FIELD_FD(prefix_, where_, field_, tcp_) \
  198. do { \
  199. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  200. printfd((tcp_), (where_).field_); \
  201. } while (0)
  202. # define PRINT_FIELD_STRN(prefix_, where_, field_, len_, tcp_) \
  203. do { \
  204. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  205. printstrn((tcp_), (where_).field_, (len_)); \
  206. } while (0)
  207. # define PRINT_FIELD_STR(prefix_, where_, field_, tcp_) \
  208. do { \
  209. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  210. printstr((tcp_), (where_).field_); \
  211. } while (0)
  212. # define PRINT_FIELD_PATH(prefix_, where_, field_, tcp_) \
  213. do { \
  214. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  215. printpath((tcp_), (where_).field_); \
  216. } while (0)
  217. # define PRINT_FIELD_MAC(prefix_, where_, field_) \
  218. PRINT_FIELD_MAC_SZ((prefix_), (where_), field_, \
  219. ARRAY_SIZE((where_).field_))
  220. # define PRINT_FIELD_MAC_SZ(prefix_, where_, field_, size_) \
  221. do { \
  222. static_assert(sizeof(((where_).field_)[0]) == 1, \
  223. "MAC address is not a byte array"); \
  224. STRACE_PRINTF("%s%s=", (prefix_), #field_); \
  225. print_mac_addr("", (const uint8_t *) ((where_).field_), \
  226. (size_)); \
  227. } while (0)
  228. #endif /* !STRACE_PRINT_FIELDS_H */