Home | History | Annotate | Download | only in testprogcgo
      1 // Copyright 2016 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 package main
      6 
      7 // Test that SIGPROF received in C code does not crash the process
      8 // looking for the C code's func pointer.
      9 
     10 // The test fails when the function is the first C function.
     11 // The exported functions are the first C functions, so we use that.
     12 
     13 // extern void GoNop();
     14 import "C"
     15 
     16 import (
     17 	"bytes"
     18 	"fmt"
     19 	"runtime/pprof"
     20 	"time"
     21 )
     22 
     23 func init() {
     24 	register("CgoCCodeSIGPROF", CgoCCodeSIGPROF)
     25 }
     26 
     27 //export GoNop
     28 func GoNop() {}
     29 
     30 func CgoCCodeSIGPROF() {
     31 	c := make(chan bool)
     32 	go func() {
     33 		<-c
     34 		start := time.Now()
     35 		for i := 0; i < 1e7; i++ {
     36 			if i%1000 == 0 {
     37 				if time.Since(start) > time.Second {
     38 					break
     39 				}
     40 			}
     41 			C.GoNop()
     42 		}
     43 		c <- true
     44 	}()
     45 
     46 	var buf bytes.Buffer
     47 	pprof.StartCPUProfile(&buf)
     48 	c <- true
     49 	<-c
     50 	pprof.StopCPUProfile()
     51 
     52 	fmt.Println("OK")
     53 }
     54