在调试过程中,如果不逐行执行代码,如何停止时间流逝?

huangapple go评论98阅读模式
英文:

How to stop the time when not stepping through lines during debugging?

问题

当调试一个使用context.WithTimeout的程序时,如果你不逐行调试,时钟会继续走动,所以在你能够调试依赖于给定上下文的代码片段之前,该上下文就已经完成了,因此你感兴趣的调试代码片段不会执行。例如,在下面的代码片段中,我必须增加timestamp的值才能够逐行调试do()retry(),否则超时会在我能够调试之前就触发:

package main

import (
	"context"
	"fmt"
	"math/rand"
	"time"
)

const success = 0.1
const timeframe = time.Microsecond * 2

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), timeframe)

	do(ctx, cancel)
}

func do(ctx context.Context, cancel context.CancelFunc) {
	defer retry(ctx, cancel)

	select {
	case <-ctx.Done(): // 在我到达这行代码之前就会触发超时
		panic("fail")
	default:
		if rand.Float64() < success {
			cancel()
			fmt.Println("success")
			return
		} else {
            fmt.Println("fail")
		}
	}
}

func retry(ctx context.Context, cancel context.CancelFunc) {
	r := recover()
	if r == nil {
		do(ctx, cancel)
	}
}

我在编程和技术方面没有太多使用英语的经验,所以请随意要求重新表达。

英文:

When debugging a program that makes use of say context.WithTimeout when you are not stepping through lines the clock keeps ticking, so before you can debug code piece that depends on given context, that context becomes done thus the code piece you are interested in debugging does not execute. For example in the following snippet I have to increase the timestamp value to be able to step through do() and retry() because otherwise timeout will be reached way before I can do it:

package main

import (
	&quot;context&quot;
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;time&quot;
)

const success = 0.1
const timeframe = time.Microsecond * 2

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), timeframe)

	do(ctx, cancel)
}

func do(ctx context.Context, cancel context.CancelFunc) {
	defer retry(ctx, cancel)

	select {
	case &lt;-ctx.Done(): // timeout will be reached before i reach this line
		panic(&quot;fail&quot;)
	default:
		if rand.Float64() &lt; success {
			cancel()
			fmt.Println(&quot;success&quot;)
			return
		} else {
            fmt.Println(&quot;fail&quot;)
	}
}

func retry(ctx context.Context, cancel context.CancelFunc) {
	r := recover()
	if r == nil {
		do(ctx, cancel)
	}
}

I haven't used English that much to talk about programming and tech so, feel free to ask to rephrase.

答案1

得分: 2

在调试过程中,如果不逐行执行代码,无法停止时间流逝。你只能通过增加超时时间来进行手动调试,或者不使用调试器。

英文:

> How to stop the time when not stepping through lines during debugging?

You simply cannot.

You either have to increase your timeouts so large that you can do your manual debugging or not use a debugger.

答案2

得分: 1

你可以使用在这个答案中解释的构建标签技巧。

基本上创建两个文件,一个用于保存正常的超时值,另一个用于在使用delve时保存较长的超时值。

// +build delve

package main
import "time"

const Timeframe = 10 * time.Hour
// +build !delve

package main
import "time"

const Timeframe = 2 * time.Microsecond

在调用delve时使用--build-flags='-tags=delve'来选择正确的文件。

英文:

You can use the build tag trick explained in this answer.

Basically create 2 files, one to hold the normal timeout value and the other to hold a longer timeout when running under delve.

// +build delve

package main
import &quot;time&quot;

const Timeframe = 10 * time.Hour
// +build !delve

package main
import &quot;time&quot;

const Timeframe = 2 * time.Microsecond

Use --build-flags=&#39;-tags=delve&#39; when calling delve to choose the correct file.

huangapple
  • 本文由 发表于 2022年3月7日 21:54:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/71382171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定