Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Occasional off-by-one in the line numbers of runtime.Func.FileLine() #1100

Closed
gopherbot opened this issue Sep 11, 2010 · 2 comments
Closed

Comments

@gopherbot
Copy link

by esko.luontola:

What steps will reproduce the problem?

package gospec

import (
    "testing"
    "runtime"
    "fmt"
)

func TestLineNumbersOfFuncFileLine(t *testing.T) {
    defer func() {
        if cause := recover(); cause != nil {
            callers := make([]uintptr, 100)
            count := runtime.Callers(0, callers)
            callers = callers[0:count]

            for _, pc := range callers {
                f := runtime.FuncForPC(pc)
                name := f.Name()
                file, line := f.FileLine(pc)
                fmt.Printf("%v %v:%v\n", name, file, line)
            }
        }
    }()
    var x *Foo = nil
    x.Fail()
}

func TestLineNumbersOfPanic(t *testing.T) {
    var x *Foo = nil
    x.Fail()
}

type Foo struct {
    x string
}

func (this *Foo) Fail() string {
    return this.x
}


What is the expected output?

The line number of sigpanic in file runtime/linux/thread.c should be 286 in both stack
traces.

What do you see instead?

panicstring /home/orfjackal/go/src/pkg/runtime/runtime.c:83
sigpanic /home/orfjackal/go/src/pkg/runtime/linux/thread.c:287
gospec.*Foo·Fail /home/orfjackal/devel/gospec/src/gospec/bug_test.go:42
...
panicstring+0x69 /home/orfjackal/go/src/pkg/runtime/runtime.c:83
    panicstring(0x48ded8, 0x2b207ce90850)
sigpanic+0x148 /home/orfjackal/go/src/pkg/runtime/linux/thread.c:286
    sigpanic()
gospec.*Foo·Fail+0x18 /home/orfjackal/devel/gospec/src/gospec/bug_test.go:42
    gospec.*Foo·Fail(0x4166e1, 0x0, 0x2b207ce8bcf0, 0x2b207ce8bcf0)

What is your $GOOS?  $GOARCH?
linux amd64
(BTW, is there a simple way to get $GOOS and $GOARCH now that they are optional since
release.2010-08-25?)

Which revision are you using?  (hg identify)
97ccc7fb2173 tip

Please provide any additional information below.
This might be related to https://golang.org/issue/754
@rsc
Copy link
Contributor

rsc commented Sep 11, 2010

Comment 1:

> (BTW, is there a simple way to get $GOOS and $GOARCH now that they are optional since
release.2010-08-25?)
Thanks.  I changed the questions to ask for the compiler name and operating system.
Thanks for these reports.  It would help if you could send programs
that use package main and define a main function, so they are easier to run.
This is actually working as intended.  It's a subtle thing,
but runtime.Callers returns the return PCs going up the
stack.  The return PCs are the PCs of the instruction that
the call returns to, not the call itself.  The code that formats
the traceback for a crash subtracts 1 from each PC before
translating it to a line number.  The equivalent change
in your code would be to call f.FileLine(pc - 1).
runtime.Callers could lie and do the subtraction, but
this is pretty subtle already and I think it's clearer for
all involved to hand out the raw data and let clients
adjust.  Otherwise the clients who want the return PCs
have to add 1, and then if that changes they're all broken,
and so on and so on.

Owner changed to r...@golang.org.

Status changed to WorkingAsIntended.

@gopherbot
Copy link
Author

Comment 2 by esko.luontola:

Thanks for the information. I'll make the -1 adjustment in GoSpec, so that it will print
correct stack traces.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants