github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/runtime/extern.go (about)

     1  // Copyright 2009 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  /*
     6  Package runtime contains operations that interact with Go's runtime system,
     7  such as functions to control goroutines. It also includes the low-level type information
     8  used by the reflect package; see reflect's documentation for the programmable
     9  interface to the run-time type system.
    10  
    11  Environment Variables
    12  
    13  The following environment variables ($name or %name%, depending on the host
    14  operating system) control the run-time behavior of Go programs. The meanings
    15  and use may change from release to release.
    16  
    17  The GOGC variable sets the initial garbage collection target percentage.
    18  A collection is triggered when the ratio of freshly allocated data to live data
    19  remaining after the previous collection reaches this percentage. The default
    20  is GOGC=100. Setting GOGC=off disables the garbage collector entirely.
    21  The runtime/debug package's SetGCPercent function allows changing this
    22  percentage at run time. See http://golang.org/pkg/runtime/debug/#SetGCPercent.
    23  
    24  The GODEBUG variable controls debug output from the runtime. GODEBUG value is
    25  a comma-separated list of name=val pairs. Supported names are:
    26  
    27  	allocfreetrace: setting allocfreetrace=1 causes every allocation to be
    28  	profiled and a stack trace printed on each object's allocation and free.
    29  
    30  	efence: setting efence=1 causes the allocator to run in a mode
    31  	where each object is allocated on a unique page and addresses are
    32  	never recycled.
    33  
    34  	gctrace: setting gctrace=1 causes the garbage collector to emit a single line to standard
    35  	error at each collection, summarizing the amount of memory collected and the
    36  	length of the pause. Setting gctrace=2 emits the same summary but also
    37  	repeats each collection.
    38  
    39  	gcdead: setting gcdead=1 causes the garbage collector to clobber all stack slots
    40  	that it thinks are dead.
    41  
    42  	scheddetail: setting schedtrace=X and scheddetail=1 causes the scheduler to emit
    43  	detailed multiline info every X milliseconds, describing state of the scheduler,
    44  	processors, threads and goroutines.
    45  
    46  	schedtrace: setting schedtrace=X causes the scheduler to emit a single line to standard
    47  	error every X milliseconds, summarizing the scheduler state.
    48  
    49  	scavenge: scavenge=1 enables debugging mode of heap scavenger.
    50  
    51  The GOMAXPROCS variable limits the number of operating system threads that
    52  can execute user-level Go code simultaneously. There is no limit to the number of threads
    53  that can be blocked in system calls on behalf of Go code; those do not count against
    54  the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes
    55  the limit.
    56  
    57  The GOTRACEBACK variable controls the amount of output generated when a Go
    58  program fails due to an unrecovered panic or an unexpected runtime condition.
    59  By default, a failure prints a stack trace for every extant goroutine, eliding functions
    60  internal to the run-time system, and then exits with exit code 2.
    61  If GOTRACEBACK=0, the per-goroutine stack traces are omitted entirely.
    62  If GOTRACEBACK=1, the default behavior is used.
    63  If GOTRACEBACK=2, the per-goroutine stack traces include run-time functions.
    64  If GOTRACEBACK=crash, the per-goroutine stack traces include run-time functions,
    65  and if possible the program crashes in an operating-specific manner instead of
    66  exiting. For example, on Unix systems, the program raises SIGABRT to trigger a
    67  core dump.
    68  
    69  The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete
    70  the set of Go environment variables. They influence the building of Go programs
    71  (see http://golang.org/cmd/go and http://golang.org/pkg/go/build).
    72  GOARCH, GOOS, and GOROOT are recorded at compile time and made available by
    73  constants or functions in this package, but they do not influence the execution
    74  of the run-time system.
    75  */
    76  package runtime
    77  
    78  // Caller reports file and line number information about function invocations on
    79  // the calling goroutine's stack.  The argument skip is the number of stack frames
    80  // to ascend, with 0 identifying the caller of Caller.  (For historical reasons the
    81  // meaning of skip differs between Caller and Callers.) The return values report the
    82  // program counter, file name, and line number within the file of the corresponding
    83  // call.  The boolean ok is false if it was not possible to recover the information.
    84  func Caller(skip int) (pc uintptr, file string, line int, ok bool) {
    85  	// Ask for two PCs: the one we were asked for
    86  	// and what it called, so that we can see if it
    87  	// "called" sigpanic.
    88  	var rpc [2]uintptr
    89  	if callers(1+skip-1, &rpc[0], 2) < 2 {
    90  		return
    91  	}
    92  	f := findfunc(rpc[1])
    93  	if f == nil {
    94  		// TODO(rsc): Probably a bug?
    95  		// The C version said "have retpc at least"
    96  		// but actually returned pc=0.
    97  		ok = true
    98  		return
    99  	}
   100  	pc = rpc[1]
   101  	xpc := pc
   102  	g := findfunc(rpc[0])
   103  	// All architectures turn faults into apparent calls to sigpanic.
   104  	// If we see a call to sigpanic, we do not back up the PC to find
   105  	// the line number of the call instruction, because there is no call.
   106  	if xpc > f.entry && (g == nil || g.entry != funcPC(sigpanic)) {
   107  		xpc--
   108  	}
   109  	line = int(funcline(f, xpc, &file))
   110  	ok = true
   111  	return
   112  }
   113  
   114  // Callers fills the slice pc with the program counters of function invocations
   115  // on the calling goroutine's stack.  The argument skip is the number of stack frames
   116  // to skip before recording in pc, with 0 identifying the frame for Callers itself and
   117  // 1 identifying the caller of Callers.
   118  // It returns the number of entries written to pc.
   119  func Callers(skip int, pc []uintptr) int {
   120  	// runtime.callers uses pc.array==nil as a signal
   121  	// to print a stack trace.  Pick off 0-length pc here
   122  	// so that we don't let a nil pc slice get to it.
   123  	if len(pc) == 0 {
   124  		return 0
   125  	}
   126  	return callers(skip, &pc[0], len(pc))
   127  }
   128  
   129  // GOROOT returns the root of the Go tree.
   130  // It uses the GOROOT environment variable, if set,
   131  // or else the root used during the Go build.
   132  func GOROOT() string {
   133  	s := gogetenv("GOROOT")
   134  	if s != "" {
   135  		return s
   136  	}
   137  	return defaultGoroot
   138  }
   139  
   140  // Version returns the Go tree's version string.
   141  // It is either the commit hash and date at the time of the build or,
   142  // when possible, a release tag like "go1.3".
   143  func Version() string {
   144  	return theVersion
   145  }
   146  
   147  // GOOS is the running program's operating system target:
   148  // one of darwin, freebsd, linux, and so on.
   149  const GOOS string = theGoos
   150  
   151  // GOARCH is the running program's architecture target:
   152  // 386, amd64, or arm.
   153  const GOARCH string = theGoarch