-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug_new.cpp
873 lines (757 loc) · 16.9 KB
/
debug_new.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
/*
* debug_new.cpp 1.11 2003/07/03
*
* Implementation of debug versions of new and delete to check leakage
*
*
*/
#include <new>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#ifdef WIN32
#include <Windows.h>
#include <process.h>
#else
#include <pthread.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#endif
//#include "avl.h"
#ifdef _MSC_VER
#pragma warning(disable: 4073)
#pragma init_seg(lib)
#endif
#ifndef DEBUG_NEW_HASHTABLESIZE
//#define DEBUG_NEW_HASHTABLESIZE 16384
#define DEBUG_NEW_HASHTABLESIZE 0x40
#endif
#ifndef DEBUG_NEW_HASH
#define DEBUG_NEW_HASH(p) (((unsigned)(p) >> 8) % DEBUG_NEW_HASHTABLESIZE)
#endif
// The default behaviour now is to copy the file name, because we found
// that the exit leakage check cannot access the address of the file
// name sometimes (in our case, a core dump will occur when trying to
// access the file name in a shared library after a SIGINT).
#ifndef DEBUG_NEW_FILENAME_LEN
#define DEBUG_NEW_FILENAME_LEN 256
#endif
#if DEBUG_NEW_FILENAME_LEN == 0 && !defined(DEBUG_NEW_NO_FILENAME_COPY)
#define DEBUG_NEW_NO_FILENAME_COPY
#endif
#ifndef DEBUG_NEW_NO_FILENAME_COPY
#include <string.h>
#endif
struct new_ptr_list_t
{
new_ptr_list_t* next;
#ifdef DEBUG_NEW_NO_FILENAME_COPY
const char* file;
#else
char file[DEBUG_NEW_FILENAME_LEN];
#endif
int line;
size_t size;
void* memory;
void* caller_add;
};
struct prt_in_avl_table
{
new_ptr_list_t* ptr;
int repeatNum;
prt_in_avl_table* next;
};
static new_ptr_list_t* new_ptr_list[DEBUG_NEW_HASHTABLESIZE] = {0};
bool new_autocheck_flag = true;
bool isPrint = false;
bool isRecord = true;
bool isPrintUnknow = true;
#ifdef WIN32
CRITICAL_SECTION global_lock;
#else
pthread_mutex_t global_lock;
#endif
void initial_locker()
{
#ifdef WIN32
InitializeCriticalSection(&global_lock);
#else
pthread_mutex_init(&global_lock, NULL);
#endif
}
void thread_lock()
{
#ifdef WIN32
EnterCriticalSection(&global_lock);
#else
pthread_mutex_lock(&global_lock);
#endif
}
void thread_unlock()
{
#ifdef WIN32
LeaveCriticalSection(&global_lock);
#else
pthread_mutex_unlock(&global_lock);
#endif
}
void uninitial_locker()
{
#ifdef WIN32
DeleteCriticalSection(&global_lock);
#else
pthread_mutex_destroy(&global_lock);
#endif
}
class Locker
{
public:
Locker(){thread_lock();}
~Locker(){thread_unlock();}
};
bool get_isRecode()
{
Locker lock;
return isRecord;
}
void set_isRecode(bool _is)
{
Locker lock;
isRecord = _is;
}
FILE* logFile = NULL;
FILE* outFile = NULL;
void initialLog(FILE* file)
{
logFile = file;
}
void initialOutFile(FILE* file)
{
outFile = file;
}
void uninitialLog()
{
if (logFile != NULL)
{
fclose(logFile);
logFile = NULL;
}
}
void uninitialOutFile()
{
if (outFile != NULL)
{
fclose(outFile);
outFile = NULL;
}
}
#ifdef WIN32
unsigned int WINAPI thread_file_monitor(void * tid);
#else
void* thread_file_monitor(void* parm);
#endif
void initial()
{
initial_locker();
FILE* log = fopen("/tmp/memory_check_log.txt", "ab+");
//initialLog(stdout);
initialLog(log);
FILE* outf = fopen("/tmp/memory_allocate_log.txt", "ab+");
initialOutFile(outf);
#ifdef WIN32
unsigned int thread_id;
uintptr_t thread_monit = _beginthreadex(NULL, 0, thread_file_monitor, NULL, 0, &thread_id);
#else
pthread_t thread_monit;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&thread_monit, &attr, thread_file_monitor, NULL);
#endif
}
void uninitial()
{
uninitial_locker();
uninitialLog();
uninitialOutFile();
}
int string_hash(char* stringLine)
{
int h = 0;
if (stringLine != NULL)
{
if (h == 0) {
int off = 0;
char * val = stringLine;
int len = strlen(stringLine);
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
}
}
return h;
}
void start_record()
{
set_isRecode(true);
}
void clean_record()
{
Locker lock;
for (int i = 0; i < DEBUG_NEW_HASHTABLESIZE; ++i)
{
new_ptr_list_t* ptr = new_ptr_list[i];
if (ptr == NULL)
continue;
while (ptr)
{
new_ptr_list_t* ptr_last = ptr;
ptr = ptr->next;
free(ptr_last);
if (ptr_last == new_ptr_list[i])
{
new_ptr_list[i] = NULL;
}
}
}
}
void stop_record()
{
if (!get_isRecode())
{
return;
}
set_isRecode(false);
clean_record();
}
/*************************************************** Don't implement collecting where leaked in the same line code
libavl_allocator* avlAllocator = NULL;
avl_table * AVL_Table = NULL;
int comparer(const void *avl_a, const void *avl_b, void *avl_param)
{
return (int)avl_a - (int)avl_b;
}
void destory (void *avl_item, void *avl_param)
{
while (avl_item != 0)
{
}
}
void create_record()
{
avlAllocator = (libavl_allocator *)malloc(sizeof(libavl_allocator));
avlAllocator->libavl_malloc = avl_malloc;
avlAllocator->libavl_free = avl_free;
AVL_Table = avl_create(comparer, NULL, avlAllocator);
}
void insert_in_recode(void * item)
{
prt_in_avl_table* ptr_in = avl_find(AVL_Table, item);
if (ptr_in == NULL)
{
ptr_in = avl_insert(AVL_Table, item);
}
ptr_in->repeatNum ++;
}
void trace_record()
{
for (int i = 0; i < DEBUG_NEW_HASHTABLESIZE; ++i)
{
new_ptr_list_t* ptr = new_ptr_list[i];
if (ptr == NULL)
continue;
while (ptr)
{
avl_insert (AVL_Table, ptr);
ptr = ptr->next;
}
}
}
*****************************************************************/
void printLog(FILE* file, const char* format, ...)
{
va_list arg_list;
char msg[2048];
va_start(arg_list, format);
vsprintf(msg, format, arg_list);
va_end(arg_list);
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p = localtime(&timep); /*取得当地时间*/
fprintf(file, "[%d/%.2d/%.2d %s %.2d:%.2d:%.2d] ",
(1900+p->tm_year),( 1+p->tm_mon), p->tm_mday, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
fprintf(file, msg);
fflush(file);
}
bool check_leaks()
{
thread_lock();
printLog(logFile, "Check Leaks ===================================================>\n");
bool fLeaked = false;
for (int i = 0; i < DEBUG_NEW_HASHTABLESIZE; ++i)
{
new_ptr_list_t* ptr = new_ptr_list[i];
if (ptr == NULL)
continue;
//printf("ptr ---> %p (size %u, %s:%d)\n", ptr->memory, ptr->size, ptr->file, ptr->line);
//printf("isPrintUnkonw = %d\n", isPrintUnknow);
fLeaked = true;
while (ptr)
{
if(true == isPrintUnknow || 0 != ptr->line)
{
fprintf(logFile, "%p ===> Memory Stay ------> %p (size %3u, %s:%-4d)\n",
ptr->caller_add,
(char*)ptr->memory,
ptr->size,
ptr->file,
ptr->line);
}
ptr = ptr->next;
}
}
printLog(logFile, "---------------------------\n");
fflush(logFile);
thread_unlock();
if (fLeaked)
return true;
else
return false;
}
void* record_new(size_t size, const char* file, int line, void* add)
{
if(0 == size)
return NULL;
thread_lock();
new_ptr_list_t* ptr = (new_ptr_list_t*)malloc(sizeof(new_ptr_list_t));
void* memory_ptr = malloc(size);
if (ptr == NULL || NULL == memory_ptr)
{
printLog(outFile, "new: !!!ERROR!!! out of memory when allocating %u bytes\n", size);
abort();
//while(1);
}
ptr->memory = memory_ptr;
size_t hash_index = DEBUG_NEW_HASH(memory_ptr);
ptr->next = new_ptr_list[hash_index];
#ifdef DEBUG_NEW_NO_FILENAME_COPY
ptr->file = file;
#else
strncpy(ptr->file, file, DEBUG_NEW_FILENAME_LEN - 1);
ptr->file[DEBUG_NEW_FILENAME_LEN - 1] = '\0';
#endif
ptr->line = line;
ptr->size = size;
ptr->caller_add = add;
new_ptr_list[hash_index] = ptr;
if (isPrint)
{
if(0 != line || true == isPrintUnknow)
{
printLog(outFile, "%p ===> new: allocated 0x%p (size %u, %s:%d)\n",add, memory_ptr, size, file, line);
fflush(outFile);
}
}
thread_unlock();
return memory_ptr;
}
void* operator new(size_t size, const char* file, int line, void* add)
{
if (get_isRecode())
{
return record_new(size, file, line, add);
}
else
{
return malloc(size);
}
}
void* operator new[](size_t size, const char* file, int line, void* add)
{
return operator new(size, file, line, add);
}
void* operator new(size_t size)
{
void* add = __builtin_return_address(0);
return operator new(size, "<Unknown>", 0, add);
}
void* operator new[](size_t size)
{
void* add = __builtin_return_address(0);
return operator new(size, "<Unknown>", 0, add);
}
void* operator new(size_t size, const std::nothrow_t&) throw()
{
void* add = __builtin_return_address(0);
return operator new(size, "<Unknown>", 0, add);
}
void* operator new[](size_t size, const std::nothrow_t&) throw()
{
void* add = __builtin_return_address(0);
return operator new(size, "<Unknown>", 0, add);
}
void recode_delete(void* pointer)
{
if (pointer == NULL)
return;
thread_lock();
size_t hash_index = DEBUG_NEW_HASH(pointer);
new_ptr_list_t* ptr = new_ptr_list[hash_index];
new_ptr_list_t* ptr_last = NULL;
while (ptr)
{
//if ((char*)ptr + sizeof(new_ptr_list_t) == pointer)
if (ptr->memory == pointer)
{
if (isPrint)
{
if(0 != ptr->line || true == isPrintUnknow)
{
printLog(outFile, "%p ===> delete: freeing 0x%p (size %u, %s:%d)\n", ptr->caller_add, pointer, ptr->size, ptr->file, ptr->line);
fflush(outFile);
}
}
if (ptr_last == NULL)
new_ptr_list[hash_index] = ptr->next;
else
ptr_last->next = ptr->next;
free(ptr->memory);
free(ptr);
ptr = NULL;
thread_unlock();
return;
}
ptr_last = ptr;
ptr = ptr->next;
}
/*
printLog(outFile, "delete: !!!ERRER!!! invalid pointer 0x%p\n", pointer);
thread_unlock();
abort();
*/
free(pointer);
thread_unlock();
}
void operator delete(void* pointer)
{
if (pointer != NULL)
{
recode_delete(pointer);
/*
if (get_isRecode())
{
recode_delete(pointer);
}
else
{
free(pointer);
}
*/
}
}
void operator delete[](void* pointer)
{
operator delete(pointer);
}
// Some older compilers like Borland C++ Compiler 5.5.1 and Digital Mars
// Compiler 8.29 do not support placement delete operators.
// NO_PLACEMENT_DELETE needs to be defined when using such compilers.
// Also note that in that case memory leakage will occur if an exception
// is thrown in the initialization (constructor) of a dynamically
// created object.
#ifndef NO_PLACEMENT_DELETE
void operator delete(void* pointer, const char* file, int line)
{
//if (new_verbose_flag)
// printf("info: exception thrown on initializing object at %p (%s:%d)\n", pointer, file, line);
operator delete(pointer);
}
void operator delete[](void* pointer, const char* file, int line)
{
operator delete(pointer, file, line);
}
void operator delete(void* pointer, const std::nothrow_t&)
{
operator delete(pointer, "<Unknown>", 0);
}
void operator delete[](void* pointer, const std::nothrow_t&)
{
operator delete(pointer, std::nothrow);
}
#endif // NO_PLACEMENT_DELETE
// Proxy class to automatically call check_leaks if new_autocheck_flag is set
class new_check_t
{
public:
new_check_t() {initial();}
~new_check_t()
{
if (new_autocheck_flag)
{
// Check for leakage.
// If any leaks are found, set new_verbose_flag so that any
// delete operations in the destruction of global/static
// objects will display information to compensate for
// possible false leakage reports.
//if (check_leaks())
// new_verbose_flag = true;
}
}
};
static new_check_t new_check_object;
void ShowMemoryLeak()
{
if (get_isRecode())
{
check_leaks();
}
else
{
printLog(logFile, "Memory Record is not opened!\n");
}
}
int command_parser(char* str)
{
printf("read msg:%s ; compare = %d\n", str, strcmp(str, "print_on"));
if(0 == strcmp(str, "start_record"))
{
start_record();
return 1;
}
if(0 == strcmp(str, "stop_record"))
{
stop_record();
return 1;
}
if(0 == strcmp(str, "memory_check"))
{
ShowMemoryLeak();
//check_leaks();
return 1;
}
if(0 == strcmp(str, "print_on"))
{
isPrint = true;
return 1;
}
if(0 == strcmp(str, "print_off"))
{
isPrint = false;
return 1;
}
if(0 == strcmp(str, "print_unknow_off"))
{
isPrintUnknow= false;
return 1;
}
if(0 == strcmp(str, "print_unkonw_on"))
{
isPrintUnknow = true;
return 1;
}
printf("Nothing has bean done!\n");
return 0;
}
void file_dealer(char* filename)
{
#ifdef WIN32
FILE* file = fopen(filename, "r");
printf("openning file:%s\n" , filename);
if(NULL == file)
{
printf("%s cannot open\n" , filename);
return;
}
char mystring [256];
while(NULL != fgets (mystring , 256 , file)){
if(strlen(mystring) > 0)
mystring[strlen(mystring)-1] = 0;
command_parser(mystring);
}
fclose(file);
#else
char filepath[256] = {0};
sprintf(filepath, "/tmp/memory_leak_check/%s", filename);
FILE* file = fopen(filepath, "r");
printf("openning file:%s\n" , filepath);
if(NULL == file)
{
printf("%s cannot open\n" , filepath);
return;
}
char mystring [256];
while(NULL != fgets (mystring , 256 , file)){
if(strlen(mystring) > 0)
mystring[strlen(mystring)-1] = 0;
command_parser(mystring);
}
fclose(file);
#endif
}
#define EVENT_SIZE ( sizeof (struct inotify_event) )+
#define BUF_LEN ( 1024 * ( EVENT_SIZE 16 ) )
extern "C" int read(int, char*, size_t);
extern "C" void close(int);
void file_monitor()
{
#ifdef WIN32
FILE* infile = fopen("C:\\memory_ckeck", "r");
if (NULL == infile)
{
infile = fopen("C:\\memory_ckeck", "w");
}
fclose(infile);
while(1)
{
file_dealer("C:\\memory_ckeck");
Sleep(1000);
}
#else
int length, i = 0;
int fd;
int wd;
char buffer[BUF_LEN];
system( "mkdir -p /tmp/memory_leak_check");
system( "touch /tmp/memory_leak_check/memory_check_cmd");
fd = inotify_init();
if ( fd < 0 ) {
return;
}
wd = inotify_add_watch( fd, "/tmp/memory_leak_check", IN_MODIFY | IN_CREATE | IN_DELETE );
while(length = read( fd, buffer, BUF_LEN )){
if ( length < 0 ) {
return;
}
while ( i < length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len ) {
if ( event->mask & IN_CREATE ) {
if ( event->mask & IN_ISDIR ) {
//printf( "The directory %s was created.\n", event->name );
}
else {
//printf( "The file %s was created.\n", event->name );
}
}
else if ( event->mask & IN_DELETE ) {
if ( event->mask & IN_ISDIR ) {
//printf( "The directory %s was deleted.\n", event->name );
}
else {
//printf( "The file %s was deleted.\n", event->name );
}
}
else if ( event->mask & IN_MODIFY ) {
if ( event->mask & IN_ISDIR ) {
//printf( "The directory %s was modified.\n", event->name );
}
else {
printf( "The file %s was modified.\n", event->name );
if( 0 == strcmp(event->name, "memory_check_cmd")){
file_dealer(event->name);
}
}
}
}
i = EVENT_SIZE event->len;
}
i = 0;
}
( void ) inotify_rm_watch( fd, wd );
( void ) close( fd );
#endif
}
static int isStarted = 0;
#define SERVER_PORT 12346
#define MAXBUF 1024
void server_monitor()
{
char buf[MAXBUF + 1];
int sockfd = 0;
int new_fd = 0;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error!\n");
//exit(1);
return;
}
else printf("socket created!\n");
isStarted = 1;
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = PF_INET;
my_addr.sin_port = htons(SERVER_PORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1)
{
perror("bind error!\n");
//exit(1);
return;
}
else printf("binded\n");
if (listen(sockfd, 1) == -1)
{
perror("listen error!\n");
//exit(1);
return;
}
else printf("begin listen prot:%d\n", SERVER_PORT);
unsigned int len = 0;
while(1) {
len = sizeof(struct sockaddr);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &len)) == -1)
{
perror("accept error!\n");
//exit(errno);
return;
}
bzero(buf, MAXBUF + 1);
/* 接收客户端的消息 */
len = recv(new_fd, buf, MAXBUF, 0);
if(len > 0)
{
printf("successfully receive message'%s', Length:%d\n", buf, len);
if(command_parser(buf))
{
len = send(new_fd, "OK", strlen("OK"), 0);
}
else
len = send(new_fd, "Command not right", strlen("Command not right"), 0);
if(len < 0) {
printf("error send:%d, errorNo:'%s'\n", buf, errno, strerror(errno));
}
else printf("Successfully sent!");
}
close(new_fd);
}
close(sockfd);
}
#ifdef WIN32
unsigned int WINAPI thread_file_monitor(void * tid)
#else
void* thread_file_monitor(void* parm)
#endif
{
printf("Monitor Threat Started!.......\n");
if(!isStarted)
{
server_monitor();
}
// while(1)
//{
// file_monitor();
//}
printf("Monitor Threat EXIT!.......\n");
#ifdef WIN32
return 0;
#endif
}