| 1 |
// Copyright 2025 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 |
|
| 5 |
//go:build !plan9 && !windows |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
// Regression test for https://go.dev/issue/72870. Go code called from C should |
| 10 |
// never be reported as external code. |
| 11 |
|
| 12 |
/* |
| 13 |
#include <pthread.h> |
| 14 |
|
| 15 |
void go_callback1(); |
| 16 |
void go_callback2(); |
| 17 |
|
| 18 |
static void *callback_pprof_thread(void *arg) { |
| 19 |
go_callback1(); |
| 20 |
return 0; |
| 21 |
} |
| 22 |
|
| 23 |
static void c_callback(void) { |
| 24 |
go_callback2(); |
| 25 |
} |
| 26 |
|
| 27 |
static void start_callback_pprof_thread() { |
| 28 |
pthread_t th; |
| 29 |
pthread_attr_t attr; |
| 30 |
pthread_attr_init(&attr); |
| 31 |
pthread_create(&th, &attr, callback_pprof_thread, 0); |
| 32 |
// Don't join, caller will watch pprof. |
| 33 |
} |
| 34 |
*/ |
| 35 |
import "C" |
| 36 |
|
| 37 |
import ( |
| 38 |
"bytes" |
| 39 |
"fmt" |
| 40 |
"internal/profile" |
| 41 |
"os" |
| 42 |
"runtime/pprof" |
| 43 |
"time" |
| 44 |
) |
| 45 |
|
| 46 |
func init() { |
| 47 |
register("CgoCallbackPprof", CgoCallbackPprof) |
| 48 |
} |
| 49 |
|
| 50 |
func CgoCallbackPprof() { |
| 51 |
C.start_callback_pprof_thread() |
| 52 |
|
| 53 |
var buf bytes.Buffer |
| 54 |
if err := pprof.StartCPUProfile(&buf); err != nil { |
| 55 |
fmt.Printf("Error starting CPU profile: %v\n", err) |
| 56 |
os.Exit(1) |
| 57 |
} |
| 58 |
time.Sleep(1 * time.Second) |
| 59 |
pprof.StopCPUProfile() |
| 60 |
|
| 61 |
p, err := profile.Parse(&buf) |
| 62 |
if err != nil { |
| 63 |
fmt.Printf("Error parsing profile: %v\n", err) |
| 64 |
os.Exit(1) |
| 65 |
} |
| 66 |
|
| 67 |
foundCallee := false |
| 68 |
for _, s := range p.Sample { |
| 69 |
funcs := flattenFrames(s) |
| 70 |
if len(funcs) == 0 { |
| 71 |
continue |
| 72 |
} |
| 73 |
|
| 74 |
leaf := funcs[0] |
| 75 |
if leaf.Name != "main.go_callback1_callee" { |
| 76 |
continue |
| 77 |
} |
| 78 |
foundCallee = true |
| 79 |
|
| 80 |
if len(funcs) < 2 { |
| 81 |
fmt.Printf("Profile: %s\n", p) |
| 82 |
frames := make([]string, len(funcs)) |
| 83 |
for i := range funcs { |
| 84 |
frames[i] = funcs[i].Name |
| 85 |
} |
| 86 |
fmt.Printf("FAIL: main.go_callback1_callee sample missing caller in frames %v\n", frames) |
| 87 |
os.Exit(1) |
| 88 |
} |
| 89 |
|
| 90 |
if funcs[1].Name != "main.go_callback1" { |
| 91 |
// In https://go.dev/issue/72870, this will be runtime._ExternalCode. |
| 92 |
fmt.Printf("Profile: %s\n", p) |
| 93 |
frames := make([]string, len(funcs)) |
| 94 |
for i := range funcs { |
| 95 |
frames[i] = funcs[i].Name |
| 96 |
} |
| 97 |
fmt.Printf("FAIL: main.go_callback1_callee sample caller got %s want main.go_callback1 in frames %v\n", funcs[1].Name, frames) |
| 98 |
os.Exit(1) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if !foundCallee { |
| 103 |
fmt.Printf("Missing main.go_callback1_callee sample in profile %s\n", p) |
| 104 |
os.Exit(1) |
| 105 |
} |
| 106 |
|
| 107 |
fmt.Printf("OK\n") |
| 108 |
} |
| 109 |
|
| 110 |
// Return the frame functions in s, regardless of inlining. |
| 111 |
func flattenFrames(s *profile.Sample) []*profile.Function { |
| 112 |
ret := make([]*profile.Function, 0, len(s.Location)) |
| 113 |
for _, loc := range s.Location { |
| 114 |
for _, line := range loc.Line { |
| 115 |
ret = append(ret, line.Function) |
| 116 |
} |
| 117 |
} |
| 118 |
return ret |
| 119 |
} |
| 120 |
|
| 121 |
//export go_callback1 |
| 122 |
func go_callback1() { |
| 123 |
// This is a separate function just to ensure we have another Go |
| 124 |
// function as the caller in the profile. |
| 125 |
go_callback1_callee() |
| 126 |
} |
| 127 |
|
| 128 |
func go_callback1_callee() { |
| 129 |
C.c_callback() |
| 130 |
|
| 131 |
// Spin for CPU samples. |
| 132 |
for { |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
//export go_callback2 |
| 137 |
func go_callback2() { |
| 138 |
} |