-
Notifications
You must be signed in to change notification settings - Fork 618
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
Set base address on OSX #313
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,14 +177,20 @@ func TestSetFastSymbolization(t *testing.T) { | |
|
||
func skipUnlessLinuxAmd64(t *testing.T) { | ||
if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" { | ||
t.Skip("Disasm only tested on x86-64 linux") | ||
t.Skip("This test only works on x86-64 Linux") | ||
} | ||
} | ||
|
||
func skipUnlessDarwinAmd64(t *testing.T) { | ||
if runtime.GOOS != "darwin" || runtime.GOARCH != "amd64" { | ||
t.Skip("This test only works on x86-64 Mac") | ||
} | ||
} | ||
|
||
func TestDisasm(t *testing.T) { | ||
skipUnlessLinuxAmd64(t) | ||
bu := &Binutils{} | ||
insts, err := bu.Disasm(filepath.Join("testdata", "hello"), 0, math.MaxUint64) | ||
insts, err := bu.Disasm(filepath.Join("testdata", "exe_linux_64"), 0, math.MaxUint64) | ||
if err != nil { | ||
t.Fatalf("Disasm: unexpected error %v", err) | ||
} | ||
|
@@ -215,7 +221,7 @@ func TestObjFile(t *testing.T) { | |
} { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
bu := &Binutils{} | ||
f, err := bu.Open(filepath.Join("testdata", "hello"), tc.start, tc.limit, tc.offset) | ||
f, err := bu.Open(filepath.Join("testdata", "exe_linux_64"), tc.start, tc.limit, tc.offset) | ||
if err != nil { | ||
t.Fatalf("Open: unexpected error %v", err) | ||
} | ||
|
@@ -255,6 +261,73 @@ func TestObjFile(t *testing.T) { | |
} | ||
} | ||
|
||
func TestMachoFiles(t *testing.T) { | ||
skipUnlessDarwinAmd64(t) | ||
|
||
// Load `file`, pretending it was mapped at `start`. Then get the symbol | ||
// table. Check that it contains the symbol `sym` and that the address | ||
// `addr` gives the `expected` stack trace. | ||
for _, tc := range []struct { | ||
desc string | ||
file string | ||
start, limit, offset uint64 | ||
addr uint64 | ||
sym string | ||
expected []plugin.Frame | ||
}{ | ||
{"normal mapping", "exe_mac_64", 0x100000000, math.MaxUint64, 0, | ||
0x100000f50, "_main", | ||
[]plugin.Frame{ | ||
{Func: "main", File: "/tmp/hello.c", Line: 3}, | ||
}}, | ||
{"other mapping", "exe_mac_64", 0x200000000, math.MaxUint64, 0, | ||
0x200000f50, "_main", | ||
[]plugin.Frame{ | ||
{Func: "main", File: "/tmp/hello.c", Line: 3}, | ||
}}, | ||
{"lib normal mapping", "lib_mac_64", 0, math.MaxUint64, 0, | ||
0xfa0, "_bar", | ||
[]plugin.Frame{ | ||
{Func: "bar", File: "/tmp/lib.c", Line: 6}, | ||
}}, | ||
} { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
bu := &Binutils{} | ||
f, err := bu.Open(filepath.Join("testdata", tc.file), tc.start, tc.limit, tc.offset) | ||
if err != nil { | ||
t.Fatalf("Open: unexpected error %v", err) | ||
} | ||
defer f.Close() | ||
syms, err := f.Symbols(nil, 0) | ||
if err != nil { | ||
t.Fatalf("Symbols: unexpected error %v", err) | ||
} | ||
|
||
find := func(name string) *plugin.Sym { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: could move this piece out to an outer function to reuse with the other test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
for _, s := range syms { | ||
for _, n := range s.Name { | ||
if n == name { | ||
return s | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
m := find(tc.sym) | ||
if m == nil { | ||
t.Fatalf("Symbols: could not find symbol %v", tc.sym) | ||
} | ||
gotFrames, err := f.SourceLine(tc.addr) | ||
if err != nil { | ||
t.Fatalf("SourceLine: unexpected error %v", err) | ||
} | ||
if !reflect.DeepEqual(gotFrames, tc.expected) { | ||
t.Fatalf("SourceLine for main: got %v; want %v\n", gotFrames, tc.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLLVMSymbolizer(t *testing.T) { | ||
if runtime.GOOS != "linux" { | ||
t.Skip("testtdata/llvm-symbolizer has only been tested on linux") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ func findSymbols(syms []byte, file string, r *regexp.Regexp, address uint64) ([] | |
var symbols []*plugin.Sym | ||
names, start := []string{}, uint64(0) | ||
buf := bytes.NewBuffer(syms) | ||
symAddr := uint64(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this symAddr will get shadowed by the assignment in the loop header, so it's probably not what the code after the loop expects. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well spotted! |
||
for symAddr, name, err := nextSymbol(buf); err == nil; symAddr, name, err = nextSymbol(buf) { | ||
if err != nil { | ||
return nil, err | ||
|
@@ -51,6 +52,12 @@ func findSymbols(syms []byte, file string, r *regexp.Regexp, address uint64) ([] | |
names, start = []string{name}, symAddr | ||
} | ||
|
||
if len(names) != 0 { | ||
if match := matchSymbol(names, start, symAddr-1, r, address); match != nil { | ||
symbols = append(symbols, &plugin.Sym{Name: match, File: file, Start: start, End: symAddr - 1}) | ||
} | ||
} | ||
|
||
return symbols, nil | ||
} | ||
|
||
|
@@ -62,7 +69,7 @@ func matchSymbol(names []string, start, end uint64, r *regexp.Regexp, address ui | |
return names | ||
} | ||
for _, name := range names { | ||
if r.MatchString(name) { | ||
if r == nil || r.MatchString(name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we pass r as nil from anywhere? Ah, you do know in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No but the comments for the |
||
return []string{name} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>English</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>com.apple.xcode.dsym.exe_mac_64</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundlePackageType</key> | ||
<string>dSYM</string> | ||
<key>CFBundleSignature</key> | ||
<string>????</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>1</string> | ||
</dict> | ||
</plist> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reading https://stackoverflow.com/questions/8058497/can-clang-build-executables-with-dsym-debug-information, it appears to me that building the test binaries from the command line it should be possible to have the debug information as part of those, and these additional dSYM files and directory would not be needed. I can look into doing that myself, can you include the source code of the executable and library test binaries that you used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep you can build it from source too. I just copied how the Linux test worked. The only weird thing is if you have linking as a separate step (i.e. |
||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>English</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>com.apple.xcode.dsym.lib_mac_64</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundlePackageType</key> | ||
<string>dSYM</string> | ||
<key>CFBundleSignature</key> | ||
<string>????</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>1</string> | ||
</dict> | ||
</plist> |
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.
I am a bit hesitant whether a better value for the base in the absence of __TEXT is zero or
start
. But I guess it's not important since files without __TEXT should be impractical case.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.
Yeah I'll add a warning if it isn't found.