forked from yobotech/WeChatRedEnvelop
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDebug.mm
More file actions
161 lines (140 loc) · 3.85 KB
/
Debug.mm
File metadata and controls
161 lines (140 loc) · 3.85 KB
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
#include <objc/runtime.h>
#include <sys/socket.h>
#include <netinet/in.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#include "Debug.h"
void send_log(const char * msg, int len)
{
struct sockaddr_in si;
memset(&si, 0, sizeof(si));
si.sin_family = AF_INET;
si.sin_addr.s_addr = 0x0100007f;
si.sin_port = htons(5555);
static int udps = -1;
if (udps<0)
udps = socket(AF_INET, SOCK_DGRAM, 0);
if (len < 0) len = (int)strlen(msg);
sendto(udps, msg, len, 0, (struct sockaddr *) &si, sizeof(si));
}
void send_multi_log(const char * p)
{
const char * p1 = p;
for (;;)
{
if (*p1 == 0 || *p1=='\n')
{
if (p1 > p)
send_log(p, int(p1-p) );
p = p1 + 1;
}
if (*p1 == 0) break;
++ p1;
}
}
void sl_printf(const char * fmt, ...)
{
char buf[3000] = {0};
va_list vg;
size_t len = sizeof(buf)-1;
time_t t = time(0);
struct tm ttt = * localtime(&t);
int np = sprintf(buf, "[%02d:%02d:%02d] ", ttt.tm_hour, ttt.tm_min, ttt.tm_sec);
va_start(vg, fmt);
vsnprintf(buf+np, len-np, fmt, vg);
va_end(vg);
send_log(buf);
}
void dump(const char * name, NSString * s)
{
char msg[1000];
if (!s)
sprintf(msg, "%s is <nil>\n", name);
else
snprintf(msg, sizeof(msg)-2, "%s=%s\n", name, [s UTF8String]);
send_log(msg);
}
void dumpi(const char * name, uint64_t code)
{
char msg[1000];
sprintf(msg, "%s is int:%llx\n", name, code);
send_log(msg);
}
int soundAlert()
{
static SystemSoundID sndid = 0;
if (sndid == 0)
{
NSString * sndPath = [[NSBundle mainBundle] pathForResource:@"hongbao" ofType:@"wav"];
if (!sndPath) sndPath = @"/var/root/hongbao.wav";
NSURL *pewPewURL = [NSURL fileURLWithPath:sndPath];
OSStatus st = AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &sndid);
(void)st;
}
if (sndid)
return AudioServicesPlaySystemSound(sndid), 0;
return -2;
}
void uncaughtExceptionHandler(NSException *exception)
{
NSLog(@"CRASH: %@", exception);
NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
}
void testall()
{
static int w = 0;
if (w == 0)
{
w = 1;
const char * fn = "/var/hehe.out";
int fid = open(fn, O_WRONLY|O_CREAT|O_TRUNC);
sl_printf("open: %s: %d %s\n", fn, fid, strerror(errno));
dup2(fid, 1);
dup2(fid, 2);
sl_printf("write: %d\n", write(fid, "hello\n", 6 ));
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
}
}
void dumpClassInfo(id object)
{
Class clazz = [object class];
u_int count;
Ivar* ivars = class_copyIvarList(clazz, &count);
NSMutableArray* ivarArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
const char* ivarName = ivar_getName(ivars[i]);
[ivarArray addObject:[NSString stringWithCString:ivarName encoding:NSUTF8StringEncoding]];
}
free(ivars);
objc_property_t* properties = class_copyPropertyList(clazz, &count);
NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
const char* propertyName = property_getName(properties[i]);
[propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
}
free(properties);
Method* methods = class_copyMethodList(clazz, &count);
NSMutableArray* methodArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
SEL selector = method_getName(methods[i]);
const char* methodName = sel_getName(selector);
[methodArray addObject:[NSString stringWithCString:methodName encoding:NSUTF8StringEncoding]];
}
free(methods);
NSDictionary* classDump = [NSDictionary dictionaryWithObjectsAndKeys:
ivarArray, @"ivars",
propertyArray, @"properties",
methodArray, @"methods",
nil];
const char * p = [[NSString stringWithFormat:@"%@", classDump] UTF8String];
send_multi_log(p);
}
void dump_unknown(id obj)
{
NSString * clsname = NSStringFromClass([obj class]);
dump("obj.class", clsname);
dumpClassInfo(obj);
}