errors.go 1003 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package internal
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "github.com/cilium/ebpf/internal/unix"
  8. )
  9. // ErrorWithLog returns an error that includes logs from the
  10. // kernel verifier.
  11. //
  12. // logErr should be the error returned by the syscall that generated
  13. // the log. It is used to check for truncation of the output.
  14. func ErrorWithLog(err error, log []byte, logErr error) error {
  15. logStr := strings.Trim(CString(log), "\t\r\n ")
  16. if errors.Is(logErr, unix.ENOSPC) {
  17. logStr += " (truncated...)"
  18. }
  19. return &VerifierError{err, logStr}
  20. }
  21. // VerifierError includes information from the eBPF verifier.
  22. type VerifierError struct {
  23. cause error
  24. log string
  25. }
  26. func (le *VerifierError) Error() string {
  27. if le.log == "" {
  28. return le.cause.Error()
  29. }
  30. return fmt.Sprintf("%s: %s", le.cause, le.log)
  31. }
  32. // CString turns a NUL / zero terminated byte buffer into a string.
  33. func CString(in []byte) string {
  34. inLen := bytes.IndexByte(in, 0)
  35. if inLen == -1 {
  36. return ""
  37. }
  38. return string(in[:inLen])
  39. }