Commit 608a1c5a authored by Kirill Smelkov's avatar Kirill Smelkov

xruntime: Factor out routine to get complete current traceback from exc

It is

	xruntime.Traceback()
parent 662c8077
...@@ -15,10 +15,10 @@ package main ...@@ -15,10 +15,10 @@ package main
import ( import (
"fmt" "fmt"
"runtime"
"strings" "strings"
"lab.nexedi.com/kirr/go123/myname" "lab.nexedi.com/kirr/go123/myname"
"lab.nexedi.com/kirr/go123/xruntime"
) )
// error type which is raised by raise(arg) // error type which is raised by raise(arg)
...@@ -31,8 +31,8 @@ func (e *Error) Error() string { ...@@ -31,8 +31,8 @@ func (e *Error) Error() string {
msgv := []string{} msgv := []string{}
msg := "" msg := ""
for e != nil { for e != nil {
// TODO(go1.7) -> runtime.Frame (see xtraceback()) // TODO(go1.7) -> runtime.Frame (see xruntime.Traceback())
if f, ok := e.arg.(Frame); ok { if f, ok := e.arg.(xruntime.Frame); ok {
//msg = f.Function //msg = f.Function
//msg = fmt.Sprintf("%s (%s:%d)", f.Function, f.File, f.Line) //msg = fmt.Sprintf("%s (%s:%d)", f.Function, f.File, f.Line)
msg = strings.TrimPrefix(f.Name(), _errorpkgdot) // XXX -> better prettyfunc msg = strings.TrimPrefix(f.Name(), _errorpkgdot) // XXX -> better prettyfunc
...@@ -135,45 +135,6 @@ func erraddcontext(e *Error, arg interface{}) *Error { ...@@ -135,45 +135,6 @@ func erraddcontext(e *Error, arg interface{}) *Error {
return &Error{arg, e} return &Error{arg, e}
} }
// TODO(go1.7) goes away in favour of runtime.Frame
type Frame struct {
*runtime.Func
pc uintptr
}
// get current calling traceback as []Frame
// nskip meaning: the same as in runtime.Callers()
// TODO(go1.7) []Frame -> []runtime.Frame
func xtraceback(nskip int) []Frame {
// all callers
var pcv = []uintptr{0}
for {
pcv = make([]uintptr, 2*len(pcv))
n := runtime.Callers(nskip+1, pcv)
if n < len(pcv) {
pcv = pcv[:n]
break
}
}
// pcv -> frames
/*
framev := make([]runtime.Frame, 0, len(pcv))
frames := runtime.CallersFrames(pcv)
for more := true; more; {
var frame runtime.Frame
frame, more = frames.Next()
framev = append(framev, frame)
}
*/
framev := make([]Frame, 0, len(pcv))
for _, pc := range pcv {
framev = append(framev, Frame{runtime.FuncForPC(pc), pc})
}
return framev
}
var ( var (
_errorpkgname string // package name under which error.go lives _errorpkgname string // package name under which error.go lives
_errorpkgdot string // errorpkg. _errorpkgdot string // errorpkg.
...@@ -191,7 +152,7 @@ func init() { ...@@ -191,7 +152,7 @@ func init() {
// see also: erraddcontext() // see also: erraddcontext()
func erraddcallingcontext(topfunc string, e *Error) *Error { func erraddcallingcontext(topfunc string, e *Error) *Error {
seenraise := false seenraise := false
for _, f := range xtraceback(2) { for _, f := range xruntime.Traceback(2) {
// do not show anything after raise*() // do not show anything after raise*()
if !seenraise && strings.HasPrefix(f.Name(), _errorraise) { if !seenraise && strings.HasPrefix(f.Name(), _errorraise) {
seenraise = true seenraise = true
......
// Copyright (C) 2015-2016 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// Package xruntime provides addons to standard package runtime
package xruntime
import (
"runtime"
)
// TODO(go1.7) goes away in favour of runtime.Frame
type Frame struct {
*runtime.Func
Pc uintptr
}
// get current calling traceback as []Frame
// nskip meaning: the same as in runtime.Callers()
// TODO(go1.7) []Frame -> []runtime.Frame
func Traceback(nskip int) []Frame {
// all callers
var pcv = []uintptr{0}
for {
pcv = make([]uintptr, 2*len(pcv))
n := runtime.Callers(nskip+1, pcv)
if n < len(pcv) {
pcv = pcv[:n]
break
}
}
// pcv -> frames
/*
framev := make([]runtime.Frame, 0, len(pcv))
frames := runtime.CallersFrames(pcv)
for more := true; more; {
var frame runtime.Frame
frame, more = frames.Next()
framev = append(framev, frame)
}
*/
framev := make([]Frame, 0, len(pcv))
for _, pc := range pcv {
framev = append(framev, Frame{runtime.FuncForPC(pc), pc})
}
return framev
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment