Skip to content

Commit

Permalink
feat(memparse): add search functionnality to memparse
Browse files Browse the repository at this point in the history
This commit adds `--search` and `--context` flags to `memparse`, enabling
pattern search in a process's memory pages.
  • Loading branch information
behouba committed Jul 21, 2024
1 parent b1f1c3b commit 3c8779b
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
58 changes: 58 additions & 0 deletions cmd/memparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"os"
"path/filepath"
"strings"

"github.com/checkpoint-restore/checkpointctl/internal"
metadata "github.com/checkpoint-restore/checkpointctl/lib"
Expand Down Expand Up @@ -49,6 +50,22 @@ func MemParse() *cobra.Command {
"Specify the output file to be written to",
)

flags.StringVarP(
searchPattern,
"search",
"s",
"",
"Search for a pattern in memory pages (accepts plain string or regex)",
)

flags.IntVarP(
searchContext,
"context",
"c",
0,
"Specify the number of bytes surrounding each match",
)

return cmd
}

Expand Down Expand Up @@ -79,6 +96,10 @@ func memparse(cmd *cobra.Command, args []string) error {
}
defer internal.CleanupTasks(tasks)

if *searchPattern != "" {
return printMemorySearchResult(tasks[0])
}

if *pID != 0 {
return printProcessMemoryPages(tasks[0])
}
Expand Down Expand Up @@ -273,3 +294,40 @@ func generateHexAndAscii(data []byte) (string, string) {

return hex, ascii
}

func printMemorySearchResult(task internal.Task) error {
memReader, err := crit.NewMemoryReader(
filepath.Join(task.OutputDir, metadata.CheckpointDirectory),
*pID, pageSize,
)
if err != nil {
return err
}

if err := internal.UntarFiles(
task.CheckpointFilePath, task.OutputDir,
[]string{filepath.Join(metadata.CheckpointDirectory, fmt.Sprintf("pages-%d.img", memReader.GetPagesID()))},
); err != nil {
return err
}

matches, err := memReader.SearchPattern(*searchPattern, *searchContext)
if err != nil {
return err
}

if len(matches) == 0 {
fmt.Printf("\nNothing found for pattern %s in memory pages of process %d", *searchPattern, *pID)
return nil
}

patternLength := len(*searchPattern) + (*searchContext * 2)

fmt.Printf("\n%-16s %-*s %-5s\n", "Address", patternLength, "Pattern", "Instance")
fmt.Println(strings.Repeat("-", 19+patternLength+9))

for i, match := range matches {
fmt.Printf("%016x %-*s [%d]\n", match.Vaddr, patternLength, match.Pattern, i+1)
}
return nil
}
2 changes: 2 additions & 0 deletions cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ var (
files *bool = &internal.Files
sockets *bool = &internal.Sockets
showAll *bool = &internal.ShowAll
searchPattern *string = &internal.SearchPattern
searchContext *int = &internal.SearchContext
)
2 changes: 2 additions & 0 deletions internal/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ var (
Files bool
Sockets bool
ShowAll bool
SearchPattern string
SearchContext int
)
91 changes: 91 additions & 0 deletions vendor/github.com/checkpoint-restore/go-criu/v7/crit/mempages.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3c8779b

Please sign in to comment.