-
Notifications
You must be signed in to change notification settings - Fork 0
/
hulib.cpp
executable file
·243 lines (213 loc) · 5.87 KB
/
hulib.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
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ucontext.h>
#include <signal.h>
#include <dlfcn.h>
#include "hulib.h"
huvar g_var;
//HuDebug debug;
//int system_noblk(const char *cmdstring) /* with appropriate signal handling */
//{
// pid_t pid;
// int status;
// struct sigaction ignore, saveintr, savequit;
// sigset_t chldmask, savemask;
// if (cmdstring == NULL)
// return (1); /* always a command processor with UNIX */
// ignore.sa_handler = SIG_IGN; /* ignore SIGINT and SIGQUIT */
// sigemptyset(&ignore.sa_mask);
// ignore.sa_flags = 0;
// if (sigaction(SIGINT, &ignore, &saveintr) < 0)
// return (-1);
// if (sigaction(SIGQUIT, &ignore, &savequit) < 0)
// return (-1);
// sigemptyset(&chldmask); /* now block SIGCHLD */
// sigaddset(&chldmask, SIGCHLD);
// if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0)
// return (-1);
// if ((pid = fork()) < 0)
// {
// status = -1; /* probably out of processes */
// }
// else if (pid == 0)
// { /* child *//* restore previous signal actions & reset signal mask */
// sigaction(SIGINT, &saveintr, NULL);
// sigaction(SIGQUIT, &savequit, NULL);
// sigprocmask(SIG_SETMASK, &savemask, NULL);
// execl("/bin/sh", "sh", "-c", cmdstring, (char *) 0);
// _exit(127); /* exec error */
// }
// else
// { /* parent */
//
// } /* restore previous signal actions & reset signal mask */
// if (sigaction(SIGINT, &saveintr, NULL) < 0)
// return (-1);
// if (sigaction(SIGQUIT, &savequit, NULL) < 0)
// return (-1);
// if (sigprocmask(SIG_SETMASK, &savemask, NULL) < 0)
// return (-1);
// return (status);
//}
unsigned long GetTickCount()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}
void debug_timer(const char * str)
{
static int oldtime = GetTickCount();
int usetime = GetTickCount() - oldtime;
if (str != NULL)
printf("debug_timer [%s] %dfps/S fps/%dms\r\n", str, usetime ? 1000 / usetime : 1000, usetime);
oldtime = GetTickCount();
}
void FPSWaitTimer(int ms)
{
static int intvl = GetTickCount();
int wait = ms + intvl;
while (GetTickCount() < wait)
{
usleep(1000);
}
intvl = wait;
}
void errexit(const char * str)
{
fprintf(stderr,"**************error exit*****************\r\n");
fprintf(stderr,"%s",str);
fprintf(stderr,"\r\n");
fprintf(stderr,"*****************************************\r\n");
#ifdef CONFIG_DUMPSTACK_DEBUG
dumpstack();
#endif
exit(-1);
}
void debug_usage()
{
struct rusage buf;
//int err = getrusage(RUSAGE_CHILDREN, &buf);
int err = getrusage(RUSAGE_SELF, &buf);
//printf("ERR=%d\r\n", err);
//printf("userCPU:%ld.%ld\t", buf.ru_utime.tv_sec, buf.ru_utime.tv_usec);
//printf("kernelCPU:%ld.%ld\t", buf.ru_stime.tv_sec, buf.ru_stime.tv_usec);
printf("RUSAGE_SELF MEM:%ld\r\n", buf.ru_maxrss);
}
void StrToHUMap(const char * str,HUMap &mp)
{
if(str==NULL)
return;
const char * p = str;
if(*p == ' ')
{
p++;
}
if(*p == '<') //如果是xml那么直接返回
return;
const char * q = NULL;
q = strchr(p,'=');
if(q==NULL)
return;
char bufk[32] = {0};
char bufv[32] = {0};
strncpy(bufk,p,q-p);
p = strchr(q+1,' ');
if(p == NULL)
{
strncpy(bufv,q+1,strlen(q+1));
}
else
{
strncpy(bufv,q+1,p-q-1);
}
mp[bufk] = bufv;
StrToHUMap(p,mp);
}
//typedef struct
//{
// const char *dli_fname; /* File name of defining object. */
// void *dli_fbase; /* Load address of that object. */
// const char *dli_sname; /* Name of nearest symbol.���纯����*/
// void *dli_saddr; /* Exact value of nearest symbol.���纯�����ʼ��ַ*/
//} Dl_info;
#ifdef CONFIG_DUMPSTACK_DEBUG
static int backtrace_xy(void **BUFFER, int SIZE)
{
volatile int n = 0;
volatile int *p;
volatile int *q;
volatile int ebp1;
volatile int eip1;
volatile int i = 0;
p = &n;
ebp1 = p[4];
eip1 = p[6];
fprintf(stderr, "======================= backtrace_xy addr: 0x%0x, param1: 0x%0x, param2: 0x%0x\n", backtrace_xy,
&BUFFER, &SIZE);
fprintf(stderr, "n addr is 0x%0x\n", &n);
fprintf(stderr, "p addr is 0x%0x\n", &p);
for (i = 0; i < SIZE; i++)
{
fprintf(stderr, "ebp1 is 0x%0x, eip1 is 0x%0x\n", ebp1, eip1);
BUFFER[i] = (void *) eip1;
p = (int*) ebp1;
q = p - 5;
eip1 = q[5];
ebp1 = q[2];
if (ebp1 == 0 || eip1 == 0)
break;
}
fprintf(stderr, "total level: %d\n", i);
return i;
}
struct ucontext_ce123
{
unsigned long uc_flags;
struct ucontext *uc_link;
stack_t uc_stack;
struct sigcontext uc_mcontext;
sigset_t uc_sigmask; /* mask last for extensibility */
} ucontext_ce123_;
struct sigframe_ce123
{
struct sigcontext sc; //����һ��Ĵ���������
unsigned long extramask[1];
unsigned long retcode; //���淵�ص�ַ
//struct aux_sigframe aux __attribute__((aligned(8)));
} sigframe_ce123;
void asm_dumpstack()
{
Dl_info info;
int status;
int *fp = 0, *next_fp = 0;
int cnt = 0;
int ret = 0;
__asm__(
"mov %0, fp\n"
: "=r"(fp)
);
next_fp = (int *) (*(fp - 3));
while (next_fp != 0)
{
void * pcur = (void *) *(next_fp - 1);
status = dladdr(pcur, &info);
if (status && info.dli_fname && info.dli_fname[0] != '\0')
{
printf("0x%08x 0x%08x %-20s < %s+%p >\r\n", pcur, info.dli_saddr, info.dli_fname, info.dli_sname,
(unsigned long) ((unsigned int) pcur - (unsigned int) info.dli_saddr));
}
else
{
printf("[%p]", (void *) *(next_fp - 1));
}
//array[cnt++] = (void *)*(next_fp - 1);
next_fp = (int *) (*(next_fp - 3));
cnt++;
}
printf("Backstrace (%d deep)\n", cnt);
}
#endif