diff --git a/docs/scip-ruby/CONTRIBUTING.md b/docs/scip-ruby/CONTRIBUTING.md index 8f2208012d..dfc18f0974 100644 --- a/docs/scip-ruby/CONTRIBUTING.md +++ b/docs/scip-ruby/CONTRIBUTING.md @@ -18,6 +18,7 @@ see the [Design Decisions doc][]. - [Writing a new snapshot test](#writing-a-new-snapshot-test) - [Writing a new repo test](#writing-a-new-snapshot-test) - [Debugging](#debugging) + - [More readable stack traces](#more-readable-stack-traces) - [Debugging with print statements](#debugging-with-print-statements) - [Debugging with LLDB](#debugging-with-lldb) - [Debugging build issues](#debugging-build-issues) @@ -205,6 +206,24 @@ file to include the relevant data. ## Debugging +### More readable stack traces + +C++ stack traces can get pretty gnarly with text like: + +```c++ +std::__1::basic_string, std::__1::allocator > +``` + +instead of `std::string`. + +If you have a stack trace, run it through the simplification script. + +``` +./bazel-bin/main/scip-ruby 2> >(./tools/scripts/simplify-cxx-fqns.py) +``` + +NOTE: The script requires [Comby](https://comby.dev/docs/get-started#install). + ### Debugging with print statements So far, I've mostly been using print debugging diff --git a/tools/scripts/comby-rewrites.toml b/tools/scripts/comby-rewrites.toml new file mode 100644 index 0000000000..a25b75a677 --- /dev/null +++ b/tools/scripts/comby-rewrites.toml @@ -0,0 +1,28 @@ +[sorbet] +match = 'sorbet:::[~(((core|realmain)::)?)]' +rewrite = '' + +[std_string] +match = 'std::__1::basic_string' +rewrite = 'string' + +[std_string_view] +match = 'std::__1::basic_string_view' +rewrite = 'string_view' + +[std_utility] +match = 'std::__1:::[name~(pair|tuple|optional)]<:[a]>' +rewrite = 'name<:[a]>' + +[std_ptr] +match = 'std::__1::shared_ptr' +rewrite = 'shared_ptr' + +[std_vector] +match = 'std::__1::vector<:[a],...>' +rewrite = 'vector<:[a]>' + +# CAUTION: Patterns are run in alphabetical order, so use zzz to deprioritize this +[std_zzz_1] +match = 'std::__1::' +rewrite = 'std::' diff --git a/tools/scripts/simplify-cxx-fqns.py b/tools/scripts/simplify-cxx-fqns.py new file mode 100755 index 0000000000..3d77aa7f86 --- /dev/null +++ b/tools/scripts/simplify-cxx-fqns.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +# Utility script to simplify fully-qualified symbol names in C++ stack traces. + +import logging +import pathlib +import os +import subprocess as sp +import sys + +# Why use comby instead of sed or backtracking regexes? +# +# 1. Comby handles < > balancing. +# 2. Comby takes care of whitespace automatically. +# 3. Comby patterns are quite readable + +def simplify(input: bytes) -> bytes: + config_path = pathlib.Path(__file__).parent.joinpath('comby-rewrites.toml') + invocation = ['comby', '-lang', '.c', '-stdin', '-stdout', '-templates', str(config_path)] + comby_ret = sp.run(invocation, input=input, capture_output=True) + comby_ret.check_returncode() + return comby_ret.stdout + +def simplify_until_fixpoint(input: bytes) -> bytes: + output = simplify(input) + if output == input: + return output + return simplify_until_fixpoint(output) + +def default_main(): + if sp.run(['comby', '--help'], stdout=sp.DEVNULL, stderr=sp.PIPE).returncode != 0: + logging.error('comby --help failed; is comby missing?') + sys.exit(1) + + if len(sys.argv) > 1 and sys.argv[1] == '--help': + print('Usage: ./my-binary 2> >(./tools/scripts/simplify-cxx-fqns.py)') + return + + input = sys.stdin.buffer.read() + output = simplify_until_fixpoint(input) + sys.stdout.buffer.write(output) + +if __name__ == '__main__': + default_main()