mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-05 10:04:12 +08:00
cef7af25c9
Detect symbols generated by the OCaml compiler based on their prefix. Demangle OCaml symbols, returning a newly allocated string (like the existing Java demangling functionality). Move a helper function (hex) from tests/code-reading.c to util/string.c To test: echo 'Printf.printf "%d\n" (Random.int 42)' > test.ml perf record ocamlopt.opt test.ml perf report -d ocamlopt.opt Signed-off-by: Fabian Hemmer <copy@copy.sh> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Peter Zijlstra <peterz@infradead.org> LPU-Reference: 20210203211537.b25ytjb6dq5jfbwx@nyu Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
81 lines
1.6 KiB
C
81 lines
1.6 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include "util/string2.h"
|
|
|
|
#include "demangle-ocaml.h"
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
static const char *caml_prefix = "caml";
|
|
static const size_t caml_prefix_len = 4;
|
|
|
|
/* mangled OCaml symbols start with "caml" followed by an upper-case letter */
|
|
static bool
|
|
ocaml_is_mangled(const char *sym)
|
|
{
|
|
return 0 == strncmp(sym, caml_prefix, caml_prefix_len)
|
|
&& isupper(sym[caml_prefix_len]);
|
|
}
|
|
|
|
/*
|
|
* input:
|
|
* sym: a symbol which may have been mangled by the OCaml compiler
|
|
* return:
|
|
* if the input doesn't look like a mangled OCaml symbol, NULL is returned
|
|
* otherwise, a newly allocated string containing the demangled symbol is returned
|
|
*/
|
|
char *
|
|
ocaml_demangle_sym(const char *sym)
|
|
{
|
|
char *result;
|
|
int j = 0;
|
|
int i;
|
|
int len;
|
|
|
|
if (!ocaml_is_mangled(sym)) {
|
|
return NULL;
|
|
}
|
|
|
|
len = strlen(sym);
|
|
|
|
/* the demangled symbol is always smaller than the mangled symbol */
|
|
result = malloc(len + 1);
|
|
if (!result)
|
|
return NULL;
|
|
|
|
/* skip "caml" prefix */
|
|
i = caml_prefix_len;
|
|
|
|
while (i < len) {
|
|
if (sym[i] == '_' && sym[i + 1] == '_') {
|
|
/* "__" -> "." */
|
|
result[j++] = '.';
|
|
i += 2;
|
|
}
|
|
else if (sym[i] == '$' && isxdigit(sym[i + 1]) && isxdigit(sym[i + 2])) {
|
|
/* "$xx" is a hex-encoded character */
|
|
result[j++] = (hex(sym[i + 1]) << 4) | hex(sym[i + 2]);
|
|
i += 3;
|
|
}
|
|
else {
|
|
result[j++] = sym[i++];
|
|
}
|
|
}
|
|
result[j] = '\0';
|
|
|
|
/* scan backwards to remove an "_" followed by decimal digits */
|
|
if (j != 0 && isdigit(result[j - 1])) {
|
|
while (--j) {
|
|
if (!isdigit(result[j])) {
|
|
break;
|
|
}
|
|
}
|
|
if (result[j] == '_') {
|
|
result[j] = '\0';
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|