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.
35 fetch_nlattr(struct tcb *const tcp, struct nlattr *const nlattr,
36 const kernel_ulong_t addr, const kernel_ulong_t len)
38 if (len < sizeof(struct nlattr)) {
39 printstrn(tcp, addr, len);
43 if (umove_or_printaddr(tcp, addr, nlattr))
50 print_nlattr(const struct nlattr *const nla,
51 const struct xlat *const table,
52 const char *const dflt)
54 tprintf("{nla_len=%u, nla_type=", nla->nla_len);
55 if (nla->nla_type & NLA_F_NESTED)
56 tprints("NLA_F_NESTED|");
57 if (nla->nla_type & NLA_F_NET_BYTEORDER)
58 tprints("NLA_F_NET_BYTEORDER|");
59 printxval(table, nla->nla_type & NLA_TYPE_MASK, dflt);
64 decode_nlattr_with_data(struct tcb *const tcp,
65 const struct nlattr *const nla,
66 const kernel_ulong_t addr,
67 const kernel_ulong_t len,
68 const struct xlat *const table,
69 const char *const dflt,
70 const nla_decoder_t *const decoders,
71 const unsigned int size,
72 const void *const opaque_data)
74 const unsigned int nla_len = nla->nla_len > len ? len : nla->nla_len;
76 if (nla_len > NLA_HDRLEN)
79 print_nlattr(nla, table, dflt);
81 if (nla_len > NLA_HDRLEN) {
84 || nla->nla_type >= size
85 || !decoders[nla->nla_type]
86 || !decoders[nla->nla_type](tcp, addr + NLA_HDRLEN,
89 printstr_ex(tcp, addr + NLA_HDRLEN,
90 len - NLA_HDRLEN, QUOTE_FORCE_HEX);
96 decode_nlattr(struct tcb *const tcp,
99 const struct xlat *const table,
100 const char *const dflt,
101 const nla_decoder_t *const decoders,
102 const unsigned int size,
103 const void *const opaque_data)
106 bool print_array = false;
109 for (elt = 0; fetch_nlattr(tcp, &nla, addr, len); elt++) {
110 if (abbrev(tcp) && elt == max_strlen) {
115 const unsigned long nla_len = NLA_ALIGN(nla.nla_len);
116 kernel_ulong_t next_addr = 0;
117 kernel_ulong_t next_len = 0;
119 if (nla.nla_len >= NLA_HDRLEN) {
120 next_len = (len >= nla_len) ? len - nla_len : 0;
122 if (next_len && addr + nla_len > addr)
123 next_addr = addr + nla_len;
126 if (!print_array && next_addr) {
131 decode_nlattr_with_data(tcp, &nla, addr, len, table, dflt,
132 decoders, size, opaque_data);
148 decode_nla_str(struct tcb *const tcp,
149 const kernel_ulong_t addr,
150 const kernel_ulong_t len,
151 const void *const opaque_data)
153 printstr_ex(tcp, addr, len, QUOTE_0_TERMINATED);
159 decode_nla_strn(struct tcb *const tcp,
160 const kernel_ulong_t addr,
161 const kernel_ulong_t len,
162 const void *const opaque_data)
164 printstrn(tcp, addr, len);
169 #define DECODE_NLA_INTEGER(name, type, fmt) \
171 decode_nla_ ## name(struct tcb *const tcp, \
172 const kernel_ulong_t addr, \
173 const kernel_ulong_t len, \
174 const void *const opaque_data) \
178 if (len < sizeof(num)) \
180 if (!umove_or_printaddr(tcp, addr, &num)) \
185 DECODE_NLA_INTEGER(u8, uint8_t, "%" PRIu8)
186 DECODE_NLA_INTEGER(u16, uint16_t, "%" PRIu16)
187 DECODE_NLA_INTEGER(u32, uint32_t, "%" PRIu32)
188 DECODE_NLA_INTEGER(u64, uint64_t, "%" PRIu64)
189 DECODE_NLA_INTEGER(s8, int8_t, "%" PRId8)
190 DECODE_NLA_INTEGER(s16, int16_t, "%" PRId16)
191 DECODE_NLA_INTEGER(s32, int32_t, "%" PRId32)
192 DECODE_NLA_INTEGER(s64, int64_t, "%" PRId64)