Home | History | Annotate | Download | only in strace
      1 /*
      2  * Copyright (c) 2015 Bart Van Assche <bart.vanassche (at) sandisk.com>
      3  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv (at) altlinux.org>
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "defs.h"
     30 
     31 #ifdef HAVE_LINUX_BSG_H
     32 
     33 # include "print_fields.h"
     34 # include <linux/bsg.h>
     35 # include "xlat/bsg_protocol.h"
     36 # include "xlat/bsg_subprotocol.h"
     37 # include "xlat/bsg_flags.h"
     38 
     39 static void
     40 print_sg_io_buffer(struct tcb *const tcp, const kernel_ulong_t addr,
     41 		   const unsigned int data_size, const unsigned int iovec_count)
     42 {
     43 	if (iovec_count) {
     44 		tprint_iov_upto(tcp, iovec_count, addr, IOV_DECODE_STR,
     45 				data_size);
     46 	} else {
     47 		printstr_ex(tcp, addr, data_size, QUOTE_FORCE_HEX);
     48 	}
     49 }
     50 
     51 #define PRINT_FIELD_SG_IO_BUFFER(prefix_, where_, field_, size_, count_, tcp_)	\
     52 	do {									\
     53 		STRACE_PRINTF("%s%s=", (prefix_), #field_);			\
     54 		print_sg_io_buffer((tcp_), (where_).field_, (size_), (count_));	\
     55 	} while (0)
     56 
     57 static int
     58 decode_request(struct tcb *const tcp, const kernel_ulong_t arg)
     59 {
     60 	struct sg_io_v4 sg_io;
     61 	static const size_t skip_iid = offsetof(struct sg_io_v4, protocol);
     62 
     63 	tprints("{guard='Q', ");
     64 	if (umoven_or_printaddr(tcp, arg + skip_iid, sizeof(sg_io) - skip_iid,
     65 				&sg_io.protocol)) {
     66 		tprints("}");
     67 		return RVAL_IOCTL_DECODED;
     68 	}
     69 
     70 	PRINT_FIELD_XVAL("", sg_io, protocol, bsg_protocol, "BSG_PROTOCOL_???");
     71 	PRINT_FIELD_XVAL(", ", sg_io, subprotocol, bsg_subprotocol,
     72 			 "BSG_SUB_PROTOCOL_???");
     73 	PRINT_FIELD_U(", ", sg_io, request_len);
     74 	PRINT_FIELD_SG_IO_BUFFER(", ", sg_io, request, sg_io.request_len,
     75 				 0, tcp);
     76 	PRINT_FIELD_X(", ", sg_io, request_tag);
     77 	PRINT_FIELD_U(", ", sg_io, request_attr);
     78 	PRINT_FIELD_U(", ", sg_io, request_priority);
     79 	PRINT_FIELD_U(", ", sg_io, request_extra);
     80 	PRINT_FIELD_U(", ", sg_io, max_response_len);
     81 
     82 	PRINT_FIELD_U(", ", sg_io, dout_iovec_count);
     83 	PRINT_FIELD_U(", ", sg_io, dout_xfer_len);
     84 	PRINT_FIELD_U(", ", sg_io, din_iovec_count);
     85 	PRINT_FIELD_U(", ", sg_io, din_xfer_len);
     86 	PRINT_FIELD_SG_IO_BUFFER(", ", sg_io, dout_xferp, sg_io.dout_xfer_len,
     87 				 sg_io.dout_iovec_count, tcp);
     88 
     89 	PRINT_FIELD_U(", ", sg_io, timeout);
     90 	PRINT_FIELD_FLAGS(", ", sg_io, flags, bsg_flags, "BSG_FLAG_???");
     91 	PRINT_FIELD_X(", ", sg_io, usr_ptr);
     92 
     93 	struct sg_io_v4 *entering_sg_io = malloc(sizeof(*entering_sg_io));
     94 	if (entering_sg_io) {
     95 		memcpy(entering_sg_io, &sg_io, sizeof(sg_io));
     96 		entering_sg_io->guard = (unsigned char) 'Q';
     97 		set_tcb_priv_data(tcp, entering_sg_io, free);
     98 	}
     99 
    100 	return 0;
    101 }
    102 
    103 static int
    104 decode_response(struct tcb *const tcp, const kernel_ulong_t arg)
    105 {
    106 	struct sg_io_v4 *entering_sg_io = get_tcb_priv_data(tcp);
    107 	struct sg_io_v4 sg_io;
    108 	uint32_t din_len;
    109 
    110 	if (umove(tcp, arg, &sg_io) < 0) {
    111 		/* print i/o fields fetched on entering syscall */
    112 		PRINT_FIELD_X(", ", *entering_sg_io, response);
    113 		PRINT_FIELD_X(", ", *entering_sg_io, din_xferp);
    114 		return RVAL_IOCTL_DECODED;
    115 	}
    116 
    117 	if (sg_io.guard != entering_sg_io->guard) {
    118 		PRINT_FIELD_U(" => ", sg_io, guard);
    119 		return RVAL_IOCTL_DECODED;
    120 	}
    121 
    122 	PRINT_FIELD_U(", ", sg_io, response_len);
    123 	PRINT_FIELD_SG_IO_BUFFER(", ", sg_io, response, sg_io.response_len,
    124 				 0, tcp);
    125 	din_len = sg_io.din_xfer_len;
    126 	if (sg_io.din_resid > 0 && (unsigned int) sg_io.din_resid <= din_len)
    127 		din_len -= sg_io.din_resid;
    128 	PRINT_FIELD_SG_IO_BUFFER(", ", sg_io, din_xferp, din_len,
    129 				 sg_io.din_iovec_count, tcp);
    130 	PRINT_FIELD_X(", ", sg_io, driver_status);
    131 	PRINT_FIELD_X(", ", sg_io, transport_status);
    132 	PRINT_FIELD_X(", ", sg_io, device_status);
    133 	PRINT_FIELD_U(", ", sg_io, retry_delay);
    134 	PRINT_FIELD_FLAGS(", ", sg_io, info, sg_io_info, "SG_INFO_???");
    135 	PRINT_FIELD_U(", ", sg_io, duration);
    136 	PRINT_FIELD_U(", ", sg_io, response_len);
    137 	PRINT_FIELD_D(", ", sg_io, din_resid);
    138 	PRINT_FIELD_D(", ", sg_io, dout_resid);
    139 	PRINT_FIELD_X(", ", sg_io, generated_tag);
    140 
    141 	return RVAL_IOCTL_DECODED;
    142 }
    143 
    144 #else /* !HAVE_LINUX_BSG_H */
    145 
    146 static int
    147 decode_request(struct tcb *const tcp, const kernel_ulong_t arg)
    148 {
    149 	tprints("{guard='Q', ...}");
    150 	return RVAL_IOCTL_DECODED;
    151 }
    152 
    153 static int
    154 decode_response(struct tcb *const tcp, const kernel_ulong_t arg)
    155 {
    156 	return 0;
    157 }
    158 
    159 #endif
    160 
    161 int
    162 decode_sg_io_v4(struct tcb *const tcp, const kernel_ulong_t arg)
    163 {
    164 	return entering(tcp) ? decode_request(tcp, arg)
    165 			     : decode_response(tcp, arg);
    166 }
    167