1// Copyright 2017 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package pprof
6
7import (
8	"context"
9	"runtime"
10	"unsafe"
11)
12
13// runtime_FrameStartLine is defined in runtime/symtab.go.
14//
15//go:noescape
16func runtime_FrameStartLine(f *runtime.Frame) int
17
18// runtime_FrameSymbolName is defined in runtime/symtab.go.
19//
20//go:noescape
21func runtime_FrameSymbolName(f *runtime.Frame) string
22
23// runtime_expandFinalInlineFrame is defined in runtime/symtab.go.
24func runtime_expandFinalInlineFrame(stk []uintptr) []uintptr
25
26// runtime_setProfLabel is defined in runtime/proflabel.go.
27func runtime_setProfLabel(labels unsafe.Pointer)
28
29// runtime_getProfLabel is defined in runtime/proflabel.go.
30func runtime_getProfLabel() unsafe.Pointer
31
32// SetGoroutineLabels sets the current goroutine's labels to match ctx.
33// A new goroutine inherits the labels of the goroutine that created it.
34// This is a lower-level API than [Do], which should be used instead when possible.
35func SetGoroutineLabels(ctx context.Context) {
36	ctxLabels, _ := ctx.Value(labelContextKey{}).(*labelMap)
37	runtime_setProfLabel(unsafe.Pointer(ctxLabels))
38}
39
40// Do calls f with a copy of the parent context with the
41// given labels added to the parent's label map.
42// Goroutines spawned while executing f will inherit the augmented label-set.
43// Each key/value pair in labels is inserted into the label map in the
44// order provided, overriding any previous value for the same key.
45// The augmented label map will be set for the duration of the call to f
46// and restored once f returns.
47func Do(ctx context.Context, labels LabelSet, f func(context.Context)) {
48	defer SetGoroutineLabels(ctx)
49	ctx = WithLabels(ctx, labels)
50	SetGoroutineLabels(ctx)
51	f(ctx)
52}
53