├── .gitignore ├── LICENSE ├── Makefile ├── Makefile.common ├── Makefile.debug ├── Makefile.release ├── README.md ├── TODO.md ├── backtrace ├── backtrace.c ├── backtrace.mk └── subdir.mk ├── doc ├── .gitignore ├── IHI0038B_ehabi.pdf └── libunwind-LDS.pdf ├── include └── backtrace.h ├── samples ├── .gitignore ├── ping-pong.c ├── samples.mk └── subdir.mk └── tools ├── .gitignore ├── c-copyright-template ├── makefiles ├── .gitignore ├── project.mk ├── target.mk └── tree.mk └── script-copyright-template /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | build/ 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | export PROJECT_ROOT ?= ${CURDIR} 10 | export OUTPUT_ROOT ?= ${PROJECT_ROOT}/build 11 | export BUILD_TYPE ?= release 12 | export CROSS_COMPILE ?= arm-none-eabi- 13 | export CORTEX_M ?= 3 14 | 15 | include ${PROJECT_ROOT}/tools/makefiles/tree.mk 16 | 17 | targets: backtrace samples 18 | 19 | distclean: 20 | @echo "DISTCLEAN ${PROJECT_ROOT}" 21 | $(Q)-${RM} -r ${OUTPUT_ROOT} 22 | -------------------------------------------------------------------------------- /Makefile.common: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | ifeq (${.DEFAULT_GOAL},) 10 | .DEFAULT_GOAL := all 11 | endif 12 | 13 | ifeq ("$(origin V)", "command line") 14 | VERBOSE = ${V} 15 | endif 16 | 17 | ifndef VERBOSE 18 | VERBOSE = 0 19 | endif 20 | 21 | ifeq (${VERBOSE},1) 22 | quiet = 23 | Q = 24 | else 25 | quiet = (${1}) 26 | Q = @ 27 | endif 28 | 29 | export TOOLS_ROOT ?= ${PROJECT_ROOT}/tools 30 | export BUILD_ROOT ?= ${OUTPUT_ROOT}/${BUILD_TYPE} 31 | 32 | CC := ${CROSS_COMPILE}gcc 33 | CXX := ${CROSS_COMPILE}g++ 34 | LD := ${CROSS_COMPILE}gcc 35 | AR := ${CROSS_COMPILE}ar 36 | AS := ${CROSS_COMPILE}as 37 | OBJCOPY := ${CROSS_COMPILE}objcopy 38 | OBJDUMP := ${CROSS_COMPILE}objdump 39 | SIZE := ${CROSS_COMPILE}size 40 | NM := ${CROSS_COMPILE}nm 41 | FIND := find 42 | 43 | CROSS_FLAGS := -mthumb -mcpu=cortex-m${CORTEX_M} 44 | 45 | ARFLAGS := cr 46 | ASFLAGS := ${CROSS_FLAGS} 47 | CFLAGS := ${CROSS_FLAGS} -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wall -Wunused -Wuninitialized -Wmissing-declarations -std=c11 -mpoke-function-name -funwind-tables -fno-omit-frame-pointer 48 | CPPFLAGS += -DBUILD_TYPE="${BUILD_TYPE}" 49 | LDFLAGS := ${CROSS_FLAGS} -Wl,--gc-sections -Wl,--cref -Wl,-Map,"$(basename ${TARGET}).map" 50 | LDLIBS := 51 | LOADLIBES := 52 | 53 | -include ${PROJECT_ROOT}/Makefile.${BUILD_TYPE} 54 | -include ${SOURCE_DIR}/Makefile.${BUILD_TYPE} 55 | -------------------------------------------------------------------------------- /Makefile.debug: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | LDFLAGS += -O0 10 | CFLAGS += -O0 -g 11 | CPPFLAGS += -DDEBUG -DBUILD_TYPE_DEBUG 12 | -------------------------------------------------------------------------------- /Makefile.release: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | LDFLAGS += -O3 10 | CFLAGS += -O3 11 | CPPFLAGS += -DNDEBUG -DBUILD_TYPE_RELEASE 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Backtrace 2 | ========= 3 | 4 | Copyright 2015, Red Rocket Computing, LLC, stephen@redrocketcomputing.com 5 | 6 | Overview 7 | -------- 8 | 9 | Backtrace is a very small (less 4.5K) stack unwinder designed for deeply embedded 10 | C applications running on the ARM Cortex-M family of microprocessors. Backtrace 11 | uses the unwind tables generated by the ARM GCC -funwind-tables option 12 | (see https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Code-Gen-Options.html#Code-Gen-Options). 13 | 14 | Backtrace fills in a pre-allocated array of backtrace frames consisting of 15 | the current function address, the calling site address and optionally a pointer 16 | to the name of the function (if the GCC -mpoke-function-name option is used). 17 | 18 | Backtrace provides the following API: 19 | 20 | ```c 21 | typedef struct backtrace 22 | { 23 | void *function; /* Address of the current address */ 24 | void *address; /* Calling site address */ 25 | const char *name; 26 | } backtrace_t; 27 | 28 | /* Unwind the stack from the current address, filling in provided 29 | backtrace bufffer, limited by size of the buffer, return a count 30 | of the valid frames */ 31 | int backtrace_unwind(backtrace_t *backtrace, int size) 32 | 33 | /* Return a pointer to the function name at the specified address or 34 | "unknown" if the function name string is not present. The address 35 | can be the entry point for the function or any address within the 36 | function's scope. */ 37 | const char *backtrace_function_name(uint32_t pc); 38 | ``` 39 | 40 | ARM estimates the overhead for the unwind tables at 2.6% to 3.6% 41 | (see /doc/IHI0038B_ehabi.pdf). Using the -mpoke-function-name option increases 42 | the overhead by the sum of the length all function names plus 4 bytes per 43 | function. 44 | 45 | License 46 | ------- 47 | 48 | To facilate the use of this library in statically linked open and closed source 49 | embedded systems, this library is licensed under the terms of the 50 | [Mozilla Public License, V2.0.](http://mozilla.org/MPL/2.0) 51 | 52 | Acknowledgments 53 | --------------- 54 | 55 | I would like to thank the following people for providing clear insight and sample 56 | code helping me untangle this dark corner of the compiler world: 57 | 58 | - Ken Werner of Linaro 59 | - Mika Westerberg of Nokia 60 | 61 | Dependencies 62 | ------------ 63 | 64 | Backtrace requires a compatible/working ARM cross compiler supporting the following built-ins: 65 | 66 | ```c 67 | __builtin_return_address /* Get the return address for the current function */ 68 | __builtin_frame_address /* Get the current frame pointer */ 69 | ``` 70 | 71 | and is known to work with recent version of gcc 72 | (see https://launchpad.net/gcc-arm-embedded). 73 | 74 | In addition your linker scripts should export the following symbols: 75 | 76 | ```c 77 | __exidx_start /* Pointing to the start of the unwind index table */ 78 | __exidx_end /* Pointing to the end of the unwind index table */ 79 | ``` 80 | 81 | Building 82 | -------- 83 | 84 | For the default release version (-O3) 85 | 86 | ```shell 87 | CD=path/to/project 88 | make 89 | ``` 90 | 91 | To build a debug (-O0 -g) version of the library: 92 | 93 | ```shell 94 | CD=path/to/project 95 | make V=1 BUILD_TYPE=debug 96 | ``` 97 | 98 | The library can be found at 99 | 100 | path/to/project/BUILD_TYPE/libbacktrace.a 101 | 102 | The build location can be adjusted with OUTPUT_ROOT which must be an absolute path: 103 | 104 | ```shell 105 | make OUTPUT_ROOT=/some/directory 106 | ``` 107 | 108 | Other build options can be adjusted in Makefile.common, Makefile.debug and Makefile.release. 109 | 110 | Using The Unwinder 111 | ------------------- 112 | 113 | ```c 114 | /* 115 | * Copyright 2015 Stephen Street 116 | * 117 | * This Source Code Form is subject to the terms of the Mozilla Public 118 | * License, v. 2.0. If a copy of the MPL was not distributed with this 119 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 120 | */ 121 | 122 | #include 123 | #include 124 | #include 125 | 126 | #define BACKTRACE_SIZE 25 127 | 128 | static int ping(int ball, backtrace_t *backtrace, int size); 129 | static int pong(int ball, backtrace_t *backtrace, int size); 130 | 131 | static void dump_backtrace(const backtrace_t *backtrace, int count) 132 | { 133 | for (int i = 0; i < count; ++i) 134 | printf("%p - %s@%p\n", backtrace[i].address, backtrace[i].name, backtrace[i].address); 135 | } 136 | 137 | static int pong(int ball, backtrace_t *backtrace, int size) 138 | { 139 | if (ball > 0) 140 | return ping(ball - 1, backtrace, size); 141 | else 142 | return backtrace_unwind(backtrace, size); 143 | } 144 | 145 | static int ping(int ball, backtrace_t *backtrace, int size) 146 | { 147 | if (ball > 0) 148 | return pong(ball - 1, backtrace, size); 149 | else 150 | return backtrace_unwind(backtrace, size); 151 | } 152 | 153 | int main(int argc, char **argv) 154 | { 155 | backtrace_t backtrace[BACKTRACE_SIZE]; 156 | 157 | /* Play ball */ 158 | int count = ping(10, backtrace, BACKTRACE_SIZE); 159 | 160 | /* Dump the backtrace */ 161 | dump_backtrace(backtrace, count); 162 | 163 | /* All good */ 164 | return 0; 165 | } 166 | ``` 167 | 168 | To compile and link this sample using a recent GCC ARM Embedded compiler: 169 | 170 | ```shell 171 | arm-none-eabi-gcc -mthumb -mcpu=cortex-m3 -I /path/to/include \ 172 | -fmessage-length=0 -fsigned-char -ffunction-sections \ 173 | -fdata-sections -std=c11 -mpoke-function-name -funwind-tables \ 174 | -fno-omit-frame-pointer ping-pong.c -o ping-pong.afx \ 175 | --specs=rdimon.specs --specs=nano.specs -L /path/to/lib -lbacktrace 176 | ``` 177 | 178 | To run ping-poing.afx with qemu: 179 | 180 | ```shell 181 | > qemu-arm-static -cpu cortex-m3 ./ping-pong.afx 182 | pong - 10 183 | pong - 9 184 | pong - 8 185 | pong - 7 186 | pong - 6 187 | pong - 5 188 | pong - 4 189 | pong - 3 190 | pong - 2 191 | pong - 1 192 | pong - 0 193 | 0x82f6 - ping@0x82f6 194 | 0x8273 - pong@0x8273 195 | 0x82e7 - ping@0x82e7 196 | 0x8273 - pong@0x8273 197 | 0x82e7 - ping@0x82e7 198 | 0x8273 - pong@0x8273 199 | 0x82e7 - ping@0x82e7 200 | 0x8273 - pong@0x8273 201 | 0x82e7 - ping@0x82e7 202 | 0x8273 - pong@0x8273 203 | 0x82e7 - ping@0x82e7 204 | 0x834d - main@0x834d 205 | 0x815b - unknown@0x815b 206 | ``` 207 | 208 | 209 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | TODO 2 | ==== 3 | 4 | - Add support for the cortex-m4 with VFP unwinding. 5 | - Add explicit support to exception stack unwinding. 6 | 7 | 8 | -------------------------------------------------------------------------------- /backtrace/backtrace.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Stephen Street 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | /* This prevents the linking of libgcc unwinder code */ 14 | void __aeabi_unwind_cpp_pr0(void); 15 | void __aeabi_unwind_cpp_pr1(void); 16 | void __aeabi_unwind_cpp_pr2(void); 17 | 18 | void __aeabi_unwind_cpp_pr0(void) 19 | { 20 | }; 21 | 22 | void __aeabi_unwind_cpp_pr1(void) 23 | { 24 | }; 25 | 26 | void __aeabi_unwind_cpp_pr2(void) 27 | { 28 | }; 29 | 30 | static inline __attribute__((always_inline)) uint32_t prel31_to_addr(const uint32_t *prel31) 31 | { 32 | int32_t offset = (((int32_t)(*prel31)) << 1) >> 1; 33 | return ((uint32_t)prel31 + offset) & 0x7fffffff; 34 | } 35 | 36 | static const struct unwind_index *unwind_search_index(const unwind_index_t *start, const unwind_index_t *end, uint32_t ip) 37 | { 38 | const struct unwind_index *middle; 39 | 40 | /* Perform a binary search of the unwind index */ 41 | while (start < end - 1) { 42 | middle = start + ((end - start + 1) >> 1); 43 | if (ip < prel31_to_addr(&middle->addr_offset)) 44 | end = middle; 45 | else 46 | start = middle; 47 | } 48 | return start; 49 | } 50 | 51 | static const char *unwind_get_function_name(void *address) 52 | { 53 | uint32_t flag_word = *(uint32_t *)(address - 4); 54 | if ((flag_word & 0xff000000) == 0xff000000) { 55 | return (const char *)(address - 4 - (flag_word & 0x00ffffff)); 56 | } 57 | return "unknown"; 58 | } 59 | 60 | static int unwind_get_next_byte(unwind_control_block_t *ucb) 61 | { 62 | int instruction; 63 | 64 | /* Are there more instructions */ 65 | if (ucb->remaining == 0) 66 | return -1; 67 | 68 | /* Extract the current instruction */ 69 | instruction = ((*ucb->current) >> (ucb->byte << 3)) & 0xff; 70 | 71 | /* Move the next byte */ 72 | --ucb->byte; 73 | if (ucb->byte < 0) { 74 | ++ucb->current; 75 | ucb->byte = 3; 76 | } 77 | --ucb->remaining; 78 | 79 | return instruction; 80 | } 81 | 82 | static int unwind_control_block_init(unwind_control_block_t *ucb, const uint32_t *instructions, const backtrace_frame_t *frame) 83 | { 84 | /* Initialize control block */ 85 | memset(ucb, 0, sizeof(unwind_control_block_t)); 86 | ucb->current = instructions; 87 | 88 | /* Is the a short unwind description */ 89 | if ((*instructions & 0xff000000) == 0x80000000) { 90 | ucb->remaining = 3; 91 | ucb->byte = 2; 92 | /* Is the a long unwind description */ 93 | } else if ((*instructions & 0xff000000) == 0x81000000) { 94 | ucb->remaining = ((*instructions & 0x00ff0000) >> 14) + 2; 95 | ucb->byte = 1; 96 | } else 97 | return -1; 98 | 99 | /* Initialize the virtual register set */ 100 | if (frame) { 101 | ucb->vrs[7] = frame->fp; 102 | ucb->vrs[13] = frame->sp; 103 | ucb->vrs[14] = frame->lr; 104 | ucb->vrs[15] = 0; 105 | } 106 | 107 | /* All good */ 108 | return 0; 109 | } 110 | 111 | static int unwind_execute_instruction(unwind_control_block_t *ucb) 112 | { 113 | int instruction; 114 | uint32_t mask; 115 | uint32_t reg; 116 | uint32_t *vsp; 117 | 118 | /* Consume all instruction byte */ 119 | while ((instruction = unwind_get_next_byte(ucb)) != -1) { 120 | 121 | if ((instruction & 0xc0) == 0x00) { 122 | /* vsp = vsp + (xxxxxx << 2) + 4 */ 123 | ucb->vrs[13] += ((instruction & 0x3f) << 2) + 4; 124 | 125 | } else if ((instruction & 0xc0) == 0x40) { 126 | /* vsp = vsp - (xxxxxx << 2) - 4 */ 127 | ucb->vrs[13] -= ((instruction & 0x3f) << 2) + 4; 128 | 129 | } else if ((instruction & 0xf0) == 0x80) { 130 | /* pop under mask {r15-r12},{r11-r4} or refuse to unwind */ 131 | instruction = instruction << 8 | unwind_get_next_byte(ucb); 132 | 133 | /* Check for refuse to unwind */ 134 | if (instruction == 0x8000) 135 | return 0; 136 | 137 | /* Pop registers using mask */ 138 | vsp = (uint32_t *)ucb->vrs[13]; 139 | mask = instruction & 0xfff; 140 | 141 | /* Loop through the mask */ 142 | reg = 4; 143 | while (mask != 0) { 144 | if ((mask & 0x001) != 0) 145 | ucb->vrs[reg] = *vsp++; 146 | mask = mask >> 1; 147 | ++reg; 148 | } 149 | 150 | /* Update the vrs sp as usual if r13 (sp) was not in the mask, 151 | * otherwise leave the popped r13 as is. */ 152 | if ((mask & (1 << (13 - 4))) == 0) 153 | ucb->vrs[13] = (uint32_t)vsp; 154 | 155 | } else if ((instruction & 0xf0) == 0x90 && instruction != 0x9d && instruction != 0x9f) { 156 | /* vsp = r[nnnn] */ 157 | ucb->vrs[13] = ucb->vrs[instruction & 0x0f]; 158 | 159 | } else if ((instruction & 0xf0) == 0xa0) { 160 | /* pop r4-r[4+nnn] or pop r4-r[4+nnn], r14*/ 161 | vsp = (uint32_t *)ucb->vrs[13]; 162 | 163 | for (reg = 4; reg <= (instruction & 0x07) + 4; ++reg) 164 | ucb->vrs[reg] = *vsp++; 165 | 166 | if (instruction & 0x08) 167 | ucb->vrs[14] = *vsp++; 168 | 169 | ucb->vrs[13] = (uint32_t)vsp; 170 | 171 | } else if (instruction == 0xb0) { 172 | /* finished */ 173 | if (ucb->vrs[15] == 0) 174 | ucb->vrs[15] = ucb->vrs[14]; 175 | 176 | /* All done unwinding */ 177 | return 0; 178 | 179 | } else if (instruction == 0xb1) { 180 | /* pop register under mask {r3,r2,r1,r0} */ 181 | vsp = (uint32_t *)ucb->vrs[13]; 182 | mask = unwind_get_next_byte(ucb); 183 | 184 | reg = 0; 185 | while (mask != 0) { 186 | if ((mask & 0x01) != 0) 187 | ucb->vrs[reg] = *vsp++; 188 | mask = mask >> 1; 189 | ++reg; 190 | } 191 | ucb->vrs[13] = (uint32_t)vsp; 192 | 193 | } else if (instruction == 0xb2) { 194 | /* vps = vsp + 0x204 + (uleb128 << 2) */ 195 | ucb->vrs[13] += 0x204 + (unwind_get_next_byte(ucb) << 2); 196 | 197 | } else if (instruction == 0xb3 || instruction == 0xc8 || instruction == 0xc9) { 198 | /* pop VFP double-precision registers */ 199 | vsp = (uint32_t *)ucb->vrs[13]; 200 | 201 | /* D[ssss]-D[ssss+cccc] or D[16+sssss]-D[16+ssss+cccc] as pushed by VPUSH or FSTMFDX */ 202 | vsp += 2 * ((unwind_get_next_byte(ucb) & 0x0f) + 1); 203 | 204 | 205 | if (instruction == 0xb3) { 206 | /* as pushed by FSTMFDX */ 207 | vsp++; 208 | } 209 | 210 | ucb->vrs[13] = (uint32_t)vsp; 211 | 212 | } else if ((instruction & 0xf8) == 0xb8 || (instruction & 0xf8) == 0xd0) { 213 | /* pop VFP double-precision registers */ 214 | vsp = (uint32_t *)ucb->vrs[13]; 215 | 216 | /* D[8]-D[8+nnn] as pushed by VPUSH or FSTMFDX */ 217 | vsp += 2 * ((instruction & 0x07) + 1); 218 | 219 | if ((instruction & 0xf8) == 0xb8) { 220 | /* as pushed by FSTMFDX */ 221 | vsp++; 222 | } 223 | 224 | ucb->vrs[13] = (uint32_t)vsp; 225 | 226 | } else 227 | return -1; 228 | } 229 | 230 | return instruction != -1; 231 | } 232 | 233 | static inline __attribute__((always_inline)) uint32_t *read_psp(void) 234 | { 235 | /* Read the current PSP and return its value as a pointer */ 236 | uint32_t psp; 237 | 238 | __asm volatile ( 239 | " mrs %0, psp \n" 240 | : "=r" (psp) : : 241 | ); 242 | 243 | return (uint32_t*)psp; 244 | } 245 | 246 | /* TODO How do I range check the stack pointer */ 247 | static int unwind_frame(backtrace_frame_t *frame) 248 | { 249 | unwind_control_block_t ucb; 250 | const unwind_index_t *index; 251 | const uint32_t *instructions; 252 | int execution_result; 253 | 254 | /* Search the unwind index for the matching unwind table */ 255 | index = unwind_search_index(__exidx_start, __exidx_end, frame->pc); 256 | if (index == NULL) 257 | return -1; 258 | 259 | /* Make sure we can unwind this frame */ 260 | if (index->insn == 0x00000001) 261 | return 0; 262 | 263 | /* Get the pointer to the first unwind instruction */ 264 | if (index->insn & 0x80000000) 265 | instructions = &index->insn; 266 | else 267 | instructions = (uint32_t *)prel31_to_addr(&index->insn); 268 | 269 | /* Initialize the unwind control block */ 270 | if (unwind_control_block_init(&ucb, instructions, frame) < 0) 271 | return -1; 272 | 273 | /* Execute the unwind instructions TODO range check the stack pointer */ 274 | while ((execution_result = unwind_execute_instruction(&ucb)) > 0); 275 | if (execution_result == -1) 276 | return -1; 277 | 278 | /* Set the virtual pc to the virtual lr if this is the first unwind */ 279 | if (ucb.vrs[15] == 0) 280 | ucb.vrs[15] = ucb.vrs[14]; 281 | 282 | /* Check for exception return */ 283 | /* TODO Test with other ARM processors to verify this method. */ 284 | if ((ucb.vrs[15] & 0xf0000000) == 0xf0000000) { 285 | /* According to the Cortex Programming Manual (p.44), the stack address is always 8-byte aligned (Cortex-M7). 286 | Depending on where the exception came from (MSP or PSP), we need the right SP value to work with. 287 | 288 | ucb.vrs[7] contains the right value, so take it and align it by 8 bytes, store it as the current 289 | SP to work with (ucb.vrs[13]) which is then saved as the current (virtual) frame's SP. 290 | */ 291 | uint32_t *stack; 292 | ucb.vrs[13] = (ucb.vrs[7] & ~7); 293 | 294 | /* If we need to start from the MSP, we need to go down X words to find the PC, where: 295 | X=2 if it was a non-floating-point exception 296 | X=20 if it was a floating-point (VFP) exception 297 | 298 | If we need to start from the PSP, we need to go up exactly 6 words to find the PC. 299 | See the ARMv7-M Architecture Reference Manual p.594 and Cortex-M7 Processor Programming Manual p.44/p.45 for details. 300 | */ 301 | if ((ucb.vrs[15] & 0xc) == 0) { 302 | /* Return to Handler Mode: MSP (0xffffff-1) */ 303 | stack = (uint32_t*)(ucb.vrs[13]); 304 | 305 | /* The PC is always 2 words down from the MSP, if it was a non-floating-point exception */ 306 | stack -= 2; 307 | 308 | /* If there was a VFP exception (0xffffffe1), the PC is located another 18 words down */ 309 | if ((ucb.vrs[15] & 0xf0) == 0xe0) 310 | { 311 | stack -= 18; 312 | } 313 | } 314 | else { 315 | /* Return to Thread Mode: PSP (0xffffff-d) */ 316 | stack = read_psp(); 317 | 318 | /* The PC is always 6 words up from the PSP */ 319 | stack += 6; 320 | } 321 | 322 | /* Store the PC */ 323 | ucb.vrs[15] = *stack--; 324 | 325 | /* Store the LR */ 326 | ucb.vrs[14] = *stack--; 327 | } 328 | 329 | /* We are done if current frame pc is equal to the virtual pc, prevent infinite loop */ 330 | if (frame->pc == ucb.vrs[15]) 331 | return 0; 332 | 333 | /* Update the frame */ 334 | frame->fp = ucb.vrs[7]; 335 | frame->sp = ucb.vrs[13]; 336 | frame->lr = ucb.vrs[14]; 337 | frame->pc = ucb.vrs[15]; 338 | 339 | /* All good */ 340 | return 1; 341 | } 342 | 343 | int _backtrace_unwind(backtrace_t *buffer, int size, backtrace_frame_t *frame) 344 | { 345 | int count = 0; 346 | 347 | /* Initialize the backtrace frame buffer */ 348 | memset(buffer, 0, sizeof(backtrace_t) * size); 349 | 350 | /* Unwind all frames */ 351 | do { 352 | if (frame->pc == 0) { 353 | /* Reached __exidx_end. */ 354 | buffer[count++].name = ""; 355 | break; 356 | } 357 | 358 | if (frame->pc == 0x00000001) { 359 | /* Reached .cantunwind instruction. */ 360 | buffer[count++].name = ""; 361 | break; 362 | } 363 | 364 | /* Find the unwind index of the current frame pc */ 365 | const unwind_index_t *index = unwind_search_index(__exidx_start, __exidx_end, frame->pc); 366 | 367 | /* Clear last bit (Thumb indicator) */ 368 | frame->pc &= 0xfffffffeU; 369 | 370 | /* Generate the backtrace information */ 371 | buffer[count].address = (void *)frame->pc; 372 | buffer[count].function = (void *)prel31_to_addr(&index->addr_offset); 373 | buffer[count].name = unwind_get_function_name(buffer[count].function); 374 | 375 | /* Next backtrace frame */ 376 | ++count; 377 | 378 | } while (unwind_frame(frame) && count < size); 379 | 380 | /* All done */ 381 | return count; 382 | } 383 | 384 | const char *backtrace_function_name(uint32_t pc) 385 | { 386 | const unwind_index_t *index = unwind_search_index(__exidx_start, __exidx_end, pc); 387 | if (!index) 388 | return 0; 389 | 390 | return unwind_get_function_name((void *)prel31_to_addr(&index->addr_offset)); 391 | } 392 | -------------------------------------------------------------------------------- /backtrace/backtrace.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | ifeq ($(findstring ${BUILD_ROOT},${CURDIR}),) 10 | include ${PROJECT_ROOT}/tools/makefiles/target.mk 11 | else 12 | 13 | TARGET := libbacktrace.a 14 | 15 | include ${PROJECT_ROOT}/tools/makefiles/project.mk 16 | 17 | CPPFLAGS += -I ${SOURCE_DIR}/../include 18 | 19 | endif 20 | 21 | -------------------------------------------------------------------------------- /backtrace/subdir.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | where-am-i := $(lastword ${MAKEFILE_LIST}) 10 | 11 | SRC += $(wildcard $(dir $(where-am-i))*.c) 12 | SRC += $(wildcard $(dir $(where-am-i))*.S) 13 | SRC += $(wildcard $(dir $(where-am-i))*.s) 14 | -------------------------------------------------------------------------------- /doc/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/red-rocket-computing/backtrace/9f963f96e3b087bdca2b9b193657d6bc1453d9d8/doc/.gitignore -------------------------------------------------------------------------------- /doc/IHI0038B_ehabi.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/red-rocket-computing/backtrace/9f963f96e3b087bdca2b9b193657d6bc1453d9d8/doc/IHI0038B_ehabi.pdf -------------------------------------------------------------------------------- /doc/libunwind-LDS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/red-rocket-computing/backtrace/9f963f96e3b087bdca2b9b193657d6bc1453d9d8/doc/libunwind-LDS.pdf -------------------------------------------------------------------------------- /include/backtrace.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Stephen Street 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | #ifndef BACKTRACE_H_ 10 | #define BACKTRACE_H_ 11 | 12 | #include 13 | 14 | typedef struct backtrace_frame 15 | { 16 | uint32_t fp; 17 | uint32_t sp; 18 | uint32_t lr; 19 | uint32_t pc; 20 | } backtrace_frame_t; 21 | 22 | typedef struct backtrace 23 | { 24 | void *function; 25 | void *address; 26 | const char *name; 27 | } backtrace_t; 28 | 29 | typedef struct unwind_control_block 30 | { 31 | uint32_t vrs[16]; 32 | const uint32_t *current; 33 | int remaining; 34 | int byte; 35 | } unwind_control_block_t; 36 | 37 | typedef struct unwind_index 38 | { 39 | uint32_t addr_offset; 40 | uint32_t insn; 41 | } unwind_index_t; 42 | 43 | /* These symbols point to the unwind index and should be provide by the linker script */ 44 | extern const unwind_index_t __exidx_start[]; 45 | extern const unwind_index_t __exidx_end[]; 46 | 47 | int _backtrace_unwind(backtrace_t *buffer, int size, backtrace_frame_t *frame); 48 | const char *backtrace_function_name(uint32_t pc); 49 | 50 | static inline int __attribute__((always_inline)) backtrace_unwind(backtrace_t *buffer, int size) 51 | { 52 | /* Get the current pc */ 53 | register uint32_t pc; 54 | __asm__ volatile("mov %0, pc" : "=r"(pc)); 55 | 56 | /* Initialize the stack frame */ 57 | backtrace_frame_t frame; 58 | frame.sp = (uint32_t)__builtin_frame_address(0); 59 | frame.fp = (uint32_t)__builtin_frame_address(0); 60 | frame.lr = (uint32_t)__builtin_return_address(0); 61 | frame.pc = pc; 62 | 63 | /* Let it rip */ 64 | return _backtrace_unwind(buffer, size, &frame); 65 | } 66 | 67 | #endif /* BACKTRACE_H_ */ 68 | -------------------------------------------------------------------------------- /samples/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/red-rocket-computing/backtrace/9f963f96e3b087bdca2b9b193657d6bc1453d9d8/samples/.gitignore -------------------------------------------------------------------------------- /samples/ping-pong.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Stephen Street 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #define BACKTRACE_SIZE 25 14 | 15 | static int ping(int ball, backtrace_t *backtrace, int size); 16 | static int pong(int ball, backtrace_t *backtrace, int size); 17 | 18 | static void dump_backtrace(const backtrace_t *backtrace, int count) 19 | { 20 | for (int i = 0; i < count; ++i) 21 | printf("%p - %s@%p\n", backtrace[i].address, backtrace[i].name, backtrace[i].address); 22 | } 23 | 24 | static void ball_location(void *function, int ball) 25 | { 26 | printf("%s - %d\n", backtrace_function_name((uint32_t)function), ball); 27 | } 28 | 29 | static int pong(int ball, backtrace_t *backtrace, int size) 30 | { 31 | ball_location(pong, ball); 32 | 33 | if (ball > 0) 34 | return ping(ball - 1, backtrace, size); 35 | else 36 | return backtrace_unwind(backtrace, size); 37 | } 38 | 39 | static int ping(int ball, backtrace_t *backtrace, int size) 40 | { 41 | ball_location(pong, ball); 42 | 43 | if (ball > 0) 44 | return pong(ball - 1, backtrace, size); 45 | else 46 | return backtrace_unwind(backtrace, size); 47 | } 48 | 49 | int main(int argc, char **argv) 50 | { 51 | backtrace_t backtrace[BACKTRACE_SIZE]; 52 | 53 | /* Play ball */ 54 | int count = ping(10, backtrace, BACKTRACE_SIZE); 55 | 56 | /* Dump the backtrace */ 57 | dump_backtrace(backtrace, count); 58 | 59 | /* All good */ 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /samples/samples.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | ifeq ($(findstring ${BUILD_ROOT},${CURDIR}),) 10 | include ${PROJECT_ROOT}/tools/makefiles/target.mk 11 | else 12 | 13 | EXTRA_DEPS += ${BUILD_ROOT}/backtrace/libbacktrace.a 14 | 15 | TARGET := ping-pong.afx 16 | 17 | include ${PROJECT_ROOT}/tools/makefiles/project.mk 18 | 19 | CPPFLAGS += -I ${SOURCE_DIR}/../include 20 | LDFLAGS += -L ${BUILD_ROOT}/backtrace --specs=rdimon.specs --specs=nano.specs 21 | LDLIBS += -lbacktrace 22 | 23 | endif 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /samples/subdir.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | where-am-i := $(lastword ${MAKEFILE_LIST}) 10 | 11 | SRC += $(wildcard $(dir $(where-am-i))*.c) 12 | SRC += $(wildcard $(dir $(where-am-i))*.S) 13 | SRC += $(wildcard $(dir $(where-am-i))*.s) 14 | -------------------------------------------------------------------------------- /tools/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/red-rocket-computing/backtrace/9f963f96e3b087bdca2b9b193657d6bc1453d9d8/tools/.gitignore -------------------------------------------------------------------------------- /tools/c-copyright-template: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Stephen Street 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ -------------------------------------------------------------------------------- /tools/makefiles/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/red-rocket-computing/backtrace/9f963f96e3b087bdca2b9b193657d6bc1453d9d8/tools/makefiles/.gitignore -------------------------------------------------------------------------------- /tools/makefiles/project.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | SUBDIRS ?= $(subst ${SOURCE_DIR}/,,$(shell $(FIND) ${SOURCE_DIR} -mindepth 2 -name "subdir.mk" -printf "%h ")) 10 | SRC := 11 | 12 | include ${PROJECT_ROOT}/Makefile.common 13 | include $(patsubst %, ${SOURCE_DIR}/%/subdir.mk, ${SUBDIRS}) 14 | -include ${SOURCE_DIR}/subdir.mk 15 | 16 | OBJ := $(patsubst %.c,%.o,$(filter %.c,${SRC})) $(patsubst %.cpp,%.o,$(filter %.cpp,${SRC})) $(patsubst %.s,%.o,$(filter %.s,${SRC})) $(patsubst %.S,%.o,$(filter %.S,${SRC})) 17 | OBJ := $(subst ${PROJECT_ROOT},${BUILD_ROOT},${OBJ}) 18 | 19 | #$(info SUBDIRS=${SUBDIRS}) 20 | #$(info SRC=${SRC}) 21 | #$(info OBJ=${OBJ}) 22 | #$(info SOURCE_DIR=${SOURCE_DIR}) 23 | #$(info TARGET=${TARGET}) 24 | #$(info TARGET=$(addprefix ${CURDIR}/,${TARGET})) 25 | #$(info CURDIR=${CURDIR}) 26 | 27 | .SECONDARY: 28 | 29 | all: ${SUBDIRS} $(addprefix ${CURDIR}/,${TARGET}) 30 | 31 | clean: ${SUBDIRS} 32 | @echo "CLEANING ${TARGET}" 33 | $(Q)${RM} ${TARGET} ${TARGET:%.afx=%.map} ${TARGET:%.afx=%.img} ${TARGET:%.afx=%.bin} ${TARGET:%.afx=%.map} ${TARGET:%.afx=%.smap} ${OBJ} ${OBJ:%.o=%.d} ${OBJ:%.o=%.dis} 34 | 35 | ${SUBDIRS}: 36 | $(Q)mkdir -p ${CURDIR}/$@ 37 | 38 | ${CURDIR}/%.a: ${OBJ} ${EXTRA_DEPS} 39 | @echo "ARCHIVING $@" 40 | $(Q)$(AR) ${ARFLAGS} $@ ${OBJ} 41 | 42 | ${CURDIR}/%.afx: ${OBJ} ${EXTRA_DEPS} 43 | @echo "LINKING $@" 44 | $(Q)$(CC) -Wl,--cref -Wl,-Map,"$(basename ${@}).map" ${LDFLAGS} ${LOADLIBES} -o $@ ${OBJ} ${LDLIBS} 45 | 46 | ${CURDIR}/%.elf: ${OBJ} ${EXTRA_DEPS} 47 | @echo "LINKING $@" 48 | $(Q)$(CC) -Wl,--cref -Wl,-Map,"$(basename ${@}).map" ${LDFLAGS} ${LOADLIBES} -o $@ ${OBJ} ${LDLIBS} 49 | 50 | ${CURDIR}/%.o: ${SOURCE_DIR}/%.c 51 | @echo "COMPILING $<" 52 | $(Q)$(CC) ${CPPFLAGS} ${CFLAGS} -MMD -MP -c -o $@ $< 53 | 54 | ${CURDIR}/%.o: ${SOURCE_DIR}/%.cpp 55 | @echo "COMPILING $<" 56 | $(Q)$(CXX) ${CPPFLAGS} ${CXXFLAGS} -MMD -MP -c -o $@ $< 57 | 58 | ${CURDIR}/%.o: ${SOURCE_DIR}/%.s 59 | @echo "ASSEMBLING $<" 60 | $(Q)$(AS) ${ASFLAGS} -o $@ $< 61 | 62 | ${CURDIR}/%.o: ${SOURCE_DIR}/%.S 63 | @echo "ASSEMBLING $<" 64 | $(Q)$(CC) ${CPPFLAGS} ${ASFLAGS} -c -o $@ $< 65 | 66 | ifneq (${MAKECMDGOALS},clean) 67 | -include ${OBJ:.o=.d} 68 | endif 69 | -------------------------------------------------------------------------------- /tools/makefiles/target.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | .SUFFIXES: 10 | 11 | include ${PROJECT_ROOT}/Makefile.common 12 | 13 | ifndef BUILD_PATH 14 | BUILD_PATH := $(subst ${PROJECT_ROOT},${BUILD_ROOT},${CURDIR}) 15 | endif 16 | 17 | ${BUILD_PATH}: 18 | +$(Q)[ -d $@ ] || mkdir -p $@ 19 | +$(Q)${MAKE} --no-print-directory -C $@ -f ${CURDIR}/$(lastword $(subst /, ,${CURDIR})).mk SOURCE_DIR=${CURDIR} ${MAKECMDGOALS} 20 | .PHONY: ${BUILD_PATH} 21 | 22 | Makefile : ; 23 | %.mk :: ; 24 | 25 | % :: ${BUILD_PATH} ; @: 26 | -------------------------------------------------------------------------------- /tools/makefiles/tree.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | 9 | where-am-i := ${CURDIR}/$(lastword $(subst $(lastword ${MAKEFILE_LIST}),,${MAKEFILE_LIST})) 10 | 11 | include ${PROJECT_ROOT}/Makefile.common 12 | 13 | BUILD_PATH := $(subst ${PROJECT_ROOT},${BUILD_ROOT},${CURDIR}) 14 | SUBDIRS ?= $(subst ${CURDIR}/,,$(shell $(FIND) ${CURDIR} -mindepth 2 -maxdepth 2 -name "*.mk" -and -not -name "subdir.mk" -printf "%h ")) 15 | 16 | all: targets 17 | 18 | clean: targets 19 | 20 | #$(info SUBDIR=${SUBDIRS}) 21 | 22 | ${SUBDIRS}: 23 | @echo "ENTERING $@" 24 | $(Q)${MAKE} --no-print-directory -C $@ -f $@.mk ${MAKECMDGOALS} 25 | .PHONY: ${SUBDIRS} 26 | -------------------------------------------------------------------------------- /tools/script-copyright-template: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Stephen Street 3 | # 4 | # This Source Code Form is subject to the terms of the Mozilla Public 5 | # License, v. 2.0. If a copy of the MPL was not distributed with this 6 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | # 8 | --------------------------------------------------------------------------------