8

Let's say I have a stacktrace for a bunch of goroutines, e. g.:

goroutine 5633 [select]:
net/http.(*persistConn).writeLoop(0xc21303ac00)
    /usr/lib/go/src/pkg/net/http/transport.go:791 +0x271
created by net/http.(*Transport).dialConn
    /usr/lib/go/src/pkg/net/http/transport.go:529 +0x61e

In my case a unique application-specific object is served by a set of goroutines, and I want to look at the stacktrace of goroutines relating to a particular object. I have hundreds of application-specific objects, so I get hundreds of identical goroutines.

How would I go about correlating my logs with goroutines in the stacktrace? There doesn't seem to be a way of identifying a current goroutine in a stack trace and no way of naming a goroutine so I can see a specific value in stack trace.

PS

I've already read the related why-would-you-want-to-do-it posts on Go mailing list, so I'm looking for alternatives/hacks/workarounds (that hopefully don't involve sprinkling the code with a log call every other line).

0

2 Answers 2

5

Play

This method is simpler than @Alex B. It use the fact that debug.Stack() return stack trace in the form of byte slice which contains goroutine its id in the second word boundary along with, well stack trace. And parse it.

package main

import (
    "bytes"
    "fmt"
    "runtime/debug"
    "sync"
)

func main() {
    w := sync.WaitGroup{}
    w.Add(1)
    go func() {
        gr := bytes.Fields(debug.Stack())[1]
        fmt.Println(string(gr))
        w.Done()
    }()
    w.Wait()
}
2
  • Personally I would optimize this by using runtime.Stack instead of (runtime/)debug.Stack, and give it just enough space to fit the string goroutine {longest possible ID}, so that you're not using the space and taking the run time to generate the whole stack trace only to throw it away. (It takes 20 digits for the largest 64-bit integer, 21 if you allow for the negative sign.)
    – mtraceur
    Sep 7, 2022 at 20:31
  • Examples using runtime.Stack instead of debug.Stack, based on this answer: one which converts to uint64 and one which just leaves the ID integer unparsed as a string. (Well, the func main in the example is just tweaked from this answer, and I might not have thought to use bytes.Fields if I hadn't seen this answer using it.)
    – mtraceur
    Sep 10, 2022 at 3:24
4

A workaround is possible, and involves a small piece of C code.

goid.c

#include <runtime.h>
void ·GetGoID(int32 ret) {
    ret = g->goid;
    USED(&ret);
}

main.go

package main
import (
    "fmt"
    "sync"
)
// Declaration is required
func GetGoID() int32
func main() {
    var wg sync.WaitGroup
    f := func() {
        wg.Add(1)
        go func() {
            fmt.Printf("goroutine %d\n", GetGoID())
            wg.Done()
        }()
    }
    for i := 0; i < 10; i++ {
        f()
    }
    wg.Wait()
}

Build and run

$ go build
$ ./example
goroutine 20
goroutine 21
goroutine 22
goroutine 23
goroutine 24
goroutine 25
goroutine 26
goroutine 27
goroutine 28
goroutine 29
2
  • 1
    I'm familiar with Golang, but not so much with C. Can you please explain what the statements in GetGoID do? "ret = g->goid; USED(&ret);"
    – Nannan AV
    Jul 24, 2018 at 9:18
  • I don't think this works anymore? - notes.volution.ro/v1/2019/08/notes/23e3644e
    – slm
    Jun 5, 2021 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.