extern.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. Package runtime contains operations that interact with Go's runtime system,
  6. such as functions to control goroutines. It also includes the low-level type information
  7. used by the reflect package; see reflect's documentation for the programmable
  8. interface to the run-time type system.
  9. Environment Variables
  10. The following environment variables ($name or %name%, depending on the host
  11. operating system) control the run-time behavior of Go programs. The meanings
  12. and use may change from release to release.
  13. The GOGC variable sets the initial garbage collection target percentage.
  14. A collection is triggered when the ratio of freshly allocated data to live data
  15. remaining after the previous collection reaches this percentage. The default
  16. is GOGC=100. Setting GOGC=off disables the garbage collector entirely.
  17. The runtime/debug package's SetGCPercent function allows changing this
  18. percentage at run time. See http://golang.org/pkg/runtime/debug/#SetGCPercent.
  19. The GODEBUG variable controls debug output from the runtime. GODEBUG value is
  20. a comma-separated list of name=val pairs. Supported names are:
  21. allocfreetrace: setting allocfreetrace=1 causes every allocation to be
  22. profiled and a stack trace printed on each object's allocation and free.
  23. efence: setting efence=1 causes the allocator to run in a mode
  24. where each object is allocated on a unique page and addresses are
  25. never recycled.
  26. gctrace: setting gctrace=1 causes the garbage collector to emit a single line to standard
  27. error at each collection, summarizing the amount of memory collected and the
  28. length of the pause. Setting gctrace=2 emits the same summary but also
  29. repeats each collection.
  30. gcdead: setting gcdead=1 causes the garbage collector to clobber all stack slots
  31. that it thinks are dead.
  32. memprofilerate: setting memprofilerate=X changes the setting for
  33. runtime.MemProfileRate. Refer to the description of this variable for how
  34. it is used and its default value.
  35. scheddetail: setting schedtrace=X and scheddetail=1 causes the scheduler to emit
  36. detailed multiline info every X milliseconds, describing state of the scheduler,
  37. processors, threads and goroutines.
  38. schedtrace: setting schedtrace=X causes the scheduler to emit a single line to standard
  39. error every X milliseconds, summarizing the scheduler state.
  40. The GOMAXPROCS variable limits the number of operating system threads that
  41. can execute user-level Go code simultaneously. There is no limit to the number of threads
  42. that can be blocked in system calls on behalf of Go code; those do not count against
  43. the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes
  44. the limit.
  45. The GOTRACEBACK variable controls the amount of output generated when a Go
  46. program fails due to an unrecovered panic or an unexpected runtime condition.
  47. By default, a failure prints a stack trace for every extant goroutine, eliding functions
  48. internal to the run-time system, and then exits with exit code 2.
  49. If GOTRACEBACK=0, the per-goroutine stack traces are omitted entirely.
  50. If GOTRACEBACK=1, the default behavior is used.
  51. If GOTRACEBACK=2, the per-goroutine stack traces include run-time functions.
  52. If GOTRACEBACK=crash, the per-goroutine stack traces include run-time functions,
  53. and if possible the program crashes in an operating-specific manner instead of
  54. exiting. For example, on Unix systems, the program raises SIGABRT to trigger a
  55. core dump.
  56. The GOARCH, GOOS, GOPATH, and GOROOT environment variables complete
  57. the set of Go environment variables. They influence the building of Go programs
  58. (see http://golang.org/cmd/go and http://golang.org/pkg/go/build).
  59. GOARCH, GOOS, and GOROOT are recorded at compile time and made available by
  60. constants or functions in this package, but they do not influence the execution
  61. of the run-time system.
  62. */
  63. package runtime
  64. // Gosched yields the processor, allowing other goroutines to run. It does not
  65. // suspend the current goroutine, so execution resumes automatically.
  66. func Gosched()
  67. // Goexit terminates the goroutine that calls it. No other goroutine is affected.
  68. // Goexit runs all deferred calls before terminating the goroutine.
  69. //
  70. // Calling Goexit from the main goroutine terminates that goroutine
  71. // without func main returning. Since func main has not returned,
  72. // the program continues execution of other goroutines.
  73. // If all other goroutines exit, the program crashes.
  74. func Goexit()
  75. // Caller reports file and line number information about function invocations on
  76. // the calling goroutine's stack. The argument skip is the number of stack frames
  77. // to ascend, with 0 identifying the caller of Caller. (For historical reasons the
  78. // meaning of skip differs between Caller and Callers.) The return values report the
  79. // program counter, file name, and line number within the file of the corresponding
  80. // call. The boolean ok is false if it was not possible to recover the information.
  81. func Caller(skip int) (pc uintptr, file string, line int, ok bool)
  82. // Callers fills the slice pc with the program counters of function invocations
  83. // on the calling goroutine's stack. The argument skip is the number of stack frames
  84. // to skip before recording in pc, with 0 identifying the frame for Callers itself and
  85. // 1 identifying the caller of Callers.
  86. // It returns the number of entries written to pc.
  87. func Callers(skip int, pc []uintptr) int
  88. type Func struct {
  89. opaque struct{} // unexported field to disallow conversions
  90. }
  91. // FuncForPC returns a *Func describing the function that contains the
  92. // given program counter address, or else nil.
  93. func FuncForPC(pc uintptr) *Func
  94. // Name returns the name of the function.
  95. func (f *Func) Name() string {
  96. return funcname_go(f)
  97. }
  98. // Entry returns the entry address of the function.
  99. func (f *Func) Entry() uintptr {
  100. return funcentry_go(f)
  101. }
  102. // FileLine returns the file name and line number of the
  103. // source code corresponding to the program counter pc.
  104. // The result will not be accurate if pc is not a program
  105. // counter within f.
  106. func (f *Func) FileLine(pc uintptr) (file string, line int) {
  107. return funcline_go(f, pc)
  108. }
  109. // implemented in symtab.c
  110. func funcline_go(*Func, uintptr) (string, int)
  111. func funcname_go(*Func) string
  112. func funcentry_go(*Func) uintptr
  113. // SetFinalizer sets the finalizer associated with x to f.
  114. // When the garbage collector finds an unreachable block
  115. // with an associated finalizer, it clears the association and runs
  116. // f(x) in a separate goroutine. This makes x reachable again, but
  117. // now without an associated finalizer. Assuming that SetFinalizer
  118. // is not called again, the next time the garbage collector sees
  119. // that x is unreachable, it will free x.
  120. //
  121. // SetFinalizer(x, nil) clears any finalizer associated with x.
  122. //
  123. // The argument x must be a pointer to an object allocated by
  124. // calling new or by taking the address of a composite literal.
  125. // The argument f must be a function that takes a single argument
  126. // to which x's type can be assigned, and can have arbitrary ignored return
  127. // values. If either of these is not true, SetFinalizer aborts the
  128. // program.
  129. //
  130. // Finalizers are run in dependency order: if A points at B, both have
  131. // finalizers, and they are otherwise unreachable, only the finalizer
  132. // for A runs; once A is freed, the finalizer for B can run.
  133. // If a cyclic structure includes a block with a finalizer, that
  134. // cycle is not guaranteed to be garbage collected and the finalizer
  135. // is not guaranteed to run, because there is no ordering that
  136. // respects the dependencies.
  137. //
  138. // The finalizer for x is scheduled to run at some arbitrary time after
  139. // x becomes unreachable.
  140. // There is no guarantee that finalizers will run before a program exits,
  141. // so typically they are useful only for releasing non-memory resources
  142. // associated with an object during a long-running program.
  143. // For example, an os.File object could use a finalizer to close the
  144. // associated operating system file descriptor when a program discards
  145. // an os.File without calling Close, but it would be a mistake
  146. // to depend on a finalizer to flush an in-memory I/O buffer such as a
  147. // bufio.Writer, because the buffer would not be flushed at program exit.
  148. //
  149. // It is not guaranteed that a finalizer will run if the size of *x is
  150. // zero bytes.
  151. //
  152. // A single goroutine runs all finalizers for a program, sequentially.
  153. // If a finalizer must run for a long time, it should do so by starting
  154. // a new goroutine.
  155. func SetFinalizer(x, f interface{})
  156. func getgoroot() string
  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 := getgoroot()
  162. if s != "" {
  163. return s
  164. }
  165. return defaultGoroot
  166. }
  167. // Version returns the Go tree's version string.
  168. // It is either the commit hash and date at the time of the build or,
  169. // when possible, a release tag like "go1.3".
  170. func Version() string {
  171. return theVersion
  172. }
  173. // GOOS is the running program's operating system target:
  174. // one of darwin, freebsd, linux, and so on.
  175. const GOOS string = theGoos
  176. // GOARCH is the running program's architecture target:
  177. // 386, amd64, arm, arm64, ppc64, ppc64le.
  178. const GOARCH string = theGoarch
  179. // GCCGOTOOLDIR is the Tool Dir for the gccgo build
  180. const GCCGOTOOLDIR string = theGccgoToolDir