當前位置: 首頁>>代碼示例>>Golang>>正文


Golang runtime.Stack函數代碼示例

本文整理匯總了Golang中runtime.Stack函數的典型用法代碼示例。如果您正苦於以下問題:Golang Stack函數的具體用法?Golang Stack怎麽用?Golang Stack使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Stack函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: onConn

func (s *Server) onConn(c net.Conn) {
	conn := s.newClientConn(c) //新建一個conn

	defer func() {
		err := recover()
		if err != nil {
			const size = 4096
			buf := make([]byte, size)
			buf = buf[:runtime.Stack(buf, false)] //獲得當前goroutine的stacktrace
			golog.Error("server", "onConn", "error", 0,
				"remoteAddr", c.RemoteAddr().String(),
				"stack", string(buf),
			)
		}

		conn.Close()
	}()

	if allowConnect := conn.IsAllowConnect(); allowConnect == false {
		err := mysql.NewError(mysql.ER_ACCESS_DENIED_ERROR, "ip address access denied by kingshard.")
		conn.writeError(err)
		conn.Close()
		return
	}
	if err := conn.Handshake(); err != nil {
		golog.Error("server", "onConn", err.Error(), 0)
		c.Close()
		return
	}

	conn.Run()
}
開發者ID:jazzsun,項目名稱:kingshard,代碼行數:32,代碼來源:server.go

示例2: valIdx

// helper function to validate index inputs
func (a *Arrayb) valIdx(index []int, mthd string) (idx int) {
	if a.HasErr() {
		return 0
	}
	if len(index) > len(a.shape) {
		a.err = InvIndexError
		if debug {
			a.debug = fmt.Sprintf("Incorrect number of indicies received by %s().  Shape: %v  Index: %v", mthd, a.shape, index)
			a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
		}
		return 0
	}
	for i, v := range index {
		if v >= a.shape[i] || v < 0 {
			a.err = IndexError
			if debug {
				a.debug = fmt.Sprintf("Index received by %s() does not exist shape: %v index: %v", mthd, a.shape, index)
				a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
			}
			return 0
		}
		idx += v * a.strides[i+1]
	}
	return
}
開發者ID:Kunde21,項目名稱:numgo,代碼行數:26,代碼來源:bool.go

示例3: _CatchPanic

func _CatchPanic(err *error, functionName string) {
	if r := recover(); r != nil {
		fmt.Printf("%s : PANIC Defered : %v\n", functionName, r)

		// Capture the stack trace
		buf := make([]byte, 10000)
		runtime.Stack(buf, false)

		fmt.Printf("%s : Stack Trace : %s", functionName, string(buf))

		if err != nil {

			*err = errors.New(fmt.Sprintf("%v", r))
		}
	} else if err != nil && *err != nil {

		fmt.Printf("%s : ERROR : %v\n", functionName, *err)

		// Capture the stack trace
		buf := make([]byte, 10000)
		runtime.Stack(buf, false)

		fmt.Printf("%s : Stack Trace : %s", functionName, string(buf))
	}
}
開發者ID:gmallard,項目名稱:go-samp,代碼行數:25,代碼來源:panrecov.go

示例4: valAxis

func (a *Array64) valAxis(axis *[]int, mthd string) bool {
	axis = cleanAxis(axis)
	switch {
	case a.HasErr():
		return true
	case len(*axis) > len(a.shape):
		a.err = ShapeError
		if debug {
			a.debug = fmt.Sprintf("Too many axes received by %s().  Shape: %v  Axes: %v", mthd, a.shape, axis)
			a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
		}
		return true
	}
	for _, v := range *axis {
		if v < 0 || v >= len(a.shape) {
			a.err = IndexError
			if debug {
				a.debug = fmt.Sprintf("Axis out of range received by %s().  Shape: %v  Axes: %v", mthd, a.shape, axis)
				a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
			}
			return true
		}
	}
	if len(*axis) == len(a.shape) {
		*axis = (*axis)[:0]
	}
	return false
}
開發者ID:Kunde21,項目名稱:numgo,代碼行數:28,代碼來源:map.go

示例5: RLock

func (m *RWMutexTracker) RLock() {
	m.logOnce.Do(m.startLogger)
	atomic.AddInt32(&m.nwaitr, 1)

	// Catch read-write-read lock. See if somebody (us? via
	// another goroutine?) already has a read lock, and then
	// somebody else is waiting to write, meaning our second read
	// will deadlock.
	if atomic.LoadInt32(&m.nhaver) > 0 && atomic.LoadInt32(&m.nwaitw) > 0 {
		buf := getBuf()
		buf = buf[:runtime.Stack(buf, false)]
		log.Printf("Potential R-W-R deadlock at: %s", buf)
		putBuf(buf)
	}

	m.mu.RLock()
	atomic.AddInt32(&m.nwaitr, -1)
	atomic.AddInt32(&m.nhaver, 1)

	gid := GoroutineID()
	m.hmu.Lock()
	defer m.hmu.Unlock()
	if m.holdr == nil {
		m.holdr = make(map[int64]bool)
	}
	if m.holdr[gid] {
		buf := getBuf()
		buf = buf[:runtime.Stack(buf, false)]
		log.Fatalf("Recursive call to RLock: %s", buf)
	}
	m.holdr[gid] = true
}
開發者ID:edrex-duex,項目名稱:go4,代碼行數:32,代碼來源:syncdebug.go

示例6: getStack

func getStack() string {
	stack := make([]byte, 1000)
	n := runtime.Stack(stack, false)
	stack = make([]byte, n)
	runtime.Stack(stack, false)
	return string(stack)
}
開發者ID:andrevalgrande,項目名稱:gonex,代碼行數:7,代碼來源:exception.go

示例7: main

func main() {
	buf1 := make([]byte, 1000)
	buf2 := make([]byte, 1000)

	runtime.Stack(buf1, false)      // CALL is last instruction on this line
	n := runtime.Stack(buf2, false) // CALL is followed by load of result from stack

	buf1 = buf1[:bytes.IndexByte(buf1, 0)]
	buf2 = buf2[:n]

	re := regexp.MustCompile(`(?m)^main\.main\(\)\n.*/issue7690.go:([0-9]+)`)
	m1 := re.FindStringSubmatch(string(buf1))
	if m1 == nil {
		println("BUG: cannot find main.main in first trace")
		return
	}
	m2 := re.FindStringSubmatch(string(buf2))
	if m2 == nil {
		println("BUG: cannot find main.main in second trace")
		return
	}

	n1, _ := strconv.Atoi(m1[1])
	n2, _ := strconv.Atoi(m2[1])
	if n1+1 != n2 {
		println("BUG: expect runtime.Stack on back to back lines, have", n1, n2)
		println(string(buf1))
		println(string(buf2))
	}
}
開發者ID:Xiahl1990,項目名稱:go,代碼行數:30,代碼來源:issue7690.go

示例8: PCall

func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
	err = nil
	sp := ls.stack.Sp()
	base := ls.reg.Top() - nargs - 1
	oldpanic := ls.Panic
	ls.Panic = panicWithoutTraceback
	defer func() {
		ls.Panic = oldpanic
		rcv := recover()
		if rcv != nil {
			if _, ok := rcv.(*ApiError); !ok {
				err = newApiErrorS(ApiErrorPanic, fmt.Sprint(rcv))
				if ls.Options.IncludeGoStackTrace {
					buf := make([]byte, 4096)
					runtime.Stack(buf, false)
					err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000") + "\n" + ls.stackTrace(true)
				}
			} else {
				err = rcv.(*ApiError)
			}
			if errfunc != nil {
				ls.Push(errfunc)
				ls.Push(err.(*ApiError).Object)
				ls.Panic = panicWithoutTraceback
				defer func() {
					ls.Panic = oldpanic
					rcv := recover()
					if rcv != nil {
						if _, ok := rcv.(*ApiError); !ok {
							err = newApiErrorS(ApiErrorPanic, fmt.Sprint(rcv))
							if ls.Options.IncludeGoStackTrace {
								buf := make([]byte, 4096)
								runtime.Stack(buf, false)
								err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000") + ls.stackTrace(true)
							}
						} else {
							err = rcv.(*ApiError)
							err.(*ApiError).StackTrace = ls.stackTrace(true)
						}
					}
				}()
				ls.Call(1, 1)
				err = newApiError(ApiErrorError, ls.Get(-1))
			} else if len(err.(*ApiError).StackTrace) == 0 {
				err.(*ApiError).StackTrace = ls.stackTrace(true)
			}
			ls.reg.SetTop(base)
		}
		ls.stack.SetSp(sp)
		if sp == 0 {
			ls.currentFrame = nil
		}
	}()

	ls.Call(nargs, nret)

	return
}
開發者ID:natural,項目名稱:gopher-lua,代碼行數:58,代碼來源:state.go

示例9: GetStackTraces

func GetStackTraces(buf *[]byte) {
	*buf = (*buf)[0:cap(*buf)]
	neededBytes := runtime.Stack(*buf, true)
	for neededBytes > len(*buf) {
		*buf = make([]byte, neededBytes)
		runtime.Stack(*buf, true)
	}
	*buf = (*buf)[0:neededBytes]
}
開發者ID:gauravrmazra,項目名稱:incubator-htrace,代碼行數:9,代碼來源:process.go

示例10: TestOpenClose

// Test that we can open and close a file
func TestOpenClose(test *testing.T) {
	fs, proc := OpenMinixImage(test)

	file, err := proc.Open("/sample/europarl-en.txt", common.O_RDONLY, 0666)
	if err != nil {
		testutils.FatalHere(test, "Failed opening file: %s", err)
	}

	found, count := checkFileAndCount(proc, file)

	if !found {
		testutils.FatalHere(test, "Did not find open file in proc.files")
	}
	if count != 1 {
		testutils.FatalHere(test, "Open file count incorrect got %d, expected %d", count, 1)
	}

	// Now close the file and make sure things are cleaned up
	err = proc.Close(file)

	found, count = checkFileAndCount(proc, file)

	if found {
		testutils.FatalHere(test, "Found file in process table, should not have")
	}
	if count != 0 {
		testutils.FatalHere(test, "Open file count mismatch got %d, expected %d", count, 0)
	}

	// How many goroutines are open right now?
	numgoros := runtime.NumGoroutine()
	stacknow := make([]byte, 4096)
	runtime.Stack(stacknow, true)

	fs.Exit(proc)
	err = fs.Shutdown()
	if err != nil {
		testutils.FatalHere(test, "Failed when shutting down filesystem: %s", err)
	}

	// We expect shutdown to have killed the following goroutines
	//  * device
	//  * block cache
	//  * inode cache
	//  * allocation table
	//  * file server

	// This test is fragile, so be careful with it!
	expected := numgoros - 5
	if runtime.NumGoroutine() != expected {
		test.Logf("Original stack:\n%s\n", stacknow)
		newstack := make([]byte, 4096)
		runtime.Stack(newstack, true)
		test.Logf("Current stack:\n%s\n", newstack)
		testutils.FatalHere(test, "Goroutine count mismatch got %d, expected %d", expected, runtime.NumGoroutine())
	}
}
開發者ID:jnwhiteh,項目名稱:minixfs,代碼行數:58,代碼來源:openclose_test.go

示例11: AuditPanic

// AuditPanic catches panicking executables. This method should be added
// in a defer statement as early as possible
// AUDIT[ Error Conditions ] 9cc4d537-8534-4970-8665-4b382abe82f3
func (log *AuditLogger) AuditPanic() {
	if err := recover(); err != nil {
		buf := make([]byte, 8192)
		log.Audit(fmt.Sprintf("Panic caused by err: %s", err))

		runtime.Stack(buf, false)
		log.Audit(fmt.Sprintf("Stack Trace (Current frame) %s", buf))

		runtime.Stack(buf, true)
		log.Warning(fmt.Sprintf("Stack Trace (All frames): %s", buf))
	}
}
開發者ID:jgillula,項目名稱:boulder,代碼行數:15,代碼來源:audit-logger.go

示例12: valRith

// valAr needs to be called before
func (a *Array64) valRith(b *Array64, mthd string) bool {
	var flag bool
	switch {
	case a.HasErr():
		return true
	case b == nil:
		a.err = NilError
		if debug {
			a.debug = "Array received by " + mthd + "() is a Nil pointer."
			a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
		}
		return true
	case b.HasErr():
		a.err = b.err
		if debug {
			a.debug = "Array received by " + mthd + "() is in error."
			a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
		}
		return true
	case len(a.shape) < len(b.shape):
		goto shape
	}

	for i, j := len(b.shape)-1, len(a.shape)-1; i >= 0; i, j = i-1, j-1 {
		if a.shape[j] != b.shape[i] {
			flag = true
			break
		}
	}
	if !flag {
		return false
	}
	if len(b.shape) != len(a.shape) || b.shape[len(b.shape)-1] != 1 {
		goto shape
	}
	for i := 0; i < len(a.shape)-1; i++ {
		if a.shape[i] != b.shape[i] {
			goto shape
		}
	}
	return false
shape:
	a.err = ShapeError
	if debug {
		a.debug = fmt.Sprintf("Array received by %s() can not be broadcast.  Shape: %v  Val shape: %v",
			mthd, a.shape, b.shape)
		a.stack = string(stackBuf[:runtime.Stack(stackBuf, false)])
	}
	return true
}
開發者ID:Kunde21,項目名稱:numgo,代碼行數:51,代碼來源:arithmetic.go

示例13: Stacktrace

// Stacktrace returns the stacktrace of the goroutine that calls it.
func Stacktrace() string {
	// Write a stack trace
	buf := make([]byte, 10000)
	n := runtime.Stack(buf, true)

	// Incrementally grow the
	// buffer as the stack trace
	// requires.
	for n > len(buf) {
		buf = make([]byte, len(buf)*2)
		n = runtime.Stack(buf, false)
	}
	return string(buf)
}
開發者ID:leonardyp,項目名稱:mandala,代碼行數:15,代碼來源:mandala.go

示例14: PCall

func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
	err = nil
	sp := ls.stack.Sp()
	base := ls.reg.Top() - nargs - 1
	oldpanic := ls.Panic
	ls.Panic = func(L *LState) {
		panic(newApiError(ApiErrorRun, "", L.Get(-1)))
	}
	defer func() {
		ls.Panic = oldpanic
		rcv := recover()
		if rcv != nil {
			if _, ok := rcv.(*ApiError); !ok {
				buf := make([]byte, 4096)
				runtime.Stack(buf, false)
				err = newApiError(ApiErrorPanic, fmt.Sprintf("%v\n%v", rcv, strings.Trim(string(buf), "\000")), LNil)
			} else {
				err = rcv.(*ApiError)
			}
			if errfunc != nil {
				ls.Push(errfunc)
				ls.Push(err.(*ApiError).Object)
				ls.Panic = func(L *LState) {
					panic(newApiError(ApiErrorError, "", L.Get(-1)))
				}
				defer func() {
					ls.Panic = oldpanic
					rcv := recover()
					if rcv != nil {
						if _, ok := rcv.(*ApiError); !ok {
							buf := make([]byte, 4096)
							runtime.Stack(buf, false)
							err = newApiError(ApiErrorPanic, fmt.Sprintf("%v\n%v", rcv, strings.Trim(string(buf), "\000")), LNil)
						} else {
							err = rcv.(*ApiError)
						}
					}
				}()
				ls.Call(1, 1)
			}
			ls.reg.SetTop(base)
		}
		ls.stack.SetSp(sp)
	}()

	ls.Call(nargs, nret)

	return
}
開發者ID:evangelistcollective,項目名稱:hustlebot,代碼行數:49,代碼來源:state.go

示例15: Panicf

func (sl SysLogger) Panicf(format string, v ...interface{}) {
	var cnt int
	sl.Logf(format, v...)
	stack := make([]byte, 8192)
	if sl.verbose {
		cnt = runtime.Stack(stack, true)
	} else {
		cnt = runtime.Stack(stack, false)
	}
	lines := strings.Split(string(stack[:cnt]), "\n")
	for _, line := range lines {
		sl.Log(strings.TrimSpace(line))
	}
	panic(v)
}
開發者ID:xrfang,項目名稱:mailing_list_daemon,代碼行數:15,代碼來源:syslog.go


注:本文中的runtime.Stack函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。