...
Run Format

Source file src/runtime/extern.go

     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 https://golang.org/pkg/runtime/debug/#SetGCPercent.
    23	
    24	The GODEBUG variable controls debugging variables within the runtime.
    25	It is a comma-separated list of name=val pairs setting these named variables:
    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		gccheckmark: setting gccheckmark=1 enables verification of the
    35		garbage collector's concurrent mark phase by performing a
    36		second mark pass while the world is stopped.  If the second
    37		pass finds a reachable object that was not found by concurrent
    38		mark, the garbage collector will panic.
    39	
    40		gcpacertrace: setting gcpacertrace=1 causes the garbage collector to
    41		print information about the internal state of the concurrent pacer.
    42	
    43		gcshrinkstackoff: setting gcshrinkstackoff=1 disables moving goroutines
    44		onto smaller stacks. In this mode, a goroutine's stack can only grow.
    45	
    46		gcstackbarrieroff: setting gcstackbarrieroff=1 disables the use of stack barriers
    47		that allow the garbage collector to avoid repeating a stack scan during the
    48		mark termination phase.
    49	
    50		gcstoptheworld: setting gcstoptheworld=1 disables concurrent garbage collection,
    51		making every garbage collection a stop-the-world event. Setting gcstoptheworld=2
    52		also disables concurrent sweeping after the garbage collection finishes.
    53	
    54		gctrace: setting gctrace=1 causes the garbage collector to emit a single line to standard
    55		error at each collection, summarizing the amount of memory collected and the
    56		length of the pause. Setting gctrace=2 emits the same summary but also
    57		repeats each collection. The format of this line is subject to change.
    58		Currently, it is:
    59			gc # @#s #%: #+...+# ms clock, #+...+# ms cpu, #->#-># MB, # MB goal, # P
    60		where the fields are as follows:
    61			gc #        the GC number, incremented at each GC
    62			@#s         time in seconds since program start
    63			#%          percentage of time spent in GC since program start
    64			#+...+#     wall-clock/CPU times for the phases of the GC
    65			#->#-># MB  heap size at GC start, at GC end, and live heap
    66			# MB goal   goal heap size
    67			# P         number of processors used
    68		The phases are stop-the-world (STW) sweep termination, scan,
    69		synchronize Ps, mark, and STW mark termination. The CPU times
    70		for mark are broken down in to assist time (GC performed in
    71		line with allocation), background GC time, and idle GC time.
    72		If the line ends with "(forced)", this GC was forced by a
    73		runtime.GC() call and all phases are STW.
    74	
    75		memprofilerate: setting memprofilerate=X will update the value of runtime.MemProfileRate.
    76		When set to 0 memory profiling is disabled.  Refer to the description of
    77		MemProfileRate for the default value.
    78	
    79		invalidptr: defaults to invalidptr=1, causing the garbage collector and stack
    80		copier to crash the program if an invalid pointer value (for example, 1)
    81		is found in a pointer-typed location. Setting invalidptr=0 disables this check.
    82		This should only be used as a temporary workaround to diagnose buggy code.
    83		The real fix is to not store integers in pointer-typed locations.
    84	
    85		sbrk: setting sbrk=1 replaces the memory allocator and garbage collector
    86		with a trivial allocator that obtains memory from the operating system and
    87		never reclaims any memory.
    88	
    89		scavenge: scavenge=1 enables debugging mode of heap scavenger.
    90	
    91		scheddetail: setting schedtrace=X and scheddetail=1 causes the scheduler to emit
    92		detailed multiline info every X milliseconds, describing state of the scheduler,
    93		processors, threads and goroutines.
    94	
    95		schedtrace: setting schedtrace=X causes the scheduler to emit a single line to standard
    96		error every X milliseconds, summarizing the scheduler state.
    97	
    98	The GOMAXPROCS variable limits the number of operating system threads that
    99	can execute user-level Go code simultaneously. There is no limit to the number of threads
   100	that can be blocked in system calls on behalf of Go code; those do not count against
   101	the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes
   102	the limit.
   103	
   104	The GOTRACEBACK variable controls the amount of output generated when a Go
   105	program fails due to an unrecovered panic or an unexpected runtime condition.
   106	By default, a failure prints a stack trace for every extant goroutine, eliding functions
   107	internal to the run-time system, and then exits with exit code 2.
   108	If GOTRACEBACK=0, the per-goroutine stack traces are omitted entirely.
   109	If GOTRACEBACK=1, the default behavior is used.
   110	If GOTRACEBACK=2, the per-goroutine stack traces include run-time functions.
   111	If GOTRACEBACK=crash, the per-goroutine stack traces include run-time functions,
   112	and if possible the program crashes in an operating-specific manner instead of
   113	exiting. For example, on Unix systems, the program raises SIGABRT to trigger a
   114	core dump.
   115	
   116	The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete
   117	the set of Go environment variables. They influence the building of Go programs
   118	(see https://golang.org/cmd/go and https://golang.org/pkg/go/build).
   119	GOARCH, GOOS, and GOROOT are recorded at compile time and made available by
   120	constants or functions in this package, but they do not influence the execution
   121	of the run-time system.
   122	*/
   123	package runtime
   124	
   125	// Caller reports file and line number information about function invocations on
   126	// the calling goroutine's stack.  The argument skip is the number of stack frames
   127	// to ascend, with 0 identifying the caller of Caller.  (For historical reasons the
   128	// meaning of skip differs between Caller and Callers.) The return values report the
   129	// program counter, file name, and line number within the file of the corresponding
   130	// call.  The boolean ok is false if it was not possible to recover the information.
   131	func Caller(skip int) (pc uintptr, file string, line int, ok bool) {
   132		// Ask for two PCs: the one we were asked for
   133		// and what it called, so that we can see if it
   134		// "called" sigpanic.
   135		var rpc [2]uintptr
   136		if callers(1+skip-1, rpc[:]) < 2 {
   137			return
   138		}
   139		f := findfunc(rpc[1])
   140		if f == nil {
   141			// TODO(rsc): Probably a bug?
   142			// The C version said "have retpc at least"
   143			// but actually returned pc=0.
   144			ok = true
   145			return
   146		}
   147		pc = rpc[1]
   148		xpc := pc
   149		g := findfunc(rpc[0])
   150		// All architectures turn faults into apparent calls to sigpanic.
   151		// If we see a call to sigpanic, we do not back up the PC to find
   152		// the line number of the call instruction, because there is no call.
   153		if xpc > f.entry && (g == nil || g.entry != funcPC(sigpanic)) {
   154			xpc--
   155		}
   156		file, line32 := funcline(f, xpc)
   157		line = int(line32)
   158		ok = true
   159		return
   160	}
   161	
   162	// Callers fills the slice pc with the return program counters of function invocations
   163	// on the calling goroutine's stack.  The argument skip is the number of stack frames
   164	// to skip before recording in pc, with 0 identifying the frame for Callers itself and
   165	// 1 identifying the caller of Callers.
   166	// It returns the number of entries written to pc.
   167	//
   168	// Note that since each slice entry pc[i] is a return program counter,
   169	// looking up the file and line for pc[i] (for example, using (*Func).FileLine)
   170	// will return the file and line number of the instruction immediately
   171	// following the call.
   172	// To look up the file and line number of the call itself, use pc[i]-1.
   173	// As an exception to this rule, if pc[i-1] corresponds to the function
   174	// runtime.sigpanic, then pc[i] is the program counter of a faulting
   175	// instruction and should be used without any subtraction.
   176	func Callers(skip int, pc []uintptr) int {
   177		// runtime.callers uses pc.array==nil as a signal
   178		// to print a stack trace.  Pick off 0-length pc here
   179		// so that we don't let a nil pc slice get to it.
   180		if len(pc) == 0 {
   181			return 0
   182		}
   183		return callers(skip, pc)
   184	}
   185	
   186	// GOROOT returns the root of the Go tree.
   187	// It uses the GOROOT environment variable, if set,
   188	// or else the root used during the Go build.
   189	func GOROOT() string {
   190		s := gogetenv("GOROOT")
   191		if s != "" {
   192			return s
   193		}
   194		return defaultGoroot
   195	}
   196	
   197	// Version returns the Go tree's version string.
   198	// It is either the commit hash and date at the time of the build or,
   199	// when possible, a release tag like "go1.3".
   200	func Version() string {
   201		return theVersion
   202	}
   203	
   204	// GOOS is the running program's operating system target:
   205	// one of darwin, freebsd, linux, and so on.
   206	const GOOS string = theGoos
   207	
   208	// GOARCH is the running program's architecture target:
   209	// 386, amd64, or arm.
   210	const GOARCH string = theGoarch
   211	

View as plain text