github.com/lzhfromustc/gofuzz@v0.0.0-20211116160056-151b3108bbd1/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 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  	clobberfree: setting clobberfree=1 causes the garbage collector to
    31  	clobber the memory content of an object with bad content when it frees
    32  	the object.
    33  
    34  	cgocheck: setting cgocheck=0 disables all checks for packages
    35  	using cgo to incorrectly pass Go pointers to non-Go code.
    36  	Setting cgocheck=1 (the default) enables relatively cheap
    37  	checks that may miss some errors.  Setting cgocheck=2 enables
    38  	expensive checks that should not miss any errors, but will
    39  	cause your program to run slower.
    40  
    41  	efence: setting efence=1 causes the allocator to run in a mode
    42  	where each object is allocated on a unique page and addresses are
    43  	never recycled.
    44  
    45  	gccheckmark: setting gccheckmark=1 enables verification of the
    46  	garbage collector's concurrent mark phase by performing a
    47  	second mark pass while the world is stopped.  If the second
    48  	pass finds a reachable object that was not found by concurrent
    49  	mark, the garbage collector will panic.
    50  
    51  	gcpacertrace: setting gcpacertrace=1 causes the garbage collector to
    52  	print information about the internal state of the concurrent pacer.
    53  
    54  	gcshrinkstackoff: setting gcshrinkstackoff=1 disables moving goroutines
    55  	onto smaller stacks. In this mode, a goroutine's stack can only grow.
    56  
    57  	gcstoptheworld: setting gcstoptheworld=1 disables concurrent garbage collection,
    58  	making every garbage collection a stop-the-world event. Setting gcstoptheworld=2
    59  	also disables concurrent sweeping after the garbage collection finishes.
    60  
    61  	gctrace: setting gctrace=1 causes the garbage collector to emit a single line to standard
    62  	error at each collection, summarizing the amount of memory collected and the
    63  	length of the pause. The format of this line is subject to change.
    64  	Currently, it is:
    65  		gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # P
    66  	where the fields are as follows:
    67  		gc #        the GC number, incremented at each GC
    68  		@#s         time in seconds since program start
    69  		#%          percentage of time spent in GC since program start
    70  		#+...+#     wall-clock/CPU times for the phases of the GC
    71  		#->#-># MB  heap size at GC start, at GC end, and live heap
    72  		# MB goal   goal heap size
    73  		# P         number of processors used
    74  	The phases are stop-the-world (STW) sweep termination, concurrent
    75  	mark and scan, and STW mark termination. The CPU times
    76  	for mark/scan are broken down in to assist time (GC performed in
    77  	line with allocation), background GC time, and idle GC time.
    78  	If the line ends with "(forced)", this GC was forced by a
    79  	runtime.GC() call.
    80  
    81  	inittrace: setting inittrace=1 causes the runtime to emit a single line to standard
    82  	error for each package with init work, summarizing the execution time and memory
    83  	allocation. No information is printed for inits executed as part of plugin loading
    84  	and for packages without both user defined and compiler generated init work.
    85  	The format of this line is subject to change. Currently, it is:
    86  		init # @#ms, # ms clock, # bytes, # allocs
    87  	where the fields are as follows:
    88  		init #      the package name
    89  		@# ms       time in milliseconds when the init started since program start
    90  		# clock     wall-clock time for package initialization work
    91  		# bytes     memory allocated on the heap
    92  		# allocs    number of heap allocations
    93  
    94  	madvdontneed: setting madvdontneed=0 will use MADV_FREE
    95  	instead of MADV_DONTNEED on Linux when returning memory to the
    96  	kernel. This is more efficient, but means RSS numbers will
    97  	drop only when the OS is under memory pressure.
    98  
    99  	memprofilerate: setting memprofilerate=X will update the value of runtime.MemProfileRate.
   100  	When set to 0 memory profiling is disabled.  Refer to the description of
   101  	MemProfileRate for the default value.
   102  
   103  	invalidptr: invalidptr=1 (the default) causes the garbage collector and stack
   104  	copier to crash the program if an invalid pointer value (for example, 1)
   105  	is found in a pointer-typed location. Setting invalidptr=0 disables this check.
   106  	This should only be used as a temporary workaround to diagnose buggy code.
   107  	The real fix is to not store integers in pointer-typed locations.
   108  
   109  	sbrk: setting sbrk=1 replaces the memory allocator and garbage collector
   110  	with a trivial allocator that obtains memory from the operating system and
   111  	never reclaims any memory.
   112  
   113  	scavenge: scavenge=1 enables debugging mode of heap scavenger.
   114  
   115  	scavtrace: setting scavtrace=1 causes the runtime to emit a single line to standard
   116  	error, roughly once per GC cycle, summarizing the amount of work done by the
   117  	scavenger as well as the total amount of memory returned to the operating system
   118  	and an estimate of physical memory utilization. The format of this line is subject
   119  	to change, but currently it is:
   120  		scav # # KiB work, # KiB total, #% util
   121  	where the fields are as follows:
   122  		scav #       the scavenge cycle number
   123  		# KiB work   the amount of memory returned to the OS since the last line
   124  		# KiB total  the total amount of memory returned to the OS
   125  		#% util      the fraction of all unscavenged memory which is in-use
   126  	If the line ends with "(forced)", then scavenging was forced by a
   127  	debug.FreeOSMemory() call.
   128  
   129  	scheddetail: setting schedtrace=X and scheddetail=1 causes the scheduler to emit
   130  	detailed multiline info every X milliseconds, describing state of the scheduler,
   131  	processors, threads and goroutines.
   132  
   133  	schedtrace: setting schedtrace=X causes the scheduler to emit a single line to standard
   134  	error every X milliseconds, summarizing the scheduler state.
   135  
   136  	tracebackancestors: setting tracebackancestors=N extends tracebacks with the stacks at
   137  	which goroutines were created, where N limits the number of ancestor goroutines to
   138  	report. This also extends the information returned by runtime.Stack. Ancestor's goroutine
   139  	IDs will refer to the ID of the goroutine at the time of creation; it's possible for this
   140  	ID to be reused for another goroutine. Setting N to 0 will report no ancestry information.
   141  
   142  	asyncpreemptoff: asyncpreemptoff=1 disables signal-based
   143  	asynchronous goroutine preemption. This makes some loops
   144  	non-preemptible for long periods, which may delay GC and
   145  	goroutine scheduling. This is useful for debugging GC issues
   146  	because it also disables the conservative stack scanning used
   147  	for asynchronously preempted goroutines.
   148  
   149  The net, net/http, and crypto/tls packages also refer to debugging variables in GODEBUG.
   150  See the documentation for those packages for details.
   151  
   152  The GOMAXPROCS variable limits the number of operating system threads that
   153  can execute user-level Go code simultaneously. There is no limit to the number of threads
   154  that can be blocked in system calls on behalf of Go code; those do not count against
   155  the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes
   156  the limit.
   157  
   158  The GORACE variable configures the race detector, for programs built using -race.
   159  See https://golang.org/doc/articles/race_detector.html for details.
   160  
   161  The GOTRACEBACK variable controls the amount of output generated when a Go
   162  program fails due to an unrecovered panic or an unexpected runtime condition.
   163  By default, a failure prints a stack trace for the current goroutine,
   164  eliding functions internal to the run-time system, and then exits with exit code 2.
   165  The failure prints stack traces for all goroutines if there is no current goroutine
   166  or the failure is internal to the run-time.
   167  GOTRACEBACK=none omits the goroutine stack traces entirely.
   168  GOTRACEBACK=single (the default) behaves as described above.
   169  GOTRACEBACK=all adds stack traces for all user-created goroutines.
   170  GOTRACEBACK=system is like ``all'' but adds stack frames for run-time functions
   171  and shows goroutines created internally by the run-time.
   172  GOTRACEBACK=crash is like ``system'' but crashes in an operating system-specific
   173  manner instead of exiting. For example, on Unix systems, the crash raises
   174  SIGABRT to trigger a core dump.
   175  For historical reasons, the GOTRACEBACK settings 0, 1, and 2 are synonyms for
   176  none, all, and system, respectively.
   177  The runtime/debug package's SetTraceback function allows increasing the
   178  amount of output at run time, but it cannot reduce the amount below that
   179  specified by the environment variable.
   180  See https://golang.org/pkg/runtime/debug/#SetTraceback.
   181  
   182  The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete
   183  the set of Go environment variables. They influence the building of Go programs
   184  (see https://golang.org/cmd/go and https://golang.org/pkg/go/build).
   185  GOARCH, GOOS, and GOROOT are recorded at compile time and made available by
   186  constants or functions in this package, but they do not influence the execution
   187  of the run-time system.
   188  */
   189  package runtime
   190  
   191  import "runtime/internal/sys"
   192  
   193  // Caller reports file and line number information about function invocations on
   194  // the calling goroutine's stack. The argument skip is the number of stack frames
   195  // to ascend, with 0 identifying the caller of Caller.  (For historical reasons the
   196  // meaning of skip differs between Caller and Callers.) The return values report the
   197  // program counter, file name, and line number within the file of the corresponding
   198  // call. The boolean ok is false if it was not possible to recover the information.
   199  func Caller(skip int) (pc uintptr, file string, line int, ok bool) {
   200  	rpc := make([]uintptr, 1)
   201  	n := callers(skip+1, rpc[:])
   202  	if n < 1 {
   203  		return
   204  	}
   205  	frame, _ := CallersFrames(rpc).Next()
   206  	return frame.PC, frame.File, frame.Line, frame.PC != 0
   207  }
   208  
   209  // Callers fills the slice pc with the return program counters of function invocations
   210  // on the calling goroutine's stack. The argument skip is the number of stack frames
   211  // to skip before recording in pc, with 0 identifying the frame for Callers itself and
   212  // 1 identifying the caller of Callers.
   213  // It returns the number of entries written to pc.
   214  //
   215  // To translate these PCs into symbolic information such as function
   216  // names and line numbers, use CallersFrames. CallersFrames accounts
   217  // for inlined functions and adjusts the return program counters into
   218  // call program counters. Iterating over the returned slice of PCs
   219  // directly is discouraged, as is using FuncForPC on any of the
   220  // returned PCs, since these cannot account for inlining or return
   221  // program counter adjustment.
   222  func Callers(skip int, pc []uintptr) int {
   223  	// runtime.callers uses pc.array==nil as a signal
   224  	// to print a stack trace. Pick off 0-length pc here
   225  	// so that we don't let a nil pc slice get to it.
   226  	if len(pc) == 0 {
   227  		return 0
   228  	}
   229  	return callers(skip, pc)
   230  }
   231  
   232  // GOROOT returns the root of the Go tree. It uses the
   233  // GOROOT environment variable, if set at process start,
   234  // or else the root used during the Go build.
   235  func GOROOT() string {
   236  	s := gogetenv("GOROOT")
   237  	if s != "" {
   238  		return s
   239  	}
   240  	return sys.DefaultGoroot
   241  }
   242  
   243  // Version returns the Go tree's version string.
   244  // It is either the commit hash and date at the time of the build or,
   245  // when possible, a release tag like "go1.3".
   246  func Version() string {
   247  	return sys.TheVersion
   248  }
   249  
   250  // GOOS is the running program's operating system target:
   251  // one of darwin, freebsd, linux, and so on.
   252  // To view possible combinations of GOOS and GOARCH, run "go tool dist list".
   253  const GOOS string = sys.GOOS
   254  
   255  // GOARCH is the running program's architecture target:
   256  // one of 386, amd64, arm, s390x, and so on.
   257  const GOARCH string = sys.GOARCH