Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[pull] master from async-profiler:master #19

Merged
merged 3 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .assets/images/flamegraph_colors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 35 additions & 36 deletions docs/ProfilerOptions.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
1. `/proc/sys/kernel/perf_event_paranoid` is set to restricted mode (>=2).
2. seccomp disables `perf_event_open` API in a container.
3. OS runs under a hypervisor that does not virtualize performance counters.
4. perf_event_open API is not supported on this system, e.g. WSL.</br>
4. perf_event_open API is not supported on this system, e.g. WSL.

</br>For permissions-related reasons (such as 1 and 2), using `--fdtransfer` while running the profiler
<br>For permissions-related reasons (such as 1 and 2), using `--fdtransfer` while running the profiler
as a privileged user may solve the issue.

If changing the configuration is not possible, you may fall back to
Expand Down
113 changes: 40 additions & 73 deletions src/demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stdlib.h>
#include <string.h>
#include "demangle.h"
#include "rustDemangle.h"


char* Demangle::demangleCpp(const char* s) {
Expand All @@ -25,80 +26,48 @@ char* Demangle::demangleCpp(const char* s) {
return result;
}

char* Demangle::demangleRust(const char* s, const char* e) {
// Demangled symbol can be 1.5x longer than original, e.g. 1A1B1C -> A::B::C
char* result = (char*)malloc((e - s) * 3 / 2 + 1);
if (result == NULL) {
return NULL;
bool Demangle::isRustSymbol(const char* s) {
// "_R" symbols (Rust "mangling V0") symbols can always be easily distinguished from C++ symbols.
if (s[0] == '_' && s[1] == 'R') {
return true;
}

char* r = result;
char* tmp;

while (s < e) {
unsigned long len = strtoul(s, &tmp, 10);
const char* next = tmp + len;
if (len == 0 || next > e) {
break;
// Rust symbols with the legacy demangling (`_ZN3foo3bar17h0123456789abcdefE`) look very much like valid
// C++ demangling symbols, but we only want to use the Rust demangling for Rust symbols since
// the Rust demangling does not support C++ anonymous namespaces (e.g. `_ZN12_GLOBAL__N_113single_threadE`
// is supposed to demangle to `(anonymous namespace)::single_thread`, but Rust will demangle it to
// `_GLOBAL__N_1::single_thread`).
//
// So try to have a heuristic to avoid sending C++ symbols to Rust demangling - if a symbol's last "E"
// refers to a Rust hash, expect it to be a Rust symbol. We don't require the `E` to be at the end
// of the string since there can be `.lto.1` suffixes.
//
// FIXME: there might be a better heuristic if there are problems with this one
const char* e = strrchr(s, 'E');
if (e != NULL && e - s > 22 && e[-19] == '1' && e[-18] == '7' && e[-17] == 'h') {
const char* h = e - 16;
while ((*h >= '0' && *h <= '9') || (*h >= 'a' && *h <= 'f')) h++;
if (h == e) {
return true;
}
}

s = tmp;
if (s[0] == '_' && s[1] == '$') s++;

if (r > result) {
*r++ = ':';
*r++ = ':';
}
return false;
}

while (s < next) {
if (s[0] == '$') {
if (s[1] == 'L' && s[2] == 'T' && s[3] == '$') {
*r++ = '<';
s += 4;
} else if (s[1] == 'G' && s[2] == 'T' && s[3] == '$') {
*r++ = '>';
s += 4;
} else if (s[1] == 'L' && s[2] == 'P' && s[3] == '$') {
*r++ = '(';
s += 4;
} else if (s[1] == 'R' && s[2] == 'P' && s[3] == '$') {
*r++ = ')';
s += 4;
} else if (s[1] == 'S' && s[2] == 'P' && s[3] == '$') {
*r++ = '@';
s += 4;
} else if (s[1] == 'B' && s[2] == 'P' && s[3] == '$') {
*r++ = '*';
s += 4;
} else if (s[1] == 'R' && s[2] == 'F' && s[3] == '$') {
*r++ = '&';
s += 4;
} else if (s[1] == 'C' && s[2] == '$') {
*r++ = ',';
s += 3;
} else if (s[1] == 'u') {
*r++ = (char)strtoul(s + 2, &tmp, 16);
s = tmp + 1;
} else {
*r++ = '$';
s++;
}
} else if (s[0] == '.' && s[1] == '.') {
*r++ = ':';
*r++ = ':';
s += 2;
} else {
*r++ = *s++;
}
char* Demangle::demangleRust(struct demangle const *demangle, bool full_signature) {
for (size_t demangled_size = 64; demangled_size < 1000000; demangled_size *= 2) {
char* result = (char*)malloc(demangled_size);
if (result == NULL) {
return NULL;
}

if (s > next) {
break;
if (rust_demangle_display_demangle(demangle, result, demangled_size, !full_signature /* alternate */) == OverflowOk) {
return result;
}
free(result);
}

*r = 0;
return result;
// demangling Rust failed, return NULL
return NULL;
}

void Demangle::cutArguments(char* s) {
Expand All @@ -117,13 +86,11 @@ void Demangle::cutArguments(char* s) {
}

char* Demangle::demangle(const char* s, bool full_signature) {
// Check if the mangled symbol ends with a Rust hash "17h<hex>E"
const char* e = strrchr(s, 'E');
if (e != NULL && e - s > 22 && e[-19] == '1' && e[-18] == '7' && e[-17] == 'h') {
const char* h = e - 16;
while ((*h >= '0' && *h <= '9') || (*h >= 'a' && *h <= 'f')) h++;
if (h == e) {
return demangleRust(s + 3, e - 19);
if (isRustSymbol(s)) {
struct demangle demangle;
rust_demangle_demangle(s, &demangle);
if (rust_demangle_is_known(&demangle)) {
return demangleRust(&demangle, full_signature);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
#ifndef _DEMANGLE_H
#define _DEMANGLE_H

struct demangle;

class Demangle {
private:
static char* demangleCpp(const char* s);
static char* demangleRust(const char* s, const char* e);
static char* demangleRust(struct demangle const *demangle, bool full_signature);
static bool isRustSymbol(const char* s);
static void cutArguments(char* s);

public:
static char* demangle(const char* s, bool full_signature);

static bool needsDemangling(const char* s) {
return s[0] == '_' && (s[1] == 'R' || s[1] == 'Z');
}
};

#endif // _DEMANGLE_H
2 changes: 1 addition & 1 deletion src/flightRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Lookup {
mi->_line_number_table_size = 0;
mi->_line_number_table = NULL;

if (name[0] == '_' && name[1] == 'Z') {
if (Demangle::needsDemangling(name)) {
char* demangled = Demangle::demangle(name, false);
if (demangled != NULL) {
mi->_name = _symbols.lookup(demangled);
Expand Down
3 changes: 2 additions & 1 deletion src/frameName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void FrameName::buildFilter(std::vector<Matcher>& vector, const char* base, int
const char* FrameName::decodeNativeSymbol(const char* name) {
const char* lib_name = (_style & STYLE_LIB_NAMES) ? Profiler::instance()->getLibraryName(name) : NULL;

if (name[0] == '_' && name[1] == 'Z') {
if (Demangle::needsDemangling(name)) {
char* demangled = Demangle::demangle(name, _style & STYLE_SIGNATURES);
if (demangled != NULL) {
if (lib_name != NULL) {
Expand Down Expand Up @@ -333,6 +333,7 @@ FrameTypeId FrameName::type(ASGCT_CallFrame& frame) {
case BCI_NATIVE_FRAME: {
const char* name = (const char*)frame.method_id;
if ((name[0] == '_' && name[1] == 'Z') ||
(name[0] == '_' && name[1] == 'R') ||
(name[0] == '+' && name[1] == '[') ||
(name[0] == '-' && name[1] == '[')) {
return FRAME_CPP;
Expand Down
Loading
Loading