github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/strace/syscalls.go (about)

     1  // Copyright 2018 Google LLC.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package strace
    16  
    17  import (
    18  	"fmt"
    19  	"syscall"
    20  )
    21  
    22  // FormatSpecifier values describe how an individual syscall argument should be
    23  // formatted.
    24  type FormatSpecifier int
    25  
    26  // Valid FormatSpecifiers.
    27  //
    28  // Unless otherwise specified, values are formatted before syscall execution
    29  // and not updated after syscall execution (the same value is output).
    30  const (
    31  	// Hex is just a hexadecimal number.
    32  	Hex FormatSpecifier = iota
    33  
    34  	// Oct is just an octal number.
    35  	Oct
    36  
    37  	// ReadBuffer is a buffer for a read-style call. The syscall return
    38  	// value is used for the length.
    39  	//
    40  	// Formatted after syscall execution.
    41  	ReadBuffer
    42  
    43  	// WriteBuffer is a buffer for a write-style call. The following arg is
    44  	// used for the length.
    45  	//
    46  	// Contents omitted after syscall execution.
    47  	WriteBuffer
    48  
    49  	// ReadIOVec is a pointer to a struct iovec for a writev-style call.
    50  	// The following arg is used for the length. The return value is used
    51  	// for the total length.
    52  	//
    53  	// Complete contents only formatted after syscall execution.
    54  	ReadIOVec
    55  
    56  	// WriteIOVec is a pointer to a struct iovec for a writev-style call.
    57  	// The following arg is used for the length.
    58  	//
    59  	// Complete contents only formatted before syscall execution, omitted
    60  	// after.
    61  	WriteIOVec
    62  
    63  	// IOVec is a generic pointer to a struct iovec. Contents are not dumped.
    64  	IOVec
    65  
    66  	// SendMsgHdr is a pointer to a struct msghdr for a sendmsg-style call.
    67  	// Contents formatted only before syscall execution, omitted after.
    68  	SendMsgHdr
    69  
    70  	// RecvMsgHdr is a pointer to a struct msghdr for a recvmsg-style call.
    71  	// Contents formatted only after syscall execution.
    72  	RecvMsgHdr
    73  
    74  	// Path is a pointer to a char* path.
    75  	Path
    76  
    77  	// PostPath is a pointer to a char* path, formatted after syscall
    78  	// execution.
    79  	PostPath
    80  
    81  	// ExecveStringVector is a NULL-terminated array of strings. Enforces
    82  	// the maximum execve array length.
    83  	ExecveStringVector
    84  
    85  	// PipeFDs is an array of two FDs, formatted after syscall execution.
    86  	PipeFDs
    87  
    88  	// Uname is a pointer to a struct uname, formatted after syscall execution.
    89  	Uname
    90  
    91  	// Stat is a pointer to a struct stat, formatted after syscall execution.
    92  	Stat
    93  
    94  	// SockAddr is a pointer to a struct sockaddr. The following arg is
    95  	// used for length.
    96  	SockAddr
    97  
    98  	// PostSockAddr is a pointer to a struct sockaddr, formatted after
    99  	// syscall execution. The following arg is a pointer to the socklen_t
   100  	// length.
   101  	PostSockAddr
   102  
   103  	// SockLen is a pointer to a socklen_t, formatted before and after
   104  	// syscall execution.
   105  	SockLen
   106  
   107  	// SockFamily is a socket protocol family value.
   108  	SockFamily
   109  
   110  	// SockType is a socket type and flags value.
   111  	SockType
   112  
   113  	// SockProtocol is a socket protocol value. Argument n-2 is the socket
   114  	// protocol family.
   115  	SockProtocol
   116  
   117  	// SockFlags are socket flags.
   118  	SockFlags
   119  
   120  	// Timespec is a pointer to a struct timespec.
   121  	Timespec
   122  
   123  	// PostTimespec is a pointer to a struct timespec, formatted after
   124  	// syscall execution.
   125  	PostTimespec
   126  
   127  	// UTimeTimespec is a pointer to a struct timespec. Formatting includes
   128  	// UTIME_NOW and UTIME_OMIT.
   129  	UTimeTimespec
   130  
   131  	// ItimerVal is a pointer to a struct itimerval.
   132  	ItimerVal
   133  
   134  	// PostItimerVal is a pointer to a struct itimerval, formatted after
   135  	// syscall execution.
   136  	PostItimerVal
   137  
   138  	// ItimerSpec is a pointer to a struct itimerspec.
   139  	ItimerSpec
   140  
   141  	// PostItimerSpec is a pointer to a struct itimerspec, formatted after
   142  	// syscall execution.
   143  	PostItimerSpec
   144  
   145  	// Timeval is a pointer to a struct timeval, formatted before and after
   146  	// syscall execution.
   147  	Timeval
   148  
   149  	// Utimbuf is a pointer to a struct utimbuf.
   150  	Utimbuf
   151  
   152  	// Rusage is a struct rusage, formatted after syscall execution.
   153  	Rusage
   154  
   155  	// CloneFlags are clone(2) flags.
   156  	CloneFlags
   157  
   158  	// OpenFlags are open(2) flags.
   159  	OpenFlags
   160  
   161  	// Mode is a mode_t.
   162  	Mode
   163  
   164  	// FutexOp is the futex(2) operation.
   165  	FutexOp
   166  
   167  	// PtraceRequest is the ptrace(2) request.
   168  	PtraceRequest
   169  
   170  	// ItimerType is an itimer type (ITIMER_REAL, etc).
   171  	ItimerType
   172  )
   173  
   174  // defaultFormat is the syscall argument format to use if the actual format is
   175  // not known. It formats all six arguments as hex.
   176  var defaultFormat = []FormatSpecifier{Hex, Hex, Hex, Hex, Hex, Hex}
   177  
   178  func defaultSyscallInfo(sysno int) *SyscallInfo {
   179  	return &SyscallInfo{name: fmt.Sprintf("%d", sysno), format: defaultFormat}
   180  }
   181  
   182  // SyscallInfo captures the name and printing format of a syscall.
   183  type SyscallInfo struct {
   184  	// name is the name of the syscall.
   185  	name string
   186  
   187  	// format contains the format specifiers for each argument.
   188  	//
   189  	// Syscall calls can have up to six arguments. Arguments without a
   190  	// corresponding entry in format will not be printed.
   191  	format []FormatSpecifier
   192  }
   193  
   194  // makeSyscallInfo returns a SyscallInfo for a syscall.
   195  func makeSyscallInfo(name string, f ...FormatSpecifier) SyscallInfo {
   196  	return SyscallInfo{name: name, format: f}
   197  }
   198  
   199  // SyscallMap maps syscalls into names and printing formats.
   200  type SyscallMap map[uintptr]SyscallInfo
   201  
   202  var mapNames = map[string]uintptr{}
   203  
   204  // ByName returns a system call number, given a name.
   205  // It uses a map that is filled in dynamically.
   206  func ByName(name string) (uintptr, error) {
   207  	n, ok := mapNames[name]
   208  	if ok {
   209  		return n, nil
   210  	}
   211  
   212  	for k, v := range syscalls {
   213  		if v.name == name {
   214  			mapNames[name] = k
   215  			return k, nil
   216  		}
   217  	}
   218  	return 0, fmt.Errorf("%s:not found", name)
   219  }
   220  
   221  // ByNumber returns a system call name given a number.
   222  func ByNumber(sysno uintptr) (string, error) {
   223  	s, ok := syscalls[sysno]
   224  	if !ok {
   225  		return "", syscall.ENOENT
   226  	}
   227  	return s.name, nil
   228  }