-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Clyve Gassant <[email protected]>
- Loading branch information
1 parent
ad91ae0
commit d51d797
Showing
5 changed files
with
94 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
open! Core | ||
|
||
let decode_two_digit_hexadecimal_number first_character second_character = | ||
let%bind.Option hex_digit_of_first_character = Char.get_hex_digit first_character in | ||
let%bind.Option hex_digit_of_second_character = Char.get_hex_digit second_character in | ||
let leftshift_first_character = hex_digit_of_first_character lsl 4 in | ||
let bit_or_on_the_hexadecimals = | ||
leftshift_first_character lor hex_digit_of_second_character | ||
in | ||
Char.of_int bit_or_on_the_hexadecimals | ||
;; | ||
|
||
let parser = | ||
let open Angstrom in | ||
let strip_numeric_suffix = | ||
char '_' *> skip_while Char.is_digit *> end_of_input *> return None | ||
in | ||
let double_underscores = string "__" >>| fun _ -> Some '.' in | ||
let two_digit_hexadecimal_number = | ||
let hex_character = satisfy Char.is_hex_digit in | ||
let hexcode = | ||
decode_two_digit_hexadecimal_number <$> char '$' *> hex_character <*> hex_character | ||
in | ||
hexcode | ||
>>= fun integer -> | ||
match integer with | ||
| None -> fail "invalid integer" | ||
| Some character -> return (Some character) | ||
in | ||
let normal_character = any_char >>| fun character -> Some character in | ||
let token = | ||
choice | ||
~failure_msg:"unrecognized token" | ||
[ strip_numeric_suffix | ||
; double_underscores | ||
; two_digit_hexadecimal_number | ||
; normal_character | ||
] | ||
in | ||
string "caml" *> many1 token | ||
;; | ||
|
||
let demangle mangled_symbol = | ||
let mangled_string = Angstrom.parse_string ~consume:All parser mangled_symbol in | ||
match mangled_string with | ||
| Ok list -> Some (String.of_char_list (List.filter_map list ~f:Fn.id)) | ||
| Error _ -> None | ||
;; |
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,11 @@ | ||
open! Core | ||
|
||
(** The logic for this function is derived from perf [0]. | ||
This function is used to change the symbols in the application | ||
executable from a mangled form to a demangled form in ocaml. Now | ||
when running [magic-trace run -trigger] the symbols will appear in | ||
their demangled form. Will return None if the symbol is not | ||
recognized as an OCaml symbol. | ||
[0]: https://github.com/torvalds/linux/blob/5bfc75d92efd494db37f5c4c173d3639d4772966/tools/perf/util/demangle-ocaml.c *) | ||
val demangle : string -> string option |
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
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,27 @@ | ||
open! Core | ||
open Magic_trace_core | ||
|
||
let demangle_symbol_test symbol = | ||
let demangle_symbol = Demangle_ocaml_symbols.demangle symbol in | ||
print_s [%sexp (demangle_symbol : string option)] | ||
;; | ||
|
||
let%expect_test "real mangled symbol" = | ||
demangle_symbol_test "camlAsync_unix__Unix_syscalls__to_string_57255"; | ||
[%expect {| (Async_unix.Unix_syscalls.to_string) |}] | ||
;; | ||
|
||
let%expect_test "proper hexcode" = | ||
demangle_symbol_test "caml$3f"; | ||
[%expect {| (?) |}] | ||
;; | ||
|
||
let%expect_test "improper hexcode" = | ||
demangle_symbol_test "caml$7l"; | ||
[%expect {| ($7l) |}] | ||
;; | ||
|
||
let%expect_test "when the symbol is not a demangled ocaml symbol" = | ||
demangle_symbol_test "dr__$3e$21_358"; | ||
[%expect {| () |}] | ||
;; |