如果它发生混乱(在Go中)我想从函数返回错误:
func getReport(filename string) (rep report, err error) {
rep.data = make(map[string]float64)
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
err, _ = r.(error)
return nil, err
}
}()
panic("Report format not recognized.")
// rest of the getReport function, which can try to out-of-bound-access a slice
...
}
Run Code Online (Sandbox Code Playgroud)
我似乎误解了恐慌和推迟的概念.任何人都可以开导我吗?
Consider the following linux kernel dump stack trace, you can trigger a panic from the kernel source code by calling panic("debugging a linux kernel panic");:
[<001360ac>] (unwind_backtrace+0x0/0xf8) from [<00147b7c>] (warn_slowpath_common+0x50/0x60)
[<00147b7c>] (warn_slowpath_common+0x50/0x60) from [<00147c40>] (warn_slowpath_null+0x1c/0x24)
[<00147c40>] (warn_slowpath_null+0x1c/0x24) from [<0014de44>] (local_bh_enable_ip+0xa0/0xac)
[<0014de44>] (local_bh_enable_ip+0xa0/0xac) from [<0019594c>] (bdi_register+0xec/0x150)
Run Code Online (Sandbox Code Playgroud)
unwind_backtrace+0x0/0xf8 what the +0x0/0xf8 stands for?unwind_backtrace+0x0/0xf8?我试图更新我们的Ubuntu服务器中的libc,但它失败了,现在当我重新启动服务器时,我收到一条错误消息:
内核恐慌 - 没有同步 - 试图杀死init!
它只是挂起.
这个问题的解决方案是什么?服务器由10个人使用,所以我不想重新安装擦除他们的数据.
Call Trace包含以下条目:
[<deadbeef>] FunctionName+0xAB/0xCD [module_name]
[<f00fface>] ? AnotherFunctionName+0x12/0x40 [module_name]
[<deaffeed>] ClearFunctionName+0x88/0x88 [module_name]
Run Code Online (Sandbox Code Playgroud)
'?'是什么意思?在AnotherFunctionName之前标记?
我在我的设备上使用Android Custom ROM,也使用自定义boot.img(自定义内核+ cmdline + ramdisk).我现在希望能够在内核崩溃后立即查看内核日志,但遗憾的是我无法使用串行控制台.
好消息:Android的Linux内核中似乎有一些源/模块正是为此目的编写的.例如,在我的内核的.config文件中激活了以下行:
CONFIG_ANDROID_RAM_CONSOLE=y
CONFIG_ANDROID_RAM_CONSOLE_ENABLE_VERBOSE=y
CONFIG_APANIC=y
CONFIG_APANIC_PLABEL="oem_log"
Run Code Online (Sandbox Code Playgroud)
我的问题是:在我强制内核恐慌以便测试它之后,即通过加载一个简单的恐慌内核模块insmod panic.ko,似乎没有日志被写入名为oem_log的MTD (存在于我的设备上).其次,RAM在重启后也不包含日志,因为它似乎被清除 - 或者也没有写入日志.
那么如何在恐慌之后获取内核日志呢?如果有一种方法可以测试正在运行的系统上的APANIC,那将会很有帮助.也许通过使用内核调试系统?截至目前,我对此很陌生.
在此先感谢您的帮助!
我已经阅读了几个关于 SO 的答案,并收集了这些用例:
panic!函数但我仍然不清楚为什么我们需要这样定义函数:
fn func() -> ! {
panic!("Error!");
}
Run Code Online (Sandbox Code Playgroud)
如果它的工作方式与此相同(没有感叹号):
fn func() {
panic!("Error!");
}
Run Code Online (Sandbox Code Playgroud)
同时,为什么我们需要!在具有无限循环的函数中使用?看起来这个签名并没有带来任何真实的使用信息。
我们有一个大型golang应用程序,它使用记录器(实际上是一个自定义记录器)将输出写入定期轮换的日志文件.
但是,当应用程序崩溃或panic()时,这些消息会转到标准错误.
有没有办法覆盖恐慌功能来使用我们的记录器?
Go提供了两种处理错误的方法,但我不确定使用哪种方法.
假设我正在实现一个ForEach接受切片或地图作为参数的经典函数.要检查是否传入了iterable,我可以这样做:
func ForEach(iterable interface{}, f interface{}) {
if isNotIterable(iterable) {
panic("Should pass in a slice or map!")
}
}
Run Code Online (Sandbox Code Playgroud)
要么
func ForEach(iterable interface{}, f interface{}) error {
if isNotIterable(iterable) {
return fmt.Errorf("Should pass in a slice or map!")
}
}
Run Code Online (Sandbox Code Playgroud)
我看到一些讨论说panic()应该避免,但人们也说如果程序无法从错误中恢复,你应该panic().
我应该使用哪一个?选择正确的主要原则是什么?
众所周知,恐慌会产生一个堆栈跟踪到stdout(Playground链接):
panic: runtime error: index out of range
goroutine 1 [running]:
main.main()
/tmp/sandbox579134920/main.go:9 +0x20
Run Code Online (Sandbox Code Playgroud)
当你从恐慌中恢复时,似乎recover()只返回error描述导致恐慌的原因(游乐场链接).
runtime error: index out of range
Run Code Online (Sandbox Code Playgroud)
我的问题是,是否可以存储写入stdout的堆栈跟踪?这提供了比字符串更好的调试信息,runtime error: index out of range因为它显示文件中引起恐慌的确切行.
我是android app dev的新手.当我创建一个新的AVD时,当我点击这个AVD的开始:我得到以下内容:
Starting emulator for AVD 'Nexus_4_16_AVD'
PANIC: Could not open: Nexus_4_16_AVD
Run Code Online (Sandbox Code Playgroud)
