-
Notifications
You must be signed in to change notification settings - Fork 609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add gsym support #636
Draft
sfc-gh-sgiesecke
wants to merge
5
commits into
google:main
Choose a base branch
from
sfc-gh-sgiesecke:add-gsym-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add gsym support #636
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fcdc1c1
TEMP enable workflow
sfc-gh-sgiesecke a875dc8
Fix typo in comment.
sfc-gh-sgiesecke 25f3fce
Add note on issue fixed in binutils 2.26 addr2line.
sfc-gh-sgiesecke 609c4a4
Add GSYM support.
sfc-gh-sgiesecke 51875c2
Resolve TODOs, remove debug output
sfc-gh-sgiesecke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Copyright 2021 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package binutils | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os/exec" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/google/pprof/internal/plugin" | ||
) | ||
|
||
const ( | ||
defaultLLVMGsymUtil = "llvm-gsymutil" | ||
) | ||
|
||
var prefixRegex *regexp.Regexp = regexp.MustCompile(`^(0x[[:xdigit:]]+:\s|\s+)`) | ||
|
||
// Matches output lines like: | ||
// _ZNK2sf12RefCountBaseILb0EE9removeRefEv + 3 @ /home/user/repo/x/../src/foo/Bar.hpp:67 [inlined] | ||
var frameRegex *regexp.Regexp = regexp.MustCompile(`(\S+).* @ (.*):([[:digit:]]+)`) | ||
|
||
// llvmGsymUtil is a connection to an llvm-symbolizer command for | ||
// obtaining address and line number information from a binary. | ||
type llvmGsymUtil struct { | ||
sync.Mutex | ||
filename string | ||
rw lineReaderWriter | ||
base uint64 | ||
} | ||
|
||
type llvmGsymUtilJob struct { | ||
cmd *exec.Cmd | ||
in io.WriteCloser | ||
out *bufio.Reader | ||
} | ||
|
||
func (a *llvmGsymUtilJob) write(s string) error { | ||
_, err := fmt.Fprintln(a.in, s) | ||
return err | ||
} | ||
|
||
func (a *llvmGsymUtilJob) readLine() (string, error) { | ||
s, err := a.out.ReadString('\n') | ||
if err != nil { | ||
return "", err | ||
} | ||
return strings.TrimSpace(s), nil | ||
} | ||
|
||
// close releases any resources used by the llvmGsymUtil object. | ||
func (a *llvmGsymUtilJob) close() { | ||
a.in.Close() | ||
a.cmd.Wait() | ||
} | ||
|
||
// newLLVMGsymUtil starts the given llvmGsymUtil command reporting | ||
// information about the given executable file. If file is a shared | ||
// library, base should be the address at which it was mapped in the | ||
// program under consideration. | ||
func newLLVMGsymUtil(cmd, file string, base uint64, isData bool) (*llvmGsymUtil, error) { | ||
if cmd == "" { | ||
cmd = defaultLLVMGsymUtil | ||
} | ||
|
||
j := &llvmGsymUtilJob{ | ||
cmd: exec.Command(cmd, "--addresses-from-stdin"), | ||
} | ||
|
||
var err error | ||
if j.in, err = j.cmd.StdinPipe(); err != nil { | ||
return nil, err | ||
} | ||
|
||
outPipe, err := j.cmd.StdoutPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
j.out = bufio.NewReader(outPipe) | ||
if err := j.cmd.Start(); err != nil { | ||
return nil, err | ||
} | ||
|
||
a := &llvmGsymUtil{ | ||
filename: file, | ||
rw: j, | ||
base: base, | ||
} | ||
|
||
return a, nil | ||
} | ||
|
||
// readFrame parses the llvm-symbolizer output for a single address. It | ||
// returns a populated plugin.Frame and whether it has reached the end of the | ||
// data. | ||
func (d *llvmGsymUtil) readFrame() (plugin.Frame, bool) { | ||
line, err := d.rw.readLine() | ||
if err != nil || len(line) == 0 { | ||
return plugin.Frame{}, true | ||
} | ||
|
||
// The first frame contains an address: prefix. We don't need that. The remaining frames start with spaces. | ||
suffix := prefixRegex.ReplaceAllString(line, "") | ||
|
||
if strings.HasPrefix(suffix, "error:") { | ||
// Skip empty line that follows. | ||
_, _ = d.rw.readLine() | ||
return plugin.Frame{}, true | ||
} | ||
|
||
frameMatch := frameRegex.FindStringSubmatch(suffix) | ||
if frameMatch == nil { | ||
return plugin.Frame{}, true | ||
} | ||
|
||
// TODO handle cases where no source file/line is available | ||
// TODO handle column number? | ||
|
||
funcname := frameMatch[1] | ||
sourceFile := frameMatch[2] | ||
sourceLineStr := frameMatch[3] | ||
|
||
sourceLine := 0 | ||
if line, err := strconv.Atoi(sourceLineStr); err == nil { | ||
sourceLine = line | ||
} | ||
|
||
return plugin.Frame{Func: funcname, File: sourceFile, Line: sourceLine}, false | ||
} | ||
|
||
// addrInfo returns the stack frame information for a specific program | ||
// address. It returns nil if the address could not be identified. | ||
func (d *llvmGsymUtil) addrInfo(addr uint64) ([]plugin.Frame, error) { | ||
d.Lock() | ||
defer d.Unlock() | ||
|
||
if err := d.rw.write(fmt.Sprintf("0x%x %s.gsym", addr-d.base, d.filename)); err != nil { | ||
return nil, err | ||
} | ||
|
||
var stack []plugin.Frame | ||
for { | ||
frame, end := d.readFrame() | ||
if end { | ||
break | ||
} | ||
|
||
if frame != (plugin.Frame{}) { | ||
stack = append(stack, frame) | ||
} | ||
} | ||
|
||
return stack, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be a part of this PR.