[6] | 1 | /**
|
---|
[28] | 2 | * @file cf_debug.c
|
---|
| 3 | * @author myusgun <myusgun@gmail.com>
|
---|
| 4 | * @version 0.1
|
---|
[6] | 5 | */
|
---|
| 6 | #include "cf_debug.h"
|
---|
| 7 | #include "cf_local.h"
|
---|
| 8 |
|
---|
| 9 | #include <stdlib.h>
|
---|
| 10 | #include <ctype.h>
|
---|
| 11 | #include <stdarg.h>
|
---|
[9] | 12 | #include <string.h>
|
---|
[6] | 13 |
|
---|
| 14 | #ifdef _WIN32
|
---|
[7] | 15 | # include <io.h>
|
---|
[6] | 16 | #else
|
---|
| 17 | # include <unistd.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 | #define IS_READABLE_CHAR(__c) (' ' <= __c && __c <= '~')
|
---|
| 21 |
|
---|
| 22 | #define CHECK_INVALID_CTX(__ctx) \
|
---|
| 23 | if (__ctx == NULL) \
|
---|
| 24 | return CF_ERROR_DEBUG_INVALID_CTX
|
---|
[10] | 25 |
|
---|
[6] | 26 | #define GET_CTX_OSTREAM(__ctx) \
|
---|
| 27 | ((((CF_DEBUG_CTX *)__ctx)->fp == NULL) \
|
---|
| 28 | ? stderr \
|
---|
| 29 | : ((CF_DEBUG_CTX *)__ctx)->fp)
|
---|
| 30 |
|
---|
| 31 | typedef struct __cf_debug_callstack__
|
---|
| 32 | {
|
---|
[9] | 33 | char file[NAME_LENGTH + 1];
|
---|
[6] | 34 | char func[NAME_LENGTH + 1];
|
---|
| 35 | int line;
|
---|
| 36 |
|
---|
| 37 | struct __cf_debug_callstack__ * caller;
|
---|
| 38 | } S_CF_DEBUG_CALLSTACK, CF_DEBUG_CALLSTACK;
|
---|
| 39 |
|
---|
[10] | 40 | typedef struct __cf_debug_ctx__
|
---|
[6] | 41 | {
|
---|
| 42 | char file[NAME_LENGTH + 1];
|
---|
| 43 | char func[NAME_LENGTH + 1];
|
---|
| 44 | int line;
|
---|
| 45 |
|
---|
| 46 | FILE * fp;
|
---|
| 47 |
|
---|
| 48 | CF_DEBUG_CALLSTACK callstack;
|
---|
| 49 | } S_CF_DEBUG_CTX, CF_DEBUG_CTX;
|
---|
| 50 |
|
---|
[23] | 51 | static int
|
---|
[26] | 52 | CF_Debug_Local_UpdateCtx (CF_Debug_Ctx ctx,
|
---|
| 53 | const char * file,
|
---|
| 54 | const char * func,
|
---|
| 55 | const int line)
|
---|
| 56 | {
|
---|
| 57 | CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
|
---|
| 58 |
|
---|
| 59 | CHECK_INVALID_CTX (ctx);
|
---|
| 60 |
|
---|
| 61 | strncpy (context->file, file, strlen (file));
|
---|
| 62 | strncpy (context->func, func, strlen (func));
|
---|
| 63 | context->line = line;
|
---|
| 64 |
|
---|
| 65 | return CF_OK;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | static int
|
---|
[16] | 69 | CF_Debug_Local_Print (FILE * fp,
|
---|
| 70 | const char * file,
|
---|
| 71 | const char * func,
|
---|
| 72 | const int line,
|
---|
[12] | 73 | const char * fmt,
|
---|
| 74 | va_list valist)
|
---|
[6] | 75 | {
|
---|
[16] | 76 | fprintf (fp, "[DEBUG][%s:%d][%s] ", file, line, func);
|
---|
| 77 | vfprintf (fp, fmt, valist);
|
---|
[14] | 78 |
|
---|
| 79 | return CF_OK;
|
---|
[6] | 80 | }
|
---|
| 81 |
|
---|
[23] | 82 | static int
|
---|
[16] | 83 | CF_Debug_Local_PrintBin (FILE * fp,
|
---|
| 84 | const char * file,
|
---|
| 85 | const char * func,
|
---|
| 86 | const int line,
|
---|
| 87 | const unsigned char * bin,
|
---|
| 88 | const int len)
|
---|
| 89 | {
|
---|
| 90 | int i, j;
|
---|
| 91 |
|
---|
| 92 | for (i = 0 ; i < len ; i += 16)
|
---|
| 93 | {
|
---|
| 94 | fprintf (fp, "[DEBUG][%s:%d][%s] ", file, line, func);
|
---|
| 95 | fprintf (fp, "%06x : ", i);
|
---|
| 96 |
|
---|
| 97 | for (j = 0 ; j < 16 ; j++)
|
---|
| 98 | {
|
---|
| 99 | if (i+j < len)
|
---|
| 100 | fprintf (fp, "%02x ", bin[i+j]);
|
---|
| 101 | else
|
---|
| 102 | fprintf (fp, " ");
|
---|
| 103 | }
|
---|
| 104 | fprintf (fp, " ");
|
---|
| 105 |
|
---|
| 106 | for (j = 0 ; j < 16 ; j++)
|
---|
| 107 | {
|
---|
| 108 | if (i+j < len)
|
---|
| 109 | fprintf (fp, "%c", IS_READABLE_CHAR(bin[i+j]) ? bin[i+j] : '.');
|
---|
| 110 | }
|
---|
| 111 | fprintf (fp, "\n");
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | return CF_OK;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[35] | 117 | /**
|
---|
| 118 | * 디버그 컨텍스트를 생성
|
---|
| 119 | *
|
---|
| 120 | * @return 성공 시, CF_Debug_Ctx 형태의 컨텍스트; 실패 시, NULL
|
---|
| 121 | * @see CF_DEBUG_CREATE_CTX
|
---|
| 122 | */
|
---|
[6] | 123 | CF_Debug_Ctx
|
---|
| 124 | CF_Debug_CreateCtx (void)
|
---|
| 125 | {
|
---|
| 126 | CF_DEBUG_CTX * ctx = NULL;
|
---|
| 127 |
|
---|
| 128 | ctx = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1);
|
---|
| 129 |
|
---|
| 130 | return (CF_Debug_Ctx) ctx;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[35] | 133 | /**
|
---|
| 134 | * 디버그 컨텍스트를 해제
|
---|
| 135 | *
|
---|
| 136 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 137 | *
|
---|
| 138 | * @param ctx 디버그 컨텍스트
|
---|
| 139 | *
|
---|
| 140 | * @see CF_DEBUG_DESTROY_CTX
|
---|
| 141 | */
|
---|
[6] | 142 | int
|
---|
| 143 | CF_Debug_DestroyCtx (CF_Debug_Ctx ctx)
|
---|
| 144 | {
|
---|
| 145 | CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
|
---|
| 146 | CF_DEBUG_CALLSTACK * pop = NULL;
|
---|
| 147 | CF_DEBUG_CALLSTACK * next = NULL;
|
---|
| 148 |
|
---|
| 149 | CHECK_INVALID_CTX (ctx);
|
---|
| 150 |
|
---|
| 151 | if (context->fp != NULL)
|
---|
| 152 | fclose (context->fp);
|
---|
| 153 |
|
---|
| 154 | for (pop = next = context->callstack.caller ; pop ; pop = next)
|
---|
| 155 | {
|
---|
| 156 | next = next->caller;
|
---|
| 157 | free (pop);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | free (context);
|
---|
| 161 |
|
---|
| 162 | return CF_OK;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[35] | 165 | /**
|
---|
| 166 | * 디버그 컨텍스트에 출력할 파일 디스크립터를 설정
|
---|
| 167 | *
|
---|
| 168 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 169 | *
|
---|
| 170 | * @param ctx 디버그 컨텍스트
|
---|
| 171 | * @param fd 파일 디스크립터
|
---|
| 172 | *
|
---|
| 173 | * @see CF_File_Open, CF_File_Create
|
---|
| 174 | */
|
---|
[6] | 175 | int
|
---|
| 176 | CF_Debug_SetOutputFD (CF_Debug_Ctx ctx,
|
---|
| 177 | int fd)
|
---|
| 178 | {
|
---|
| 179 | int result = 0;
|
---|
| 180 | int dupfd = 0;
|
---|
| 181 |
|
---|
| 182 | CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
|
---|
| 183 | FILE * fp = NULL;
|
---|
| 184 |
|
---|
| 185 | CHECK_INVALID_CTX (ctx);
|
---|
| 186 |
|
---|
| 187 | TRY
|
---|
| 188 | {
|
---|
| 189 | dupfd = dup (fd);
|
---|
| 190 | if (dupfd < 0)
|
---|
| 191 | {
|
---|
| 192 | result = -1;
|
---|
| 193 | TRY_BREAK;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[34] | 196 | fp = fdopen (dupfd, "a");
|
---|
[6] | 197 | if (fp == NULL)
|
---|
| 198 | {
|
---|
| 199 | close (dupfd);
|
---|
| 200 | result = -2;
|
---|
| 201 | TRY_BREAK;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | context->fp = fp;
|
---|
| 205 | }
|
---|
| 206 | CATCH_IF (result < 0)
|
---|
| 207 | {
|
---|
| 208 | return CF_ERROR_DEBUG_SET_OUTPUT_FD;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | return CF_OK;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[35] | 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 | */
|
---|
[6] | 228 | int
|
---|
[16] | 229 | CF_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 |
|
---|
[35] | 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 | */
|
---|
[16] | 260 | int
|
---|
| 261 | CF_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 |
|
---|
[35] | 280 | /**
|
---|
| 281 | * 컨텍스트를 업데이트하고 디버그 메시지를 출력
|
---|
| 282 | *
|
---|
| 283 | * @return CF_OK 반환
|
---|
| 284 | *
|
---|
| 285 | * @param ctx 디버그 컨텍스트
|
---|
| 286 | * @param file 파일 경로
|
---|
| 287 | * @param func 함수 이름
|
---|
| 288 | * @param line 라인 넘버
|
---|
| 289 | * @param fmt 포맷 스트링
|
---|
| 290 | * @param ... 가변 인자
|
---|
| 291 | *
|
---|
[36] | 292 | * @see CF_Debug_Trace
|
---|
[35] | 293 | */
|
---|
[16] | 294 | int
|
---|
[6] | 295 | CF_Debug_Trace (CF_Debug_Ctx ctx,
|
---|
[26] | 296 | const char * file,
|
---|
| 297 | const char * func,
|
---|
| 298 | const int line,
|
---|
[6] | 299 | const char * fmt, ...)
|
---|
| 300 | {
|
---|
| 301 | va_list valist;
|
---|
[16] | 302 | CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
|
---|
[6] | 303 |
|
---|
| 304 | CHECK_INVALID_CTX (ctx);
|
---|
| 305 |
|
---|
[26] | 306 | CF_Debug_Local_UpdateCtx (ctx, file, func, line);
|
---|
| 307 |
|
---|
[6] | 308 | va_start (valist, fmt);
|
---|
[16] | 309 | CF_Debug_Local_Print (GET_CTX_OSTREAM (context),
|
---|
| 310 | context->file,
|
---|
| 311 | context->func,
|
---|
| 312 | context->line,
|
---|
| 313 | fmt, valist);
|
---|
[6] | 314 | va_end (valist);
|
---|
| 315 |
|
---|
| 316 | return CF_OK;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[35] | 319 | /**
|
---|
| 320 | * 컨텍스트를 업데이트하고 바이너리 데이터를 디버그 메시지와 함께 출력
|
---|
| 321 | *
|
---|
| 322 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 323 | *
|
---|
| 324 | * @param ctx 디버그 컨텍스트
|
---|
| 325 | * @param file 파일 경로
|
---|
| 326 | * @param func 함수 이름
|
---|
| 327 | * @param line 라인 넘버
|
---|
| 328 | * @param bin 바이너리 데이터
|
---|
| 329 | * @param len 바이너리 길이
|
---|
| 330 | * @param fmt 포맷 스트링
|
---|
| 331 | * @param ... 가변 인자
|
---|
| 332 | *
|
---|
[36] | 333 | * @see CF_DEBUG_TRACE_BIN
|
---|
[35] | 334 | */
|
---|
[6] | 335 | int
|
---|
| 336 | CF_Debug_TraceBin (CF_Debug_Ctx ctx,
|
---|
[26] | 337 | const char * file,
|
---|
| 338 | const char * func,
|
---|
| 339 | const int line,
|
---|
[6] | 340 | const unsigned char * bin,
|
---|
| 341 | const int len,
|
---|
| 342 | const char * fmt, ...)
|
---|
| 343 | {
|
---|
[16] | 344 | va_list valist;
|
---|
| 345 | CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
|
---|
| 346 |
|
---|
[6] | 347 | CHECK_INVALID_CTX (ctx);
|
---|
| 348 |
|
---|
[26] | 349 | CF_Debug_Local_UpdateCtx (ctx, file, func, line);
|
---|
| 350 |
|
---|
[6] | 351 | va_start (valist, fmt);
|
---|
[16] | 352 | CF_Debug_Local_Print (GET_CTX_OSTREAM (context),
|
---|
| 353 | context->file,
|
---|
| 354 | context->func,
|
---|
| 355 | context->line,
|
---|
| 356 | fmt, valist);
|
---|
| 357 | va_end (valist);
|
---|
[6] | 358 |
|
---|
[18] | 359 | CF_Debug_Local_PrintBin (GET_CTX_OSTREAM (context),
|
---|
[16] | 360 | context->file,
|
---|
| 361 | context->func,
|
---|
| 362 | context->line,
|
---|
| 363 | bin, len);
|
---|
[6] | 364 |
|
---|
| 365 | return CF_OK;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
[35] | 368 | /**
|
---|
| 369 | * 컨텍스트에 콜스택 푸시
|
---|
| 370 | *
|
---|
| 371 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 372 | *
|
---|
| 373 | * @param ctx 디버그 컨텍스트
|
---|
| 374 | * @param file 파일 경로
|
---|
| 375 | * @param func 함수 이름
|
---|
| 376 | * @param line 라인 넘버
|
---|
| 377 | *
|
---|
| 378 | * @see CF_DEBUG_CALLSTACK_PUSH
|
---|
| 379 | */
|
---|
[6] | 380 | int
|
---|
| 381 | CF_Debug_CallStackPush (CF_Debug_Ctx ctx,
|
---|
[9] | 382 | const char * file,
|
---|
[6] | 383 | const char * func,
|
---|
| 384 | const int line)
|
---|
| 385 | {
|
---|
| 386 | CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
|
---|
| 387 | CF_DEBUG_CALLSTACK * push = NULL;
|
---|
| 388 |
|
---|
| 389 | CHECK_INVALID_CTX (ctx);
|
---|
| 390 |
|
---|
| 391 | push = (CF_DEBUG_CALLSTACK *) calloc (sizeof (CF_DEBUG_CALLSTACK), 1);
|
---|
| 392 | if (push == NULL)
|
---|
| 393 | return CF_ERROR_DEBUG_PUSH_CALLSTACK;
|
---|
| 394 |
|
---|
[9] | 395 | /* push to callstack */
|
---|
| 396 | sprintf (push->file, "%s", file);
|
---|
[6] | 397 | sprintf (push->func, "%s", func);
|
---|
| 398 | push->line = line;
|
---|
| 399 | push->caller = context->callstack.caller;
|
---|
| 400 | context->callstack.caller = push;
|
---|
| 401 |
|
---|
[26] | 402 | CF_Debug_Local_UpdateCtx (ctx, file, func, line);
|
---|
[9] | 403 |
|
---|
[6] | 404 | return CF_OK;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[35] | 407 | /**
|
---|
| 408 | * 컨텍스트에서 콜스택 팝
|
---|
| 409 | *
|
---|
| 410 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 411 | *
|
---|
| 412 | * @param ctx 디버그 컨텍스트
|
---|
| 413 | * @param callstack 콜스택 정보를 가져올 콜스택 데이터 구조체 포인터
|
---|
| 414 | *
|
---|
| 415 | * @see CF_Debug_CallStackPop, CF_Debug_CallStack
|
---|
| 416 | */
|
---|
[6] | 417 | int
|
---|
| 418 | CF_Debug_CallStackPop (CF_Debug_Ctx ctx,
|
---|
| 419 | CF_Debug_CallStack * callstack)
|
---|
| 420 | {
|
---|
| 421 | CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
|
---|
| 422 | CF_DEBUG_CALLSTACK * pop = NULL;
|
---|
| 423 |
|
---|
| 424 | CHECK_INVALID_CTX (ctx);
|
---|
| 425 |
|
---|
| 426 | pop = context->callstack.caller;
|
---|
| 427 | if (pop == NULL)
|
---|
| 428 | return CF_ERROR_DEBUG_POP_CALLSTACK;
|
---|
| 429 |
|
---|
[9] | 430 | if (callstack != NULL)
|
---|
| 431 | {
|
---|
[14] | 432 | sprintf (callstack->file , "%s", pop->file);
|
---|
[9] | 433 | sprintf (callstack->function, "%s", pop->func);
|
---|
| 434 | callstack->line = pop->line;
|
---|
| 435 | }
|
---|
[6] | 436 |
|
---|
[9] | 437 | memset (context->file, 0x00, sizeof (context->file));
|
---|
| 438 | memset (context->func, 0x00, sizeof (context->func));
|
---|
| 439 | context->line = 0;
|
---|
| 440 |
|
---|
| 441 | /* restore current context */
|
---|
| 442 | if (pop->caller != NULL)
|
---|
| 443 | {
|
---|
[26] | 444 | CF_Debug_Local_UpdateCtx (ctx,
|
---|
| 445 | pop->caller->file,
|
---|
| 446 | pop->caller->func,
|
---|
| 447 | pop->caller->line);
|
---|
[9] | 448 | }
|
---|
| 449 |
|
---|
[14] | 450 | context->callstack.caller = pop->caller;
|
---|
[6] | 451 | free (pop);
|
---|
| 452 |
|
---|
| 453 | return CF_OK;
|
---|
| 454 | }
|
---|