elvish/pkg/eval/builtin_fn_debug.go

40 lines
595 B
Go
Raw Normal View History

2018-02-03 14:12:13 +08:00
package eval
import (
"runtime"
"src.elv.sh/pkg/logutil"
"src.elv.sh/pkg/parse"
2018-02-03 14:12:13 +08:00
)
func init() {
addBuiltinFns(map[string]any{
"src": src,
"-gc": _gc,
"-stack": _stack,
"-log": _log,
})
}
func src(fm *Frame) parse.Source {
return fm.srcMeta
}
func _gc() {
runtime.GC()
}
func _stack(fm *Frame) error {
// TODO(xiaq): Dup with main.go.
2018-02-03 14:12:13 +08:00
buf := make([]byte, 1024)
for runtime.Stack(buf, true) == cap(buf) {
buf = make([]byte, cap(buf)*2)
}
_, err := fm.ByteOutput().Write(buf)
return err
2018-02-03 14:12:13 +08:00
}
func _log(fname string) error {
return logutil.SetOutputFile(fname)
2018-02-03 14:12:13 +08:00
}