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.
     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.
    11	Environment Variables
    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.
    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.
    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:
    27		allocfreetrace: setting allocfreetrace=1 causes every allocation to be
    28		profiled and a stack trace printed on each object's allocation and free.
    30		cgocheck: setting cgocheck=0 disables all checks for packages
    31		using cgo to incorrectly pass Go pointers to non-Go code.
    32		Setting cgocheck=1 (the default) enables relatively cheap
    33		checks that may miss some errors.  Setting cgocheck=2 enables
    34		expensive checks that should not miss any errors, but will
    35		cause your program to run slower.
    37		efence: setting efence=1 causes the allocator to run in a mode
    38		where each object is allocated on a unique page and addresses are
    39		never recycled.
    41		gccheckmark: setting gccheckmark=1 enables verification of the
    42		garbage collector's concurrent mark phase by performing a
    43		second mark pass while the world is stopped.  If the second
    44		pass finds a reachable object that was not found by concurrent
    45		mark, the garbage collector will panic.
    47		gcpacertrace: setting gcpacertrace=1 causes the garbage collector to
    48		print information about the internal state of the concurrent pacer.
    50		gcshrinkstackoff: setting gcshrinkstackoff=1 disables moving goroutines
    51		onto smaller stacks. In this mode, a goroutine's stack can only grow.
    53		gcstackbarrieroff: setting gcstackbarrieroff=1 disables the use of stack barriers
    54		that allow the garbage collector to avoid repeating a stack scan during the
    55		mark termination phase.
    57		gcstackbarrierall: setting gcstackbarrierall=1 installs stack barriers
    58		in every stack frame, rather than in exponentially-spaced frames.
    60		gcrescanstacks: setting gcrescanstacks=1 enables stack
    61		re-scanning during the STW mark termination phase. This is
    62		helpful for debugging if objects are being prematurely
    63		garbage collected.
    65		gcstoptheworld: setting gcstoptheworld=1 disables concurrent garbage collection,
    66		making every garbage collection a stop-the-world event. Setting gcstoptheworld=2
    67		also disables concurrent sweeping after the garbage collection finishes.
    69		gctrace: setting gctrace=1 causes the garbage collector to emit a single line to standard
    70		error at each collection, summarizing the amount of memory collected and the
    71		length of the pause. Setting gctrace=2 emits the same summary but also
    72		repeats each collection. The format of this line is subject to change.
    73		Currently, it is:
    74			gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # P
    75		where the fields are as follows:
    76			gc #        the GC number, incremented at each GC
    77			@#s         time in seconds since program start
    78			#%          percentage of time spent in GC since program start
    79			#+...+#     wall-clock/CPU times for the phases of the GC
    80			#->#-># MB  heap size at GC start, at GC end, and live heap
    81			# MB goal   goal heap size
    82			# P         number of processors used
    83		The phases are stop-the-world (STW) sweep termination, concurrent
    84		mark and scan, and STW mark termination. The CPU times
    85		for mark/scan are broken down in to assist time (GC performed in
    86		line with allocation), background GC time, and idle GC time.
    87		If the line ends with "(forced)", this GC was forced by a
    88		runtime.GC() call and all phases are STW.
    90		Setting gctrace to any value > 0 also causes the garbage collector
    91		to emit a summary when memory is released back to the system.
    92		This process of returning memory to the system is called scavenging.
    93		The format of this summary is subject to change.
    94		Currently it is:
    95			scvg#: # MB released  printed only if non-zero
    96			scvg#: inuse: # idle: # sys: # released: # consumed: # (MB)
    97		where the fields are as follows:
    98			scvg#        the scavenge cycle number, incremented at each scavenge
    99			inuse: #     MB used or partially used spans
   100			idle: #      MB spans pending scavenging
   101			sys: #       MB mapped from the system
   102			released: #  MB released to the system
   103			consumed: #  MB allocated from the system
   105		memprofilerate: setting memprofilerate=X will update the value of runtime.MemProfileRate.
   106		When set to 0 memory profiling is disabled.  Refer to the description of
   107		MemProfileRate for the default value.
   109		invalidptr: defaults to invalidptr=1, causing the garbage collector and stack
   110		copier to crash the program if an invalid pointer value (for example, 1)
   111		is found in a pointer-typed location. Setting invalidptr=0 disables this check.
   112		This should only be used as a temporary workaround to diagnose buggy code.
   113		The real fix is to not store integers in pointer-typed locations.
   115		sbrk: setting sbrk=1 replaces the memory allocator and garbage collector
   116		with a trivial allocator that obtains memory from the operating system and
   117		never reclaims any memory.
   119		scavenge: scavenge=1 enables debugging mode of heap scavenger.
   121		scheddetail: setting schedtrace=X and scheddetail=1 causes the scheduler to emit
   122		detailed multiline info every X milliseconds, describing state of the scheduler,
   123		processors, threads and goroutines.
   125		schedtrace: setting schedtrace=X causes the scheduler to emit a single line to standard
   126		error every X milliseconds, summarizing the scheduler state.
   128	The net and net/http packages also refer to debugging variables in GODEBUG.
   129	See the documentation for those packages for details.
   131	The GOMAXPROCS variable limits the number of operating system threads that
   132	can execute user-level Go code simultaneously. There is no limit to the number of threads
   133	that can be blocked in system calls on behalf of Go code; those do not count against
   134	the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes
   135	the limit.
   137	The GOTRACEBACK variable controls the amount of output generated when a Go
   138	program fails due to an unrecovered panic or an unexpected runtime condition.
   139	By default, a failure prints a stack trace for the current goroutine,
   140	eliding functions internal to the run-time system, and then exits with exit code 2.
   141	The failure prints stack traces for all goroutines if there is no current goroutine
   142	or the failure is internal to the run-time.
   143	GOTRACEBACK=none omits the goroutine stack traces entirely.
   144	GOTRACEBACK=single (the default) behaves as described above.
   145	GOTRACEBACK=all adds stack traces for all user-created goroutines.
   146	GOTRACEBACK=system is like ``all'' but adds stack frames for run-time functions
   147	and shows goroutines created internally by the run-time.
   148	GOTRACEBACK=crash is like ``system'' but crashes in an operating system-specific
   149	manner instead of exiting. For example, on Unix systems, the crash raises
   150	SIGABRT to trigger a core dump.
   151	For historical reasons, the GOTRACEBACK settings 0, 1, and 2 are synonyms for
   152	none, all, and system, respectively.
   153	The runtime/debug package's SetTraceback function allows increasing the
   154	amount of output at run time, but it cannot reduce the amount below that
   155	specified by the environment variable.
   156	See https://golang.org/pkg/runtime/debug/#SetTraceback.
   158	The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete
   159	the set of Go environment variables. They influence the building of Go programs
   160	(see https://golang.org/cmd/go and https://golang.org/pkg/go/build).
   161	GOARCH, GOOS, and GOROOT are recorded at compile time and made available by
   162	constants or functions in this package, but they do not influence the execution
   163	of the run-time system.
   164	*/
   165	package runtime
   167	import "runtime/internal/sys"
   169	// Caller reports file and line number information about function invocations on
   170	// the calling goroutine's stack. The argument skip is the number of stack frames
   171	// to ascend, with 0 identifying the caller of Caller.  (For historical reasons the
   172	// meaning of skip differs between Caller and Callers.) The return values report the
   173	// program counter, file name, and line number within the file of the corresponding
   174	// call. The boolean ok is false if it was not possible to recover the information.
   175	func Caller(skip int) (pc uintptr, file string, line int, ok bool) {
   176		// Ask for two PCs: the one we were asked for
   177		// and what it called, so that we can see if it
   178		// "called" sigpanic.
   179		var rpc [2]uintptr
   180		if callers(1+skip-1, rpc[:]) < 2 {
   181			return
   182		}
   183		f := findfunc(rpc[1])
   184		if f == nil {
   185			// TODO(rsc): Probably a bug?
   186			// The C version said "have retpc at least"
   187			// but actually returned pc=0.
   188			ok = true
   189			return
   190		}
   191		pc = rpc[1]
   192		xpc := pc
   193		g := findfunc(rpc[0])
   194		// All architectures turn faults into apparent calls to sigpanic.
   195		// If we see a call to sigpanic, we do not back up the PC to find
   196		// the line number of the call instruction, because there is no call.
   197		if xpc > f.entry && (g == nil || g.entry != funcPC(sigpanic)) {
   198			xpc--
   199		}
   200		file, line32 := funcline(f, xpc)
   201		line = int(line32)
   202		ok = true
   203		return
   204	}
   206	// Callers fills the slice pc with the return program counters of function invocations
   207	// on the calling goroutine's stack. The argument skip is the number of stack frames
   208	// to skip before recording in pc, with 0 identifying the frame for Callers itself and
   209	// 1 identifying the caller of Callers.
   210	// It returns the number of entries written to pc.
   211	//
   212	// Note that since each slice entry pc[i] is a return program counter,
   213	// looking up the file and line for pc[i] (for example, using (*Func).FileLine)
   214	// will normally return the file and line number of the instruction immediately
   215	// following the call.
   216	// To easily look up file/line information for the call sequence, use Frames.
   217	func Callers(skip int, pc []uintptr) int {
   218		// runtime.callers uses pc.array==nil as a signal
   219		// to print a stack trace. Pick off 0-length pc here
   220		// so that we don't let a nil pc slice get to it.
   221		if len(pc) == 0 {
   222			return 0
   223		}
   224		return callers(skip, pc)
   225	}
   227	// GOROOT returns the root of the Go tree.
   228	// It uses the GOROOT environment variable, if set,
   229	// or else the root used during the Go build.
   230	func GOROOT() string {
   231		s := gogetenv("GOROOT")
   232		if s != "" {
   233			return s
   234		}
   235		return sys.DefaultGoroot
   236	}
   238	// Version returns the Go tree's version string.
   239	// It is either the commit hash and date at the time of the build or,
   240	// when possible, a release tag like "go1.3".
   241	func Version() string {
   242		return sys.TheVersion
   243	}
   245	// GOOS is the running program's operating system target:
   246	// one of darwin, freebsd, linux, and so on.
   247	const GOOS string = sys.GOOS
   249	// GOARCH is the running program's architecture target:
   250	// 386, amd64, arm, or s390x.
   251	const GOARCH string = sys.GOARCH

View as plain text