1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright 1997 Sean Eric Fagan 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. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Sean Eric Fagan 17 * 4. Neither the name of the author may be used to endorse or promote 18 * products derived from this software without specific prior written 19 * permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * This file has routines used to print out system calls and their 36 * arguments. 37 */ 38 39 #include <sys/aio.h> 40 #include <sys/capsicum.h> 41 #include <sys/types.h> 42 #define _WANT_FREEBSD11_KEVENT 43 #include <sys/event.h> 44 #include <sys/ioccom.h> 45 #include <sys/mman.h> 46 #include <sys/mount.h> 47 #include <sys/poll.h> 48 #include <sys/procfs.h> 49 #include <sys/ptrace.h> 50 #include <sys/resource.h> 51 #include <sys/sched.h> 52 #include <sys/socket.h> 53 #define _WANT_FREEBSD11_STAT 54 #include <sys/stat.h> 55 #include <sys/sysctl.h> 56 #include <sys/time.h> 57 #include <sys/un.h> 58 #include <sys/wait.h> 59 #include <netinet/in.h> 60 #include <netinet/sctp.h> 61 #include <arpa/inet.h> 62 63 #include <assert.h> 64 #include <ctype.h> 65 #include <err.h> 66 #define _WANT_KERNEL_ERRNO 67 #include <errno.h> 68 #include <fcntl.h> 69 #include <signal.h> 70 #include <stdbool.h> 71 #include <stddef.h> 72 #include <stdio.h> 73 #include <stdlib.h> 74 #include <string.h> 75 #include <sysdecode.h> 76 #include <unistd.h> 77 #include <vis.h> 78 79 #include "truss.h" 80 #include "extern.h" 81 #include "syscall.h" 82 83 /* 84 * This should probably be in its own file, sorted alphabetically. 85 * 86 * Note: We only scan this table on the initial syscall number to calling 87 * convention lookup, i.e. once each time a new syscall is encountered. This 88 * is unlikely to be a performance issue, but if it is we could sort this array 89 * and use a binary search instead. 90 */ 91 static const struct syscall_decode decoded_syscalls[] = { 92 /* Native ABI */ 93 { .name = "__acl_aclcheck_fd", .ret_type = 1, .nargs = 3, 94 .args = { { Int, 0 }, { Acltype, 1 }, { Ptr, 2 } } }, 95 { .name = "__acl_aclcheck_file", .ret_type = 1, .nargs = 3, 96 .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } }, 97 { .name = "__acl_aclcheck_link", .ret_type = 1, .nargs = 3, 98 .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } }, 99 { .name = "__acl_delete_fd", .ret_type = 1, .nargs = 2, 100 .args = { { Int, 0 }, { Acltype, 1 } } }, 101 { .name = "__acl_delete_file", .ret_type = 1, .nargs = 2, 102 .args = { { Name, 0 }, { Acltype, 1 } } }, 103 { .name = "__acl_delete_link", .ret_type = 1, .nargs = 2, 104 .args = { { Name, 0 }, { Acltype, 1 } } }, 105 { .name = "__acl_get_fd", .ret_type = 1, .nargs = 3, 106 .args = { { Int, 0 }, { Acltype, 1 }, { Ptr, 2 } } }, 107 { .name = "__acl_get_file", .ret_type = 1, .nargs = 3, 108 .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } }, 109 { .name = "__acl_get_link", .ret_type = 1, .nargs = 3, 110 .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } }, 111 { .name = "__acl_set_fd", .ret_type = 1, .nargs = 3, 112 .args = { { Int, 0 }, { Acltype, 1 }, { Ptr, 2 } } }, 113 { .name = "__acl_set_file", .ret_type = 1, .nargs = 3, 114 .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } }, 115 { .name = "__acl_set_link", .ret_type = 1, .nargs = 3, 116 .args = { { Name, 0 }, { Acltype, 1 }, { Ptr, 2 } } }, 117 { .name = "__cap_rights_get", .ret_type = 1, .nargs = 3, 118 .args = { { Int, 0 }, { Int, 1 }, { CapRights | OUT, 2 } } }, 119 { .name = "__getcwd", .ret_type = 1, .nargs = 2, 120 .args = { { Name | OUT, 0 }, { Int, 1 } } }, 121 { .name = "__realpathat", .ret_type = 1, .nargs = 5, 122 .args = { { Atfd, 0 }, { Name | IN, 1 }, { Name | OUT, 2 }, 123 { Sizet, 3 }, { Int, 4} } }, 124 { .name = "_umtx_op", .ret_type = 1, .nargs = 5, 125 .args = { { Ptr, 0 }, { Umtxop, 1 }, { LongHex, 2 }, { Ptr, 3 }, 126 { Ptr, 4 } } }, 127 { .name = "accept", .ret_type = 1, .nargs = 3, 128 .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 129 { .name = "access", .ret_type = 1, .nargs = 2, 130 .args = { { Name | IN, 0 }, { Accessmode, 1 } } }, 131 { .name = "aio_cancel", .ret_type = 1, .nargs = 2, 132 .args = { { Int, 0 }, { Aiocb, 1 } } }, 133 { .name = "aio_error", .ret_type = 1, .nargs = 1, 134 .args = { { Aiocb, 0 } } }, 135 { .name = "aio_fsync", .ret_type = 1, .nargs = 2, 136 .args = { { AiofsyncOp, 0 }, { Aiocb, 1 } } }, 137 { .name = "aio_mlock", .ret_type = 1, .nargs = 1, 138 .args = { { Aiocb, 0 } } }, 139 { .name = "aio_read", .ret_type = 1, .nargs = 1, 140 .args = { { Aiocb, 0 } } }, 141 { .name = "aio_return", .ret_type = 1, .nargs = 1, 142 .args = { { Aiocb, 0 } } }, 143 { .name = "aio_suspend", .ret_type = 1, .nargs = 3, 144 .args = { { AiocbArray, 0 }, { Int, 1 }, { Timespec, 2 } } }, 145 { .name = "aio_waitcomplete", .ret_type = 1, .nargs = 2, 146 .args = { { AiocbPointer | OUT, 0 }, { Timespec, 1 } } }, 147 { .name = "aio_write", .ret_type = 1, .nargs = 1, 148 .args = { { Aiocb, 0 } } }, 149 { .name = "bind", .ret_type = 1, .nargs = 3, 150 .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Socklent, 2 } } }, 151 { .name = "bindat", .ret_type = 1, .nargs = 4, 152 .args = { { Atfd, 0 }, { Int, 1 }, { Sockaddr | IN, 2 }, 153 { Int, 3 } } }, 154 { .name = "break", .ret_type = 1, .nargs = 1, 155 .args = { { Ptr, 0 } } }, 156 { .name = "cap_fcntls_get", .ret_type = 1, .nargs = 2, 157 .args = { { Int, 0 }, { CapFcntlRights | OUT, 1 } } }, 158 { .name = "cap_fcntls_limit", .ret_type = 1, .nargs = 2, 159 .args = { { Int, 0 }, { CapFcntlRights, 1 } } }, 160 { .name = "cap_getmode", .ret_type = 1, .nargs = 1, 161 .args = { { PUInt | OUT, 0 } } }, 162 { .name = "cap_rights_limit", .ret_type = 1, .nargs = 2, 163 .args = { { Int, 0 }, { CapRights, 1 } } }, 164 { .name = "chdir", .ret_type = 1, .nargs = 1, 165 .args = { { Name, 0 } } }, 166 { .name = "chflags", .ret_type = 1, .nargs = 2, 167 .args = { { Name | IN, 0 }, { FileFlags, 1 } } }, 168 { .name = "chflagsat", .ret_type = 1, .nargs = 4, 169 .args = { { Atfd, 0 }, { Name | IN, 1 }, { FileFlags, 2 }, 170 { Atflags, 3 } } }, 171 { .name = "chmod", .ret_type = 1, .nargs = 2, 172 .args = { { Name, 0 }, { Octal, 1 } } }, 173 { .name = "chown", .ret_type = 1, .nargs = 3, 174 .args = { { Name, 0 }, { Int, 1 }, { Int, 2 } } }, 175 { .name = "chroot", .ret_type = 1, .nargs = 1, 176 .args = { { Name, 0 } } }, 177 { .name = "clock_gettime", .ret_type = 1, .nargs = 2, 178 .args = { { Int, 0 }, { Timespec | OUT, 1 } } }, 179 { .name = "close", .ret_type = 1, .nargs = 1, 180 .args = { { Int, 0 } } }, 181 { .name = "closefrom", .ret_type = 1, .nargs = 1, 182 .args = { { Int, 0 } } }, 183 { .name = "close_range", .ret_type = 1, .nargs = 3, 184 .args = { { Int, 0 }, { Int, 1 }, { Closerangeflags, 2 } } }, 185 { .name = "compat11.fstat", .ret_type = 1, .nargs = 2, 186 .args = { { Int, 0 }, { Stat11 | OUT, 1 } } }, 187 { .name = "compat11.fstatat", .ret_type = 1, .nargs = 4, 188 .args = { { Atfd, 0 }, { Name | IN, 1 }, { Stat11 | OUT, 2 }, 189 { Atflags, 3 } } }, 190 { .name = "compat11.kevent", .ret_type = 1, .nargs = 6, 191 .args = { { Int, 0 }, { Kevent11, 1 }, { Int, 2 }, 192 { Kevent11 | OUT, 3 }, { Int, 4 }, { Timespec, 5 } } }, 193 { .name = "compat11.lstat", .ret_type = 1, .nargs = 2, 194 .args = { { Name | IN, 0 }, { Stat11 | OUT, 1 } } }, 195 { .name = "compat11.mknod", .ret_type = 1, .nargs = 3, 196 .args = { { Name, 0 }, { Octal, 1 }, { Int, 2 } } }, 197 { .name = "compat11.mknodat", .ret_type = 1, .nargs = 4, 198 .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 }, { Int, 3 } } }, 199 { .name = "compat11.stat", .ret_type = 1, .nargs = 2, 200 .args = { { Name | IN, 0 }, { Stat11 | OUT, 1 } } }, 201 { .name = "connect", .ret_type = 1, .nargs = 3, 202 .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Socklent, 2 } } }, 203 { .name = "connectat", .ret_type = 1, .nargs = 4, 204 .args = { { Atfd, 0 }, { Int, 1 }, { Sockaddr | IN, 2 }, 205 { Int, 3 } } }, 206 { .name = "dup", .ret_type = 1, .nargs = 1, 207 .args = { { Int, 0 } } }, 208 { .name = "dup2", .ret_type = 1, .nargs = 2, 209 .args = { { Int, 0 }, { Int, 1 } } }, 210 { .name = "eaccess", .ret_type = 1, .nargs = 2, 211 .args = { { Name | IN, 0 }, { Accessmode, 1 } } }, 212 { .name = "execve", .ret_type = 1, .nargs = 3, 213 .args = { { Name | IN, 0 }, { ExecArgs | IN, 1 }, 214 { ExecEnv | IN, 2 } } }, 215 { .name = "exit", .ret_type = 0, .nargs = 1, 216 .args = { { Hex, 0 } } }, 217 { .name = "extattr_delete_fd", .ret_type = 1, .nargs = 3, 218 .args = { { Int, 0 }, { Extattrnamespace, 1 }, { Name, 2 } } }, 219 { .name = "extattr_delete_file", .ret_type = 1, .nargs = 3, 220 .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 } } }, 221 { .name = "extattr_delete_link", .ret_type = 1, .nargs = 3, 222 .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 } } }, 223 { .name = "extattr_get_fd", .ret_type = 1, .nargs = 5, 224 .args = { { Int, 0 }, { Extattrnamespace, 1 }, { Name, 2 }, 225 { BinString | OUT, 3 }, { Sizet, 4 } } }, 226 { .name = "extattr_get_file", .ret_type = 1, .nargs = 5, 227 .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 }, 228 { BinString | OUT, 3 }, { Sizet, 4 } } }, 229 { .name = "extattr_get_link", .ret_type = 1, .nargs = 5, 230 .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 }, 231 { BinString | OUT, 3 }, { Sizet, 4 } } }, 232 { .name = "extattr_list_fd", .ret_type = 1, .nargs = 4, 233 .args = { { Int, 0 }, { Extattrnamespace, 1 }, { BinString | OUT, 2 }, 234 { Sizet, 3 } } }, 235 { .name = "extattr_list_file", .ret_type = 1, .nargs = 4, 236 .args = { { Name, 0 }, { Extattrnamespace, 1 }, { BinString | OUT, 2 }, 237 { Sizet, 3 } } }, 238 { .name = "extattr_list_link", .ret_type = 1, .nargs = 4, 239 .args = { { Name, 0 }, { Extattrnamespace, 1 }, { BinString | OUT, 2 }, 240 { Sizet, 3 } } }, 241 { .name = "extattr_set_fd", .ret_type = 1, .nargs = 5, 242 .args = { { Int, 0 }, { Extattrnamespace, 1 }, { Name, 2 }, 243 { BinString | IN, 3 }, { Sizet, 4 } } }, 244 { .name = "extattr_set_file", .ret_type = 1, .nargs = 5, 245 .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 }, 246 { BinString | IN, 3 }, { Sizet, 4 } } }, 247 { .name = "extattr_set_link", .ret_type = 1, .nargs = 5, 248 .args = { { Name, 0 }, { Extattrnamespace, 1 }, { Name, 2 }, 249 { BinString | IN, 3 }, { Sizet, 4 } } }, 250 { .name = "extattrctl", .ret_type = 1, .nargs = 5, 251 .args = { { Name, 0 }, { Hex, 1 }, { Name, 2 }, 252 { Extattrnamespace, 3 }, { Name, 4 } } }, 253 { .name = "faccessat", .ret_type = 1, .nargs = 4, 254 .args = { { Atfd, 0 }, { Name | IN, 1 }, { Accessmode, 2 }, 255 { Atflags, 3 } } }, 256 { .name = "fchflags", .ret_type = 1, .nargs = 2, 257 .args = { { Int, 0 }, { FileFlags, 1 } } }, 258 { .name = "fchmod", .ret_type = 1, .nargs = 2, 259 .args = { { Int, 0 }, { Octal, 1 } } }, 260 { .name = "fchmodat", .ret_type = 1, .nargs = 4, 261 .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 }, { Atflags, 3 } } }, 262 { .name = "fchown", .ret_type = 1, .nargs = 3, 263 .args = { { Int, 0 }, { Int, 1 }, { Int, 2 } } }, 264 { .name = "fchownat", .ret_type = 1, .nargs = 5, 265 .args = { { Atfd, 0 }, { Name, 1 }, { Int, 2 }, { Int, 3 }, 266 { Atflags, 4 } } }, 267 { .name = "fcntl", .ret_type = 1, .nargs = 3, 268 .args = { { Int, 0 }, { Fcntl, 1 }, { Fcntlflag, 2 } } }, 269 { .name = "fdatasync", .ret_type = 1, .nargs = 1, 270 .args = { { Int, 0 } } }, 271 { .name = "flock", .ret_type = 1, .nargs = 2, 272 .args = { { Int, 0 }, { Flockop, 1 } } }, 273 { .name = "fstat", .ret_type = 1, .nargs = 2, 274 .args = { { Int, 0 }, { Stat | OUT, 1 } } }, 275 { .name = "fstatat", .ret_type = 1, .nargs = 4, 276 .args = { { Atfd, 0 }, { Name | IN, 1 }, { Stat | OUT, 2 }, 277 { Atflags, 3 } } }, 278 { .name = "fstatfs", .ret_type = 1, .nargs = 2, 279 .args = { { Int, 0 }, { StatFs | OUT, 1 } } }, 280 { .name = "fsync", .ret_type = 1, .nargs = 1, 281 .args = { { Int, 0 } } }, 282 { .name = "ftruncate", .ret_type = 1, .nargs = 2, 283 .args = { { Int | IN, 0 }, { QuadHex | IN, 1 } } }, 284 { .name = "futimens", .ret_type = 1, .nargs = 2, 285 .args = { { Int, 0 }, { Timespec2 | IN, 1 } } }, 286 { .name = "futimes", .ret_type = 1, .nargs = 2, 287 .args = { { Int, 0 }, { Timeval2 | IN, 1 } } }, 288 { .name = "futimesat", .ret_type = 1, .nargs = 3, 289 .args = { { Atfd, 0 }, { Name | IN, 1 }, { Timeval2 | IN, 2 } } }, 290 { .name = "getdirentries", .ret_type = 1, .nargs = 4, 291 .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 }, 292 { PQuadHex | OUT, 3 } } }, 293 { .name = "getfsstat", .ret_type = 1, .nargs = 3, 294 .args = { { Ptr, 0 }, { Long, 1 }, { Getfsstatmode, 2 } } }, 295 { .name = "getitimer", .ret_type = 1, .nargs = 2, 296 .args = { { Itimerwhich, 0 }, { Itimerval | OUT, 2 } } }, 297 { .name = "getpeername", .ret_type = 1, .nargs = 3, 298 .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 299 { .name = "getpgid", .ret_type = 1, .nargs = 1, 300 .args = { { Int, 0 } } }, 301 { .name = "getpriority", .ret_type = 1, .nargs = 2, 302 .args = { { Priowhich, 0 }, { Int, 1 } } }, 303 { .name = "getrandom", .ret_type = 1, .nargs = 3, 304 .args = { { BinString | OUT, 0 }, { Sizet, 1 }, { UInt, 2 } } }, 305 { .name = "getrlimit", .ret_type = 1, .nargs = 2, 306 .args = { { Resource, 0 }, { Rlimit | OUT, 1 } } }, 307 { .name = "getrusage", .ret_type = 1, .nargs = 2, 308 .args = { { RusageWho, 0 }, { Rusage | OUT, 1 } } }, 309 { .name = "getsid", .ret_type = 1, .nargs = 1, 310 .args = { { Int, 0 } } }, 311 { .name = "getsockname", .ret_type = 1, .nargs = 3, 312 .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, 313 { .name = "getsockopt", .ret_type = 1, .nargs = 5, 314 .args = { { Int, 0 }, { Sockoptlevel, 1 }, { Sockoptname, 2 }, 315 { Ptr | OUT, 3 }, { Ptr | OUT, 4 } } }, 316 { .name = "gettimeofday", .ret_type = 1, .nargs = 2, 317 .args = { { Timeval | OUT, 0 }, { Ptr, 1 } } }, 318 { .name = "inotify_add_watch_at", .ret_type = 1, .nargs = 4, 319 .args = { { Int, 0 }, { Atfd, 1 }, { Name | IN, 2 }, 320 { Inotifyflags, 3 } } }, 321 { .name = "ioctl", .ret_type = 1, .nargs = 3, 322 .args = { { Int, 0 }, { Ioctl, 1 }, { Ptr, 2 } } }, 323 { .name = "kevent", .ret_type = 1, .nargs = 6, 324 .args = { { Int, 0 }, { Kevent, 1 }, { Int, 2 }, { Kevent | OUT, 3 }, 325 { Int, 4 }, { Timespec, 5 } } }, 326 { .name = "kill", .ret_type = 1, .nargs = 2, 327 .args = { { Int | IN, 0 }, { Signal | IN, 1 } } }, 328 { .name = "kldfind", .ret_type = 1, .nargs = 1, 329 .args = { { Name | IN, 0 } } }, 330 { .name = "kldfirstmod", .ret_type = 1, .nargs = 1, 331 .args = { { Int, 0 } } }, 332 { .name = "kldload", .ret_type = 1, .nargs = 1, 333 .args = { { Name | IN, 0 } } }, 334 { .name = "kldnext", .ret_type = 1, .nargs = 1, 335 .args = { { Int, 0 } } }, 336 { .name = "kldstat", .ret_type = 1, .nargs = 2, 337 .args = { { Int, 0 }, { Ptr, 1 } } }, 338 { .name = "kldsym", .ret_type = 1, .nargs = 3, 339 .args = { { Int, 0 }, { Kldsymcmd, 1 }, { Ptr, 2 } } }, 340 { .name = "kldunload", .ret_type = 1, .nargs = 1, 341 .args = { { Int, 0 } } }, 342 { .name = "kldunloadf", .ret_type = 1, .nargs = 2, 343 .args = { { Int, 0 }, { Kldunloadflags, 1 } } }, 344 { .name = "kse_release", .ret_type = 0, .nargs = 1, 345 .args = { { Timespec, 0 } } }, 346 { .name = "lchflags", .ret_type = 1, .nargs = 2, 347 .args = { { Name | IN, 0 }, { FileFlags, 1 } } }, 348 { .name = "lchmod", .ret_type = 1, .nargs = 2, 349 .args = { { Name, 0 }, { Octal, 1 } } }, 350 { .name = "lchown", .ret_type = 1, .nargs = 3, 351 .args = { { Name, 0 }, { Int, 1 }, { Int, 2 } } }, 352 { .name = "link", .ret_type = 1, .nargs = 2, 353 .args = { { Name, 0 }, { Name, 1 } } }, 354 { .name = "linkat", .ret_type = 1, .nargs = 5, 355 .args = { { Atfd, 0 }, { Name, 1 }, { Atfd, 2 }, { Name, 3 }, 356 { Atflags, 4 } } }, 357 { .name = "lio_listio", .ret_type = 1, .nargs = 4, 358 .args = { { LioMode, 0 }, { AiocbArray, 1 }, { Int, 2 }, 359 { Sigevent, 3 } } }, 360 { .name = "listen", .ret_type = 1, .nargs = 2, 361 .args = { { Int, 0 }, { Int, 1 } } }, 362 { .name = "lseek", .ret_type = 2, .nargs = 3, 363 .args = { { Int, 0 }, { QuadHex, 1 }, { Whence, 2 } } }, 364 { .name = "lstat", .ret_type = 1, .nargs = 2, 365 .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } }, 366 { .name = "lutimes", .ret_type = 1, .nargs = 2, 367 .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } }, 368 { .name = "madvise", .ret_type = 1, .nargs = 3, 369 .args = { { Ptr, 0 }, { Sizet, 1 }, { Madvice, 2 } } }, 370 { .name = "minherit", .ret_type = 1, .nargs = 3, 371 .args = { { Ptr, 0 }, { Sizet, 1 }, { Minherit, 2 } } }, 372 { .name = "mkdir", .ret_type = 1, .nargs = 2, 373 .args = { { Name, 0 }, { Octal, 1 } } }, 374 { .name = "mkdirat", .ret_type = 1, .nargs = 3, 375 .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 } } }, 376 { .name = "mkfifo", .ret_type = 1, .nargs = 2, 377 .args = { { Name, 0 }, { Octal, 1 } } }, 378 { .name = "mkfifoat", .ret_type = 1, .nargs = 3, 379 .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 } } }, 380 { .name = "mknod", .ret_type = 1, .nargs = 3, 381 .args = { { Name, 0 }, { Octal, 1 }, { Quad, 2 } } }, 382 { .name = "mknodat", .ret_type = 1, .nargs = 4, 383 .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 }, { Quad, 3 } } }, 384 { .name = "mlock", .ret_type = 1, .nargs = 2, 385 .args = { { Ptr, 0 }, { Sizet, 1 } } }, 386 { .name = "mlockall", .ret_type = 1, .nargs = 1, 387 .args = { { Mlockall, 0 } } }, 388 { .name = "mmap", .ret_type = 1, .nargs = 6, 389 .args = { { Ptr, 0 }, { Sizet, 1 }, { Mprot, 2 }, { Mmapflags, 3 }, 390 { Int, 4 }, { QuadHex, 5 } } }, 391 { .name = "modfind", .ret_type = 1, .nargs = 1, 392 .args = { { Name | IN, 0 } } }, 393 { .name = "mount", .ret_type = 1, .nargs = 4, 394 .args = { { Name, 0 }, { Name, 1 }, { Mountflags, 2 }, { Ptr, 3 } } }, 395 { .name = "mprotect", .ret_type = 1, .nargs = 3, 396 .args = { { Ptr, 0 }, { Sizet, 1 }, { Mprot, 2 } } }, 397 { .name = "msync", .ret_type = 1, .nargs = 3, 398 .args = { { Ptr, 0 }, { Sizet, 1 }, { Msync, 2 } } }, 399 { .name = "munlock", .ret_type = 1, .nargs = 2, 400 .args = { { Ptr, 0 }, { Sizet, 1 } } }, 401 { .name = "munmap", .ret_type = 1, .nargs = 2, 402 .args = { { Ptr, 0 }, { Sizet, 1 } } }, 403 { .name = "nanosleep", .ret_type = 1, .nargs = 1, 404 .args = { { Timespec, 0 } } }, 405 { .name = "nmount", .ret_type = 1, .nargs = 3, 406 .args = { { Ptr, 0 }, { UInt, 1 }, { Mountflags, 2 } } }, 407 { .name = "open", .ret_type = 1, .nargs = 3, 408 .args = { { Name | IN, 0 }, { Open, 1 }, { Octal, 2 } } }, 409 { .name = "openat", .ret_type = 1, .nargs = 4, 410 .args = { { Atfd, 0 }, { Name | IN, 1 }, { Open, 2 }, 411 { Octal, 3 } } }, 412 { .name = "pathconf", .ret_type = 1, .nargs = 2, 413 .args = { { Name | IN, 0 }, { Pathconf, 1 } } }, 414 { .name = "pipe", .ret_type = 1, .nargs = 1, 415 .args = { { PipeFds | OUT, 0 } } }, 416 { .name = "pipe2", .ret_type = 1, .nargs = 2, 417 .args = { { Ptr, 0 }, { Pipe2, 1 } } }, 418 { .name = "poll", .ret_type = 1, .nargs = 3, 419 .args = { { Pollfd, 0 }, { Int, 1 }, { Int, 2 } } }, 420 { .name = "posix_fadvise", .ret_type = 1, .nargs = 4, 421 .args = { { Int, 0 }, { QuadHex, 1 }, { QuadHex, 2 }, 422 { Fadvice, 3 } } }, 423 { .name = "posix_openpt", .ret_type = 1, .nargs = 1, 424 .args = { { Open, 0 } } }, 425 { .name = "ppoll", .ret_type = 1, .nargs = 4, 426 .args = { { Pollfd, 0 }, { Int, 1 }, { Timespec | IN, 2 }, 427 { Sigset | IN, 3 } } }, 428 { .name = "pread", .ret_type = 1, .nargs = 4, 429 .args = { { Int, 0 }, { BinString | OUT, 1 }, { Sizet, 2 }, 430 { QuadHex, 3 } } }, 431 { .name = "preadv", .ret_type = 1, .nargs = 4, 432 .args = { { Int, 0 }, { Iovec | OUT, 1 }, { Int, 2 }, 433 { QuadHex, 3 } } }, 434 { .name = "procctl", .ret_type = 1, .nargs = 4, 435 .args = { { Idtype, 0 }, { Quad, 1 }, { Procctl, 2 }, { Ptr, 3 } } }, 436 { .name = "ptrace", .ret_type = 1, .nargs = 4, 437 .args = { { Ptraceop, 0 }, { Int, 1 }, { Ptr, 2 }, { Int, 3 } } }, 438 { .name = "pwrite", .ret_type = 1, .nargs = 4, 439 .args = { { Int, 0 }, { BinString | IN, 1 }, { Sizet, 2 }, 440 { QuadHex, 3 } } }, 441 { .name = "pwritev", .ret_type = 1, .nargs = 4, 442 .args = { { Int, 0 }, { Iovec | IN, 1 }, { Int, 2 }, 443 { QuadHex, 3 } } }, 444 { .name = "quotactl", .ret_type = 1, .nargs = 4, 445 .args = { { Name, 0 }, { Quotactlcmd, 1 }, { Int, 2 }, { Ptr, 3 } } }, 446 { .name = "read", .ret_type = 1, .nargs = 3, 447 .args = { { Int, 0 }, { BinString | OUT, 1 }, { Sizet, 2 } } }, 448 { .name = "readlink", .ret_type = 1, .nargs = 3, 449 .args = { { Name, 0 }, { Readlinkres | OUT, 1 }, { Sizet, 2 } } }, 450 { .name = "readlinkat", .ret_type = 1, .nargs = 4, 451 .args = { { Atfd, 0 }, { Name, 1 }, { Readlinkres | OUT, 2 }, 452 { Sizet, 3 } } }, 453 { .name = "readv", .ret_type = 1, .nargs = 3, 454 .args = { { Int, 0 }, { Iovec | OUT, 1 }, { Int, 2 } } }, 455 { .name = "reboot", .ret_type = 1, .nargs = 1, 456 .args = { { Reboothowto, 0 } } }, 457 { .name = "recvfrom", .ret_type = 1, .nargs = 6, 458 .args = { { Int, 0 }, { BinString | OUT, 1 }, { Sizet, 2 }, 459 { Msgflags, 3 }, { Sockaddr | OUT, 4 }, 460 { Ptr | OUT, 5 } } }, 461 { .name = "recvmsg", .ret_type = 1, .nargs = 3, 462 .args = { { Int, 0 }, { Msghdr | OUT, 1 }, { Msgflags, 2 } } }, 463 { .name = "rename", .ret_type = 1, .nargs = 2, 464 .args = { { Name, 0 }, { Name, 1 } } }, 465 { .name = "renameat", .ret_type = 1, .nargs = 4, 466 .args = { { Atfd, 0 }, { Name, 1 }, { Atfd, 2 }, { Name, 3 } } }, 467 { .name = "rfork", .ret_type = 1, .nargs = 1, 468 .args = { { Rforkflags, 0 } } }, 469 { .name = "rmdir", .ret_type = 1, .nargs = 1, 470 .args = { { Name, 0 } } }, 471 { .name = "rtprio", .ret_type = 1, .nargs = 3, 472 .args = { { Rtpriofunc, 0 }, { Int, 1 }, { Ptr, 2 } } }, 473 { .name = "rtprio_thread", .ret_type = 1, .nargs = 3, 474 .args = { { Rtpriofunc, 0 }, { Int, 1 }, { Ptr, 2 } } }, 475 { .name = "sched_get_priority_max", .ret_type = 1, .nargs = 1, 476 .args = { { Schedpolicy, 0 } } }, 477 { .name = "sched_get_priority_min", .ret_type = 1, .nargs = 1, 478 .args = { { Schedpolicy, 0 } } }, 479 { .name = "sched_getparam", .ret_type = 1, .nargs = 2, 480 .args = { { Int, 0 }, { Schedparam | OUT, 1 } } }, 481 { .name = "sched_getscheduler", .ret_type = 1, .nargs = 1, 482 .args = { { Int, 0 } } }, 483 { .name = "sched_rr_get_interval", .ret_type = 1, .nargs = 2, 484 .args = { { Int, 0 }, { Timespec | OUT, 1 } } }, 485 { .name = "sched_setparam", .ret_type = 1, .nargs = 2, 486 .args = { { Int, 0 }, { Schedparam, 1 } } }, 487 { .name = "sched_setscheduler", .ret_type = 1, .nargs = 3, 488 .args = { { Int, 0 }, { Schedpolicy, 1 }, { Schedparam, 2 } } }, 489 { .name = "sctp_generic_recvmsg", .ret_type = 1, .nargs = 7, 490 .args = { { Int, 0 }, { Iovec | OUT, 1 }, { Int, 2 }, 491 { Sockaddr | OUT, 3 }, { Ptr | OUT, 4 }, 492 { Sctpsndrcvinfo | OUT, 5 }, { Ptr | OUT, 6 } } }, 493 { .name = "sctp_generic_sendmsg", .ret_type = 1, .nargs = 7, 494 .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 }, 495 { Sockaddr | IN, 3 }, { Socklent, 4 }, 496 { Sctpsndrcvinfo | IN, 5 }, { Msgflags, 6 } } }, 497 { .name = "sctp_generic_sendmsg_iov", .ret_type = 1, .nargs = 7, 498 .args = { { Int, 0 }, { Iovec | IN, 1 }, { Int, 2 }, 499 { Sockaddr | IN, 3 }, { Socklent, 4 }, 500 { Sctpsndrcvinfo | IN, 5 }, { Msgflags, 6 } } }, 501 { .name = "sendfile", .ret_type = 1, .nargs = 7, 502 .args = { { Int, 0 }, { Int, 1 }, { QuadHex, 2 }, { Sizet, 3 }, 503 { Sendfilehdtr, 4 }, { QuadHex | OUT, 5 }, 504 { Sendfileflags, 6 } } }, 505 { .name = "select", .ret_type = 1, .nargs = 5, 506 .args = { { Int, 0 }, { Fd_set, 1 }, { Fd_set, 2 }, { Fd_set, 3 }, 507 { Timeval, 4 } } }, 508 { .name = "sendmsg", .ret_type = 1, .nargs = 3, 509 .args = { { Int, 0 }, { Msghdr | IN, 1 }, { Msgflags, 2 } } }, 510 { .name = "sendto", .ret_type = 1, .nargs = 6, 511 .args = { { Int, 0 }, { BinString | IN, 1 }, { Sizet, 2 }, 512 { Msgflags, 3 }, { Sockaddr | IN, 4 }, 513 { Socklent | IN, 5 } } }, 514 { .name = "setitimer", .ret_type = 1, .nargs = 3, 515 .args = { { Itimerwhich, 0 }, { Itimerval, 1 }, 516 { Itimerval | OUT, 2 } } }, 517 { .name = "setpriority", .ret_type = 1, .nargs = 3, 518 .args = { { Priowhich, 0 }, { Int, 1 }, { Int, 2 } } }, 519 { .name = "setrlimit", .ret_type = 1, .nargs = 2, 520 .args = { { Resource, 0 }, { Rlimit | IN, 1 } } }, 521 { .name = "setsockopt", .ret_type = 1, .nargs = 5, 522 .args = { { Int, 0 }, { Sockoptlevel, 1 }, { Sockoptname, 2 }, 523 { Ptr | IN, 3 }, { Socklent, 4 } } }, 524 { .name = "shm_open", .ret_type = 1, .nargs = 3, 525 .args = { { ShmName | IN, 0 }, { Open, 1 }, { Octal, 2 } } }, 526 { .name = "shm_open2", .ret_type = 1, .nargs = 5, 527 .args = { { ShmName | IN, 0 }, { Open, 1 }, { Octal, 2 }, 528 { ShmFlags, 3 }, { Name | IN, 4 } } }, 529 { .name = "shm_rename", .ret_type = 1, .nargs = 3, 530 .args = { { Name | IN, 0 }, { Name | IN, 1 }, { Hex, 2 } } }, 531 { .name = "shm_unlink", .ret_type = 1, .nargs = 1, 532 .args = { { Name | IN, 0 } } }, 533 { .name = "shutdown", .ret_type = 1, .nargs = 2, 534 .args = { { Int, 0 }, { Shutdown, 1 } } }, 535 { .name = "sigaction", .ret_type = 1, .nargs = 3, 536 .args = { { Signal, 0 }, { Sigaction | IN, 1 }, 537 { Sigaction | OUT, 2 } } }, 538 { .name = "sigpending", .ret_type = 1, .nargs = 1, 539 .args = { { Sigset | OUT, 0 } } }, 540 { .name = "sigprocmask", .ret_type = 1, .nargs = 3, 541 .args = { { Sigprocmask, 0 }, { Sigset, 1 }, { Sigset | OUT, 2 } } }, 542 { .name = "sigqueue", .ret_type = 1, .nargs = 3, 543 .args = { { Int, 0 }, { Signal, 1 }, { LongHex, 2 } } }, 544 { .name = "sigreturn", .ret_type = 1, .nargs = 1, 545 .args = { { Ptr, 0 } } }, 546 { .name = "sigsuspend", .ret_type = 1, .nargs = 1, 547 .args = { { Sigset | IN, 0 } } }, 548 { .name = "sigtimedwait", .ret_type = 1, .nargs = 3, 549 .args = { { Sigset | IN, 0 }, { Siginfo | OUT, 1 }, 550 { Timespec | IN, 2 } } }, 551 { .name = "sigwait", .ret_type = 1, .nargs = 2, 552 .args = { { Sigset | IN, 0 }, { PSig | OUT, 1 } } }, 553 { .name = "sigwaitinfo", .ret_type = 1, .nargs = 2, 554 .args = { { Sigset | IN, 0 }, { Siginfo | OUT, 1 } } }, 555 { .name = "socket", .ret_type = 1, .nargs = 3, 556 .args = { { Sockdomain, 0 }, { Socktype, 1 }, { Sockprotocol, 2 } } }, 557 { .name = "stat", .ret_type = 1, .nargs = 2, 558 .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } }, 559 { .name = "statfs", .ret_type = 1, .nargs = 2, 560 .args = { { Name | IN, 0 }, { StatFs | OUT, 1 } } }, 561 { .name = "symlink", .ret_type = 1, .nargs = 2, 562 .args = { { Name, 0 }, { Name, 1 } } }, 563 { .name = "symlinkat", .ret_type = 1, .nargs = 3, 564 .args = { { Name, 0 }, { Atfd, 1 }, { Name, 2 } } }, 565 { .name = "sysarch", .ret_type = 1, .nargs = 2, 566 .args = { { Sysarch, 0 }, { Ptr, 1 } } }, 567 { .name = "__sysctl", .ret_type = 1, .nargs = 6, 568 .args = { { Sysctl, 0 }, { Sizet, 1 }, { Ptr, 2 }, { Ptr, 3 }, 569 { Ptr, 4 }, { Sizet, 5 } } }, 570 { .name = "__sysctlbyname", .ret_type = 1, .nargs = 6, 571 .args = { { Name, 0 }, { Sizet, 1 }, { Ptr, 2 }, { Ptr, 3 }, 572 { Ptr, 4}, { Sizet, 5 } } }, 573 { .name = "thr_kill", .ret_type = 1, .nargs = 2, 574 .args = { { Long, 0 }, { Signal, 1 } } }, 575 { .name = "thr_self", .ret_type = 1, .nargs = 1, 576 .args = { { Ptr, 0 } } }, 577 { .name = "thr_set_name", .ret_type = 1, .nargs = 2, 578 .args = { { Long, 0 }, { Name, 1 } } }, 579 { .name = "truncate", .ret_type = 1, .nargs = 2, 580 .args = { { Name | IN, 0 }, { QuadHex | IN, 1 } } }, 581 { .name = "unlink", .ret_type = 1, .nargs = 1, 582 .args = { { Name, 0 } } }, 583 { .name = "unlinkat", .ret_type = 1, .nargs = 3, 584 .args = { { Atfd, 0 }, { Name, 1 }, { Atflags, 2 } } }, 585 { .name = "unmount", .ret_type = 1, .nargs = 2, 586 .args = { { Name, 0 }, { Mountflags, 1 } } }, 587 { .name = "utimensat", .ret_type = 1, .nargs = 4, 588 .args = { { Atfd, 0 }, { Name | IN, 1 }, { Timespec2 | IN, 2 }, 589 { Atflags, 3 } } }, 590 { .name = "utimes", .ret_type = 1, .nargs = 2, 591 .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } }, 592 { .name = "utrace", .ret_type = 1, .nargs = 1, 593 .args = { { Utrace, 0 } } }, 594 { .name = "wait4", .ret_type = 1, .nargs = 4, 595 .args = { { Int, 0 }, { ExitStatus | OUT, 1 }, { Waitoptions, 2 }, 596 { Rusage | OUT, 3 } } }, 597 { .name = "wait6", .ret_type = 1, .nargs = 6, 598 .args = { { Idtype, 0 }, { Quad, 1 }, { ExitStatus | OUT, 2 }, 599 { Waitoptions, 3 }, { Rusage | OUT, 4 }, 600 { Siginfo | OUT, 5 } } }, 601 { .name = "write", .ret_type = 1, .nargs = 3, 602 .args = { { Int, 0 }, { BinString | IN, 1 }, { Sizet, 2 } } }, 603 { .name = "writev", .ret_type = 1, .nargs = 3, 604 .args = { { Int, 0 }, { Iovec | IN, 1 }, { Int, 2 } } }, 605 606 /* Linux ABI */ 607 { .name = "linux_access", .ret_type = 1, .nargs = 2, 608 .args = { { Name, 0 }, { Accessmode, 1 } } }, 609 { .name = "linux_execve", .ret_type = 1, .nargs = 3, 610 .args = { { Name | IN, 0 }, { ExecArgs | IN, 1 }, 611 { ExecEnv | IN, 2 } } }, 612 { .name = "linux_getitimer", .ret_type = 1, .nargs = 2, 613 .args = { { Itimerwhich, 0 }, { Itimerval | OUT, 2 } } }, 614 { .name = "linux_lseek", .ret_type = 2, .nargs = 3, 615 .args = { { Int, 0 }, { Int, 1 }, { Whence, 2 } } }, 616 { .name = "linux_mkdir", .ret_type = 1, .nargs = 2, 617 .args = { { Name | IN, 0 }, { Int, 1 } } }, 618 { .name = "linux_newfstat", .ret_type = 1, .nargs = 2, 619 .args = { { Int, 0 }, { Ptr | OUT, 1 } } }, 620 { .name = "linux_newlstat", .ret_type = 1, .nargs = 2, 621 .args = { { Name | IN, 0 }, { Ptr | OUT, 1 } } }, 622 { .name = "linux_newstat", .ret_type = 1, .nargs = 2, 623 .args = { { Name | IN, 0 }, { Ptr | OUT, 1 } } }, 624 { .name = "linux_open", .ret_type = 1, .nargs = 3, 625 .args = { { Name, 0 }, { Hex, 1 }, { Octal, 2 } } }, 626 { .name = "linux_readlink", .ret_type = 1, .nargs = 3, 627 .args = { { Name, 0 }, { Name | OUT, 1 }, { Sizet, 2 } } }, 628 { .name = "linux_setitimer", .ret_type = 1, .nargs = 3, 629 .args = { { Itimerwhich, 0 }, { Itimerval, 1 }, 630 { Itimerval | OUT, 2 } } }, 631 { .name = "linux_socketcall", .ret_type = 1, .nargs = 2, 632 .args = { { Int, 0 }, { LinuxSockArgs, 1 } } }, 633 { .name = "linux_stat64", .ret_type = 1, .nargs = 2, 634 .args = { { Name | IN, 0 }, { Ptr | OUT, 1 } } }, 635 }; 636 static STAILQ_HEAD(, syscall) seen_syscalls; 637 638 /* Xlat idea taken from strace */ 639 struct xlat { 640 int val; 641 const char *str; 642 }; 643 644 #define X(a) { a, #a }, 645 #define XEND { 0, NULL } 646 647 static struct xlat poll_flags[] = { 648 X(POLLSTANDARD) X(POLLIN) X(POLLPRI) X(POLLOUT) X(POLLERR) 649 X(POLLHUP) X(POLLNVAL) X(POLLRDNORM) X(POLLRDBAND) 650 X(POLLWRBAND) X(POLLINIGNEOF) X(POLLRDHUP) XEND 651 }; 652 653 static struct xlat sigaction_flags[] = { 654 X(SA_ONSTACK) X(SA_RESTART) X(SA_RESETHAND) X(SA_NOCLDSTOP) 655 X(SA_NODEFER) X(SA_NOCLDWAIT) X(SA_SIGINFO) XEND 656 }; 657 658 static struct xlat linux_socketcall_ops[] = { 659 X(LINUX_SOCKET) X(LINUX_BIND) X(LINUX_CONNECT) X(LINUX_LISTEN) 660 X(LINUX_ACCEPT) X(LINUX_GETSOCKNAME) X(LINUX_GETPEERNAME) 661 X(LINUX_SOCKETPAIR) X(LINUX_SEND) X(LINUX_RECV) X(LINUX_SENDTO) 662 X(LINUX_RECVFROM) X(LINUX_SHUTDOWN) X(LINUX_SETSOCKOPT) 663 X(LINUX_GETSOCKOPT) X(LINUX_SENDMSG) X(LINUX_RECVMSG) 664 XEND 665 }; 666 667 static struct xlat lio_modes[] = { 668 X(LIO_WAIT) X(LIO_NOWAIT) 669 XEND 670 }; 671 672 static struct xlat lio_opcodes[] = { 673 X(LIO_WRITE) X(LIO_READ) X(LIO_READV) X(LIO_WRITEV) X(LIO_NOP) 674 XEND 675 }; 676 677 static struct xlat aio_fsync_ops[] = { 678 X(O_SYNC) 679 XEND 680 }; 681 682 #undef X 683 #undef XEND 684 685 /* 686 * Searches an xlat array for a value, and returns it if found. Otherwise 687 * return a string representation. 688 */ 689 static const char * 690 lookup(struct xlat *xlat, int val, int base) 691 { 692 static char tmp[16]; 693 694 for (; xlat->str != NULL; xlat++) 695 if (xlat->val == val) 696 return (xlat->str); 697 switch (base) { 698 case 8: 699 sprintf(tmp, "0%o", val); 700 break; 701 case 16: 702 sprintf(tmp, "0x%x", val); 703 break; 704 case 10: 705 sprintf(tmp, "%u", val); 706 break; 707 default: 708 errx(1, "Unknown lookup base"); 709 } 710 return (tmp); 711 } 712 713 static const char * 714 xlookup(struct xlat *xlat, int val) 715 { 716 717 return (lookup(xlat, val, 16)); 718 } 719 720 /* 721 * Searches an xlat array containing bitfield values. Remaining bits 722 * set after removing the known ones are printed at the end: 723 * IN|0x400. 724 */ 725 static char * 726 xlookup_bits(struct xlat *xlat, int val) 727 { 728 int len, rem; 729 static char str[512]; 730 731 len = 0; 732 rem = val; 733 for (; xlat->str != NULL; xlat++) { 734 if ((xlat->val & rem) == xlat->val) { 735 /* 736 * Don't print the "all-bits-zero" string unless all 737 * bits are really zero. 738 */ 739 if (xlat->val == 0 && val != 0) 740 continue; 741 len += sprintf(str + len, "%s|", xlat->str); 742 rem &= ~(xlat->val); 743 } 744 } 745 746 /* 747 * If we have leftover bits or didn't match anything, print 748 * the remainder. 749 */ 750 if (rem || len == 0) 751 len += sprintf(str + len, "0x%x", rem); 752 if (len && str[len - 1] == '|') 753 len--; 754 str[len] = 0; 755 return (str); 756 } 757 758 static void 759 print_integer_arg(const char *(*decoder)(int), FILE *fp, int value) 760 { 761 const char *str; 762 763 str = decoder(value); 764 if (str != NULL) 765 fputs(str, fp); 766 else 767 fprintf(fp, "%d", value); 768 } 769 770 static bool 771 print_mask_arg_part(bool (*decoder)(FILE *, int, int *), FILE *fp, int value, 772 int *rem) 773 { 774 775 return (decoder(fp, value, rem)); 776 } 777 778 static void 779 print_mask_arg(bool (*decoder)(FILE *, int, int *), FILE *fp, int value) 780 { 781 int rem; 782 783 if (!print_mask_arg_part(decoder, fp, value, &rem)) 784 fprintf(fp, "0x%x", rem); 785 else if (rem != 0) 786 fprintf(fp, "|0x%x", rem); 787 } 788 789 static void 790 print_mask_arg32(bool (*decoder)(FILE *, uint32_t, uint32_t *), FILE *fp, 791 uint32_t value) 792 { 793 uint32_t rem; 794 795 if (!decoder(fp, value, &rem)) 796 fprintf(fp, "0x%x", rem); 797 else if (rem != 0) 798 fprintf(fp, "|0x%x", rem); 799 } 800 801 /* 802 * Add argument padding to subsequent system calls after Quad 803 * syscall arguments as needed. This used to be done by hand in the 804 * decoded_syscalls table which was ugly and error prone. It is 805 * simpler to do the fixup of offsets at initialization time than when 806 * decoding arguments. 807 */ 808 static void 809 quad_fixup(struct syscall_decode *sc) 810 { 811 int offset, prev; 812 u_int i; 813 814 offset = 0; 815 prev = -1; 816 for (i = 0; i < sc->nargs; i++) { 817 /* This arg type is a dummy that doesn't use offset. */ 818 if ((sc->args[i].type & ARG_MASK) == PipeFds) 819 continue; 820 821 assert(prev < sc->args[i].offset); 822 prev = sc->args[i].offset; 823 sc->args[i].offset += offset; 824 switch (sc->args[i].type & ARG_MASK) { 825 case Quad: 826 case QuadHex: 827 #if defined(__powerpc__) || defined(__arm__) || defined(__aarch64__) 828 /* 829 * 64-bit arguments on 32-bit powerpc and arm must be 830 * 64-bit aligned. If the current offset is 831 * not aligned, the calling convention inserts 832 * a 32-bit pad argument that should be skipped. 833 */ 834 if (sc->args[i].offset % 2 == 1) { 835 sc->args[i].offset++; 836 offset++; 837 } 838 #endif 839 offset++; 840 default: 841 break; 842 } 843 } 844 } 845 846 static struct syscall * 847 find_syscall(struct procabi *abi, u_int number) 848 { 849 struct extra_syscall *es; 850 851 if (number < nitems(abi->syscalls)) 852 return (abi->syscalls[number]); 853 STAILQ_FOREACH(es, &abi->extra_syscalls, entries) { 854 if (es->number == number) 855 return (es->sc); 856 } 857 return (NULL); 858 } 859 860 static void 861 add_syscall(struct procabi *abi, u_int number, struct syscall *sc) 862 { 863 struct extra_syscall *es; 864 865 /* 866 * quad_fixup() is currently needed for all 32-bit ABIs. 867 * TODO: This should probably be a function pointer inside struct 868 * procabi instead. 869 */ 870 if (abi->pointer_size == 4) 871 quad_fixup(&sc->decode); 872 873 if (number < nitems(abi->syscalls)) { 874 assert(abi->syscalls[number] == NULL); 875 abi->syscalls[number] = sc; 876 } else { 877 es = malloc(sizeof(*es)); 878 es->sc = sc; 879 es->number = number; 880 STAILQ_INSERT_TAIL(&abi->extra_syscalls, es, entries); 881 } 882 883 STAILQ_INSERT_HEAD(&seen_syscalls, sc, entries); 884 } 885 886 /* 887 * If/when the list gets big, it might be desirable to do it 888 * as a hash table or binary search. 889 */ 890 struct syscall * 891 get_syscall(struct threadinfo *t, u_int number, u_int nargs) 892 { 893 struct syscall *sc; 894 struct procabi *procabi; 895 const char *sysdecode_name; 896 const char *lookup_name; 897 const char *name; 898 u_int i; 899 900 procabi = t->proc->abi; 901 sc = find_syscall(procabi, number); 902 if (sc != NULL) 903 return (sc); 904 905 /* Memory is not explicitly deallocated, it's released on exit(). */ 906 sysdecode_name = sysdecode_syscallname(procabi->abi, number); 907 if (sysdecode_name == NULL) 908 asprintf(__DECONST(char **, &name), "#%d", number); 909 else 910 name = sysdecode_name; 911 912 sc = calloc(1, sizeof(*sc)); 913 sc->name = name; 914 915 /* Also decode compat syscalls arguments by stripping the prefix. */ 916 lookup_name = name; 917 if (procabi->compat_prefix != NULL && strncmp(procabi->compat_prefix, 918 name, strlen(procabi->compat_prefix)) == 0) 919 lookup_name += strlen(procabi->compat_prefix); 920 921 for (i = 0; i < nitems(decoded_syscalls); i++) { 922 if (strcmp(lookup_name, decoded_syscalls[i].name) == 0) { 923 sc->decode = decoded_syscalls[i]; 924 add_syscall(t->proc->abi, number, sc); 925 return (sc); 926 } 927 } 928 929 /* It is unknown. Add it into the list. */ 930 #if DEBUG 931 fprintf(stderr, "unknown syscall %s -- setting args to %d\n", name, 932 nargs); 933 #endif 934 sc->unknown = sysdecode_name == NULL; 935 sc->decode.ret_type = 1; /* Assume 1 return value. */ 936 sc->decode.nargs = nargs; 937 for (i = 0; i < nargs; i++) { 938 sc->decode.args[i].offset = i; 939 /* Treat all unknown arguments as LongHex. */ 940 sc->decode.args[i].type = LongHex; 941 } 942 add_syscall(t->proc->abi, number, sc); 943 return (sc); 944 } 945 946 /* 947 * Copy a fixed amount of bytes from the process. 948 */ 949 static int 950 get_struct(pid_t pid, psaddr_t offset, void *buf, size_t len) 951 { 952 struct ptrace_io_desc iorequest; 953 954 iorequest.piod_op = PIOD_READ_D; 955 iorequest.piod_offs = (void *)(uintptr_t)offset; 956 iorequest.piod_addr = buf; 957 iorequest.piod_len = len; 958 if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) 959 return (-1); 960 return (0); 961 } 962 963 #define MAXSIZE 4096 964 965 /* 966 * Copy a string from the process. Note that it is 967 * expected to be a C string, but if max is set, it will 968 * only get that much. 969 */ 970 static char * 971 get_string(pid_t pid, psaddr_t addr, int max) 972 { 973 struct ptrace_io_desc iorequest; 974 char *buf, *nbuf; 975 size_t offset, size, totalsize; 976 977 offset = 0; 978 if (max) 979 size = max + 1; 980 else { 981 /* Read up to the end of the current page. */ 982 size = PAGE_SIZE - (addr % PAGE_SIZE); 983 if (size > MAXSIZE) 984 size = MAXSIZE; 985 } 986 totalsize = size; 987 buf = malloc(totalsize); 988 if (buf == NULL) 989 return (NULL); 990 for (;;) { 991 iorequest.piod_op = PIOD_READ_D; 992 iorequest.piod_offs = (void *)((uintptr_t)addr + offset); 993 iorequest.piod_addr = buf + offset; 994 iorequest.piod_len = size; 995 if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) { 996 free(buf); 997 return (NULL); 998 } 999 if (memchr(buf + offset, '\0', size) != NULL) 1000 return (buf); 1001 offset += size; 1002 if (totalsize < MAXSIZE && max == 0) { 1003 size = MAXSIZE - totalsize; 1004 if (size > PAGE_SIZE) 1005 size = PAGE_SIZE; 1006 nbuf = realloc(buf, totalsize + size); 1007 if (nbuf == NULL) { 1008 buf[totalsize - 1] = '\0'; 1009 return (buf); 1010 } 1011 buf = nbuf; 1012 totalsize += size; 1013 } else { 1014 buf[totalsize - 1] = '\0'; 1015 return (buf); 1016 } 1017 } 1018 } 1019 1020 static const char * 1021 strsig2(int sig) 1022 { 1023 static char tmp[32]; 1024 const char *signame; 1025 1026 signame = sysdecode_signal(sig); 1027 if (signame == NULL) { 1028 snprintf(tmp, sizeof(tmp), "%d", sig); 1029 signame = tmp; 1030 } 1031 return (signame); 1032 } 1033 1034 static void 1035 print_kevent(FILE *fp, struct kevent *ke) 1036 { 1037 1038 switch (ke->filter) { 1039 case EVFILT_READ: 1040 case EVFILT_WRITE: 1041 case EVFILT_VNODE: 1042 case EVFILT_PROC: 1043 case EVFILT_TIMER: 1044 case EVFILT_PROCDESC: 1045 case EVFILT_EMPTY: 1046 fprintf(fp, "%ju", (uintmax_t)ke->ident); 1047 break; 1048 case EVFILT_SIGNAL: 1049 fputs(strsig2(ke->ident), fp); 1050 break; 1051 default: 1052 fprintf(fp, "%p", (void *)ke->ident); 1053 } 1054 fprintf(fp, ","); 1055 print_integer_arg(sysdecode_kevent_filter, fp, ke->filter); 1056 fprintf(fp, ","); 1057 print_mask_arg(sysdecode_kevent_flags, fp, ke->flags); 1058 fprintf(fp, ","); 1059 sysdecode_kevent_fflags(fp, ke->filter, ke->fflags, 16); 1060 fprintf(fp, ",%#jx,%p", (uintmax_t)ke->data, ke->udata); 1061 } 1062 1063 static void 1064 print_utrace(FILE *fp, void *utrace_addr, size_t len) 1065 { 1066 unsigned char *utrace_buffer; 1067 1068 fprintf(fp, "{ "); 1069 if (sysdecode_utrace(fp, utrace_addr, len)) { 1070 fprintf(fp, " }"); 1071 return; 1072 } 1073 1074 utrace_buffer = utrace_addr; 1075 fprintf(fp, "%zu:", len); 1076 while (len--) 1077 fprintf(fp, " %02x", *utrace_buffer++); 1078 fprintf(fp, " }"); 1079 } 1080 1081 static void 1082 print_pointer(FILE *fp, uintptr_t arg) 1083 { 1084 1085 fprintf(fp, "%p", (void *)arg); 1086 } 1087 1088 static void 1089 print_sockaddr(FILE *fp, struct trussinfo *trussinfo, uintptr_t arg, 1090 socklen_t len) 1091 { 1092 char addr[64]; 1093 struct sockaddr_in *lsin; 1094 struct sockaddr_in6 *lsin6; 1095 struct sockaddr_un *sun; 1096 struct sockaddr *sa; 1097 u_char *q; 1098 pid_t pid = trussinfo->curthread->proc->pid; 1099 1100 if (arg == 0) { 1101 fputs("NULL", fp); 1102 return; 1103 } 1104 /* If the length is too small, just bail. */ 1105 if (len < sizeof(*sa)) { 1106 print_pointer(fp, arg); 1107 return; 1108 } 1109 1110 sa = calloc(1, len); 1111 if (get_struct(pid, arg, sa, len) == -1) { 1112 free(sa); 1113 print_pointer(fp, arg); 1114 return; 1115 } 1116 1117 switch (sa->sa_family) { 1118 case AF_INET: 1119 if (len < sizeof(*lsin)) 1120 goto sockaddr_short; 1121 lsin = (struct sockaddr_in *)(void *)sa; 1122 inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof(addr)); 1123 fprintf(fp, "{ AF_INET %s:%d }", addr, 1124 htons(lsin->sin_port)); 1125 break; 1126 case AF_INET6: 1127 if (len < sizeof(*lsin6)) 1128 goto sockaddr_short; 1129 lsin6 = (struct sockaddr_in6 *)(void *)sa; 1130 inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, 1131 sizeof(addr)); 1132 fprintf(fp, "{ AF_INET6 [%s]:%d }", addr, 1133 htons(lsin6->sin6_port)); 1134 break; 1135 case AF_UNIX: 1136 sun = (struct sockaddr_un *)sa; 1137 fprintf(fp, "{ AF_UNIX \"%.*s\" }", 1138 (int)(len - offsetof(struct sockaddr_un, sun_path)), 1139 sun->sun_path); 1140 break; 1141 default: 1142 sockaddr_short: 1143 fprintf(fp, 1144 "{ sa_len = %d, sa_family = %d, sa_data = {", 1145 (int)sa->sa_len, (int)sa->sa_family); 1146 for (q = (u_char *)sa->sa_data; 1147 q < (u_char *)sa + len; q++) 1148 fprintf(fp, "%s 0x%02x", 1149 q == (u_char *)sa->sa_data ? "" : ",", 1150 *q); 1151 fputs(" } }", fp); 1152 } 1153 free(sa); 1154 } 1155 1156 #define IOV_LIMIT 16 1157 1158 static void 1159 print_iovec(FILE *fp, struct trussinfo *trussinfo, uintptr_t arg, int iovcnt) 1160 { 1161 struct iovec iov[IOV_LIMIT]; 1162 size_t max_string = trussinfo->strsize; 1163 char tmp2[max_string + 1], *tmp3; 1164 size_t len; 1165 pid_t pid = trussinfo->curthread->proc->pid; 1166 int i; 1167 bool buf_truncated, iov_truncated; 1168 1169 if (iovcnt <= 0) { 1170 print_pointer(fp, arg); 1171 return; 1172 } 1173 if (iovcnt > IOV_LIMIT) { 1174 iovcnt = IOV_LIMIT; 1175 iov_truncated = true; 1176 } else { 1177 iov_truncated = false; 1178 } 1179 if (get_struct(pid, arg, &iov, iovcnt * sizeof(struct iovec)) == -1) { 1180 print_pointer(fp, arg); 1181 return; 1182 } 1183 1184 fputs("[", fp); 1185 for (i = 0; i < iovcnt; i++) { 1186 len = iov[i].iov_len; 1187 if (len > max_string) { 1188 len = max_string; 1189 buf_truncated = true; 1190 } else { 1191 buf_truncated = false; 1192 } 1193 fprintf(fp, "%s{", (i > 0) ? "," : ""); 1194 if (len && get_struct(pid, (uintptr_t)iov[i].iov_base, &tmp2, len) != -1) { 1195 tmp3 = malloc(len * 4 + 1); 1196 while (len) { 1197 if (strvisx(tmp3, tmp2, len, 1198 VIS_CSTYLE|VIS_TAB|VIS_NL) <= 1199 (int)max_string) 1200 break; 1201 len--; 1202 buf_truncated = true; 1203 } 1204 fprintf(fp, "\"%s\"%s", tmp3, 1205 buf_truncated ? "..." : ""); 1206 free(tmp3); 1207 } else { 1208 print_pointer(fp, (uintptr_t)iov[i].iov_base); 1209 } 1210 fprintf(fp, ",%zu}", iov[i].iov_len); 1211 } 1212 fprintf(fp, "%s%s", iov_truncated ? ",..." : "", "]"); 1213 } 1214 1215 static void 1216 print_sigval(FILE *fp, union sigval *sv) 1217 { 1218 fprintf(fp, "{ %d, %p }", sv->sival_int, sv->sival_ptr); 1219 } 1220 1221 static void 1222 print_sigevent(FILE *fp, struct sigevent *se) 1223 { 1224 fputs("{ sigev_notify=", fp); 1225 switch (se->sigev_notify) { 1226 case SIGEV_NONE: 1227 fputs("SIGEV_NONE", fp); 1228 break; 1229 case SIGEV_SIGNAL: 1230 fprintf(fp, "SIGEV_SIGNAL, sigev_signo=%s, sigev_value=", 1231 strsig2(se->sigev_signo)); 1232 print_sigval(fp, &se->sigev_value); 1233 break; 1234 case SIGEV_THREAD: 1235 fputs("SIGEV_THREAD, sigev_value=", fp); 1236 print_sigval(fp, &se->sigev_value); 1237 break; 1238 case SIGEV_KEVENT: 1239 fprintf(fp, "SIGEV_KEVENT, sigev_notify_kqueue=%d, sigev_notify_kevent_flags=", 1240 se->sigev_notify_kqueue); 1241 print_mask_arg(sysdecode_kevent_flags, fp, se->sigev_notify_kevent_flags); 1242 break; 1243 case SIGEV_THREAD_ID: 1244 fprintf(fp, "SIGEV_THREAD_ID, sigev_notify_thread_id=%d, sigev_signo=%s, sigev_value=", 1245 se->sigev_notify_thread_id, strsig2(se->sigev_signo)); 1246 print_sigval(fp, &se->sigev_value); 1247 break; 1248 default: 1249 fprintf(fp, "%d", se->sigev_notify); 1250 break; 1251 } 1252 fputs(" }", fp); 1253 } 1254 1255 static void 1256 print_aiocb(FILE *fp, struct aiocb *cb) 1257 { 1258 fprintf(fp, "{ %d,%jd,%p,%zu,%s,", 1259 cb->aio_fildes, 1260 cb->aio_offset, 1261 cb->aio_buf, 1262 cb->aio_nbytes, 1263 xlookup(lio_opcodes, cb->aio_lio_opcode)); 1264 print_sigevent(fp, &cb->aio_sigevent); 1265 fputs(" }", fp); 1266 } 1267 1268 static void 1269 print_gen_cmsg(FILE *fp, struct cmsghdr *cmsghdr) 1270 { 1271 u_char *q; 1272 1273 fputs("{", fp); 1274 for (q = CMSG_DATA(cmsghdr); 1275 q < (u_char *)cmsghdr + cmsghdr->cmsg_len; q++) { 1276 fprintf(fp, "%s0x%02x", q == CMSG_DATA(cmsghdr) ? "" : ",", *q); 1277 } 1278 fputs("}", fp); 1279 } 1280 1281 static void 1282 print_sctp_initmsg(FILE *fp, struct sctp_initmsg *init) 1283 { 1284 fprintf(fp, "{out=%u,", init->sinit_num_ostreams); 1285 fprintf(fp, "in=%u,", init->sinit_max_instreams); 1286 fprintf(fp, "max_rtx=%u,", init->sinit_max_attempts); 1287 fprintf(fp, "max_rto=%u}", init->sinit_max_init_timeo); 1288 } 1289 1290 static void 1291 print_sctp_sndrcvinfo(FILE *fp, bool receive, struct sctp_sndrcvinfo *info) 1292 { 1293 fprintf(fp, "{sid=%u,", info->sinfo_stream); 1294 if (receive) { 1295 fprintf(fp, "ssn=%u,", info->sinfo_ssn); 1296 } 1297 fputs("flgs=", fp); 1298 sysdecode_sctp_sinfo_flags(fp, info->sinfo_flags); 1299 fprintf(fp, ",ppid=%u,", ntohl(info->sinfo_ppid)); 1300 if (!receive) { 1301 fprintf(fp, "ctx=%u,", info->sinfo_context); 1302 fprintf(fp, "ttl=%u,", info->sinfo_timetolive); 1303 } 1304 if (receive) { 1305 fprintf(fp, "tsn=%u,", info->sinfo_tsn); 1306 fprintf(fp, "cumtsn=%u,", info->sinfo_cumtsn); 1307 } 1308 fprintf(fp, "id=%u}", info->sinfo_assoc_id); 1309 } 1310 1311 static void 1312 print_sctp_sndinfo(FILE *fp, struct sctp_sndinfo *info) 1313 { 1314 fprintf(fp, "{sid=%u,", info->snd_sid); 1315 fputs("flgs=", fp); 1316 print_mask_arg(sysdecode_sctp_snd_flags, fp, info->snd_flags); 1317 fprintf(fp, ",ppid=%u,", ntohl(info->snd_ppid)); 1318 fprintf(fp, "ctx=%u,", info->snd_context); 1319 fprintf(fp, "id=%u}", info->snd_assoc_id); 1320 } 1321 1322 static void 1323 print_sctp_rcvinfo(FILE *fp, struct sctp_rcvinfo *info) 1324 { 1325 fprintf(fp, "{sid=%u,", info->rcv_sid); 1326 fprintf(fp, "ssn=%u,", info->rcv_ssn); 1327 fputs("flgs=", fp); 1328 print_mask_arg(sysdecode_sctp_rcv_flags, fp, info->rcv_flags); 1329 fprintf(fp, ",ppid=%u,", ntohl(info->rcv_ppid)); 1330 fprintf(fp, "tsn=%u,", info->rcv_tsn); 1331 fprintf(fp, "cumtsn=%u,", info->rcv_cumtsn); 1332 fprintf(fp, "ctx=%u,", info->rcv_context); 1333 fprintf(fp, "id=%u}", info->rcv_assoc_id); 1334 } 1335 1336 static void 1337 print_sctp_nxtinfo(FILE *fp, struct sctp_nxtinfo *info) 1338 { 1339 fprintf(fp, "{sid=%u,", info->nxt_sid); 1340 fputs("flgs=", fp); 1341 print_mask_arg(sysdecode_sctp_nxt_flags, fp, info->nxt_flags); 1342 fprintf(fp, ",ppid=%u,", ntohl(info->nxt_ppid)); 1343 fprintf(fp, "len=%u,", info->nxt_length); 1344 fprintf(fp, "id=%u}", info->nxt_assoc_id); 1345 } 1346 1347 static void 1348 print_sctp_prinfo(FILE *fp, struct sctp_prinfo *info) 1349 { 1350 fputs("{pol=", fp); 1351 print_integer_arg(sysdecode_sctp_pr_policy, fp, info->pr_policy); 1352 fprintf(fp, ",val=%u}", info->pr_value); 1353 } 1354 1355 static void 1356 print_sctp_authinfo(FILE *fp, struct sctp_authinfo *info) 1357 { 1358 fprintf(fp, "{num=%u}", info->auth_keynumber); 1359 } 1360 1361 static void 1362 print_sctp_ipv4_addr(FILE *fp, struct in_addr *addr) 1363 { 1364 char buf[INET_ADDRSTRLEN]; 1365 const char *s; 1366 1367 s = inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN); 1368 if (s != NULL) 1369 fprintf(fp, "{addr=%s}", s); 1370 else 1371 fputs("{addr=???}", fp); 1372 } 1373 1374 static void 1375 print_sctp_ipv6_addr(FILE *fp, struct in6_addr *addr) 1376 { 1377 char buf[INET6_ADDRSTRLEN]; 1378 const char *s; 1379 1380 s = inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN); 1381 if (s != NULL) 1382 fprintf(fp, "{addr=%s}", s); 1383 else 1384 fputs("{addr=???}", fp); 1385 } 1386 1387 static void 1388 print_sctp_cmsg(FILE *fp, bool receive, struct cmsghdr *cmsghdr) 1389 { 1390 void *data; 1391 socklen_t len; 1392 1393 len = cmsghdr->cmsg_len; 1394 data = CMSG_DATA(cmsghdr); 1395 switch (cmsghdr->cmsg_type) { 1396 case SCTP_INIT: 1397 if (len == CMSG_LEN(sizeof(struct sctp_initmsg))) 1398 print_sctp_initmsg(fp, (struct sctp_initmsg *)data); 1399 else 1400 print_gen_cmsg(fp, cmsghdr); 1401 break; 1402 case SCTP_SNDRCV: 1403 if (len == CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) 1404 print_sctp_sndrcvinfo(fp, receive, 1405 (struct sctp_sndrcvinfo *)data); 1406 else 1407 print_gen_cmsg(fp, cmsghdr); 1408 break; 1409 #if 0 1410 case SCTP_EXTRCV: 1411 if (len == CMSG_LEN(sizeof(struct sctp_extrcvinfo))) 1412 print_sctp_extrcvinfo(fp, 1413 (struct sctp_extrcvinfo *)data); 1414 else 1415 print_gen_cmsg(fp, cmsghdr); 1416 break; 1417 #endif 1418 case SCTP_SNDINFO: 1419 if (len == CMSG_LEN(sizeof(struct sctp_sndinfo))) 1420 print_sctp_sndinfo(fp, (struct sctp_sndinfo *)data); 1421 else 1422 print_gen_cmsg(fp, cmsghdr); 1423 break; 1424 case SCTP_RCVINFO: 1425 if (len == CMSG_LEN(sizeof(struct sctp_rcvinfo))) 1426 print_sctp_rcvinfo(fp, (struct sctp_rcvinfo *)data); 1427 else 1428 print_gen_cmsg(fp, cmsghdr); 1429 break; 1430 case SCTP_NXTINFO: 1431 if (len == CMSG_LEN(sizeof(struct sctp_nxtinfo))) 1432 print_sctp_nxtinfo(fp, (struct sctp_nxtinfo *)data); 1433 else 1434 print_gen_cmsg(fp, cmsghdr); 1435 break; 1436 case SCTP_PRINFO: 1437 if (len == CMSG_LEN(sizeof(struct sctp_prinfo))) 1438 print_sctp_prinfo(fp, (struct sctp_prinfo *)data); 1439 else 1440 print_gen_cmsg(fp, cmsghdr); 1441 break; 1442 case SCTP_AUTHINFO: 1443 if (len == CMSG_LEN(sizeof(struct sctp_authinfo))) 1444 print_sctp_authinfo(fp, (struct sctp_authinfo *)data); 1445 else 1446 print_gen_cmsg(fp, cmsghdr); 1447 break; 1448 case SCTP_DSTADDRV4: 1449 if (len == CMSG_LEN(sizeof(struct in_addr))) 1450 print_sctp_ipv4_addr(fp, (struct in_addr *)data); 1451 else 1452 print_gen_cmsg(fp, cmsghdr); 1453 break; 1454 case SCTP_DSTADDRV6: 1455 if (len == CMSG_LEN(sizeof(struct in6_addr))) 1456 print_sctp_ipv6_addr(fp, (struct in6_addr *)data); 1457 else 1458 print_gen_cmsg(fp, cmsghdr); 1459 break; 1460 default: 1461 print_gen_cmsg(fp, cmsghdr); 1462 } 1463 } 1464 1465 static void 1466 print_cmsgs(FILE *fp, pid_t pid, bool receive, struct msghdr *msghdr) 1467 { 1468 struct cmsghdr *cmsghdr; 1469 char *cmsgbuf; 1470 const char *temp; 1471 socklen_t len; 1472 int level, type; 1473 bool first; 1474 1475 len = msghdr->msg_controllen; 1476 if (len == 0) { 1477 fputs("{}", fp); 1478 return; 1479 } 1480 cmsgbuf = calloc(1, len); 1481 if (get_struct(pid, (uintptr_t)msghdr->msg_control, cmsgbuf, len) == -1) { 1482 print_pointer(fp, (uintptr_t)msghdr->msg_control); 1483 free(cmsgbuf); 1484 return; 1485 } 1486 msghdr->msg_control = cmsgbuf; 1487 first = true; 1488 fputs("{", fp); 1489 for (cmsghdr = CMSG_FIRSTHDR(msghdr); 1490 cmsghdr != NULL; 1491 cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr)) { 1492 if (cmsghdr->cmsg_len < sizeof(*cmsghdr)) { 1493 fprintf(fp, "{<invalid cmsg, len=%u>}", 1494 cmsghdr->cmsg_len); 1495 if (cmsghdr->cmsg_len == 0) { 1496 /* Avoid looping forever. */ 1497 break; 1498 } 1499 continue; 1500 } 1501 1502 level = cmsghdr->cmsg_level; 1503 type = cmsghdr->cmsg_type; 1504 len = cmsghdr->cmsg_len; 1505 fprintf(fp, "%s{level=", first ? "" : ","); 1506 print_integer_arg(sysdecode_sockopt_level, fp, level); 1507 fputs(",type=", fp); 1508 temp = sysdecode_cmsg_type(level, type); 1509 if (temp) { 1510 fputs(temp, fp); 1511 } else { 1512 fprintf(fp, "%d", type); 1513 } 1514 fputs(",data=", fp); 1515 switch (level) { 1516 case IPPROTO_SCTP: 1517 print_sctp_cmsg(fp, receive, cmsghdr); 1518 break; 1519 default: 1520 print_gen_cmsg(fp, cmsghdr); 1521 break; 1522 } 1523 fputs("}", fp); 1524 first = false; 1525 } 1526 fputs("}", fp); 1527 free(cmsgbuf); 1528 } 1529 1530 static void 1531 print_sysctl_oid(FILE *fp, int *oid, size_t len) 1532 { 1533 size_t i; 1534 bool first; 1535 1536 first = true; 1537 fprintf(fp, "{ "); 1538 for (i = 0; i < len; i++) { 1539 fprintf(fp, "%s%d", first ? "" : ".", oid[i]); 1540 first = false; 1541 } 1542 fprintf(fp, " }"); 1543 } 1544 1545 static void 1546 print_sysctl(FILE *fp, int *oid, size_t len) 1547 { 1548 char name[BUFSIZ]; 1549 int qoid[CTL_MAXNAME + 2]; 1550 size_t i; 1551 1552 qoid[0] = CTL_SYSCTL; 1553 qoid[1] = CTL_SYSCTL_NAME; 1554 memcpy(qoid + 2, oid, len * sizeof(int)); 1555 i = sizeof(name); 1556 if (sysctl(qoid, len + 2, name, &i, 0, 0) == -1) 1557 print_sysctl_oid(fp, oid, len); 1558 else 1559 fprintf(fp, "%s", name); 1560 } 1561 1562 /* 1563 * Convert a 32-bit user-space pointer to psaddr_t by zero-extending. 1564 */ 1565 static psaddr_t 1566 user_ptr32_to_psaddr(int32_t user_pointer) 1567 { 1568 return ((psaddr_t)(uintptr_t)user_pointer); 1569 } 1570 1571 /* 1572 * Converts a syscall argument into a string. Said string is 1573 * allocated via malloc(), so needs to be free()'d. sc is 1574 * a pointer to the syscall description (see above); args is 1575 * an array of all of the system call arguments. 1576 */ 1577 char * 1578 print_arg(struct syscall_arg *sc, syscallarg_t *args, syscallarg_t *retval, 1579 struct trussinfo *trussinfo) 1580 { 1581 FILE *fp; 1582 char *tmp; 1583 size_t tmplen; 1584 pid_t pid; 1585 1586 fp = open_memstream(&tmp, &tmplen); 1587 pid = trussinfo->curthread->proc->pid; 1588 switch (sc->type & ARG_MASK) { 1589 case Hex: 1590 fprintf(fp, "0x%x", (int)args[sc->offset]); 1591 break; 1592 case Octal: 1593 fprintf(fp, "0%o", (int)args[sc->offset]); 1594 break; 1595 case Int: 1596 fprintf(fp, "%d", (int)args[sc->offset]); 1597 break; 1598 case UInt: 1599 fprintf(fp, "%u", (unsigned int)args[sc->offset]); 1600 break; 1601 case PUInt: { 1602 unsigned int val; 1603 1604 if (get_struct(pid, args[sc->offset], &val, 1605 sizeof(val)) == 0) 1606 fprintf(fp, "{ %u }", val); 1607 else 1608 print_pointer(fp, args[sc->offset]); 1609 break; 1610 } 1611 case LongHex: 1612 fprintf(fp, "0x%lx", (long)args[sc->offset]); 1613 break; 1614 case Long: 1615 fprintf(fp, "%ld", (long)args[sc->offset]); 1616 break; 1617 case Sizet: 1618 fprintf(fp, "%zu", (size_t)args[sc->offset]); 1619 break; 1620 case ShmName: 1621 /* Handle special SHM_ANON value. */ 1622 if ((char *)(uintptr_t)args[sc->offset] == SHM_ANON) { 1623 fprintf(fp, "SHM_ANON"); 1624 break; 1625 } 1626 /* FALLTHROUGH */ 1627 case Name: { 1628 /* NULL-terminated string. */ 1629 char *tmp2; 1630 1631 tmp2 = get_string(pid, args[sc->offset], 0); 1632 fprintf(fp, "\"%s\"", tmp2); 1633 free(tmp2); 1634 break; 1635 } 1636 case BinString: { 1637 /* 1638 * Binary block of data that might have printable characters. 1639 * XXX If type|OUT, assume that the length is the syscall's 1640 * return value. Otherwise, assume that the length of the block 1641 * is in the next syscall argument. 1642 */ 1643 int max_string = trussinfo->strsize; 1644 char tmp2[max_string + 1], *tmp3; 1645 int len; 1646 int truncated = 0; 1647 1648 if (sc->type & OUT) 1649 len = retval[0]; 1650 else 1651 len = args[sc->offset + 1]; 1652 1653 /* 1654 * Don't print more than max_string characters, to avoid word 1655 * wrap. If we have to truncate put some ... after the string. 1656 */ 1657 if (len > max_string) { 1658 len = max_string; 1659 truncated = 1; 1660 } 1661 if (len && get_struct(pid, args[sc->offset], &tmp2, len) 1662 != -1) { 1663 tmp3 = malloc(len * 4 + 1); 1664 while (len) { 1665 if (strvisx(tmp3, tmp2, len, 1666 VIS_CSTYLE|VIS_TAB|VIS_NL) <= max_string) 1667 break; 1668 len--; 1669 truncated = 1; 1670 } 1671 fprintf(fp, "\"%s\"%s", tmp3, truncated ? 1672 "..." : ""); 1673 free(tmp3); 1674 } else { 1675 print_pointer(fp, args[sc->offset]); 1676 } 1677 break; 1678 } 1679 case ExecArgs: 1680 case ExecEnv: 1681 case StringArray: { 1682 psaddr_t addr; 1683 union { 1684 int32_t strarray32[PAGE_SIZE / sizeof(int32_t)]; 1685 int64_t strarray64[PAGE_SIZE / sizeof(int64_t)]; 1686 char buf[PAGE_SIZE]; 1687 } u; 1688 char *string; 1689 size_t len; 1690 u_int first, i; 1691 size_t pointer_size = 1692 trussinfo->curthread->proc->abi->pointer_size; 1693 1694 /* 1695 * Only parse argv[] and environment arrays from exec calls 1696 * if requested. 1697 */ 1698 if (((sc->type & ARG_MASK) == ExecArgs && 1699 (trussinfo->flags & EXECVEARGS) == 0) || 1700 ((sc->type & ARG_MASK) == ExecEnv && 1701 (trussinfo->flags & EXECVEENVS) == 0)) { 1702 print_pointer(fp, args[sc->offset]); 1703 break; 1704 } 1705 1706 /* 1707 * Read a page of pointers at a time. Punt if the top-level 1708 * pointer is not aligned. Note that the first read is of 1709 * a partial page. 1710 */ 1711 addr = args[sc->offset]; 1712 if (!__is_aligned(addr, pointer_size)) { 1713 print_pointer(fp, args[sc->offset]); 1714 break; 1715 } 1716 1717 len = PAGE_SIZE - (addr & PAGE_MASK); 1718 if (get_struct(pid, addr, u.buf, len) == -1) { 1719 print_pointer(fp, args[sc->offset]); 1720 break; 1721 } 1722 assert(len > 0); 1723 1724 fputc('[', fp); 1725 first = 1; 1726 i = 0; 1727 for (;;) { 1728 psaddr_t straddr; 1729 if (pointer_size == 4) { 1730 straddr = user_ptr32_to_psaddr(u.strarray32[i]); 1731 } else if (pointer_size == 8) { 1732 straddr = (psaddr_t)u.strarray64[i]; 1733 } else { 1734 errx(1, "Unsupported pointer size: %zu", 1735 pointer_size); 1736 } 1737 1738 /* Stop once we read the first NULL pointer. */ 1739 if (straddr == 0) 1740 break; 1741 string = get_string(pid, straddr, 0); 1742 fprintf(fp, "%s \"%s\"", first ? "" : ",", string); 1743 free(string); 1744 first = 0; 1745 1746 i++; 1747 if (i == len / pointer_size) { 1748 addr += len; 1749 len = PAGE_SIZE; 1750 if (get_struct(pid, addr, u.buf, len) == -1) { 1751 fprintf(fp, ", <inval>"); 1752 break; 1753 } 1754 i = 0; 1755 } 1756 } 1757 fputs(" ]", fp); 1758 break; 1759 } 1760 case Quad: 1761 case QuadHex: { 1762 uint64_t value; 1763 size_t pointer_size = 1764 trussinfo->curthread->proc->abi->pointer_size; 1765 1766 if (pointer_size == 4) { 1767 #if _BYTE_ORDER == _LITTLE_ENDIAN 1768 value = (uint64_t)args[sc->offset + 1] << 32 | 1769 args[sc->offset]; 1770 #else 1771 value = (uint64_t)args[sc->offset] << 32 | 1772 args[sc->offset + 1]; 1773 #endif 1774 } else { 1775 value = (uint64_t)args[sc->offset]; 1776 } 1777 if ((sc->type & ARG_MASK) == Quad) 1778 fprintf(fp, "%jd", (intmax_t)value); 1779 else 1780 fprintf(fp, "0x%jx", (intmax_t)value); 1781 break; 1782 } 1783 case PQuadHex: { 1784 uint64_t val; 1785 1786 if (get_struct(pid, args[sc->offset], &val, 1787 sizeof(val)) == 0) 1788 fprintf(fp, "{ 0x%jx }", (uintmax_t)val); 1789 else 1790 print_pointer(fp, args[sc->offset]); 1791 break; 1792 } 1793 case Ptr: 1794 print_pointer(fp, args[sc->offset]); 1795 break; 1796 case Readlinkres: { 1797 char *tmp2; 1798 1799 if (retval[0] == -1) 1800 break; 1801 tmp2 = get_string(pid, args[sc->offset], retval[0]); 1802 fprintf(fp, "\"%s\"", tmp2); 1803 free(tmp2); 1804 break; 1805 } 1806 case Ioctl: { 1807 const char *temp; 1808 unsigned long cmd; 1809 1810 cmd = args[sc->offset]; 1811 temp = sysdecode_ioctlname(cmd); 1812 if (temp) 1813 fputs(temp, fp); 1814 else { 1815 fprintf(fp, "0x%lx { IO%s%s 0x%lx('%c'), %lu, %lu }", 1816 cmd, cmd & IOC_OUT ? "R" : "", 1817 cmd & IOC_IN ? "W" : "", IOCGROUP(cmd), 1818 isprint(IOCGROUP(cmd)) ? (char)IOCGROUP(cmd) : '?', 1819 cmd & 0xFF, IOCPARM_LEN(cmd)); 1820 } 1821 break; 1822 } 1823 case Timespec: { 1824 struct timespec ts; 1825 1826 if (get_struct(pid, args[sc->offset], &ts, sizeof(ts)) != -1) 1827 fprintf(fp, "{ %jd.%09ld }", (intmax_t)ts.tv_sec, 1828 ts.tv_nsec); 1829 else 1830 print_pointer(fp, args[sc->offset]); 1831 break; 1832 } 1833 case Timespec2: { 1834 struct timespec ts[2]; 1835 const char *sep; 1836 unsigned int i; 1837 1838 if (get_struct(pid, args[sc->offset], &ts, sizeof(ts)) != -1) { 1839 fputs("{ ", fp); 1840 sep = ""; 1841 for (i = 0; i < nitems(ts); i++) { 1842 fputs(sep, fp); 1843 sep = ", "; 1844 switch (ts[i].tv_nsec) { 1845 case UTIME_NOW: 1846 fprintf(fp, "UTIME_NOW"); 1847 break; 1848 case UTIME_OMIT: 1849 fprintf(fp, "UTIME_OMIT"); 1850 break; 1851 default: 1852 fprintf(fp, "%jd.%09ld", 1853 (intmax_t)ts[i].tv_sec, 1854 ts[i].tv_nsec); 1855 break; 1856 } 1857 } 1858 fputs(" }", fp); 1859 } else 1860 print_pointer(fp, args[sc->offset]); 1861 break; 1862 } 1863 case Timeval: { 1864 struct timeval tv; 1865 1866 if (get_struct(pid, args[sc->offset], &tv, sizeof(tv)) != -1) 1867 fprintf(fp, "{ %jd.%06ld }", (intmax_t)tv.tv_sec, 1868 tv.tv_usec); 1869 else 1870 print_pointer(fp, args[sc->offset]); 1871 break; 1872 } 1873 case Timeval2: { 1874 struct timeval tv[2]; 1875 1876 if (get_struct(pid, args[sc->offset], &tv, sizeof(tv)) != -1) 1877 fprintf(fp, "{ %jd.%06ld, %jd.%06ld }", 1878 (intmax_t)tv[0].tv_sec, tv[0].tv_usec, 1879 (intmax_t)tv[1].tv_sec, tv[1].tv_usec); 1880 else 1881 print_pointer(fp, args[sc->offset]); 1882 break; 1883 } 1884 case Itimerval: { 1885 struct itimerval itv; 1886 1887 if (get_struct(pid, args[sc->offset], &itv, sizeof(itv)) != -1) 1888 fprintf(fp, "{ %jd.%06ld, %jd.%06ld }", 1889 (intmax_t)itv.it_interval.tv_sec, 1890 itv.it_interval.tv_usec, 1891 (intmax_t)itv.it_value.tv_sec, 1892 itv.it_value.tv_usec); 1893 else 1894 print_pointer(fp, args[sc->offset]); 1895 break; 1896 } 1897 case LinuxSockArgs: 1898 { 1899 struct linux_socketcall_args largs; 1900 1901 if (get_struct(pid, args[sc->offset], (void *)&largs, 1902 sizeof(largs)) != -1) 1903 fprintf(fp, "{ %s, 0x%lx }", 1904 lookup(linux_socketcall_ops, largs.what, 10), 1905 (long unsigned int)largs.args); 1906 else 1907 print_pointer(fp, args[sc->offset]); 1908 break; 1909 } 1910 case Pollfd: { 1911 /* 1912 * XXX: A Pollfd argument expects the /next/ syscall argument 1913 * to be the number of fds in the array. This matches the poll 1914 * syscall. 1915 */ 1916 struct pollfd *pfd; 1917 int numfds = args[sc->offset + 1]; 1918 size_t bytes = sizeof(struct pollfd) * numfds; 1919 int i; 1920 1921 if ((pfd = malloc(bytes)) == NULL) 1922 err(1, "Cannot malloc %zu bytes for pollfd array", 1923 bytes); 1924 if (get_struct(pid, args[sc->offset], pfd, bytes) != -1) { 1925 fputs("{", fp); 1926 for (i = 0; i < numfds; i++) { 1927 fprintf(fp, " %d/%s", pfd[i].fd, 1928 xlookup_bits(poll_flags, pfd[i].events)); 1929 } 1930 fputs(" }", fp); 1931 } else { 1932 print_pointer(fp, args[sc->offset]); 1933 } 1934 free(pfd); 1935 break; 1936 } 1937 case Fd_set: { 1938 /* 1939 * XXX: A Fd_set argument expects the /first/ syscall argument 1940 * to be the number of fds in the array. This matches the 1941 * select syscall. 1942 */ 1943 fd_set *fds; 1944 int numfds = args[0]; 1945 size_t bytes = _howmany(numfds, _NFDBITS) * _NFDBITS; 1946 int i; 1947 1948 if ((fds = malloc(bytes)) == NULL) 1949 err(1, "Cannot malloc %zu bytes for fd_set array", 1950 bytes); 1951 if (get_struct(pid, args[sc->offset], fds, bytes) != -1) { 1952 fputs("{", fp); 1953 for (i = 0; i < numfds; i++) { 1954 if (FD_ISSET(i, fds)) 1955 fprintf(fp, " %d", i); 1956 } 1957 fputs(" }", fp); 1958 } else 1959 print_pointer(fp, args[sc->offset]); 1960 free(fds); 1961 break; 1962 } 1963 case Signal: 1964 fputs(strsig2(args[sc->offset]), fp); 1965 break; 1966 case Sigset: { 1967 sigset_t ss; 1968 int i, first; 1969 1970 if (get_struct(pid, args[sc->offset], (void *)&ss, 1971 sizeof(ss)) == -1) { 1972 print_pointer(fp, args[sc->offset]); 1973 break; 1974 } 1975 fputs("{ ", fp); 1976 first = 1; 1977 for (i = 1; i < sys_nsig; i++) { 1978 if (sigismember(&ss, i)) { 1979 fprintf(fp, "%s%s", !first ? "|" : "", 1980 strsig2(i)); 1981 first = 0; 1982 } 1983 } 1984 if (!first) 1985 fputc(' ', fp); 1986 fputc('}', fp); 1987 break; 1988 } 1989 case Sigprocmask: 1990 print_integer_arg(sysdecode_sigprocmask_how, fp, 1991 args[sc->offset]); 1992 break; 1993 case Fcntlflag: 1994 /* XXX: Output depends on the value of the previous argument. */ 1995 if (sysdecode_fcntl_arg_p(args[sc->offset - 1])) 1996 sysdecode_fcntl_arg(fp, args[sc->offset - 1], 1997 args[sc->offset], 16); 1998 break; 1999 case Open: 2000 print_mask_arg(sysdecode_open_flags, fp, args[sc->offset]); 2001 break; 2002 case Fcntl: 2003 print_integer_arg(sysdecode_fcntl_cmd, fp, args[sc->offset]); 2004 break; 2005 case Closerangeflags: 2006 print_mask_arg(sysdecode_close_range_flags, fp, args[sc->offset]); 2007 break; 2008 case Mprot: 2009 print_mask_arg(sysdecode_mmap_prot, fp, args[sc->offset]); 2010 break; 2011 case Mmapflags: 2012 print_mask_arg(sysdecode_mmap_flags, fp, args[sc->offset]); 2013 break; 2014 case Whence: 2015 print_integer_arg(sysdecode_whence, fp, args[sc->offset]); 2016 break; 2017 case ShmFlags: 2018 print_mask_arg(sysdecode_shmflags, fp, args[sc->offset]); 2019 break; 2020 case Sockdomain: 2021 print_integer_arg(sysdecode_socketdomain, fp, args[sc->offset]); 2022 break; 2023 case Socktype: 2024 print_mask_arg(sysdecode_socket_type, fp, args[sc->offset]); 2025 break; 2026 case Shutdown: 2027 print_integer_arg(sysdecode_shutdown_how, fp, args[sc->offset]); 2028 break; 2029 case Resource: 2030 print_integer_arg(sysdecode_rlimit, fp, args[sc->offset]); 2031 break; 2032 case RusageWho: 2033 print_integer_arg(sysdecode_getrusage_who, fp, args[sc->offset]); 2034 break; 2035 case Pathconf: 2036 print_integer_arg(sysdecode_pathconf_name, fp, args[sc->offset]); 2037 break; 2038 case Rforkflags: 2039 print_mask_arg(sysdecode_rfork_flags, fp, args[sc->offset]); 2040 break; 2041 case Sockaddr: { 2042 socklen_t len; 2043 2044 if (args[sc->offset] == 0) { 2045 fputs("NULL", fp); 2046 break; 2047 } 2048 2049 /* 2050 * Extract the address length from the next argument. If 2051 * this is an output sockaddr (OUT is set), then the 2052 * next argument is a pointer to a socklen_t. Otherwise 2053 * the next argument contains a socklen_t by value. 2054 */ 2055 if (sc->type & OUT) { 2056 if (get_struct(pid, args[sc->offset + 1], &len, 2057 sizeof(len)) == -1) { 2058 print_pointer(fp, args[sc->offset]); 2059 break; 2060 } 2061 } else 2062 len = args[sc->offset + 1]; 2063 2064 print_sockaddr(fp, trussinfo, args[sc->offset], len); 2065 break; 2066 } 2067 case Sigaction: { 2068 struct sigaction sa; 2069 2070 if (get_struct(pid, args[sc->offset], &sa, sizeof(sa)) != -1) { 2071 fputs("{ ", fp); 2072 if (sa.sa_handler == SIG_DFL) 2073 fputs("SIG_DFL", fp); 2074 else if (sa.sa_handler == SIG_IGN) 2075 fputs("SIG_IGN", fp); 2076 else 2077 fprintf(fp, "%p", sa.sa_handler); 2078 fprintf(fp, " %s ss_t }", 2079 xlookup_bits(sigaction_flags, sa.sa_flags)); 2080 } else 2081 print_pointer(fp, args[sc->offset]); 2082 break; 2083 } 2084 case Sigevent: { 2085 struct sigevent se; 2086 2087 if (get_struct(pid, args[sc->offset], &se, sizeof(se)) != -1) 2088 print_sigevent(fp, &se); 2089 else 2090 print_pointer(fp, args[sc->offset]); 2091 break; 2092 } 2093 case Kevent: { 2094 /* 2095 * XXX XXX: The size of the array is determined by either the 2096 * next syscall argument, or by the syscall return value, 2097 * depending on which argument number we are. This matches the 2098 * kevent syscall, but luckily that's the only syscall that uses 2099 * them. 2100 */ 2101 struct kevent *ke; 2102 int numevents = -1; 2103 size_t bytes; 2104 int i; 2105 2106 if (sc->offset == 1) 2107 numevents = args[sc->offset+1]; 2108 else if (sc->offset == 3 && retval[0] != -1) 2109 numevents = retval[0]; 2110 2111 if (numevents >= 0) { 2112 bytes = sizeof(struct kevent) * numevents; 2113 if ((ke = malloc(bytes)) == NULL) 2114 err(1, 2115 "Cannot malloc %zu bytes for kevent array", 2116 bytes); 2117 } else 2118 ke = NULL; 2119 if (numevents >= 0 && get_struct(pid, args[sc->offset], 2120 ke, bytes) != -1) { 2121 fputc('{', fp); 2122 for (i = 0; i < numevents; i++) { 2123 fputc(' ', fp); 2124 print_kevent(fp, &ke[i]); 2125 } 2126 fputs(" }", fp); 2127 } else { 2128 print_pointer(fp, args[sc->offset]); 2129 } 2130 free(ke); 2131 break; 2132 } 2133 case Kevent11: { 2134 struct freebsd11_kevent *ke11; 2135 struct kevent ke; 2136 int numevents = -1; 2137 size_t bytes; 2138 int i; 2139 2140 if (sc->offset == 1) 2141 numevents = args[sc->offset+1]; 2142 else if (sc->offset == 3 && retval[0] != -1) 2143 numevents = retval[0]; 2144 2145 if (numevents >= 0) { 2146 bytes = sizeof(struct freebsd11_kevent) * numevents; 2147 if ((ke11 = malloc(bytes)) == NULL) 2148 err(1, 2149 "Cannot malloc %zu bytes for kevent array", 2150 bytes); 2151 } else 2152 ke11 = NULL; 2153 memset(&ke, 0, sizeof(ke)); 2154 if (numevents >= 0 && get_struct(pid, args[sc->offset], 2155 ke11, bytes) != -1) { 2156 fputc('{', fp); 2157 for (i = 0; i < numevents; i++) { 2158 fputc(' ', fp); 2159 ke.ident = ke11[i].ident; 2160 ke.filter = ke11[i].filter; 2161 ke.flags = ke11[i].flags; 2162 ke.fflags = ke11[i].fflags; 2163 ke.data = ke11[i].data; 2164 ke.udata = ke11[i].udata; 2165 print_kevent(fp, &ke); 2166 } 2167 fputs(" }", fp); 2168 } else { 2169 print_pointer(fp, args[sc->offset]); 2170 } 2171 free(ke11); 2172 break; 2173 } 2174 case Stat: { 2175 struct stat st; 2176 2177 if (get_struct(pid, args[sc->offset], &st, sizeof(st)) 2178 != -1) { 2179 char mode[12]; 2180 2181 strmode(st.st_mode, mode); 2182 fprintf(fp, 2183 "{ mode=%s,inode=%ju,size=%jd,blksize=%ld }", mode, 2184 (uintmax_t)st.st_ino, (intmax_t)st.st_size, 2185 (long)st.st_blksize); 2186 } else { 2187 print_pointer(fp, args[sc->offset]); 2188 } 2189 break; 2190 } 2191 case Stat11: { 2192 struct freebsd11_stat st; 2193 2194 if (get_struct(pid, args[sc->offset], &st, sizeof(st)) 2195 != -1) { 2196 char mode[12]; 2197 2198 strmode(st.st_mode, mode); 2199 fprintf(fp, 2200 "{ mode=%s,inode=%ju,size=%jd,blksize=%ld }", mode, 2201 (uintmax_t)st.st_ino, (intmax_t)st.st_size, 2202 (long)st.st_blksize); 2203 } else { 2204 print_pointer(fp, args[sc->offset]); 2205 } 2206 break; 2207 } 2208 case StatFs: { 2209 unsigned int i; 2210 struct statfs buf; 2211 2212 if (get_struct(pid, args[sc->offset], &buf, 2213 sizeof(buf)) != -1) { 2214 char fsid[17]; 2215 2216 bzero(fsid, sizeof(fsid)); 2217 if (buf.f_fsid.val[0] != 0 || buf.f_fsid.val[1] != 0) { 2218 for (i = 0; i < sizeof(buf.f_fsid); i++) 2219 snprintf(&fsid[i*2], 2220 sizeof(fsid) - (i*2), "%02x", 2221 ((u_char *)&buf.f_fsid)[i]); 2222 } 2223 fprintf(fp, 2224 "{ fstypename=%s,mntonname=%s,mntfromname=%s," 2225 "fsid=%s }", buf.f_fstypename, buf.f_mntonname, 2226 buf.f_mntfromname, fsid); 2227 } else 2228 print_pointer(fp, args[sc->offset]); 2229 break; 2230 } 2231 2232 case Rusage: { 2233 struct rusage ru; 2234 2235 if (get_struct(pid, args[sc->offset], &ru, sizeof(ru)) 2236 != -1) { 2237 fprintf(fp, 2238 "{ u=%jd.%06ld,s=%jd.%06ld,in=%ld,out=%ld }", 2239 (intmax_t)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec, 2240 (intmax_t)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec, 2241 ru.ru_inblock, ru.ru_oublock); 2242 } else 2243 print_pointer(fp, args[sc->offset]); 2244 break; 2245 } 2246 case Rlimit: { 2247 struct rlimit rl; 2248 2249 if (get_struct(pid, args[sc->offset], &rl, sizeof(rl)) 2250 != -1) { 2251 fprintf(fp, "{ cur=%ju,max=%ju }", 2252 rl.rlim_cur, rl.rlim_max); 2253 } else 2254 print_pointer(fp, args[sc->offset]); 2255 break; 2256 } 2257 case ExitStatus: { 2258 int status; 2259 2260 if (get_struct(pid, args[sc->offset], &status, 2261 sizeof(status)) != -1) { 2262 fputs("{ ", fp); 2263 if (WIFCONTINUED(status)) 2264 fputs("CONTINUED", fp); 2265 else if (WIFEXITED(status)) 2266 fprintf(fp, "EXITED,val=%d", 2267 WEXITSTATUS(status)); 2268 else if (WIFSIGNALED(status)) 2269 fprintf(fp, "SIGNALED,sig=%s%s", 2270 strsig2(WTERMSIG(status)), 2271 WCOREDUMP(status) ? ",cored" : ""); 2272 else 2273 fprintf(fp, "STOPPED,sig=%s", 2274 strsig2(WTERMSIG(status))); 2275 fputs(" }", fp); 2276 } else 2277 print_pointer(fp, args[sc->offset]); 2278 break; 2279 } 2280 case Waitoptions: 2281 print_mask_arg(sysdecode_wait6_options, fp, args[sc->offset]); 2282 break; 2283 case Idtype: 2284 print_integer_arg(sysdecode_idtype, fp, args[sc->offset]); 2285 break; 2286 case Procctl: 2287 print_integer_arg(sysdecode_procctl_cmd, fp, args[sc->offset]); 2288 break; 2289 case Umtxop: { 2290 int rem; 2291 2292 if (print_mask_arg_part(sysdecode_umtx_op_flags, fp, 2293 args[sc->offset], &rem)) 2294 fprintf(fp, "|"); 2295 print_integer_arg(sysdecode_umtx_op, fp, rem); 2296 break; 2297 } 2298 case Atfd: 2299 print_integer_arg(sysdecode_atfd, fp, args[sc->offset]); 2300 break; 2301 case Atflags: 2302 print_mask_arg(sysdecode_atflags, fp, args[sc->offset]); 2303 break; 2304 case Accessmode: 2305 print_mask_arg(sysdecode_access_mode, fp, args[sc->offset]); 2306 break; 2307 case Sysarch: 2308 print_integer_arg(sysdecode_sysarch_number, fp, 2309 args[sc->offset]); 2310 break; 2311 case Sysctl: { 2312 char name[BUFSIZ]; 2313 int oid[CTL_MAXNAME + 2]; 2314 size_t len; 2315 2316 memset(name, 0, sizeof(name)); 2317 len = args[sc->offset + 1]; 2318 if (get_struct(pid, args[sc->offset], oid, 2319 len * sizeof(oid[0])) != -1) { 2320 fprintf(fp, "\""); 2321 if (oid[0] == CTL_SYSCTL) { 2322 fprintf(fp, "sysctl."); 2323 switch (oid[1]) { 2324 case CTL_SYSCTL_DEBUG: 2325 fprintf(fp, "debug"); 2326 break; 2327 case CTL_SYSCTL_NAME: 2328 fprintf(fp, "name "); 2329 print_sysctl_oid(fp, oid + 2, len - 2); 2330 break; 2331 case CTL_SYSCTL_NEXT: 2332 fprintf(fp, "next"); 2333 break; 2334 case CTL_SYSCTL_NAME2OID: 2335 fprintf(fp, "name2oid %s", 2336 get_string(pid, 2337 args[sc->offset + 4], 2338 args[sc->offset + 5])); 2339 break; 2340 case CTL_SYSCTL_OIDFMT: 2341 fprintf(fp, "oidfmt "); 2342 print_sysctl(fp, oid + 2, len - 2); 2343 break; 2344 case CTL_SYSCTL_OIDDESCR: 2345 fprintf(fp, "oiddescr "); 2346 print_sysctl(fp, oid + 2, len - 2); 2347 break; 2348 case CTL_SYSCTL_OIDLABEL: 2349 fprintf(fp, "oidlabel "); 2350 print_sysctl(fp, oid + 2, len - 2); 2351 break; 2352 case CTL_SYSCTL_NEXTNOSKIP: 2353 fprintf(fp, "nextnoskip"); 2354 break; 2355 default: 2356 print_sysctl(fp, oid + 1, len - 1); 2357 } 2358 } else { 2359 print_sysctl(fp, oid, len); 2360 } 2361 fprintf(fp, "\""); 2362 } 2363 break; 2364 } 2365 case PipeFds: 2366 /* 2367 * The pipe() system call in the kernel returns its 2368 * two file descriptors via return values. However, 2369 * the interface exposed by libc is that pipe() 2370 * accepts a pointer to an array of descriptors. 2371 * Format the output to match the libc API by printing 2372 * the returned file descriptors as a fake argument. 2373 * 2374 * Overwrite the first retval to signal a successful 2375 * return as well. 2376 */ 2377 fprintf(fp, "{ %d, %d }", (int)retval[0], (int)retval[1]); 2378 retval[0] = 0; 2379 break; 2380 case Utrace: { 2381 size_t len; 2382 void *utrace_addr; 2383 2384 len = args[sc->offset + 1]; 2385 utrace_addr = calloc(1, len); 2386 if (get_struct(pid, args[sc->offset], 2387 (void *)utrace_addr, len) != -1) 2388 print_utrace(fp, utrace_addr, len); 2389 else 2390 print_pointer(fp, args[sc->offset]); 2391 free(utrace_addr); 2392 break; 2393 } 2394 case IntArray: { 2395 int descriptors[16]; 2396 unsigned long i, ndescriptors; 2397 bool truncated; 2398 2399 ndescriptors = args[sc->offset + 1]; 2400 truncated = false; 2401 if (ndescriptors > nitems(descriptors)) { 2402 ndescriptors = nitems(descriptors); 2403 truncated = true; 2404 } 2405 if (get_struct(pid, args[sc->offset], 2406 descriptors, ndescriptors * sizeof(descriptors[0])) != -1) { 2407 fprintf(fp, "{"); 2408 for (i = 0; i < ndescriptors; i++) 2409 fprintf(fp, i == 0 ? " %d" : ", %d", 2410 descriptors[i]); 2411 fprintf(fp, truncated ? ", ... }" : " }"); 2412 } else 2413 print_pointer(fp, args[sc->offset]); 2414 break; 2415 } 2416 case Pipe2: 2417 print_mask_arg(sysdecode_pipe2_flags, fp, args[sc->offset]); 2418 break; 2419 case CapFcntlRights: { 2420 uint32_t rights; 2421 2422 if (sc->type & OUT) { 2423 if (get_struct(pid, args[sc->offset], &rights, 2424 sizeof(rights)) == -1) { 2425 print_pointer(fp, args[sc->offset]); 2426 break; 2427 } 2428 } else 2429 rights = args[sc->offset]; 2430 print_mask_arg32(sysdecode_cap_fcntlrights, fp, rights); 2431 break; 2432 } 2433 case Fadvice: 2434 print_integer_arg(sysdecode_fadvice, fp, args[sc->offset]); 2435 break; 2436 case FileFlags: { 2437 fflags_t rem; 2438 2439 if (!sysdecode_fileflags(fp, args[sc->offset], &rem)) 2440 fprintf(fp, "0x%x", rem); 2441 else if (rem != 0) 2442 fprintf(fp, "|0x%x", rem); 2443 break; 2444 } 2445 case Flockop: 2446 print_mask_arg(sysdecode_flock_operation, fp, args[sc->offset]); 2447 break; 2448 case Getfsstatmode: 2449 print_integer_arg(sysdecode_getfsstat_mode, fp, 2450 args[sc->offset]); 2451 break; 2452 case Inotifyflags: 2453 print_mask_arg(sysdecode_inotifyflags, fp, args[sc->offset]); 2454 break; 2455 case Itimerwhich: 2456 print_integer_arg(sysdecode_itimer, fp, args[sc->offset]); 2457 break; 2458 case Kldsymcmd: 2459 print_integer_arg(sysdecode_kldsym_cmd, fp, args[sc->offset]); 2460 break; 2461 case Kldunloadflags: 2462 print_integer_arg(sysdecode_kldunload_flags, fp, 2463 args[sc->offset]); 2464 break; 2465 case AiofsyncOp: 2466 fputs(xlookup(aio_fsync_ops, args[sc->offset]), fp); 2467 break; 2468 case LioMode: 2469 fputs(xlookup(lio_modes, args[sc->offset]), fp); 2470 break; 2471 case Madvice: 2472 print_integer_arg(sysdecode_madvice, fp, args[sc->offset]); 2473 break; 2474 case Socklent: 2475 fprintf(fp, "%u", (socklen_t)args[sc->offset]); 2476 break; 2477 case Sockprotocol: { 2478 const char *temp; 2479 int domain, protocol; 2480 2481 domain = args[sc->offset - 2]; 2482 protocol = args[sc->offset]; 2483 if (protocol == 0) { 2484 fputs("0", fp); 2485 } else { 2486 temp = sysdecode_socket_protocol(domain, protocol); 2487 if (temp) { 2488 fputs(temp, fp); 2489 } else { 2490 fprintf(fp, "%d", protocol); 2491 } 2492 } 2493 break; 2494 } 2495 case Sockoptlevel: 2496 print_integer_arg(sysdecode_sockopt_level, fp, 2497 args[sc->offset]); 2498 break; 2499 case Sockoptname: { 2500 const char *temp; 2501 int level, name; 2502 2503 level = args[sc->offset - 1]; 2504 name = args[sc->offset]; 2505 temp = sysdecode_sockopt_name(level, name); 2506 if (temp) { 2507 fputs(temp, fp); 2508 } else { 2509 fprintf(fp, "%d", name); 2510 } 2511 break; 2512 } 2513 case Msgflags: 2514 print_mask_arg(sysdecode_msg_flags, fp, args[sc->offset]); 2515 break; 2516 case CapRights: { 2517 cap_rights_t rights; 2518 2519 if (get_struct(pid, args[sc->offset], &rights, 2520 sizeof(rights)) != -1) { 2521 fputs("{ ", fp); 2522 sysdecode_cap_rights(fp, &rights); 2523 fputs(" }", fp); 2524 } else 2525 print_pointer(fp, args[sc->offset]); 2526 break; 2527 } 2528 case Acltype: 2529 print_integer_arg(sysdecode_acltype, fp, args[sc->offset]); 2530 break; 2531 case Extattrnamespace: 2532 print_integer_arg(sysdecode_extattrnamespace, fp, 2533 args[sc->offset]); 2534 break; 2535 case Minherit: 2536 print_integer_arg(sysdecode_minherit_inherit, fp, 2537 args[sc->offset]); 2538 break; 2539 case Mlockall: 2540 print_mask_arg(sysdecode_mlockall_flags, fp, args[sc->offset]); 2541 break; 2542 case Mountflags: 2543 print_mask_arg(sysdecode_mount_flags, fp, args[sc->offset]); 2544 break; 2545 case Msync: 2546 print_mask_arg(sysdecode_msync_flags, fp, args[sc->offset]); 2547 break; 2548 case Priowhich: 2549 print_integer_arg(sysdecode_prio_which, fp, args[sc->offset]); 2550 break; 2551 case Ptraceop: 2552 print_integer_arg(sysdecode_ptrace_request, fp, 2553 args[sc->offset]); 2554 break; 2555 case Sendfileflags: 2556 print_mask_arg(sysdecode_sendfile_flags, fp, args[sc->offset]); 2557 break; 2558 case Sendfilehdtr: { 2559 struct sf_hdtr hdtr; 2560 2561 if (get_struct(pid, args[sc->offset], &hdtr, sizeof(hdtr)) != 2562 -1) { 2563 fprintf(fp, "{"); 2564 print_iovec(fp, trussinfo, (uintptr_t)hdtr.headers, 2565 hdtr.hdr_cnt); 2566 print_iovec(fp, trussinfo, (uintptr_t)hdtr.trailers, 2567 hdtr.trl_cnt); 2568 fprintf(fp, "}"); 2569 } else 2570 print_pointer(fp, args[sc->offset]); 2571 break; 2572 } 2573 case Quotactlcmd: 2574 if (!sysdecode_quotactl_cmd(fp, args[sc->offset])) 2575 fprintf(fp, "%#x", (int)args[sc->offset]); 2576 break; 2577 case Reboothowto: 2578 print_mask_arg(sysdecode_reboot_howto, fp, args[sc->offset]); 2579 break; 2580 case Rtpriofunc: 2581 print_integer_arg(sysdecode_rtprio_function, fp, 2582 args[sc->offset]); 2583 break; 2584 case Schedpolicy: 2585 print_integer_arg(sysdecode_scheduler_policy, fp, 2586 args[sc->offset]); 2587 break; 2588 case Schedparam: { 2589 struct sched_param sp; 2590 2591 if (get_struct(pid, args[sc->offset], &sp, sizeof(sp)) != -1) 2592 fprintf(fp, "{ %d }", sp.sched_priority); 2593 else 2594 print_pointer(fp, args[sc->offset]); 2595 break; 2596 } 2597 case PSig: { 2598 int sig; 2599 2600 if (get_struct(pid, args[sc->offset], &sig, sizeof(sig)) == 0) 2601 fprintf(fp, "{ %s }", strsig2(sig)); 2602 else 2603 print_pointer(fp, args[sc->offset]); 2604 break; 2605 } 2606 case Siginfo: { 2607 siginfo_t si; 2608 2609 if (get_struct(pid, args[sc->offset], &si, sizeof(si)) != -1) { 2610 fprintf(fp, "{ signo=%s", strsig2(si.si_signo)); 2611 decode_siginfo(fp, &si); 2612 fprintf(fp, " }"); 2613 } else 2614 print_pointer(fp, args[sc->offset]); 2615 break; 2616 } 2617 case Iovec: 2618 /* 2619 * Print argument as an array of struct iovec, where the next 2620 * syscall argument is the number of elements of the array. 2621 */ 2622 2623 print_iovec(fp, trussinfo, args[sc->offset], 2624 (int)args[sc->offset + 1]); 2625 break; 2626 case Aiocb: { 2627 struct aiocb cb; 2628 2629 if (get_struct(pid, args[sc->offset], &cb, sizeof(cb)) != -1) 2630 print_aiocb(fp, &cb); 2631 else 2632 print_pointer(fp, args[sc->offset]); 2633 break; 2634 } 2635 case AiocbArray: { 2636 /* 2637 * Print argment as an array of pointers to struct aiocb, where 2638 * the next syscall argument is the number of elements. 2639 */ 2640 uintptr_t cbs[16]; 2641 unsigned int nent; 2642 bool truncated; 2643 2644 nent = args[sc->offset + 1]; 2645 truncated = false; 2646 if (nent > nitems(cbs)) { 2647 nent = nitems(cbs); 2648 truncated = true; 2649 } 2650 2651 if (get_struct(pid, args[sc->offset], cbs, sizeof(uintptr_t) * nent) != -1) { 2652 unsigned int i; 2653 fputs("[", fp); 2654 for (i = 0; i < nent; ++i) { 2655 struct aiocb cb; 2656 if (i > 0) 2657 fputc(',', fp); 2658 if (get_struct(pid, cbs[i], &cb, sizeof(cb)) != -1) 2659 print_aiocb(fp, &cb); 2660 else 2661 print_pointer(fp, cbs[i]); 2662 } 2663 if (truncated) 2664 fputs(",...", fp); 2665 fputs("]", fp); 2666 } else 2667 print_pointer(fp, args[sc->offset]); 2668 break; 2669 } 2670 case AiocbPointer: { 2671 /* 2672 * aio_waitcomplete(2) assigns a pointer to a pointer to struct 2673 * aiocb, so we need to handle the extra layer of indirection. 2674 */ 2675 uintptr_t cbp; 2676 struct aiocb cb; 2677 2678 if (get_struct(pid, args[sc->offset], &cbp, sizeof(cbp)) != -1) { 2679 if (get_struct(pid, cbp, &cb, sizeof(cb)) != -1) 2680 print_aiocb(fp, &cb); 2681 else 2682 print_pointer(fp, cbp); 2683 } else 2684 print_pointer(fp, args[sc->offset]); 2685 break; 2686 } 2687 case Sctpsndrcvinfo: { 2688 struct sctp_sndrcvinfo info; 2689 2690 if (get_struct(pid, args[sc->offset], 2691 &info, sizeof(struct sctp_sndrcvinfo)) == -1) { 2692 print_pointer(fp, args[sc->offset]); 2693 break; 2694 } 2695 print_sctp_sndrcvinfo(fp, sc->type & OUT, &info); 2696 break; 2697 } 2698 case Msghdr: { 2699 struct msghdr msghdr; 2700 2701 if (get_struct(pid, args[sc->offset], 2702 &msghdr, sizeof(struct msghdr)) == -1) { 2703 print_pointer(fp, args[sc->offset]); 2704 break; 2705 } 2706 fputs("{", fp); 2707 print_sockaddr(fp, trussinfo, (uintptr_t)msghdr.msg_name, msghdr.msg_namelen); 2708 fprintf(fp, ",%d,", msghdr.msg_namelen); 2709 print_iovec(fp, trussinfo, (uintptr_t)msghdr.msg_iov, msghdr.msg_iovlen); 2710 fprintf(fp, ",%d,", msghdr.msg_iovlen); 2711 print_cmsgs(fp, pid, sc->type & OUT, &msghdr); 2712 fprintf(fp, ",%u,", msghdr.msg_controllen); 2713 print_mask_arg(sysdecode_msg_flags, fp, msghdr.msg_flags); 2714 fputs("}", fp); 2715 break; 2716 } 2717 2718 default: 2719 errx(1, "Invalid argument type %d\n", sc->type & ARG_MASK); 2720 } 2721 fclose(fp); 2722 return (tmp); 2723 } 2724 2725 /* 2726 * Print (to outfile) the system call and its arguments. 2727 */ 2728 void 2729 print_syscall(struct trussinfo *trussinfo) 2730 { 2731 struct threadinfo *t; 2732 const char *name; 2733 char **s_args; 2734 int i, len, nargs; 2735 2736 t = trussinfo->curthread; 2737 2738 name = t->cs.sc->name; 2739 nargs = t->cs.nargs; 2740 s_args = t->cs.s_args; 2741 2742 len = print_line_prefix(trussinfo); 2743 len += fprintf(trussinfo->outfile, "%s(", name); 2744 2745 for (i = 0; i < nargs; i++) { 2746 if (s_args[i] != NULL) 2747 len += fprintf(trussinfo->outfile, "%s", s_args[i]); 2748 else 2749 len += fprintf(trussinfo->outfile, 2750 "<missing argument>"); 2751 len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ? 2752 "," : ""); 2753 } 2754 len += fprintf(trussinfo->outfile, ")"); 2755 for (i = 0; i < 6 - (len / 8); i++) 2756 fprintf(trussinfo->outfile, "\t"); 2757 } 2758 2759 void 2760 print_syscall_ret(struct trussinfo *trussinfo, int error, syscallarg_t *retval) 2761 { 2762 struct timespec timediff; 2763 struct threadinfo *t; 2764 struct syscall *sc; 2765 2766 t = trussinfo->curthread; 2767 sc = t->cs.sc; 2768 if (trussinfo->flags & COUNTONLY) { 2769 timespecsub(&t->after, &t->before, &timediff); 2770 timespecadd(&sc->time, &timediff, &sc->time); 2771 sc->ncalls++; 2772 if (error != 0) 2773 sc->nerror++; 2774 return; 2775 } 2776 2777 print_syscall(trussinfo); 2778 fflush(trussinfo->outfile); 2779 2780 if (retval == NULL) { 2781 /* 2782 * This system call resulted in the current thread's exit, 2783 * so there is no return value or error to display. 2784 */ 2785 fprintf(trussinfo->outfile, "\n"); 2786 return; 2787 } 2788 2789 if (error == ERESTART) 2790 fprintf(trussinfo->outfile, " ERESTART\n"); 2791 else if (error == EJUSTRETURN) 2792 fprintf(trussinfo->outfile, " EJUSTRETURN\n"); 2793 else if (error != 0) { 2794 fprintf(trussinfo->outfile, " ERR#%d '%s'\n", 2795 sysdecode_freebsd_to_abi_errno(t->proc->abi->abi, error), 2796 strerror(error)); 2797 } else if (sc->decode.ret_type == 2 && 2798 t->proc->abi->pointer_size == 4) { 2799 off_t off; 2800 #if _BYTE_ORDER == _LITTLE_ENDIAN 2801 off = (off_t)retval[1] << 32 | retval[0]; 2802 #else 2803 off = (off_t)retval[0] << 32 | retval[1]; 2804 #endif 2805 fprintf(trussinfo->outfile, " = %jd (0x%jx)\n", (intmax_t)off, 2806 (intmax_t)off); 2807 } else { 2808 fprintf(trussinfo->outfile, " = %jd (0x%jx)\n", 2809 (intmax_t)retval[0], (intmax_t)retval[0]); 2810 } 2811 } 2812 2813 void 2814 print_summary(struct trussinfo *trussinfo) 2815 { 2816 struct timespec total = {0, 0}; 2817 struct syscall *sc; 2818 int ncall, nerror; 2819 2820 fprintf(trussinfo->outfile, "%-20s%15s%8s%8s\n", 2821 "syscall", "seconds", "calls", "errors"); 2822 ncall = nerror = 0; 2823 STAILQ_FOREACH(sc, &seen_syscalls, entries) { 2824 if (sc->ncalls) { 2825 fprintf(trussinfo->outfile, "%-20s%5jd.%09ld%8d%8d\n", 2826 sc->name, (intmax_t)sc->time.tv_sec, 2827 sc->time.tv_nsec, sc->ncalls, sc->nerror); 2828 timespecadd(&total, &sc->time, &total); 2829 ncall += sc->ncalls; 2830 nerror += sc->nerror; 2831 } 2832 } 2833 fprintf(trussinfo->outfile, "%20s%15s%8s%8s\n", 2834 "", "-------------", "-------", "-------"); 2835 fprintf(trussinfo->outfile, "%-20s%5jd.%09ld%8d%8d\n", 2836 "", (intmax_t)total.tv_sec, total.tv_nsec, ncall, nerror); 2837 } 2838