]> granicus.if.org Git - strace/blob - scsi.c
Update siginfo codes
[strace] / scsi.c
1 /*
2  * Copyright (c) 2007 Vladimir Nadvornik <nadvornik@suse.cz>
3  * Copyright (c) 2007 Dmitry V. Levin <ldv@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_SCSI_SG_H
32
33 # include <sys/ioctl.h>
34 # include <scsi/sg.h>
35
36 static const struct xlat sg_io_dxfer_direction[] = {
37         XLAT(SG_DXFER_NONE),
38         XLAT(SG_DXFER_TO_DEV),
39         XLAT(SG_DXFER_FROM_DEV),
40         XLAT(SG_DXFER_TO_FROM_DEV),
41         XLAT_END
42 };
43
44 static void
45 print_sg_io_buffer(struct tcb *tcp, unsigned char *addr, int len)
46 {
47         unsigned char *buf = NULL;
48         int     allocated, i;
49
50         if (len == 0)
51                 return;
52         allocated = (len > max_strlen) ? max_strlen : len;
53         if (len < 0 ||
54             (buf = malloc(allocated)) == NULL ||
55             umoven(tcp, (unsigned long) addr, allocated, (char *) buf) < 0) {
56                 tprintf("%p", addr);
57                 free(buf);
58                 return;
59         }
60         tprintf("%02x", buf[0]);
61         for (i = 1; i < allocated; ++i)
62                 tprintf(", %02x", buf[i]);
63         free(buf);
64         if (allocated != len)
65                 tprints(", ...");
66 }
67
68 static void
69 print_sg_io_req(struct tcb *tcp, struct sg_io_hdr *sg_io)
70 {
71         tprintf("{'%c', ", sg_io->interface_id);
72         printxval(sg_io_dxfer_direction, sg_io->dxfer_direction,
73                   "SG_DXFER_???");
74         tprintf(", cmd[%u]=[", sg_io->cmd_len);
75         print_sg_io_buffer(tcp, sg_io->cmdp, sg_io->cmd_len);
76         tprintf("], mx_sb_len=%d, ", sg_io->mx_sb_len);
77         tprintf("iovec_count=%d, ", sg_io->iovec_count);
78         tprintf("dxfer_len=%u, ", sg_io->dxfer_len);
79         tprintf("timeout=%u, ", sg_io->timeout);
80         tprintf("flags=%#x", sg_io->flags);
81
82         if (sg_io->dxfer_direction == SG_DXFER_TO_DEV ||
83             sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
84                 tprintf(", data[%u]=[", sg_io->dxfer_len);
85                 printstr(tcp, (unsigned long) sg_io->dxferp,
86                          sg_io->dxfer_len);
87                 tprints("]");
88         }
89 }
90
91 static void
92 print_sg_io_res(struct tcb *tcp, struct sg_io_hdr *sg_io)
93 {
94         if (sg_io->dxfer_direction == SG_DXFER_FROM_DEV ||
95             sg_io->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
96                 tprintf(", data[%u]=[", sg_io->dxfer_len);
97                 printstr(tcp, (unsigned long) sg_io->dxferp,
98                          sg_io->dxfer_len);
99                 tprints("]");
100         }
101         tprintf(", status=%02x, ", sg_io->status);
102         tprintf("masked_status=%02x, ", sg_io->masked_status);
103         tprintf("sb[%u]=[", sg_io->sb_len_wr);
104         print_sg_io_buffer(tcp, sg_io->sbp, sg_io->sb_len_wr);
105         tprintf("], host_status=%#x, ", sg_io->host_status);
106         tprintf("driver_status=%#x, ", sg_io->driver_status);
107         tprintf("resid=%d, ", sg_io->resid);
108         tprintf("duration=%d, ", sg_io->duration);
109         tprintf("info=%#x}", sg_io->info);
110 }
111
112 int
113 scsi_ioctl(struct tcb *tcp, long code, long arg)
114 {
115         switch (code) {
116         case SG_IO:
117                 if (entering(tcp)) {
118                         struct sg_io_hdr sg_io;
119
120                         if (umove(tcp, arg, &sg_io) < 0)
121                                 tprintf(", %#lx", arg);
122                         else {
123                                 tprints(", ");
124                                 print_sg_io_req(tcp, &sg_io);
125                         }
126                 }
127                 if (exiting(tcp)) {
128                         struct sg_io_hdr sg_io;
129
130                         if (!syserror(tcp) && umove(tcp, arg, &sg_io) >= 0)
131                                 print_sg_io_res(tcp, &sg_io);
132                         else
133                                 tprints("}");
134                 }
135                 break;
136         default:
137                 if (entering(tcp))
138                         tprintf(", %#lx", arg);
139                 break;
140         }
141         return 1;
142 }
143
144 #endif /* HAVE_SCSI_SG_H */