@@ -139,13 +139,13 @@ void printCallstack() {
139139 }
140140}
141141
142- void bar(void ) { printCallstack(); }
142+ void bar() { printCallstack(); }
143143
144- void foo(void ) { bar(); }
145- void bar2(void ) { foo(); }
146- void foo2(void ) { bar2(); }
144+ void foo() { bar(); }
145+ void bar2() { foo(); }
146+ void foo2() { bar2(); }
147147
148- int main(void ) {
148+ int main() {
149149 foo2();
150150}
151151` ` `
@@ -167,7 +167,91 @@ In: (/usr/lib/dyld) start + 1903 (???:0)
167167> The ** C++** functions can be enabled as described [here][6].
168168
169169# ## Callstack exceptions
170- _Description and examples coming soon! _
170+ With the [callstack exception][8] an exception capable of printing its construction stacktrace is available.
171+
172+ It can be thrown directly:
173+ ` ` ` C++
174+ // main.cpp
175+
176+ #include <iostream>
177+
178+ #include <callstack_exception.hpp>
179+
180+ void printCallstack() {
181+ throw lcs::exception("Callstack exception with a message");
182+ }
183+
184+ void bar2() { printCallstack(); }
185+ void foo2() { bar2(); }
186+
187+ void bar() { foo2(); }
188+ void foo() { bar(); }
189+
190+ int main() {
191+ try {
192+ foo();
193+ } catch (std::exception& e) {
194+ std::cerr << e.what() << std::endl;
195+ }
196+ }
197+ ` ` `
198+ Compiled and linked on macOS using ` c++ -g main.cpp -I<path/to/library>/include -L<path/to/library> -lcallstack` and
199+ after [enabling ** C++** functions][6] of the library creates the following output:
200+ ` ` `
201+ lcs::exception: "Callstack exception with a message", stacktrace:
202+ At: (a.out) lcs::exception::exception(char const*, bool) (include/callstack_exception.hpp:123)
203+ in: (a.out) printCallstack() (main.cpp:8)
204+ in: (a.out) bar2() (main.cpp:11)
205+ in: (a.out) foo2() (main.cpp:12)
206+ in: (a.out) bar() (main.cpp:14)
207+ in: (a.out) foo() (main.cpp:15)
208+ in: (a.out) main (main.cpp:19)
209+ in: (/usr/lib/dyld) start + 1903
210+ ` ` `
211+
212+ # ### Extending the callstack exception
213+ The [callstack exception][8] can easily serve as base class for other exceptions:
214+ ` ` ` C++
215+ // main.cpp
216+
217+ #include <iostream>
218+
219+ #include <callstack_exception.hpp>
220+
221+ class CustomStacktraceException: public lcs::exception {};
222+
223+ void printCallstack() {
224+ throw CustomStacktraceException();
225+ }
226+
227+ void bar2() { printCallstack(); }
228+ void foo2() { bar2(); }
229+
230+ void bar() { foo2(); }
231+ void foo() { bar(); }
232+
233+ int main() {
234+ try {
235+ foo();
236+ } catch (std::exception& e) {
237+ std::cerr << e.what() << std::endl;
238+ }
239+ }
240+ ` ` `
241+ Compiled and linked on macOS using ` c++ -g main.cpp -I<path/to/library>/include -L<path/to/library> -lcallstack` and
242+ after [enabling ** C++** functions][6] of the library creates the following output:
243+ ` ` `
244+ CustomStacktraceException, stacktrace:
245+ At: (a.out) CustomStacktraceException::CustomStacktraceException() (main.cpp:7)
246+ in: (a.out) CustomStacktraceException::CustomStacktraceException() (main.cpp:7)
247+ in: (a.out) printCallstack() (main.cpp:10)
248+ in: (a.out) bar2() (main.cpp:13)
249+ in: (a.out) foo2() (main.cpp:14)
250+ in: (a.out) bar() (main.cpp:16)
251+ in: (a.out) foo() (main.cpp:17)
252+ in: (a.out) main (main.cpp:21)
253+ in: (/usr/lib/dyld) start + 1903
254+ ` ` `
171255
172256# # Symbolization
173257The generated callstacks are generally symbolized using the information obtained by the dynamic loader (hence the
@@ -202,3 +286,4 @@ This library is licensed under the terms of the GPL 3.0.
202286[5]: https://github.com/mhahnFr/CallstackLibrary/blob/main/include/callstack_exception.hpp
203287[6]: https://github.com/mhahnFr/CallstackLibrary/wiki/Home#enabling-additional-c-exclusive-functions
204288[7]: # callstacks
289+ [8]: https://github.com/mhahnFr/CallstackLibrary/wiki/callstack.hpp#class-callstack
0 commit comments