-
Notifications
You must be signed in to change notification settings - Fork 526
Add custom kernel name demangler #9134
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
9 commits
Select commit
Hold shift + click to select a range
0968938
Add custom demangling in XRT
donwalkarsoham 0fe240b
Only support arg types int,char,void
donwalkarsoham 410c5a8
Remove magic numbers
donwalkarsoham af17fc0
Add exception handling
donwalkarsoham 9f00942
Remove file static map
donwalkarsoham f675672
Identation
donwalkarsoham c03af86
Reduce func args and replace recurrsion with loops
donwalkarsoham f452dc2
formatting
donwalkarsoham ad788f1
Add comments
donwalkarsoham 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,10 @@ static const char* const Control_ScratchPad_Symbol = "scratch-pad-ctrl"; | |
| static const char* const Control_Packet_Symbol = "control-packet"; | ||
| static const char* const Control_Code_Symbol = "control-code"; | ||
|
|
||
| // length of "_Z" prefix in mangled names | ||
| static constexpr uint8_t mangled_prefix_length = 2; | ||
| static constexpr uint8_t decimal_base = 10; | ||
|
|
||
| struct buf | ||
| { | ||
| std::vector<uint8_t> m_data; | ||
|
|
@@ -425,25 +429,83 @@ generate_key_string(const std::string& argument_name, xrt_core::patcher::buf_typ | |
| return argument_name + buf_string; | ||
| } | ||
|
|
||
|
|
||
| // Basic Itanium ABI type decoding. get_demangled_type() is referenced from ChatGPT response | ||
| static std::string | ||
| demangle(const std::string& mangled_name) | ||
| get_demangle_type(char c) | ||
| { | ||
| #ifdef _WIN32 | ||
| char demangled_name[1024]; | ||
| if (UnDecorateSymbolName(mangled_name.c_str(), demangled_name, sizeof(demangled_name), UNDNAME_COMPLETE)) | ||
| return std::string(demangled_name); | ||
| else | ||
| throw std::runtime_error("Error demangling kernel signature"); | ||
| #else | ||
| int status = 0; | ||
| std::unique_ptr<char, decltype(&std::free)> demangled_name | ||
| (abi::__cxa_demangle(mangled_name.c_str(), nullptr, nullptr, &status), std::free); | ||
|
|
||
| if (status) | ||
| throw std::runtime_error("Error demangling kernel signature"); | ||
| static const std::map<char, std::string> demangle_type_map = { | ||
| {'v', "void"}, | ||
| {'c', "char"}, | ||
| {'i', "int"} | ||
| }; | ||
| auto it = demangle_type_map.find(c); | ||
| if (it == demangle_type_map.end()) | ||
| throw std::runtime_error("Unknown type character in mangled name: " + std::string(1, c)); | ||
| return it->second; | ||
| } | ||
|
|
||
| return demangled_name.get(); | ||
| #endif | ||
| // Parse mangled name in Itanium ABI style: _Z<length><name><types> | ||
| // length : number of characters in the name string. | ||
| // name : kernel name in string | ||
| // types : kernel argument data type as below. | ||
| // 'c' represents the arg is a char. | ||
| // 'v' represents the arg is a void. | ||
| // 'i' represents the arg is an int. | ||
| // 'P' represents the arg is a pointer. | ||
| // Hence, "Pc" = char*, "Pv" = void*, "Pi" = int*, "PPc" = char**, etc. | ||
| // demangle() is referenced from ChatGPT response | ||
| static std::string | ||
| demangle(const std::string& mangled) | ||
| { | ||
| //Check if mangled prefix "_Z" is present and length is greater than mangled_prefix_length | ||
| if (mangled.size() <= mangled_prefix_length || mangled.substr(0, mangled_prefix_length) != "_Z") | ||
| throw std::runtime_error("Doesn't have prefix _Z, not a mangled kernel name"); | ||
|
|
||
| size_t idx = 2; | ||
| size_t len = 0; | ||
|
|
||
| // Extract length of function name | ||
| while (idx < mangled.size() && std::isdigit(mangled[idx])) | ||
| len = len * decimal_base + (mangled[idx++] - '0'); | ||
|
|
||
| if (idx + len > mangled.size()) | ||
| throw std::runtime_error("Invalid mangled name, doesn't have expected kernel name length"); | ||
|
|
||
| std::string name = mangled.substr(idx, len); | ||
| idx += len; | ||
| std::vector<std::string> args; | ||
|
|
||
| // Parse the argument types from the mangled name | ||
| // Each argument can have multiple 'P' prefixes indicating pointer depth | ||
| // followed by a single character representing the base type | ||
| while (idx < mangled.size()) { | ||
| int pointer_depth = 0; | ||
| // Count pointer depth (number of 'P' characters indicating pointer levels) | ||
| // For example: "P" = 1 level (char*), "PP" = 2 levels (char**), etc. | ||
| while (idx < mangled.size() && mangled[idx] == 'P') { | ||
|
Collaborator
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. And this one too. |
||
| ++pointer_depth; | ||
| ++idx; | ||
| } | ||
| if (idx >= mangled.size()) | ||
| throw std::runtime_error("demangle arg index out of bounds"); | ||
|
|
||
| std::string type = get_demangle_type(mangled[idx++]); | ||
| for (int i = 0; i < pointer_depth; ++i) | ||
| type += "*"; | ||
| args.push_back(type); | ||
| } | ||
|
|
||
| // Append arguments to the function name | ||
| std::string result = name + "("; | ||
| for (size_t i = 0; i < args.size(); ++i) { | ||
| if (i > 0) | ||
| result += ", "; | ||
| result += args[i]; | ||
| } | ||
| result += ")"; | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| // checks if ELF has .group sections | ||
|
|
||
Oops, something went wrong.
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.
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.
Please comment what this
whilealgorithm is doing.