mirror of
https://github.com/jimeh/rbheap.git
synced 2026-02-19 04:46:40 +00:00
Have leak.Finder write to given io.Writer instead of STDOUT
This commit is contained in:
@@ -2,6 +2,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/jimeh/rbheap/leak"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -29,11 +30,13 @@ objects are present in both B and C, and not present in A.`,
|
||||
er(err)
|
||||
}
|
||||
|
||||
output := os.Stdout
|
||||
|
||||
switch leakOpts.Format {
|
||||
case "hex":
|
||||
finder.PrintLeakedAddresses()
|
||||
finder.WriteLeakedAddresses(output)
|
||||
case "json":
|
||||
err := finder.PrintLeakedObjects()
|
||||
err := finder.WriteLeakedObjects(output)
|
||||
if err != nil {
|
||||
er(err)
|
||||
}
|
||||
|
||||
12
leak/dump.go
12
leak/dump.go
@@ -55,20 +55,20 @@ func (s *Dump) Process() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrintEntryAddress prints the memory addresses in hex (0x...) format of the
|
||||
// WriteEntryAddresses prints the memory addresses in hex (0x...) format of the
|
||||
// entries for the list of given indexes.
|
||||
func (s *Dump) PrintEntryAddress(indexes []*string) {
|
||||
func (s *Dump) WriteEntryAddresses(w io.Writer, indexes []*string) {
|
||||
for _, index := range indexes {
|
||||
if entry, ok := s.Entries[*index]; ok {
|
||||
fmt.Println(entry.Address())
|
||||
fmt.Fprintln(w, entry.Address())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PrintEntryJSON prints the full JSON blob from the input file for the entries
|
||||
// WriteEntryJSON prints the full JSON blob from the input file for the entries
|
||||
// with the given indexes. It does this by using the Offset value of the entries
|
||||
// to avoid having to load up the whole dump file in memory.
|
||||
func (s *Dump) PrintEntryJSON(indexes []*string) error {
|
||||
func (s *Dump) WriteEntryJSON(w io.Writer, indexes []*string) error {
|
||||
file, err := os.Open(s.FilePath)
|
||||
defer file.Close()
|
||||
|
||||
@@ -97,7 +97,7 @@ func (s *Dump) PrintEntryJSON(indexes []*string) error {
|
||||
|
||||
if offset == offsets[current] {
|
||||
current++
|
||||
fmt.Print(string(line))
|
||||
fmt.Fprint(w, string(line))
|
||||
}
|
||||
|
||||
if current >= offsetsLength-1 {
|
||||
|
||||
@@ -3,6 +3,7 @@ package leak
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -48,18 +49,18 @@ func (s *Finder) Process() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrintLeakedAddresses prints the memory addresses in hex (0x...) format for
|
||||
// WriteLeakedAddresses prints the memory addresses in hex (0x...) format for
|
||||
// all objects which are likely to be leaked memory.
|
||||
func (s *Finder) PrintLeakedAddresses() {
|
||||
s.log("\nLeaked Addresses:")
|
||||
s.Dumps[1].PrintEntryAddress(s.FindLeaks())
|
||||
func (s *Finder) WriteLeakedAddresses(w io.Writer) {
|
||||
s.verbose("\nLeaked Addresses:")
|
||||
s.Dumps[1].WriteEntryAddresses(w, s.FindLeaks())
|
||||
}
|
||||
|
||||
// PrintLeakedObjects prints the full JSON blobs for all objects which are
|
||||
// WriteLeakedObjects prints the full JSON blobs for all objects which are
|
||||
// likely to be memory leaks.
|
||||
func (s *Finder) PrintLeakedObjects() error {
|
||||
s.log("\nLeaked Objects:")
|
||||
return s.Dumps[1].PrintEntryJSON(s.FindLeaks())
|
||||
func (s *Finder) WriteLeakedObjects(w io.Writer) error {
|
||||
s.verbose("\nLeaked Objects:")
|
||||
return s.Dumps[1].WriteEntryJSON(w, s.FindLeaks())
|
||||
}
|
||||
|
||||
// FindLeaks finds potential memory leaks by removing all objects in heap dump
|
||||
|
||||
Reference in New Issue
Block a user