github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/tools/syz-showprio/showprio.go (about)

     1  // Copyright 2019 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  // syz-showprio visualizes the call to call priorities from the prog package.
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  	"runtime"
    12  	"strings"
    13  
    14  	"github.com/google/syzkaller/pkg/db"
    15  	"github.com/google/syzkaller/pkg/mgrconfig"
    16  	"github.com/google/syzkaller/prog"
    17  )
    18  
    19  var (
    20  	flagOS     = flag.String("os", runtime.GOOS, "target os")
    21  	flagArch   = flag.String("arch", runtime.GOARCH, "target arch")
    22  	flagEnable = flag.String("enable", "", "comma-separated list of enabled syscalls")
    23  	flagCorpus = flag.String("corpus", "", "name of the corpus file")
    24  )
    25  
    26  func main() {
    27  	flag.Parse()
    28  	target, err := prog.GetTarget(*flagOS, *flagArch)
    29  	if err != nil {
    30  		fmt.Fprintf(os.Stderr, "%v\n", err)
    31  		os.Exit(1)
    32  	}
    33  	if *flagEnable == "" {
    34  		fmt.Fprintf(os.Stderr, "no syscalls enabled\n")
    35  		os.Exit(1)
    36  	}
    37  	enabled := strings.Split(*flagEnable, ",")
    38  	_, err = mgrconfig.ParseEnabledSyscalls(target, enabled, nil, mgrconfig.AnyDescriptions)
    39  	if err != nil {
    40  		fmt.Fprintf(os.Stderr, "failed to parse enabled syscalls: %v\n", err)
    41  		os.Exit(1)
    42  	}
    43  	corpus, err := db.ReadCorpus(*flagCorpus, target)
    44  	if err != nil {
    45  		fmt.Fprintf(os.Stderr, "failed to read corpus: %v\n", err)
    46  		os.Exit(1)
    47  	}
    48  	matrix, _ := target.CalculatePriorities(corpus, nil)
    49  	showPriorities(enabled, matrix, target)
    50  }
    51  
    52  func showPriorities(calls []string, prios [][]int32, target *prog.Target) {
    53  	printLine(append([]string{"CALLS"}, calls...))
    54  	for _, callRow := range calls {
    55  		line := []string{callRow}
    56  		for _, callCol := range calls {
    57  			val := prios[target.SyscallMap[callRow].ID][target.SyscallMap[callCol].ID]
    58  			line = append(line, fmt.Sprintf("%v", val))
    59  		}
    60  		printLine(line)
    61  	}
    62  }
    63  
    64  func printLine(values []string) {
    65  	fmt.Printf("|")
    66  	for _, val := range values {
    67  		fmt.Printf("%-20v|", val)
    68  	}
    69  	fmt.Printf("\n")
    70  }