github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/tools/syz-trace2syz/parser/parser.go (about)

     1  // Copyright 2018 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  //go:build !codeanalysis
     5  
     6  package parser
     7  
     8  import (
     9  	"bufio"
    10  	"bytes"
    11  	"fmt"
    12  	"strings"
    13  
    14  	"github.com/google/syzkaller/pkg/log"
    15  )
    16  
    17  func parseSyscall(scanner *bufio.Scanner) (int, *Syscall) {
    18  	lex := newStraceLexer(scanner.Bytes())
    19  	ret := StraceParse(lex)
    20  	return ret, lex.result
    21  }
    22  
    23  func shouldSkip(line string) bool {
    24  	return strings.Contains(line, "ERESTART") ||
    25  		strings.Contains(line, "+++") ||
    26  		strings.Contains(line, "---") ||
    27  		strings.Contains(line, "<ptrace(SYSCALL):No such process>")
    28  }
    29  
    30  // ParseData parses each line of a strace file in a loop.
    31  func ParseData(data []byte) (*TraceTree, error) {
    32  	tree := NewTraceTree()
    33  	// Creating the process tree
    34  	scanner := bufio.NewScanner(bytes.NewReader(data))
    35  	scanner.Buffer(nil, 64<<20)
    36  	for scanner.Scan() {
    37  		line := scanner.Text()
    38  		if shouldSkip(line) {
    39  			continue
    40  		}
    41  		log.Logf(4, "scanning call: %s", line)
    42  		ret, call := parseSyscall(scanner)
    43  		if call == nil || ret != 0 {
    44  			return nil, fmt.Errorf("failed to parse line: %v", line)
    45  		}
    46  		tree.add(call)
    47  	}
    48  	if err := scanner.Err(); err != nil {
    49  		return nil, err
    50  	}
    51  	if len(tree.TraceMap) == 0 {
    52  		return nil, nil
    53  	}
    54  	return tree, nil
    55  }