2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4 * Copyright (c) 2016-2018 The strace developers.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <linux/sock_diag.h>
37 #include "static_assert.h"
40 fetch_nlattr(struct tcb *const tcp, struct nlattr *const nlattr,
41 const kernel_ulong_t addr, const unsigned int len)
43 if (len < sizeof(struct nlattr)) {
44 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
48 if (umove_or_printaddr(tcp, addr, nlattr))
55 print_nlattr(const struct nlattr *const nla,
56 const struct xlat *const table,
57 const char *const dflt)
59 static_assert(NLA_TYPE_MASK == ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER),
60 "wrong NLA_TYPE_MASK");
62 tprintf("{nla_len=%u, nla_type=", nla->nla_len);
63 if (nla->nla_type & NLA_F_NESTED) {
64 print_xlat(NLA_F_NESTED);
67 if (nla->nla_type & NLA_F_NET_BYTEORDER) {
68 print_xlat(NLA_F_NET_BYTEORDER);
71 printxval(table, nla->nla_type & NLA_TYPE_MASK, dflt);
76 decode_nlattr_with_data(struct tcb *const tcp,
77 const struct nlattr *const nla,
78 const kernel_ulong_t addr,
79 const unsigned int len,
80 const struct xlat *const table,
81 const char *const dflt,
82 const nla_decoder_t *const decoders,
83 const unsigned int size,
84 const void *const opaque_data)
86 const unsigned int nla_len = nla->nla_len > len ? len : nla->nla_len;
88 if (nla_len > NLA_HDRLEN)
91 print_nlattr(nla, table, dflt);
93 if (nla_len > NLA_HDRLEN) {
96 || nla->nla_type >= size
97 || !decoders[nla->nla_type]
98 || !decoders[nla->nla_type](tcp, addr + NLA_HDRLEN,
101 printstr_ex(tcp, addr + NLA_HDRLEN,
102 nla_len - NLA_HDRLEN, QUOTE_FORCE_HEX);
108 decode_nlattr(struct tcb *const tcp,
111 const struct xlat *const table,
112 const char *const dflt,
113 const nla_decoder_t *const decoders,
114 const unsigned int size,
115 const void *const opaque_data)
118 bool print_array = false;
121 for (elt = 0; fetch_nlattr(tcp, &nla, addr, len); elt++) {
122 if (abbrev(tcp) && elt == max_strlen) {
127 const unsigned int nla_len = NLA_ALIGN(nla.nla_len);
128 kernel_ulong_t next_addr = 0;
129 unsigned int next_len = 0;
131 if (nla.nla_len >= NLA_HDRLEN) {
132 next_len = (len >= nla_len) ? len - nla_len : 0;
134 if (next_len && addr + nla_len > addr)
135 next_addr = addr + nla_len;
138 if (!print_array && next_addr) {
143 decode_nlattr_with_data(tcp, &nla, addr, len, table, dflt,
144 decoders, size, opaque_data);
160 decode_nla_str(struct tcb *const tcp,
161 const kernel_ulong_t addr,
162 const unsigned int len,
163 const void *const opaque_data)
165 printstr_ex(tcp, addr, len, QUOTE_0_TERMINATED);
171 decode_nla_strn(struct tcb *const tcp,
172 const kernel_ulong_t addr,
173 const unsigned int len,
174 const void *const opaque_data)
176 printstrn(tcp, addr, len);
182 print_meminfo(struct tcb *const tcp,
183 void *const elem_buf,
184 const size_t elem_size,
185 void *const opaque_data)
187 unsigned int *const count = opaque_data;
189 if ((*count)++ >= SK_MEMINFO_VARS) {
194 tprintf("%" PRIu32, *(uint32_t *) elem_buf);
200 decode_nla_meminfo(struct tcb *const tcp,
201 const kernel_ulong_t addr,
202 const unsigned int len,
203 const void *const opaque_data)
206 const size_t nmemb = len / sizeof(mem);
211 unsigned int count = 0;
212 print_array(tcp, addr, nmemb, &mem, sizeof(mem),
213 umoven_or_printaddr, print_meminfo, &count);
219 decode_nla_fd(struct tcb *const tcp,
220 const kernel_ulong_t addr,
221 const unsigned int len,
222 const void *const opaque_data)
226 if (len < sizeof(fd))
228 else if (!umove_or_printaddr(tcp, addr, &fd))
235 decode_nla_ifindex(struct tcb *const tcp,
236 const kernel_ulong_t addr,
237 const unsigned int len,
238 const void *const opaque_data)
242 if (len < sizeof(ifindex))
244 else if (!umove_or_printaddr(tcp, addr, &ifindex))
245 print_ifindex(ifindex);
251 decode_nla_be16(struct tcb *const tcp,
252 const kernel_ulong_t addr,
253 const unsigned int len,
254 const void *const opaque_data)
258 if (len < sizeof(num))
260 else if (!umove_or_printaddr(tcp, addr, &num))
261 tprintf("htons(%u)", ntohs(num));
267 decode_nla_be64(struct tcb *const tcp,
268 const kernel_ulong_t addr,
269 const unsigned int len,
270 const void *const opaque_data)
272 #if defined HAVE_BE64TOH || defined be64toh
275 if (len < sizeof(num))
277 else if (!umove_or_printaddr(tcp, addr, &num))
278 tprintf("htobe64(%" PRIu64 ")", be64toh(num));
286 #define DECODE_NLA_INTEGER(name, type, fmt) \
288 decode_nla_ ## name(struct tcb *const tcp, \
289 const kernel_ulong_t addr, \
290 const unsigned int len, \
291 const void *const opaque_data) \
295 if (len < sizeof(num)) \
297 if (!umove_or_printaddr(tcp, addr, &num)) \
302 DECODE_NLA_INTEGER(u8, uint8_t, "%" PRIu8)
303 DECODE_NLA_INTEGER(u16, uint16_t, "%" PRIu16)
304 DECODE_NLA_INTEGER(u32, uint32_t, "%" PRIu32)
305 DECODE_NLA_INTEGER(u64, uint64_t, "%" PRIu64)
306 DECODE_NLA_INTEGER(s8, int8_t, "%" PRId8)
307 DECODE_NLA_INTEGER(s16, int16_t, "%" PRId16)
308 DECODE_NLA_INTEGER(s32, int32_t, "%" PRId32)
309 DECODE_NLA_INTEGER(s64, int64_t, "%" PRId64)