diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2025-11-17 11:17:23 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2025-11-20 10:10:05 +0000 |
| commit | 280ea308c321115445df610f1a75b05bbadca5f3 (patch) | |
| tree | c195c76723c4a08986d74edbfc9e15a4f07fa6c1 /tools/syz-codesearch/codesearch.go | |
| parent | 94d1e3f8b1838e8a04074464a957e979a5c5e36b (diff) | |
pkg/codesearch: add skeleton for code searching tool
Add a clang tool that is used for code indexing (tools/clang/codesearch/).
It follows conventions and build procedure of the declextract tool.
Add pkg/codesearch package that aggregates the info exposed by the clang tools,
and allows doing simple queries:
- show source code of an entity (function, struct, etc)
- show entity comment
- show all entities defined in a source file
Add tools/syz-codesearch wrapper tool that allows to create index for a kernel build,
and then run code queries on it.
Diffstat (limited to 'tools/syz-codesearch/codesearch.go')
| -rw-r--r-- | tools/syz-codesearch/codesearch.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/tools/syz-codesearch/codesearch.go b/tools/syz-codesearch/codesearch.go new file mode 100644 index 000000000..afd3840c7 --- /dev/null +++ b/tools/syz-codesearch/codesearch.go @@ -0,0 +1,66 @@ +// Copyright 2025 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/google/syzkaller/pkg/clangtool" + "github.com/google/syzkaller/pkg/codesearch" + "github.com/google/syzkaller/pkg/tool" +) + +func main() { + var ( + flagDatabase = flag.String("database", "", "path to input/output database file (mandatory)") + flagKernelSrc = flag.String("kernel-src", "", "path to kernel source directory (mandatory)") + flagKernelObj = flag.String("kernel-obj", "", "path to kernel build directory (mandatory)") + ) + flag.Parse() + if len(flag.Args()) == 0 || *flagDatabase == "" || *flagKernelSrc == "" || *flagKernelObj == "" { + printUsageAndExit() + } + cmd, args := flag.Args()[0], flag.Args()[1:] + if cmd == "index" { + if len(args) != 1 { + printUsageAndExit() + } + cfg := &clangtool.Config{ + ToolBin: args[0], + KernelSrc: *flagKernelSrc, + KernelObj: *flagKernelObj, + CacheFile: *flagDatabase, + DebugTrace: os.Stderr, + } + + if _, err := clangtool.Run[codesearch.Database](cfg); err != nil { + tool.Fail(err) + } + return + } + index, err := codesearch.NewIndex(*flagDatabase, []string{*flagKernelSrc, *flagKernelObj}) + if err != nil { + tool.Fail(err) + } + res, err := index.Command(cmd, args) + if err != nil { + tool.Fail(err) + } + os.Stdout.WriteString(res) +} + +func printUsageAndExit() { + fmt.Printf(`syz-codesearch usage: +syz-codesearch [flags] command [command arguments] +commands and their arguments: +`) + for _, cmd := range codesearch.Commands { + fmt.Printf(" - %v [%v args]\n", cmd.Name, cmd.NArgs) + } + fmt.Printf("\nflags:\n") + flag.PrintDefaults() + os.Exit(1) +} |
