| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 1 | // This uses the unstable overrideWrite interface to also emit all runtime |
| 2 | // writes to a dedicated runtime file descriptor to catch and debug crash dumps. |
| 3 | // See https://go-review.googlesource.com/c/go/+/278792 for details about the |
| 4 | // interface. This interface is relatively special, refrain from using most Go |
| 5 | // features in here as it might cause unexpected behavior. Especially yielding |
| 6 | // is a bad idea as the scheduler might be in an inconsistent state. But using |
| 7 | // this interface was judged to be vastly more maintenance-friendly than |
| 8 | // attempting to parse out this information from a combined stderr. |
| 9 | package main |
| 10 | |
| 11 | import ( |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 12 | "os" |
| 13 | "unsafe" |
| 14 | |
| 15 | "golang.org/x/sys/unix" |
| 16 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame^] | 17 | "source.monogon.dev/osbase/logtree" |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | // This hooks into a global variable which is checked by runtime.write and used |
| 21 | // instead of runtime.write1 if populated. |
| Serge Bazanski | 3b09823 | 2023-03-16 17:57:02 +0100 | [diff] [blame] | 22 | // |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 23 | //go:linkname overrideWrite runtime.overrideWrite |
| 24 | var overrideWrite func(fd uintptr, p unsafe.Pointer, n int32) int32 |
| 25 | |
| Serge Bazanski | 5f8414d | 2022-06-24 13:02:11 +0200 | [diff] [blame] | 26 | // Contains the files into which runtime logs and crashes are written. |
| 27 | var runtimeFds []int |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 28 | |
| 29 | // This is essentially a reimplementation of the assembly function |
| 30 | // runtime.write1, just with a hardcoded file descriptor and using the assembly |
| 31 | // function unix.RawSyscall to not get a dependency on Go's calling convention |
| 32 | // and needing an implementation for every architecture. |
| Serge Bazanski | 3b09823 | 2023-03-16 17:57:02 +0100 | [diff] [blame] | 33 | // |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 34 | //go:nosplit |
| 35 | func runtimeWrite(fd uintptr, p unsafe.Pointer, n int32) int32 { |
| Serge Bazanski | 5f8414d | 2022-06-24 13:02:11 +0200 | [diff] [blame] | 36 | // Only redirect writes to stderr. |
| 37 | if fd != 2 { |
| 38 | a, _, err := unix.RawSyscall(unix.SYS_WRITE, fd, uintptr(p), uintptr(n)) |
| 39 | if err == 0 { |
| 40 | return int32(a) |
| 41 | } |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 42 | return int32(err) |
| 43 | } |
| Serge Bazanski | 5f8414d | 2022-06-24 13:02:11 +0200 | [diff] [blame] | 44 | // Write to the runtime panic FDs. |
| 45 | for _, f := range runtimeFds { |
| 46 | _, _, _ = unix.RawSyscall(unix.SYS_WRITE, uintptr(f), uintptr(p), uintptr(n)) |
| 47 | } |
| 48 | |
| 49 | // Finally, write to original FD |
| 50 | a, _, err := unix.RawSyscall(unix.SYS_WRITE, fd, uintptr(p), uintptr(n)) |
| 51 | if err == 0 { |
| 52 | return int32(a) |
| 53 | } |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 54 | return int32(err) |
| 55 | } |
| 56 | |
| Tim Windelschmidt | 13b83f7 | 2024-04-11 23:10:16 +0200 | [diff] [blame] | 57 | func initPanicHandler(lt *logtree.LogTree, consoles []*console) { |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 58 | l := lt.MustLeveledFor("panichandler") |
| Serge Bazanski | 5f8414d | 2022-06-24 13:02:11 +0200 | [diff] [blame] | 59 | |
| Lorenz Brun | d1f82e9 | 2024-02-08 19:27:46 +0100 | [diff] [blame] | 60 | // Setup pstore userspace message buffer |
| 61 | fd, err := unix.Open("/dev/pmsg0", os.O_WRONLY, 0) |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 62 | if err != nil { |
| Lorenz Brun | d1f82e9 | 2024-02-08 19:27:46 +0100 | [diff] [blame] | 63 | l.Errorf("Failed to open pstore userspace device (pstore probably unavailable): %v", err) |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 64 | l.Warningf("Continuing without persistent panic storage.") |
| Serge Bazanski | 5f8414d | 2022-06-24 13:02:11 +0200 | [diff] [blame] | 65 | } else { |
| 66 | runtimeFds = append(runtimeFds, fd) |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 67 | } |
| Serge Bazanski | 5f8414d | 2022-06-24 13:02:11 +0200 | [diff] [blame] | 68 | |
| Tim Windelschmidt | 13b83f7 | 2024-04-11 23:10:16 +0200 | [diff] [blame] | 69 | for _, c := range consoles { |
| 70 | fd, err := unix.Open(c.path, os.O_WRONLY, 0) |
| Serge Bazanski | 5f8414d | 2022-06-24 13:02:11 +0200 | [diff] [blame] | 71 | if err == nil { |
| 72 | runtimeFds = append(runtimeFds, fd) |
| Tim Windelschmidt | 13b83f7 | 2024-04-11 23:10:16 +0200 | [diff] [blame] | 73 | l.Infof("Panic console: %s", c.path) |
| Serge Bazanski | 5f8414d | 2022-06-24 13:02:11 +0200 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 77 | // This could cause a data race if the runtime crashed while we're |
| 78 | // initializing the crash handler, but there is no locking infrastructure |
| 79 | // for this so we have to take that risk. |
| 80 | overrideWrite = runtimeWrite |
| Lorenz Brun | 4025c9b | 2022-06-16 16:12:53 +0000 | [diff] [blame] | 81 | } |