{"id":28717404,"url":"https://github.com/thesprog/sst","last_synced_at":"2025-06-15T03:15:37.644Z","repository":{"id":291270579,"uuid":"977131836","full_name":"theSprog/sst","owner":"theSprog","description":"C/C++ simple stack trace，Header-only, zero-dependency and implement in C++11, avaliable in C API","archived":false,"fork":false,"pushed_at":"2025-06-14T07:51:46.000Z","size":45,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-14T08:35:51.706Z","etag":null,"topics":["c-api","cpp11","header-only","stacktrace"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theSprog.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-03T13:53:00.000Z","updated_at":"2025-06-14T07:51:49.000Z","dependencies_parsed_at":"2025-05-18T10:36:12.143Z","dependency_job_id":null,"html_url":"https://github.com/theSprog/sst","commit_stats":null,"previous_names":["thesprog/sst"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/theSprog/sst","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theSprog%2Fsst","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theSprog%2Fsst/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theSprog%2Fsst/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theSprog%2Fsst/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theSprog","download_url":"https://codeload.github.com/theSprog/sst/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theSprog%2Fsst/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259914924,"owners_count":22931334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-api","cpp11","header-only","stacktrace"],"created_at":"2025-06-15T03:15:36.969Z","updated_at":"2025-06-15T03:15:37.620Z","avatar_url":"https://github.com/theSprog.png","language":"C++","readme":"中文版文档在[这里](./README_zh.md)\n\n`sst(simple stack trace)` is a lightweight, zero-dependency, **header-only** C++ stacktrace library designed for minimal intrusion and high compatibility. It supports:\n\n- ✅ **No DWARF Required**: Uses only the symbol table (`.symtab` / `.dynsym`), no reliance on DWARF debug info\n- ✅ **PIE / No-PIE Support**: Automatically detects whether the main binary is PIE and handles address relocation properly\n- ✅ **Static / Dynamic Linking**: Works with `-static`, `-no-pie`, `-pie`, and `-lxxx` builds out of the box\n- ✅ **Dynamic Library Resolution**: Can resolve symbols from modules loaded via `dlopen()`\n- ✅ **C API Export**: Provides a C-compatible API via `libsst.so` / `libsst.a` for integration with C or other languages\n\n---\n\n\n\n## 📁 Project Structure\n\n```\n.\n├── include/\n│   └── sst.hpp          # ✅ Core header-only file for direct C++ use\n├── src/\n│   ├── sst.cpp          # 🔁 C API implementation\n│   └── sst.h            # 🔁 C API header (useful for Python FFI or other bindings)\n├── exmaple/\n│   └── *.cpp            # 📦 Example programs under various build configurations (PIE, no-PIE, static, shared, dlopen)\n├── test/\n│   ├── test_capi.c      # 🧪 Test program demonstrating the C API\n└── README.md            # 📖 Project documentation\n\n````\n\n---\n## 🔧 Quick Start (C++)\n\nYou only need a single header:\n\n```cpp\n#include \"sst.hpp\"\n\nint main() {\n    stacktrace::Stacktrace st = stacktrace::Stacktrace::capture();\n    st.print();  // Print current stack frames\n}\n```\n\n---\n\n### Raw Frame Structure\n\n\u003e 💡 `RawFrame` is a lower-level structure designed to work well with tools like `addr2line`, which allow symbolic address resolution via commands like:\n\u003e\n\u003e `addr2line -e /lib/libc.so.6 0x1234`\n\nIf you want to integrate with `addr2line`, or build custom backtrace analysis tools, you can use `resolve_to_raw()` or `get_raw_frames()` to retrieve absolute addresses, module offsets, and file paths:\n\n```cpp\n#include \"sst.hpp\"\n\nint main() {\n    using stacktrace::Stacktrace;\n    using stacktrace::RawFrame;\n\n    Stacktrace st = Stacktrace::capture();\n    std::vector\u003cRawFrame\u003e raw = st.get_raw_frames();\n\n    for (const auto\u0026 f : raw) {\n        printf(\"abs: 0x%lx, offset: 0x%lx, module: %s\\n\",\n               (unsigned long)f.abs_addr,\n               (unsigned long)f.offset,\n               f.module_name.c_str());\n    }\n\n    // You can also resolve a single address\n    RawFrame one = Stacktrace::resolve_to_raw((void*)main);\n}\n```\n\n---\n\n## 🌐 C API Usage\n\nYou can build `libsst.a` or `libsst.so` to use the library from C projects or foreign language bindings:\n\n```c\n#include \"sst.h\"\n\nint main() {\n    sst_backtrace bt;\n    sst_capture(\u0026bt);\n    sst_print_stdout(\u0026bt);\n    sst_print(\u0026bt, stderr); // Print to a custom file stream\n\n    // Extract raw frame info (can be used with addr2line)\n    void* pcs[SST_MAX_FRAMES];\n    sst_raw_frame raw[SST_MAX_FRAMES];\n    for (size_t i = 0; i \u003c bt.size; ++i) {\n        pcs[i] = (void*)bt.frames[i].abs_addr;\n    }\n    sst_resolve_raw_batch(pcs, bt.size, raw);\n\n    for (size_t i = 0; i \u003c bt.size; ++i) {\n        printf(\"addr: 0x%lx, offset: 0x%lx, module: %s\\n\",\n               (unsigned long)raw[i].abs_addr,\n               (unsigned long)raw[i].offset,\n               raw[i].module ?: \"\u003cunknown\u003e\");\n    }\n\n    sst_free_raw_frames(raw, bt.size); // Free allocated module strings\n    return 0;\n}\n```\n\n\u003e 💡 To ensure the full module path is preserved for tools like `addr2line`, the `module` field must be a dynamically allocated `char*`, not a fixed-size array. Fixed-size buffers may truncate long paths and break tooling. All `sst_raw_frame.module` values are allocated via `strdup()` internally, and must be manually freed to avoid memory leaks.\n\n---\n\n## 🛠️ Build Instructions\n\n```bash\ncd src \u0026\u0026 make       # Build both static and shared libraries\n\n# If you want to run tests\ncd test \u0026\u0026 make      # Build C test binaries\n```\n\n---\n\n## 🧼 Memory Management (C)\n\nAll `sst_raw_frame.module` strings returned by `sst_resolve_to_raw()` or `sst_resolve_raw_batch()` must be manually freed:\n\n```c\nsst_raw_frame frame;\nsst_resolve_to_raw(ptr, \u0026frame);\n// Use frame.module ...\nsst_free_raw_frames(\u0026frame, 1);\n```\n\nFor batch allocations, use `sst_free_raw_frames` to release each internal `char*`. This function does **not** free the array itself — you are responsible for that:\n\n```c\nsst_raw_frame* arr = malloc(sizeof(sst_raw_frame) * count);\nsst_resolve_raw_batch(addrs, count, arr);\n// Use arr[i].module ...\nsst_free_raw_frames(arr, count);\nfree(arr); // Optional: free the array body itself\n```\n\n---\n\n\n\n## 💡 Example Usage\n\nCheck out the `exmaple/` directory for sample programs covering different scenarios:\n\n* `pie.cpp`, `nopie.cpp`, `static.cpp`: Test various main binary configurations\n* `pie_dlopen.cpp`, `nopie_dlopen.cpp`: Test symbol resolution for `dlopen()`-loaded modules\n* `*_shared*.cpp`: Test whether symbols from linked shared libraries are correctly resolved\n\n---\n\n\n\n## 🛠️ Technical Details\n\n* Uses `dl_iterate_phdr()` to enumerate all loaded modules (including the main binary and shared libraries)\n* Parses `.symtab` and `.dynsym` from ELF files directly\n* Resolves symbol addresses as `dlpi_addr + st_value` for PIE binaries, or just `st_value` for no-PIE\n* For static or no-PIE binaries (where `dlpi_addr == 0`), uses `/proc/self/maps` to determine the true base address\n\n## 📬 Contact me\nFeel free to submit an Issue or PR for contribution(If there are bugs, PLEASE TELL ME!😘)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthesprog%2Fsst","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthesprog%2Fsst","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthesprog%2Fsst/lists"}