github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/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  	invalidptr: defaults to invalidptr=1, causing the garbage collector and stack
    43  	copier to crash the program if an invalid pointer value (for example, 1)
    44  	is found in a pointer-typed location. Setting invalidptr=0 disables this check.
    45  	This should only be used as a temporary workaround to diagnose buggy code.
    46  	The real fix is to not store integers in pointer-typed locations.
    47  
    48  	scheddetail: setting schedtrace=X and scheddetail=1 causes the scheduler to emit
    49  	detailed multiline info every X milliseconds, describing state of the scheduler,
    50  	processors, threads and goroutines.
    51  
    52  	schedtrace: setting schedtrace=X causes the scheduler to emit a single line to standard
    53  	error every X milliseconds, summarizing the scheduler state.
    54  
    55  	scavenge: scavenge=1 enables debugging mode of heap scavenger.
    56  
    57  	wbshadow: setting wbshadow=1 enables a shadow copy of the heap
    58  	used to detect missing write barriers at the next write to a
    59  	given location. If a bug can be detected in this mode it is
    60  	typically easy to understand, since the crash says quite
    61  	clearly what kind of word has missed a write barrier.
    62  	Setting wbshadow=2 checks the shadow copy during garbage
    63  	collection as well. Bugs detected at garbage collection can be
    64  	difficult to understand, because there is no context for what
    65  	the found word means. Typically you have to reproduce the
    66  	problem with allocfreetrace=1 in order to understand the type
    67  	of the badly updated word.
    68  
    69  The GOMAXPROCS variable limits the number of operating system threads that
    70  can execute user-level Go code simultaneously. There is no limit to the number of threads
    71  that can be blocked in system calls on behalf of Go code; those do not count against
    72  the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes
    73  the limit.
    74  
    75  The GOTRACEBACK variable controls the amount of output generated when a Go
    76  program fails due to an unrecovered panic or an unexpected runtime condition.
    77  By default, a failure prints a stack trace for every extant goroutine, eliding functions
    78  internal to the run-time system, and then exits with exit code 2.
    79  If GOTRACEBACK=0, the per-goroutine stack traces are omitted entirely.
    80  If GOTRACEBACK=1, the default behavior is used.
    81  If GOTRACEBACK=2, the per-goroutine stack traces include run-time functions.
    82  If GOTRACEBACK=crash, the per-goroutine stack traces include run-time functions,
    83  and if possible the program crashes in an operating-specific manner instead of
    84  exiting. For example, on Unix systems, the program raises SIGABRT to trigger a
    85  core dump.
    86  
    87  The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete
    88  the set of Go environment variables. They influence the building of Go programs
    89  (see http://golang.org/cmd/go and http://golang.org/pkg/go/build).
    90  GOARCH, GOOS, and GOROOT are recorded at compile time and made available by
    91  constants or functions in this package, but they do not influence the execution
    92  of the run-time system.
    93  */
    94  package runtime
    95  
    96  // Caller reports file and line number information about function invocations on
    97  // the calling goroutine's stack.  The argument skip is the number of stack frames
    98  // to ascend, with 0 identifying the caller of Caller.  (For historical reasons the
    99  // meaning of skip differs between Caller and Callers.) The return values report the
   100  // program counter, file name, and line number within the file of the corresponding
   101  // call.  The boolean ok is false if it was not possible to recover the information.
   102  func Caller(skip int) (pc uintptr, file string, line int, ok bool) {
   103  	// Ask for two PCs: the one we were asked for
   104  	// and what it called, so that we can see if it
   105  	// "called" sigpanic.
   106  	var rpc [2]uintptr
   107  	if callers(1+skip-1, &rpc[0], 2) < 2 {
   108  		return
   109  	}
   110  	f := findfunc(rpc[1])
   111  	if f == nil {
   112  		// TODO(rsc): Probably a bug?
   113  		// The C version said "have retpc at least"
   114  		// but actually returned pc=0.
   115  		ok = true
   116  		return
   117  	}
   118  	pc = rpc[1]
   119  	xpc := pc
   120  	g := findfunc(rpc[0])
   121  	// All architectures turn faults into apparent calls to sigpanic.
   122  	// If we see a call to sigpanic, we do not back up the PC to find
   123  	// the line number of the call instruction, because there is no call.
   124  	if xpc > f.entry && (g == nil || g.entry != funcPC(sigpanic)) {
   125  		xpc--
   126  	}
   127  	file, line32 := funcline(f, xpc)
   128  	line = int(line32)
   129  	ok = true
   130  	return
   131  }
   132  
   133  // Callers fills the slice pc with the return program counters of function invocations
   134  // on the calling goroutine's stack.  The argument skip is the number of stack frames
   135  // to skip before recording in pc, with 0 identifying the frame for Callers itself and
   136  // 1 identifying the caller of Callers.
   137  // It returns the number of entries written to pc.
   138  //
   139  // Note that since each slice entry pc[i] is a return program counter,
   140  // looking up the file and line for pc[i] (for example, using (*Func).FileLine)
   141  // will return the file and line number of the instruction immediately
   142  // following the call.
   143  // To look up the file and line number of the call itself, use pc[i]-1.
   144  // As an exception to this rule, if pc[i-1] corresponds to the function
   145  // runtime.sigpanic, then pc[i] is the program counter of a faulting
   146  // instruction and should be used without any subtraction.
   147  func Callers(skip int, pc []uintptr) int {
   148  	// runtime.callers uses pc.array==nil as a signal
   149  	// to print a stack trace.  Pick off 0-length pc here
   150  	// so that we don't let a nil pc slice get to it.
   151  	if len(pc) == 0 {
   152  		return 0
   153  	}
   154  	return callers(skip, &pc[0], len(pc))
   155  }
   156  
   157  // GOROOT returns the root of the Go tree.
   158  // It uses the GOROOT environment variable, if set,
   159  // or else the root used during the Go build.
   160  func GOROOT() string {
   161  	s := gogetenv("GOROOT")
   162  	if s != "" {
   163  		return s
   164  	}
   165  	return defaultGoroot
   166  }
   167  
   168  // Version returns the Go tree's version string.
   169  // It is either the commit hash and date at the time of the build or,
   170  // when possible, a release tag like "go1.3".
   171  func Version() string {
   172  	return theVersion
   173  }
   174  
   175  // GOOS is the running program's operating system target:
   176  // one of darwin, freebsd, linux, and so on.
   177  const GOOS string = theGoos
   178  
   179  // GOARCH is the running program's architecture target:
   180  // 386, amd64, or arm.
   181  const GOARCH string = theGoarch