Movatterモバイル変換


[0]ホーム

URL:


bisect

package
v0.37.0Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2025 License:BSD-3-ClauseImports:0Imported by:0

Details

Repository

cs.opensource.google/go/x/tools

Links

Documentation

Overview

Package bisect can be used by compilers and other programsto serve as a target for the bisect debugging tool.Seegolang.org/x/tools/cmd/bisect for details about using the tool.

To be a bisect target, allowing bisect to help determine which of a set of independentchanges provokes a failure, a program needs to:

  1. Define a way to accept a change pattern on its command line or in its environment.The most common mechanism is a command-line flag.The pattern can be passed toNew to create aMatcher, the compiled form of a pattern.

  2. Assign each change a unique ID. One possibility is to use a sequence number,but the most common mechanism is to hash some kind of identifying informationlike the file and line number where the change might be applied.Hash hashes its arguments to compute an ID.

  3. Enable each change that the pattern says should be enabled.The [Matcher.Enable] method answers this question for a given change ID.

  4. Report each change that the pattern says should be reported.The [Matcher.Report] method answers this question for a given change ID.The report consists of one more lines on standard error or standard outputthat contain a “match marker”.Marker returns the match marker for a given ID.When bisect reports a change as causing the failure, it identifies the changeby printing those report lines, with the match marker removed.

Example Usage

A program starts by defining how it receives the pattern. In this example, we will assume a flag.The next step is to compile the pattern:

m, err := bisect.New(patternFlag)if err != nil {log.Fatal(err)}

Then, each time a potential change is considered, the program computesa change ID by hashing identifying information (source file and line, in this case)and then calls m.ShouldEnable and m.ShouldReport to decide whether toenable and report the change, respectively:

for each change {h := bisect.Hash(file, line)if m.ShouldEnable(h) {enableChange()}if m.ShouldReport(h) {log.Printf("%v %s:%d", bisect.Marker(h), file, line)}}

Note that the two return different values when bisect is searching for aminimal set of changes to disable to provoke a failure.

Finally, note that New returns a nil Matcher when there is no pattern,meaning that the target is not running under bisect at all.In that common case, the computation of the hash can be avoided entirelyby checking for m == nil first:

for each change {if m == nil {enableChange()} else {h := bisect.Hash(file, line)if m.ShouldEnable(h) {enableChange()}if m.ShouldReport(h) {log.Printf("%v %s:%d", bisect.Marker(h), file, line)}}}

Pattern Syntax

Patterns are generated by the bisect tool and interpreted byNew.Users should not have to understand the patterns except whendebugging a target's bisect support or debugging the bisect tool itself.

The pattern syntax selecting a change is a sequence of bit stringsseparated by + and - operators. Each bit string denotes the set ofchanges with IDs ending in those bits, + is set addition, - is set subtraction,and the expression is evaluated in the usual left-to-right order.The special binary number “y” denotes the set of all changes,standing in for the empty bit string.In the expression, all the + operators must appear before all the - operators.A leading + adds to an empty set. A leading - subtracts from the set of allpossible suffixes.

For example:

  • “01+10” and “+01+10” both denote the set of changeswith IDs ending with the bits 01 or 10.

  • “01+10-1001” denotes the set of changes with IDsending with the bits 01 or 10, but excluding those ending in 1001.

  • “-01-1000” and “y-01-1000 both denote the set of all changeswith IDs not ending in 01 nor 1000.

  • “0+1-01+001” is not a valid pattern, because all the + operators do notappear before all the - operators.

In the syntaxes described so far, the pattern specifies the changes toenable and report. If a pattern is prefixed by a “!”, the meaningchanges: the pattern specifies the changes to DISABLE and report. Thismode of operation is needed when a program passes with all changesenabled but fails with no changes enabled. In this case, bisectsearches for minimal sets of changes to disable.Put another way, the leading “!” inverts the result fromMatcher.ShouldEnablebut does not invert the result fromMatcher.ShouldReport.

As a convenience for manual debugging, “n” is an alias for “!y”,meaning to disable and report all changes.

Finally, a leading “v” in the pattern indicates that the reports will be shownto the user of bisect to describe the changes involved in a failure.At the API level, the leading “v” causesMatcher.Verbose to return true.See the next section for details.

Match Reports

The target program must enable only those changed matchedby the pattern, and it must print a match report for each such change.A match report consists of one or more lines of text that will beprinted by the bisect tool to describe a change implicated in causinga failure. Each line in the report for a given change must contain amatch marker with that change ID, as returned byMarker.The markers are elided when displaying the lines to the user.

A match marker has the form “[bisect-match 0x1234]” where0x1234 is the change ID in hexadecimal.An alternate form is “[bisect-match 010101]”, giving the change ID in binary.

WhenMatcher.Verbose returns false, the match reports are onlybeing processed by bisect to learn the set of enabled changes,not shown to the user, meaning that each report can be a matchmarker on a line by itself, eliding the usual textual description.When the textual description is expensive to compute,checkingMatcher.Verbose can help the avoid that expensein most runs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

funcAppendMarker

func AppendMarker(dst []byte, iduint64) []byte

AppendMarker is likeMarker but appends the marker to dst.

funcCutMarker

func CutMarker(linestring) (shortstring, iduint64, okbool)

CutMarker finds the first match marker in line and removes it,returning the shortened line (with the marker removed),the ID from the match marker,and whether a marker was found at all.If there is no marker, CutMarker returns line, 0, false.

funcHash

func Hash(data ...any)uint64

Hash computes a hash of the data arguments,each of which must be of type string, byte, int, uint, int32, uint32, int64, uint64, uintptr, or a slice of one of those types.

funcMarker

func Marker(iduint64)string

Marker returns the match marker text to use on any line reporting detailsabout a match of the given ID.It always returns the hexadecimal format.

Types

typeMatcher

type Matcher struct {// contains filtered or unexported fields}

A Matcher is the parsed, compiled form of a PATTERN string.The nil *Matcher is valid: it has all changes enabled but none reported.

funcNew

func New(patternstring) (*Matcher,error)

New creates and returns a new Matcher implementing the given pattern.The pattern syntax is defined in the package doc comment.

In addition to the pattern syntax syntax, New("") returns nil, nil.The nil *Matcher is valid for use: it returns true from ShouldEnableand false from ShouldReport for all changes. Callers can avoid callingHash,Matcher.ShouldEnable, and [Matcher.ShouldPrint] entirelywhen they recognize the nil Matcher.

func (*Matcher)ShouldEnable

func (m *Matcher) ShouldEnable(iduint64)bool

ShouldEnable reports whether the change with the given id should be enabled.

func (*Matcher)ShouldReport

func (m *Matcher) ShouldReport(iduint64)bool

ShouldReport reports whether the change with the given id should be reported.

func (*Matcher)Verbose

func (m *Matcher) Verbose()bool

Verbose reports whether the reports will be shown to usersand need to include a human-readable change description.If not, the target can print just the Marker on a line by itselfand perhaps save some computation.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp