-
Notifications
You must be signed in to change notification settings - Fork 398
symblib: expose API for single point lookups #380
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
057368e
rust: add C API to look up function for Go executables
florianl d8d3dc7
fixup: drop Go specific and switch to trait
florianl b8377ce
fixup: don't leak objfile::File instance
athre0z a73f6f8
fixup: use Option<> instead of Vec<> for file_name and line_number
florianl ee8050c
fixup: add comment about inlined functions
florianl 9025013
fixup: rewording
florianl 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 hidden or 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 hidden or 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,67 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use crate::{FfiResult, StatusCode, SymblibPointResolver}; | ||
| use std::ffi::{c_char, CStr}; | ||
| use std::mem; | ||
| use std::path::Path; | ||
| use symblib::symbconv::{PointResolver, ResolvedSymbol, Result as SymconvResult}; | ||
| use symblib::{gosym::GoRuntimeInfo, objfile}; | ||
|
|
||
| pub struct SymblibGoRuntime { | ||
| #[allow(unused)] | ||
| obj: Box<objfile::File>, | ||
| runtime: GoRuntimeInfo<'static>, | ||
| } | ||
|
|
||
| impl PointResolver for SymblibGoRuntime { | ||
| fn symbols_for_pc(&self, pc: symblib::VirtAddr) -> SymconvResult<Vec<ResolvedSymbol>> { | ||
| self.runtime.symbols_for_pc(pc) | ||
| } | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub unsafe extern "C" fn symblib_goruntime_new( | ||
| executable: *const c_char, | ||
| runtime: *mut *mut SymblibPointResolver, | ||
| ) -> StatusCode { | ||
| match goruntime_new_impl(executable, runtime) { | ||
| Ok(()) => StatusCode::Ok, | ||
| Err(e) => e, | ||
| } | ||
| } | ||
|
|
||
| unsafe fn goruntime_new_impl( | ||
| executable: *const c_char, | ||
| runtime: *mut *mut SymblibPointResolver, | ||
| ) -> FfiResult { | ||
| let executable = CStr::from_ptr(executable) | ||
| .to_str() | ||
| .map(Path::new) | ||
| .map_err(|_| StatusCode::BadUtf8)?; | ||
|
|
||
| let obj = Box::new(objfile::File::load(executable)?); | ||
| let obj_reader = obj.parse()?; | ||
| let go_runtime = GoRuntimeInfo::open(&obj_reader)?; | ||
|
|
||
| // Transmute away lifetime to allow for self-referential struct. | ||
| let go_runtime: GoRuntimeInfo<'static> = mem::transmute(go_runtime); | ||
|
|
||
| let resolver = SymblibGoRuntime { | ||
| obj, | ||
| runtime: go_runtime, | ||
| }; | ||
|
|
||
| let point_resolver = Box::new(SymblibPointResolver::new( | ||
| Box::new(resolver) as Box<dyn PointResolver + Send> | ||
| )); | ||
| *runtime = Box::into_raw(point_resolver); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub unsafe extern "C" fn symblib_goruntime_free(runtime: *mut SymblibPointResolver) { | ||
| if !runtime.is_null() { | ||
| drop(Box::from_raw(runtime)); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,66 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use crate::{StatusCode, SymblibSlice, SymblibString}; | ||
| use symblib::symbconv; | ||
| use symblib::symbconv::PointResolver; | ||
| use symblib::VirtAddr; | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Debug)] | ||
| pub struct SymblibResolvedSymbol { | ||
| pub start_addr: VirtAddr, | ||
| pub function_name: SymblibString, | ||
| pub file_name: SymblibString, // may be empty | ||
| pub line_number: u32, // 0 = unknown | ||
| } | ||
|
|
||
| impl From<symbconv::ResolvedSymbol> for SymblibResolvedSymbol { | ||
| fn from(sym: symbconv::ResolvedSymbol) -> Self { | ||
| Self { | ||
| start_addr: sym.start_addr, | ||
| function_name: sym.function_name.into(), | ||
| file_name: sym.file_name.unwrap_or("".to_string()).into(), | ||
| line_number: sym.line_number.unwrap_or(0), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| pub struct SymblibPointResolver { | ||
| inner: Box<dyn PointResolver + Send>, | ||
| } | ||
|
|
||
| impl SymblibPointResolver { | ||
| pub fn new(resolver: Box<dyn PointResolver + Send>) -> Self { | ||
| Self { inner: resolver } | ||
| } | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "C" fn symblib_point_resolver_symbols_for_pc( | ||
| resolver: &SymblibPointResolver, | ||
| pc: VirtAddr, | ||
| out_symbols: *mut *mut SymblibSlice<SymblibResolvedSymbol>, | ||
| ) -> StatusCode { | ||
| let symbols: Vec<_> = match resolver.inner.symbols_for_pc(pc) { | ||
| Ok(syms) => syms.into_iter().map(Into::into).collect(), | ||
| Err(e) => return StatusCode::from(e), | ||
| }; | ||
|
|
||
| unsafe { | ||
| *out_symbols = Box::into_raw(Box::new(symbols.into())); | ||
| } | ||
| StatusCode::Ok | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub extern "C" fn symblib_slice_symblibresolved_symbol_free( | ||
| slice: *mut SymblibSlice<SymblibResolvedSymbol>, | ||
| ) { | ||
| if !slice.is_null() { | ||
| unsafe { | ||
| drop(Box::from_raw(slice)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.