Skip to content

Commit

Permalink
Add keep-only-debug mode as default
Browse files Browse the repository at this point in the history
Signed-off-by: Kemal Akkoyun <[email protected]>
  • Loading branch information
kakkoyun committed Oct 31, 2023
1 parent 9698205 commit 5f5b908
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 176 deletions.
53 changes: 44 additions & 9 deletions cmd/parca-debuginfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ import (
"github.com/klauspost/compress/zstd"
grun "github.com/oklog/run"
"github.com/parca-dev/parca-agent/pkg/buildid"
"github.com/parca-dev/parca-agent/pkg/debuginfo"
"github.com/parca-dev/parca-agent/pkg/elfwriter"
debuginfopb "github.com/parca-dev/parca/gen/proto/go/parca/debuginfo/v1alpha1"
parcadebuginfo "github.com/parca-dev/parca/pkg/debuginfo"
"github.com/parca-dev/parca/pkg/hash"
"github.com/prometheus/client_golang/prometheus"
"github.com/rzajac/flexbuf"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -69,7 +70,9 @@ type flags struct {
Extract struct {
OutputDir string `kong:"help='Output directory path to use for extracted debug information files.',default='out'"`

Paths []string `kong:"required,arg,name='path',help='Paths to extract debug information.',type:'path'"`
Paths []string `kong:"required,arg,name='path',help='Paths to extract debug information.',type:'path'"`
CompressDWARFSections bool `kong:"default=false,help:'Compress debuginfo files DWARF sections before uploading.'"`
Mode string `kong:"default='keep-only-debug',enum='keep-only-debug,strip-debug'"`
} `cmd:"" help:"Extract debug information."`

Buildid struct {
Expand Down Expand Up @@ -99,7 +102,11 @@ type uploadInfo struct {
}

func run(kongCtx *kong.Context, flags flags) error {
extractor := debuginfo.NewExtractor(log.NewNopLogger())
opts := []elfwriter.Option{}
if flags.Extract.CompressDWARFSections {
opts = append(opts, elfwriter.WithCompressDWARFSections())
}
extractor := elfwriter.NewExtractor(log.NewNopLogger(), trace.NewNoopTracerProvider().Tracer("noop"), opts...)

var g grun.Group
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -126,7 +133,7 @@ func run(kongCtx *kong.Context, flags flags) error {
}
defer ef.Close()

buildID, err := buildid.BuildID(&buildid.ElfFile{Path: path, File: ef})
buildID, err := buildid.FromELF(ef)
if err != nil {
return fmt.Errorf("get Build ID for %q: %w", path, err)
}
Expand All @@ -145,7 +152,7 @@ func run(kongCtx *kong.Context, flags flags) error {
return errors.New("failed to find actionable files")
}

if err := extractor.ExtractAll(ctx, srcDst); err != nil {
if err := extractAll(ctx, extractor, flags.Extract.Mode, srcDst); err != nil {
return fmt.Errorf("failed to extract debug information: %w", err)
}
for _, upload := range uploads {
Expand All @@ -168,7 +175,7 @@ func run(kongCtx *kong.Context, flags flags) error {
}
defer ef.Close()

buildID, err = buildid.BuildID(&buildid.ElfFile{Path: path, File: ef})
buildID, err = buildid.FromELF(ef)
if err != nil {
return fmt.Errorf("get Build ID for %q: %w", path, err)
}
Expand Down Expand Up @@ -288,7 +295,7 @@ func run(kongCtx *kong.Context, flags flags) error {
}
defer ef.Close()

buildID, err := buildid.BuildID(&buildid.ElfFile{Path: path, File: ef})
buildID, err := buildid.FromELF(ef)
if err != nil {
return fmt.Errorf("get Build ID for %q: %w", path, err)
}
Expand All @@ -315,7 +322,7 @@ func run(kongCtx *kong.Context, flags flags) error {
return errors.New("failed to find actionable files")
}

return extractor.ExtractAll(ctx, srcDst)
return extractAll(ctx, extractor, flags.Extract.Mode, srcDst)
}, func(error) {
cancel()
})
Expand All @@ -328,7 +335,7 @@ func run(kongCtx *kong.Context, flags flags) error {
}
defer ef.Close()

buildID, err := buildid.BuildID(&buildid.ElfFile{Path: flags.Buildid.Path, File: ef})
buildID, err := buildid.FromELF(ef)
if err != nil {
return fmt.Errorf("get Build ID for %q: %w", flags.Buildid.Path, err)
}
Expand Down Expand Up @@ -445,6 +452,34 @@ func run(kongCtx *kong.Context, flags flags) error {
return g.Run()
}

// extractAll extracts debug information from the given executables.
// It consumes a map of file sources to extract and a destination io.Writer.
func extractAll(ctx context.Context, e *elfwriter.Extractor, mode string, srcDsts map[string]io.WriteSeeker) error {
var result error
for src, dst := range srcDsts {
f, err := os.Open(src)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open file: %s, %v", src, err)
result = errors.Join(result, err)
continue
}
defer f.Close()

var extractFn func(context.Context, io.WriteSeeker, io.ReaderAt) error
if mode == "strip-debug" {
extractFn = e.StripDebug
} else {
extractFn = e.OnlyKeepDebug
}

if err := extractFn(ctx, dst, f); err != nil {
fmt.Fprintf(os.Stderr, "failed to extract debug information: %s, %v", src, err)
result = errors.Join(result, err)
}
}
return result
}

func grpcConn(reg prometheus.Registerer, flags flags) (*grpc.ClientConn, error) {
met := grpc_prometheus.NewClientMetrics()
met.EnableClientHandlingTimeHistogram()
Expand Down
70 changes: 34 additions & 36 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
module github.com/parca-dev/parca-debuginfo

go 1.20
go 1.21.1

toolchain go1.21.3

require (
github.com/alecthomas/kong v0.8.0
github.com/alecthomas/kong v0.8.1
github.com/go-kit/log v0.2.1
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/klauspost/compress v1.16.7
github.com/klauspost/compress v1.17.2
github.com/oklog/run v1.1.0
github.com/parca-dev/parca v0.18.1-0.20230816074650-c9b9bed904c3
github.com/parca-dev/parca-agent v0.12.1-0.20230216133018-8dd5ccaeef0f
github.com/prometheus/client_golang v1.16.0
github.com/parca-dev/parca v0.20.0
github.com/parca-dev/parca-agent v0.26.1-0.20231030161640-40aeb6d1fa15
github.com/prometheus/client_golang v1.17.0
github.com/rzajac/flexbuf v0.14.0
google.golang.org/grpc v1.57.0
go.opentelemetry.io/otel/trace v1.19.0
google.golang.org/grpc v1.59.0
)

require (
cloud.google.com/go v0.110.6 // indirect
cloud.google.com/go v0.110.7 // indirect
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
cloud.google.com/go/storage v1.31.0 // indirect
cloud.google.com/go/storage v1.33.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
Expand Down Expand Up @@ -48,26 +51,22 @@ require (
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/goburrow/cache v0.1.4 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20230811205829-9131a7e9cc17 // indirect
github.com/google/s2a-go v0.1.5 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 // indirect
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.23.3+incompatible // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/minio-go/v7 v7.0.61 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
Expand All @@ -79,32 +78,31 @@ require (
github.com/oracle/oci-go-sdk/v65 v65.41.1 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sony/gobreaker v0.5.0 // indirect
github.com/tencentyun/cos-go-sdk-v5 v0.7.40 // indirect
github.com/thanos-io/objstore v0.0.0-20230804084840-c042a6a16c58 // indirect
github.com/thanos-io/objstore v0.0.0-20230913122821-eb06103887ab // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.42.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.45.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.137.0 // indirect
google.golang.org/api v0.141.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 // indirect
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230911183012-2d3300fd4832 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 5f5b908

Please sign in to comment.