-
Notifications
You must be signed in to change notification settings - Fork 21
/
tracey.cpp
3939 lines (3357 loc) · 123 KB
/
tracey.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Tracey, a simple and lightweight memory leak detector
* Copyright (c) 2011,2012,2013,2014 r-lyeh
* ZLIB/LibPNG licensed.
* Features:
* - tiny
* - clean
* - callstack based
* - lazy. should work with no source modification
* - macroless API. no new/delete macros
* - embeddable. just link it against your project
* - C hooks (optional) (win32 only)
* - C++ hooks (optional)
* - web server (optional)
* To do:
* - support for C hooks on non-win archs:
* - http://stackoverflow.com/questions/262439/create-a-wrapper-function-for-malloc-and-free-in-c
* - http://src.chromium.org/svn/trunk/src/tools/memory_watcher/memory_hook.cc
* - void __f () { } // Do something.
* void f () __attribute__ ((weak, alias ("__f")));
* - or http://man7.org/linux/man-pages/man3/malloc_hook.3.html
* - as seen in https://github.com/emeryberger/Heap-Layers/blob/master/wrappers/gnuwrapper.cpp
* - memory manager checks
* - Read before allocating memory
* - Write before allocating memory
* - Read before beginning of allocation
* - Write before beginning of allocation
* - Read after end of allocation
* - Write after end of allocation
* - Read after deallocation
* - Write after deallocation
* - Failure to deallocate memory
* - Deallocating memory twice
* - Deallocating nonallocated memory
* - Zero-size memory allocation
* - optional pools for microallocations
* - reports per:
* - symbol: namespace/class/function
* - author: blame, candidates (caller and callee for highest apparitions founds on blame(symbol(ns/cl/fn)))
* - location: filename, directory
* - origin: module/thread
* - wasted size, % total, hits
* - ignores per project/session:
* - symbol: namespace/class/function
* - author: blame, candidates
* - location: filename, directory
* - origin: module/thread
* - versioning support (svn/git...)
* - cloud account linking support (github/gitorius/etc):
* - auto commit/pull symbol maps
* - auto commit/pull settings
* - auto commit reports
* - auto updates on ui: load webserver templates from official github repo
* References:
* - http://www.codeproject.com/KB/cpp/allocator.aspx
* - http://drdobbs.com/cpp/184403759
* - rlyeh ~~ listening to Long Distance Calling / Metulsky Curse Revisited
*/
// A few speed tweaks before loading STL on MSVC
#ifdef _SECURE_SCL
#undef _SECURE_SCL
#endif
#define _SECURE_SCL 0
#ifdef _HAS_ITERATOR_DEBUGGING
#undef _HAS_ITERATOR_DEBUGGING
#endif
//#define _HAS_ITERATOR_DEBUGGING 0
// Include C, then C++
#include <cassert>
#include <cctype>
// #include <cstddef> // (stddef.h fails on ArchLinux w/ clang 3.4)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <new>
#include <set>
#include <sstream>
#include <string>
#include <vector>
// Our API {
# include "tracey.hpp"
// }
// external; tree implementation {
//#line 1 "oak.hpp"
// a simple tree container, zlib/libpng licensed.
// - rlyeh ~~ listening to Buckethead - The Moltrail #2
//#pragma once
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#define OAK_VERSION "1.0.0" // (2015/10/25) Semantic versioning adherence; fix csv() with non-string keys
namespace oak
{
// config
# ifndef OAK_VERBOSE
enum { OAK_VERBOSE = false };
# endif
// tree class
// [] means read-writeable (so is <<)
// () means read-only access
template< typename K, typename V = int, typename P = std::less< K > >
class tree : public std::map< K, tree<K,V,P>, P > {
typedef typename std::map< K, tree<K,V,P>, P > map;
template< typename T >
T zero() const {
return std::pair<T,T>().first;
}
template<typename T>
T &invalid() const {
static T t;
return t = T(), t;
}
V value;
tree *parent;
public:
tree() : map(), value(zero<V>()) {
parent = this;
}
tree( const tree &t ) : map(), value(zero<V>()) {
parent = this;
operator=(t);
}
// tree clone
tree &operator=( const tree &t ) {
if( this != &t ) {
this->clear();
get() = zero<V>();
operator+=(t);
}
return *this;
}
// tree merge
tree &operator+=( const tree &t ) {
if( this != &t ) {
for( typename tree::const_iterator it = t.begin(), end = t.end(); it != end; ++it ) {
this->map::insert( *it );
}
get() = t.get();
}
return *this;
}
// tree search ; const safe find: no insertions on new searches
const tree &at( const K &t ) const {
typename map::const_iterator find = this->find( t );
return find != this->end() ? find->second : invalid<tree>();
}
// tree insertion
tree &insert( const K &t ) {
map &children = *this;
( children[t] = children[t] ).parent = this;
return children[t];
}
tree &erase( const K &t ) {
typename map::iterator find = this->find(t);
if( find != this->end() ) this->map::erase(t);
return *this;
}
// recursive values
V &get() {
return value;
}
const V &get() const {
return value;
}
template<typename other>
tree &set( const other &t ) {
get() = t;
return *this;
}
template<typename other>
tree &setup( const other &t ) {
if( !is_root() ) {
up().set(t).setup(t);
}
return *this;
}
template<typename other>
tree &setdown( const other &t ) {
for( typename tree::iterator it = this->begin(), end = this->end(); it != end; ++it ) {
it->second.set(t).setdown( t );
}
return *this;
}
V getdown() const {
V value = get();
for( typename tree::const_iterator it = this->begin(), end = this->end(); it != end; ++it ) {
value += it->second.getdown();
}
return value;
}
// sugars
tree &clone( const tree &t ) {
return operator=( t );
}
tree &assign( const tree &t ) {
return operator=( t );
}
template<typename other>
tree &operator=( const other &t ) {
return set(t);
}
template<typename other>
tree &operator+=( const other &t ) {
return get() += t, *this;
}
tree &merge( const tree &t ) {
return operator +=( t );
}
tree &operator[]( const K &t ) {
return insert( t );
}
const tree &operator()( const K &t ) const {
return at( t );
}
bool empty( const K &t ) const { // @todo: subempty
return this->find(t) == this->end();
}
bool has( const K &t ) const {
return !empty( t );
}
bool is_valid() const {
return this != &invalid<tree>();
}
bool operator!() const {
return !is_valid();
}
const map &children() const {
return *this;
}
map &children() {
return *this;
}
tree &up() {
return *parent;
}
const tree &up() const {
return *parent;
}
bool is_root() const {
return parent == this;
}
const tree &root() const {
if( !is_root() ) return parent->root();
return *this;
}
tree &root() {
if( !is_root() ) return parent->root();
return *this;
}
// tools
template<typename ostream>
void csv( ostream &cout = std::cout, const std::string &prefix = std::string(), unsigned depth = 0 ) const {
for( typename tree::const_iterator it = this->begin(), end = this->end(); it != end; ++it ) {
cout << prefix << "/" << it->first << "," << it->second.get() << std::endl;
std::stringstream ss;
ss << prefix << "/" << it->first;
it->second.csv( cout, ss.str(), depth + 1 );
}
}
std::string as_csv() const {
std::stringstream ss;
return csv( ss ), ss.str();
}
template<typename ostream>
void print( ostream &cout = std::cout, unsigned depth = 0 ) const {
std::string tabs( depth, '\t' );
for( typename tree::const_iterator it = this->begin(), end = this->end(); it != end; ++it ) {
cout << tabs << "[" << this->size() << "] " << it->first << " (" << it->second.get() << ")";
if( !OAK_VERBOSE ) cout << std::endl;
else cout << ".t=" << this << ",.p=" << parent << std::endl;
it->second.print( cout, depth + 1 );
}
}
template<typename U, typename ostream>
void print( const std::map< K, U > &tmap, ostream &cout, unsigned depth = 0 ) const {
std::string tabs( depth, '\t' );
for( typename tree::const_iterator it = this->begin(), end = this->end(); it != end; ++it ) {
cout << tabs << "[" << this->size() << "] " << tmap.find( it->first )->second << " (" << it->second.get() << ")";
if( !OAK_VERBOSE ) cout << std::endl;
else cout << ".t=" << this << ",.p=" << parent << std::endl;
it->second.print( tmap, cout, depth + 1 );
}
}
template<typename ostream>
inline friend ostream &operator<<( ostream &os, const tree &self ) {
return self.print( os ), os;
}
template<typename U>
tree<U> rekey( const std::map< K, U > &map ) const {
tree<U> utree;
for( typename tree::const_iterator it = this->begin(), end = this->end(); it != end; ++it ) {
typename std::map< K, U >::const_iterator find = map.find( it->first );
assert( find != map.end() );
utree[ find->second ] += it->second.rekey( map );
utree[ find->second ].get() = it->second.get();
}
return utree;
}
template<typename U>
tree<U> rekey( const std::map< U, K > &map ) const {
// this could be faster
tree<U> utree;
for( typename std::map< U, K >::const_iterator it = map.begin(), end = map.end(); it != end; ++it ) {
typename tree::const_iterator find = this->find( it->second );
if( find == this->end() ) continue;
utree[ it->first ] += find->second.rekey( map );
utree[ it->first ].get() = find->second.get();
}
return utree;
}
tree collapse() const {
tree t;
if( this->size() == 1 ) {
return this->begin()->second.collapse();
} else {
for( typename tree::const_iterator it = this->begin(), end = this->end(); it != end; ++it ) {
t[ it->first ] += it->second.collapse();
t[ it->first ].get() = it->second.get();
}
}
return t;
}
V refresh() {
V value = !this->size() ? get() : zero<V>();
for( typename tree::iterator it = this->begin(), end = this->end(); it != end; ++it ) {
value += it->second.refresh();
}
return get() = value;
}
// c++03 case, user defined operator()
// c++11 case, accept lambdas as arguments
template<typename T>
tree &walk( const T &predicate ) {
for( typename tree::iterator it = this->begin(), it_next = it; it != this->end(); it = it_next) {
++it_next;
if( predicate(*this, it) ) {
it->second.walk( predicate );
}
}
return *this;
}
// alias
template<typename T>
tree &walk() {
return walk( T() );
}
};
}
// }
// external; macros, OS utils. Here is where the fun starts {
# define HEAL_MAX_TRACES kTraceyMaxStacktraces
# define heal tracey_heal
//#line 1 "heal.cpp"
// Heal is a lightweight C++ framework to aid and debug applications.
// - rlyeh, zlib/libpng licensed // ~listening to Kalas - Monuments to Ruins
// Callstack code is based on code by Magnus Norddahl (See http://goo.gl/LM5JB)
// Mem/CPU OS code is based on code by David Robert Nadeau (See http://goo.gl/8P5Jqv)
// Distributed under Creative Commons Attribution 3.0 Unported License
// http://creativecommons.org/licenses/by/3.0/deed.en_US
// A few tweaks before loading STL on MSVC
// This improves stack unwinding.
#ifdef _SECURE_SCL
#undef _SECURE_SCL
#endif
#define _SECURE_SCL 0
#ifdef _ITERATOR_DEBUG_LEVEL
#undef _ITERATOR_DEBUG_LEVEL
#define _ITERATOR_DEBUG_LEVEL 1
#endif
//#define _HAS_ITERATOR_DEBUGGING 0
// Standard headers
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
// System headers
#ifdef _WIN32
//# define UNICODE
//# define _UNICODE
# include <winsock2.h>
# include <ws2tcpip.h>
# include <windows.h>
# include <commctrl.h>
# pragma comment(lib, "comctl32.lib")
# if defined _M_IX86
# pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
# elif defined _M_IA64
# pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
# elif defined _M_X64
# pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
# else
# pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
# endif
# // unwinding
# if defined(DEBUG) || defined(_DEBUG)
# include <CrtDbg.h>
# endif
# include <DbgHelp.h>
# pragma comment(lib, "dbghelp.lib")
# ifndef TD_SHIELD_ICON
# define TD_SHIELD_ICON MAKEINTRESOURCEW(-4)
# endif
#else
# include <unistd.h>
# include <signal.h>
# include <sys/time.h>
# include <sys/types.h>
// --
# if defined(HAVE_SYS_SYSCTL_H) && \
!defined(_SC_NPROCESSORS_ONLN) && !defined(_SC_NPROC_ONLN)
# include <sys/param.h>
# include <sys/sysctl.h>
# endif
// --
# include <execinfo.h>
// --
# include <cxxabi.h>
#endif
#ifdef __MINGW32__
#define backtrace(a,b) 0
#define backtrace_symbols(a,b) 0
#endif
// API
//#line 1 "heal.hpp"
// -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< --
// std 11 <-> 03/w-boost bridge compatiblity layer, plus a few macro utils.
// - rlyeh, zlib/libpng licensed.
#ifndef __BRIDGE_HPP__
#define __BRIDGE_HPP__
#ifdef __SSE__
# define BOOST_HAS_INT128 1
# include <xmmintrin.h>
#endif
#if (__cplusplus < 201103L && !defined(_MSC_VER)) || (defined(_MSC_VER) && (_MSC_VER < 1700)) || (defined(__GLIBCXX__) && __GLIBCXX__ < 20130322L)
# define BRIDGE_VERSION 2003
# include <boost/functional.hpp> // if old libstdc++ or msc libs are found, use boost::function
# include <boost/function.hpp> //
# include <boost/thread.hpp> // and boost::thread
# include <boost/cstdint.hpp>
# include <boost/type_traits.hpp>
# include <boost/bind.hpp>
# include <boost/bind/placeholders.hpp>
namespace std {
namespace placeholders {
//...
}
using namespace boost;
}
#else
# define BRIDGE_VERSION 2011
# include <functional> // else assume modern c++11 and use std::function<> instead
# include <mutex> // and std::mutex
# include <thread> // and std::thread
# include <cstdint>
#endif
#if BRIDGE_VERSION >= 2011
#define $cpp11 $yes
#define $cpp03 $no
#else
#define $cpp11 $no
#define $cpp03 $yes
#endif
// Thread Local Storage
#if defined(__MINGW32__) || defined(__SUNPRO_C) || defined(__xlc__) || defined(__GNUC__) || defined(__clang__) || defined(__GNUC__) // __INTEL_COMPILER on linux
// MingW, Solaris Studio C/C++, IBM XL C/C++,[3] GNU C,[4] Clang[5] and Intel C++ Compiler (Linux systems)
# define $tls(x) __thread x
#else
// Visual C++,[7] Intel C/C++ (Windows systems),[8] C++Builder, and Digital Mars C++
# define $tls(x) __declspec(thread) x
#endif
// OS utils. Here is where the fun starts... good luck
#define $quote(...) #__VA_ARGS__
#define $comment(...) $no
#define $uncomment(...) $yes
#define $yes(...) __VA_ARGS__
#define $no(...)
#define $on(v) (0 v(+1)) // usage: #if $on($msvc)
#define $is $on // usage: #if $is($debug)
#define $has(...) $clang(__has_feature(__VA_ARGS__)) $celse(__VA_ARGS__) // usage: #if $has(cxx_exceptions)
#if defined(_WIN32)
# define $windows $yes
# define $welse $no
#else
# define $windows $no
# define $welse $yes
#endif
#ifdef __APPLE__
# define $apple $yes
# define $aelse $no
#else
# define $apple $no
# define $aelse $yes
#endif
#ifdef __linux__
# define $linux $yes
# define $lelse $no
#else
# define $linux $no
# define $lelse $yes
#endif
// Compiler utils
#if ULONG_MAX == 4294967295
# define $bits64 $yes
# define $bits32 $no
#else
# define $bits64 $no
# define $bits32 $yes
#endif
#if defined(NDEBUG) || defined(_NDEBUG) || defined(RELEASE)
# define $release $yes
# define $debug $no
#else
# define $release $no
# define $debug $yes
#endif
#if defined(NDEVEL) || defined(_NDEVEL) || defined(PUBLIC)
# define $public $yes
# define $devel $no
#else
# define $public $no
# define $devel $yes
#endif
#if defined(__GNUC__) || defined(__MINGW32__)
# define $gnuc $yes
# define $gelse $no
#else
# define $gnuc $no
# define $gelse $yes
#endif
#ifdef _MSC_VER
# define $msvc $yes
# define $melse $no
#else
# define $msvc $no
# define $melse $yes
#endif
#ifdef __clang__
# define $clang $yes
# define $celse $no
#else
# define $clang $no
# define $celse $yes
#endif
#if $on($msvc) || $on($gnuc) || $on($clang)
# define $undefined_compiler $no
#else
# define $undefined_compiler $yes
#endif
#if $on($windows) || $on($linux) || $on($apple)
# define $undefined_os $no
#else
# define $undefined_os $yes
#endif
#if $on($gnuc) || $on($clang)
# define $likely(expr) (__builtin_expect(!!(expr), 1))
# define $unlikely(expr) (__builtin_expect(!!(expr), 0))
#else
# define $likely(expr) ((expr))
# define $unlikely(expr) ((expr))
#endif
// try to detect if exceptions are enabled...
#if (defined(_HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS > 0)) || \
(defined(_STLP_USE_EXCEPTIONS) && (_STLP_USE_EXCEPTIONS > 0)) || \
(defined(HAVE_EXCEPTIONS)) || \
(defined(__EXCEPTIONS)) || \
(defined(_CPPUNWIND)) || \
($has(cxx_exceptions)) /*(__has_feature(cxx_exceptions))*/
# define $throw $yes
# define $telse $no
#else
# define $throw $no
# define $telse $yes
#endif
// create a $warning(...) macro
// usage: $warning("this is shown at compile time")
#if $on($msvc)
# define $warning(msg) __pragma( message( msg ) )
#elif $on($gnuc) || $on($clang)
# define $$warning$impl(msg) _Pragma(#msg)
# define $warning(msg) $$warning$impl( message( msg ) )
#else
# define $warning(msg)
#endif
// create a $warning(...) macro
// usage: $warning("this is shown at compile time")
#define $$todo$stringize$impl(X) #X
#define $$todo$stringize(X) $$todo$stringize$impl(X)
#define $todo(...) $warning( __FILE__ "(" $$todo$stringize(__LINE__)") : $todo - " #__VA_ARGS__ " - [ "__func__ " ]" )
#endif // __BRIDGE_HPP__
// -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< --
// Heal is a lightweight C++ framework to aid and debug applications.
// - rlyeh, zlib/libpng licensed // ~listening to Kalas - Monuments to Ruins
// Callstack code is based on code by Magnus Norddahl (See http://goo.gl/LM5JB)
// Mem/CPU OS code is based on code by David Robert Nadeau (See http://goo.gl/8P5Jqv)
// Distributed under Creative Commons Attribution 3.0 Unported License
// http://creativecommons.org/licenses/by/3.0/deed.en_US
#ifndef __HEALHPP__
#define __HEALHPP__
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <iostream>
#include <istream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#define HEAL_VERSION "1.0.0" // (2015/10/01) Semantic versioning adherence and clean ups
/* public API */
namespace heal {
typedef std::function< int( const std::string &in ) > heal_callback_in;
extern std::vector< heal_callback_in > warns;
extern std::vector< heal_callback_in > fails;
void warn( const std::string &error );
void fail( const std::string &error );
void add_worker( heal_callback_in fn );
void die( const std::string &reason, int errorcode = -1 );
void die( int errorcode = -1, const std::string &reason = std::string() );
void breakpoint();
bool debugger( const std::string &reason = std::string() );
void alert();
void alert( const char *t, const std::string &title = std::string() );
void alert( const std::string &t, const std::string &title = std::string() );
void alert( const std::istream &is, const std::string &title = std::string() );
void alert( const size_t &t, const std::string &title = std::string() );
void alert( const double &t, const std::string &title = std::string() );
void alert( const float &t, const std::string &title = std::string() );
void alert( const int &t, const std::string &title = std::string() );
void alert( const char &t, const std::string &title = std::string() );
void alert( const bool &t, const std::string &title = std::string() );
void errorbox( const std::string &body = std::string(), const std::string &title = std::string() );
std::string prompt( const std::string ¤t_value = std::string(), const std::string &title = std::string(), const std::string &caption = std::string() );
bool is_debug();
bool is_release();
bool is_asserting();
bool is_devel();
bool is_public();
#ifndef HEAL_MAX_TRACES
#define HEAL_MAX_TRACES 128
#endif
struct callstack /* : public std::vector<const void*> */ {
enum { max_frames = HEAL_MAX_TRACES };
std::vector<void *> frames;
callstack( bool autosave = false );
size_t space() const;
void save( unsigned frames_to_skip = 0 );
std::vector<std::string> unwind( unsigned from = 0, unsigned to = ~0 ) const;
std::vector<std::string> str( const char *format12 = "#\1 \2\n", size_t skip_begin = 0 ) const;
std::string flat( const char *format12 = "#\1 \2\n", size_t skip_begin = 0 ) const;
};
template<typename T>
static inline
std::string lookup( T *ptr ) {
callstack cs;
cs.frames.push_back( (void *)ptr );
std::vector<std::string> stacktrace = cs.unwind();
return stacktrace.size() ? stacktrace[0] : std::string("????");
}
std::string demangle( const std::string &mangled );
std::vector<std::string> stacktrace( const char *format12 = "#\1 \2\n", size_t skip_initial = 0 );
std::string stackstring( const char *format12 = "#\1 \2\n", size_t skip_initial = 0 );
std::string hexdump( const void *data, size_t num_bytes, const void *self = 0 );
template<typename T> inline std::string hexdump( const T& obj ) {
return hexdump( obj.data(), obj.size() * sizeof(*obj.begin()), &obj );
}
$cpp11(
template<> inline std::string hexdump( const std::nullptr_t &obj ) {
return hexdump( 0,0,0 );
}
)
template<> inline std::string hexdump( const char &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const short &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const long &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const long long &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const unsigned char &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const unsigned short &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const unsigned long &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const unsigned long long &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const float &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const double &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}
template<> inline std::string hexdump( const long double &obj ) {
return hexdump( &obj, sizeof(obj), &obj );
}/*
template<size_t N> inline std::string hexdump( const char (&obj)[N] ) {
return hexdump( &obj, sizeof(char) * N, &obj );
}*/
template<typename T> inline std::string hexdump( const T* obj ) {
if( !obj ) return hexdump(0,0,0);
return hexdump( *obj );
}
template<> inline std::string hexdump( const char *obj ) {
if( !obj ) return hexdump(0,0,0);
return hexdump( std::string(obj) );
}
std::string timestamp();
}
#endif // __HEALHPP__
namespace heal {
// sfstring is a safe string replacement that does not rely on stringstream
// this is actually safer on corner cases, like crashes, exception unwinding and in exit conditions
class sfstring : public std::string
{
public:
// basic constructors
sfstring() : std::string()
{}
template<size_t N>
sfstring( const char (&cstr)[N] ) : std::string( cstr )
{}
// constructor sugars
#if 0
// version that may crash on /MT on destructors
// (just because it depends on std::locale which may be deinitialized before crashing code)
template <typename T>
/* explicit */ sfstring( const T &t ) : std::string()
{
std::stringstream ss;
ss.precision( std::numeric_limits< long double >::digits10 + 1 );
if( ss << t )
this->assign( ss.str() );
}
#else
template<typename T>
sfstring( const T &t ) : std::string( std::string(t) )
{}
sfstring( const std::string &t ) : std::string( t )
{}
sfstring( const int &t ) : std::string() {
char buf[128];
if( sprintf(buf, "%d", t ) > 0 ) this->assign(buf);
}
sfstring( const uint16_t &t ) : std::string() {
char buf[128];
if( sprintf(buf, "%" SCNu16, t ) > 0 ) this->assign(buf);
}
sfstring( const uint32_t &t ) : std::string() {
char buf[128];
if( sprintf(buf, "%" SCNu32, t ) > 0 ) this->assign(buf);
}
sfstring( const uint64_t &t ) : std::string() {
char buf[128];
if( sprintf(buf, "%" SCNu64, t ) > 0 ) this->assign(buf);
}
sfstring( const float &t ) : std::string() {
char buf[128];
if( sprintf(buf, "%f", t ) > 0 ) this->assign(buf);
}
sfstring( const double &t ) : std::string() {
char buf[128];
if( sprintf(buf, "%lf", t ) > 0 ) this->assign(buf);
}
$msvc(
sfstring( const DWORD &t ) : std::string() {
char buf[128];
if( sprintf(buf, "%" SCNu32, t ) > 0 ) this->assign(buf);
}
)
sfstring( char *t ) : std::string( t ? t : "" )
{}
sfstring( const char *t ) : std::string( t ? t : "" )
{}
sfstring( void *t ) : std::string() {
char buf[128];
if( sprintf(buf, "%p", t ) > 0 ) this->assign(buf);
}
sfstring( const void *t ) : std::string() {
char buf[128];
if( sprintf(buf, "%p", t ) > 0 ) this->assign(buf);
}
#endif
// extended constructors; safe formatting
private:
template<unsigned N>
std::string &safefmt( const std::string &fmt, std::string (&t)[N] ) {
for( std::string::const_iterator it = fmt.begin(), end = fmt.end(); it != end; ++it ) {
unsigned index(*it);
if( index <= N ) t[0] += t[index];
else t[0] += *it;
}
return t[0];
}
public:
template< typename T1 >
sfstring( const std::string &fmt, const T1 &t1 ) : std::string() {
std::string t[] = { std::string(), sfstring(t1) };
assign( safefmt( fmt, t ) );
}
template< typename T1, typename T2 >
sfstring( const std::string &fmt, const T1 &t1, const T2 &t2 ) : std::string() {
std::string t[] = { std::string(), sfstring(t1), sfstring(t2) };
assign( safefmt( fmt, t ) );
}
template< typename T1, typename T2, typename T3 >
sfstring( const std::string &fmt, const T1 &t1, const T2 &t2, const T3 &t3 ) : std::string() {
std::string t[] = { std::string(), sfstring(t1), sfstring(t2), sfstring(t3) };
assign( safefmt( fmt, t ) );
}
template< typename T1, typename T2, typename T3, typename T4 >
sfstring( const std::string &fmt, const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4 ) : std::string() {
std::string t[] = { std::string(), sfstring(t1), sfstring(t2), sfstring(t3), sfstring(t4) };
assign( safefmt( fmt, t ) );
}
template< typename T1, typename T2, typename T3, typename T4, typename T5 >
sfstring( const std::string &fmt, const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4, const T5 &t5 ) : std::string() {
std::string t[] = { std::string(), sfstring(t1), sfstring(t2), sfstring(t3), sfstring(t4), sfstring(t5) };
assign( safefmt( fmt, t ) );
}
template< typename T1, typename T2, typename T3, typename T4, typename T5, typename T6 >
sfstring( const std::string &fmt, const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4, const T5 &t5, const T6 &t6 ) : std::string() {
std::string t[] = { std::string(), sfstring(t1), sfstring(t2), sfstring(t3), sfstring(t4), sfstring(t5), sfstring(t6) };
assign( safefmt( fmt, t ) );
}