source: libcf/trunk/src/cf_debug.c@ 53

Last change on this file since 53 was 53, checked in by cheese, 11 years ago

#1 remove structure for remote host information interface from socket

File size: 9.7 KB
Line 
1/**
2 * @file cf_debug.c
3 * @author myusgun <myusgun@gmail.com>
4 * @version 0.1
5 */
6#include "cf_debug.h"
7#include "cf_local.h"
8#include "cf_error.h"
9#include "cf_thread.h"
10
11#include <stdlib.h>
12#include <ctype.h>
13#include <stdarg.h>
14#include <string.h>
15
16#if defined(_WIN32) || defined(_WIN64)
17# include <io.h>
18#else
19# include <unistd.h>
20#endif
21
22#define IS_READABLE_CHAR(__c) (' ' <= __c && __c <= '~')
23
24#define CHECK_INVALID_CTX(__ctx) \
25 if (__ctx == NULL) \
26 return CF_ERROR_DEBUG_INVALID_CTX
27
28#define GET_CTX_OSTREAM(__ctx) \
29 ((((CF_DEBUG_CTX *)__ctx)->fp == NULL) \
30 ? stderr \
31 : ((CF_DEBUG_CTX *)__ctx)->fp)
32
33/**
34 * 디버그 컨텍스트
35 *
36 * @remark change from public to private
37 */
38typedef void * CF_Debug_Ctx;
39
40/* *
41 * 콜스택 데이터
42 *
43 * @remark change from public to private
44 */
45typedef struct cf_debug_callStack {
46 char file[NAME_LENGTH + 1]; /* *< 파일 이름 */
47 char function[NAME_LENGTH + 1]; /* *< 함수 이름 */
48 int line; /* *< 라인 넘버 */
49} CF_Debug_CallStack;
50
51typedef struct __cf_debug_callstack__
52{
53 char file[NAME_LENGTH + 1];
54 char func[NAME_LENGTH + 1];
55 int line;
56
57 int frameIndex;
58
59 struct __cf_debug_callstack__ * caller;
60} S_CF_DEBUG_CALLSTACK, CF_DEBUG_CALLSTACK;
61
62typedef struct __cf_debug_ctx__
63{
64 char file[NAME_LENGTH + 1];
65 char func[NAME_LENGTH + 1];
66 int line;
67
68 FILE * fp;
69 CF_Mutex mutex;
70
71 CF_DEBUG_CALLSTACK callstack;
72} S_CF_DEBUG_CTX, CF_DEBUG_CTX;
73
74CF_Debug_Ctx gDebugSingleCtx = NULL;
75
76static int
77CF_Debug_Local_UpdateCtx (CF_Debug_Ctx ctx,
78 const char * file,
79 const char * func,
80 const int line)
81{
82 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
83
84 CHECK_INVALID_CTX (ctx);
85
86 strncpy (context->file, file, strlen (file));
87 strncpy (context->func, func, strlen (func));
88 context->line = line;
89
90 return CF_OK;
91}
92
93static int
94CF_Debug_Local_Print (FILE * fp,
95 const char * file,
96 const char * func,
97 const int line,
98 const char * fmt,
99 va_list valist)
100{
101 fprintf (fp, "[DEBUG][%s:%d][%s] ", file, line, func);
102 vfprintf (fp, fmt, valist);
103
104 return CF_OK;
105}
106
107static int
108CF_Debug_Local_PrintBin (FILE * fp,
109 const char * file,
110 const char * func,
111 const int line,
112 const unsigned char * bin,
113 const int len)
114{
115 int i, j;
116
117 for (i = 0 ; i < len ; i += 16)
118 {
119 fprintf (fp, "[DEBUG][%s:%d][%s] ", file, line, func);
120 fprintf (fp, "%06x : ", i);
121
122 for (j = 0 ; j < 16 ; j++)
123 {
124 if (i+j < len)
125 fprintf (fp, "%02x ", bin[i+j]);
126 else
127 fprintf (fp, " ");
128 }
129 fprintf (fp, " ");
130
131 for (j = 0 ; j < 16 ; j++)
132 {
133 if (i+j < len)
134 fprintf (fp, "%c", IS_READABLE_CHAR(bin[i+j]) ? bin[i+j] : '.');
135 }
136 fprintf (fp, "\n");
137 }
138
139 return CF_OK;
140}
141
142/**
143 * 디버그 컨텍스트를 해제
144 *
145 * @return 성공 시, CF_OK; 실패 시, 오류 코드
146 *
147 * @param ctx 디버그 컨텍스트
148 *
149 * @see CF_DEBUG_DESTROY_CTX
150 */
151static int
152CF_Debug_DestroyCtx (CF_Debug_Ctx ctx)
153{
154 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
155 CF_DEBUG_CALLSTACK * pop = NULL;
156 CF_DEBUG_CALLSTACK * next = NULL;
157
158 CHECK_INVALID_CTX (ctx);
159
160 if (context->fp != NULL)
161 fclose (context->fp);
162
163 for (pop = next = context->callstack.caller ; pop ; pop = next)
164 {
165 next = next->caller;
166 free (pop);
167 }
168
169 if (context->mutex)
170 CF_Mutex_Destory (&context->mutex);
171
172 free (context);
173
174 return CF_OK;
175}
176
177/**
178 * 디버그 컨텍스트를 생성
179 *
180 * @return 성공 시, CF_Debug_Ctx 형태의 컨텍스트; 실패 시, NULL
181 * @see CF_DEBUG_CREATE_CTX
182 */
183static int
184CF_Debug_CreateCtx (CF_Debug_Ctx * ctx)
185{
186 int result = 0;
187 CF_DEBUG_CTX * context = NULL;
188
189 TRY
190 {
191 context = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1);
192 if (context == NULL)
193 {
194 result = CF_ERROR_DEBUG_ALLOCATE_CTX;
195 TRY_BREAK;
196 }
197
198 result = CF_Mutex_Create (&context->mutex);
199 if (result < 0)
200 {
201 TRY_BREAK;
202 }
203
204 *ctx = (CF_Debug_Ctx) context;
205 }
206 CATCH_IF (result < 0)
207 {
208 CF_Debug_DestroyCtx (context);
209 }
210
211 return CF_OK;
212}
213
214/**
215 * 디버그 메시지를 지정된 파일 포인터로 출력
216 *
217 * @return 성공 시, CF_OK; 실패 시, 오류 코드
218 *
219 * @param fp 파일 포인터. 표준출력(stdout) 및 표준오류(stderr) 사용 가능
220 * @param file 파일 경로
221 * @param func 함수 이름
222 * @param line 라인 넘버
223 * @param fmt 포맷 스트링
224 * @param ... 가변 인자
225 *
226 * @see CF_DEBUG_PRINT
227 */
228int
229CF_Debug_Print (FILE * fp,
230 const char * file,
231 const char * func,
232 const int line,
233 const char * fmt, ...)
234{
235 va_list valist;
236
237 va_start (valist, fmt);
238 CF_Debug_Local_Print (fp, file, func, line, fmt, valist);
239 va_end (valist);
240
241 return CF_OK;
242}
243
244/**
245 * 바이너리 데이터를 디버그 메시지와 함께 지정된 파일 포인터로 출력
246 *
247 * @return CF_OK 반환
248 *
249 * @param fp 파일 포인터. 표준출력(stdout) 및 표준오류(stderr) 사용 가능
250 * @param file 파일 경로
251 * @param func 함수 이름
252 * @param line 라인 넘버
253 * @param bin 라인 넘버
254 * @param len 바이너리 길이
255 * @param fmt 포맷 스트링
256 * @param ... 가변 인자
257 *
258 * @see CF_DEBUG_PRINT_BIN
259 */
260int
261CF_Debug_PrintBin (FILE * fp,
262 const char * file,
263 const char * func,
264 const int line,
265 const unsigned char * bin,
266 const int len,
267 const char * fmt, ...)
268{
269 va_list valist;
270
271 va_start (valist, fmt);
272 CF_Debug_Local_Print (fp, file, func, line, fmt, valist);
273 va_end (valist);
274
275 CF_Debug_Local_PrintBin (fp, file, func, line, bin, len);
276
277 return CF_OK;
278}
279
280/**
281 * 컨텍스트에 콜스택 푸시
282 *
283 * @return 성공 시, CF_OK; 실패 시, 오류 코드
284 *
285 * @param ctx 디버그 컨텍스트
286 * @param file 파일 경로
287 * @param func 함수 이름
288 * @param line 라인 넘버
289 */
290static int
291CF_Debug_CallStackPush (CF_Debug_Ctx ctx,
292 const char * file,
293 const char * func,
294 const int line)
295{
296 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
297 CF_DEBUG_CALLSTACK * push = NULL;
298
299 CHECK_INVALID_CTX (ctx);
300
301 push = (CF_DEBUG_CALLSTACK *) calloc (sizeof (CF_DEBUG_CALLSTACK), 1);
302 if (push == NULL)
303 return CF_ERROR_DEBUG_PUSH_CALLSTACK;
304
305 /* push to callstack */
306 sprintf (push->file, "%s", file);
307 sprintf (push->func, "%s", func);
308 push->line = line;
309 push->caller = context->callstack.caller;
310 push->frameIndex = push->caller == NULL
311 ? 0
312 : push->caller->frameIndex + 1;
313 context->callstack.caller = push;
314
315 CF_Debug_Local_UpdateCtx (ctx, file, func, line);
316
317 return CF_OK;
318}
319
320/**
321 * 컨텍스트에서 콜스택에서 TOP을 제거하지 않고 가져옴
322 *
323 * @return 성공 시, CF_OK; 실패 시, 오류 코드
324 *
325 * @param ctx 디버그 컨텍스트
326 * @param callstack 콜스택 정보를 가져올 콜스택 데이터 구조체 포인터
327 *
328 * @see CF_Debug_CallStack
329 */
330static int
331CF_Debug_CallStackPeek (CF_Debug_Ctx ctx,
332 CF_Debug_CallStack * callstack)
333{
334 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
335 CF_DEBUG_CALLSTACK * pop = NULL;
336
337 pop = context->callstack.caller;
338 if (pop == NULL)
339 return CF_ERROR_DEBUG_PEEK_CALLSTACK;
340
341 if (callstack != NULL)
342 {
343 sprintf (callstack->file , "%s", pop->file);
344 sprintf (callstack->function, "%s", pop->func);
345 callstack->line = pop->line;
346 }
347
348 return CF_OK;
349}
350
351/**
352 * 컨텍스트에서 콜스택 팝
353 *
354 * @return 성공 시, CF_OK; 실패 시, 오류 코드
355 *
356 * @param ctx 디버그 컨텍스트
357 * @param callstack 콜스택 정보를 가져올 콜스택 데이터 구조체 포인터
358 *
359 * @see CF_Debug_CallStack
360 */
361static int
362CF_Debug_CallStackPop (CF_Debug_Ctx ctx,
363 CF_Debug_CallStack * callstack)
364{
365 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
366 CF_DEBUG_CALLSTACK * pop = NULL;
367
368 CHECK_INVALID_CTX (ctx);
369
370 pop = context->callstack.caller;
371 if (pop == NULL)
372 return CF_ERROR_DEBUG_POP_CALLSTACK;
373
374 if (CF_Debug_CallStackPeek (ctx, callstack) < 0)
375 return CF_ERROR_DEBUG_PEEK_CALLSTACK;
376
377 memset (context->file, 0x00, sizeof (context->file));
378 memset (context->func, 0x00, sizeof (context->func));
379 context->line = 0;
380
381 /* restore current context */
382 if (pop->caller != NULL)
383 {
384 CF_Debug_Local_UpdateCtx (ctx,
385 pop->caller->file,
386 pop->caller->func,
387 pop->caller->line);
388 }
389
390 context->callstack.caller = pop->caller;
391 free (pop);
392
393 return CF_OK;
394}
395
396/**
397 * 현재 콜스택을 출력
398 *
399 * @return 성공 시, CF_OK; 실패 시, 오류 코드
400 *
401 * @param fp 출력 할 파일 포인터
402 */
403int
404CF_Debug_PrintCallStack (FILE * fp)
405{
406 int iter = 0;
407
408 CF_DEBUG_CTX * ctx = gDebugSingleCtx;
409 CF_DEBUG_CALLSTACK * callstack = NULL;
410
411 if (ctx == NULL)
412 return CF_ERROR_DEBUG_INVALID_CTX;
413
414 for ( callstack = ctx->callstack.caller
415 ; callstack
416 ; callstack = callstack->caller)
417 {
418 fprintf (fp, "#%-4d %s <%s:%d>\n",
419 iter++,
420 callstack->func,
421 callstack->file,
422 callstack->line);
423 }
424
425 return CF_OK;
426}
427
428/**
429 * 함수 진입을 명시
430 *
431 * @return 성공 시, CF_OK; 실패 시, 오류 코드
432 *
433 * @param file 파일 경로
434 * @param func 함수 이름
435 * @param line 라인 넘버
436 *
437 * @see CF_Debug_LeaveFunction
438 */
439int
440CF_Debug_EnterFunction (const char * file,
441 const char * func,
442 const int line)
443{
444 int result = 0;
445 CF_DEBUG_CTX * ctx = NULL;
446
447 if (gDebugSingleCtx == NULL)
448 {
449 result = CF_Debug_CreateCtx (&gDebugSingleCtx);
450 if (result != CF_OK)
451 return result;
452 }
453 ctx = (CF_DEBUG_CTX *)gDebugSingleCtx;
454
455 CF_Mutex_Lock (&ctx->mutex);
456 CF_Debug_CallStackPush (gDebugSingleCtx, file, func, line);
457 CF_Mutex_Unlock (&ctx->mutex);
458
459 return CF_OK;
460}
461
462/**
463 * 함수 종료를 명시
464 *
465 * @return 성공 시, CF_OK; 실패 시, 오류 코드
466 *
467 * @see CF_Debug_EnterFunction
468 */
469int
470CF_Debug_LeaveFunction (void)
471{
472 CF_DEBUG_CTX * ctx = (CF_DEBUG_CTX *)gDebugSingleCtx;
473
474 if (ctx == NULL)
475 return CF_ERROR_DEBUG_INVALID_CTX;
476
477 CF_Mutex_Lock (&ctx->mutex);
478 CF_Debug_CallStackPop (gDebugSingleCtx, NULL);
479 CF_Mutex_Unlock (&ctx->mutex);
480
481 return CF_OK;
482}
Note: See TracBrowser for help on using the repository browser.