2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4 * Copyright (c) 2016-2017 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>
39 fetch_nlattr(struct tcb *const tcp, struct nlattr *const nlattr,
40 const kernel_ulong_t addr, const unsigned int len)
42 if (len < sizeof(struct nlattr)) {
43 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
47 if (umove_or_printaddr(tcp, addr, nlattr))
54 print_nlattr(const struct nlattr *const nla,
55 const struct xlat *const table,
56 const char *const dflt)
58 tprintf("{nla_len=%u, nla_type=", nla->nla_len);
59 if (nla->nla_type & NLA_F_NESTED)
60 tprints("NLA_F_NESTED|");
61 if (nla->nla_type & NLA_F_NET_BYTEORDER)
62 tprints("NLA_F_NET_BYTEORDER|");
63 printxval(table, nla->nla_type & NLA_TYPE_MASK, dflt);
68 decode_nlattr_with_data(struct tcb *const tcp,
69 const struct nlattr *const nla,
70 const kernel_ulong_t addr,
71 const unsigned int len,
72 const struct xlat *const table,
73 const char *const dflt,
74 const nla_decoder_t *const decoders,
75 const unsigned int size,
76 const void *const opaque_data)
78 const unsigned int nla_len = nla->nla_len > len ? len : nla->nla_len;
80 if (nla_len > NLA_HDRLEN)
83 print_nlattr(nla, table, dflt);
85 if (nla_len > NLA_HDRLEN) {
88 || nla->nla_type >= size
89 || !decoders[nla->nla_type]
90 || !decoders[nla->nla_type](tcp, addr + NLA_HDRLEN,
93 printstr_ex(tcp, addr + NLA_HDRLEN,
94 nla_len - NLA_HDRLEN, QUOTE_FORCE_HEX);
100 decode_nlattr(struct tcb *const tcp,
103 const struct xlat *const table,
104 const char *const dflt,
105 const nla_decoder_t *const decoders,
106 const unsigned int size,
107 const void *const opaque_data)
110 bool print_array = false;
113 for (elt = 0; fetch_nlattr(tcp, &nla, addr, len); elt++) {
114 if (abbrev(tcp) && elt == max_strlen) {
119 const unsigned int nla_len = NLA_ALIGN(nla.nla_len);
120 kernel_ulong_t next_addr = 0;
121 unsigned int next_len = 0;
123 if (nla.nla_len >= NLA_HDRLEN) {
124 next_len = (len >= nla_len) ? len - nla_len : 0;
126 if (next_len && addr + nla_len > addr)
127 next_addr = addr + nla_len;
130 if (!print_array && next_addr) {
135 decode_nlattr_with_data(tcp, &nla, addr, len, table, dflt,
136 decoders, size, opaque_data);
152 decode_nla_str(struct tcb *const tcp,
153 const kernel_ulong_t addr,
154 const unsigned int len,
155 const void *const opaque_data)
157 printstr_ex(tcp, addr, len, QUOTE_0_TERMINATED);
163 decode_nla_strn(struct tcb *const tcp,
164 const kernel_ulong_t addr,
165 const unsigned int len,
166 const void *const opaque_data)
168 printstrn(tcp, addr, len);
174 print_meminfo(struct tcb *const tcp,
175 void *const elem_buf,
176 const size_t elem_size,
177 void *const opaque_data)
179 unsigned int *const count = opaque_data;
181 if ((*count)++ >= SK_MEMINFO_VARS) {
186 tprintf("%" PRIu32, *(uint32_t *) elem_buf);
192 decode_nla_meminfo(struct tcb *const tcp,
193 const kernel_ulong_t addr,
194 const unsigned int len,
195 const void *const opaque_data)
198 const size_t nmemb = len / sizeof(mem);
203 unsigned int count = 0;
204 print_array(tcp, addr, nmemb, &mem, sizeof(mem),
205 umoven_or_printaddr, print_meminfo, &count);
211 decode_nla_fd(struct tcb *const tcp,
212 const kernel_ulong_t addr,
213 const unsigned int len,
214 const void *const opaque_data)
218 if (len < sizeof(fd))
220 else if (!umove_or_printaddr(tcp, addr, &fd))
227 decode_nla_ifindex(struct tcb *const tcp,
228 const kernel_ulong_t addr,
229 const unsigned int len,
230 const void *const opaque_data)
234 if (len < sizeof(ifindex))
236 else if (!umove_or_printaddr(tcp, addr, &ifindex))
237 print_ifindex(ifindex);
243 decode_nla_be16(struct tcb *const tcp,
244 const kernel_ulong_t addr,
245 const unsigned int len,
246 const void *const opaque_data)
250 if (len < sizeof(num))
252 else if (!umove_or_printaddr(tcp, addr, &num))
253 tprintf("htons(%u)", ntohs(num));
259 decode_nla_be64(struct tcb *const tcp,
260 const kernel_ulong_t addr,
261 const unsigned int len,
262 const void *const opaque_data)
264 #if defined HAVE_BE64TOH || defined be64toh
267 if (len < sizeof(num))
269 else if (!umove_or_printaddr(tcp, addr, &num))
270 tprintf("htobe64(%" PRIu64 ")", be64toh(num));
278 #define DECODE_NLA_INTEGER(name, type, fmt) \
280 decode_nla_ ## name(struct tcb *const tcp, \
281 const kernel_ulong_t addr, \
282 const unsigned int len, \
283 const void *const opaque_data) \
287 if (len < sizeof(num)) \
289 if (!umove_or_printaddr(tcp, addr, &num)) \
294 DECODE_NLA_INTEGER(u8, uint8_t, "%" PRIu8)
295 DECODE_NLA_INTEGER(u16, uint16_t, "%" PRIu16)
296 DECODE_NLA_INTEGER(u32, uint32_t, "%" PRIu32)
297 DECODE_NLA_INTEGER(u64, uint64_t, "%" PRIu64)
298 DECODE_NLA_INTEGER(s8, int8_t, "%" PRId8)
299 DECODE_NLA_INTEGER(s16, int16_t, "%" PRId16)
300 DECODE_NLA_INTEGER(s32, int32_t, "%" PRId32)
301 DECODE_NLA_INTEGER(s64, int64_t, "%" PRId64)