| 1 | /**************************************************************************** |
|---|
| 2 | * This file is part of the v2lin Library. |
|---|
| 3 | * VxWorks is a registered trademark of Wind River Systems, Inc. |
|---|
| 4 | * |
|---|
| 5 | * Copyright (C) 2006 Constantine Shulyupin, conan.sh@gmail.com |
|---|
| 6 | * |
|---|
| 7 | * The v2lin library is free software; you can redistribute it and/or |
|---|
| 8 | * modify it under the terms of the GNU Lesser General Public |
|---|
| 9 | * License as published by the Free Software Foundation; either |
|---|
| 10 | * version 2.1 of the License, or (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * The v2lin Library is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 15 | * Lesser General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | ****************************************************************************/ |
|---|
| 18 | |
|---|
| 19 | #include <stdio.h> |
|---|
| 20 | #include <unistd.h> |
|---|
| 21 | #include <signal.h> |
|---|
| 22 | #include "v2lpthread.h" |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include "v2ldebug.h" |
|---|
| 25 | #include <sys/wait.h> |
|---|
| 26 | |
|---|
| 27 | //_syscall0(pid_t,gettid) |
|---|
| 28 | |
|---|
| 29 | int tid; |
|---|
| 30 | extern char * _argv[]; |
|---|
| 31 | char trace_fn[100]; |
|---|
| 32 | struct timeval start_tv={0,0}; |
|---|
| 33 | int trace = 1; |
|---|
| 34 | struct timeval prev_tv={0,0}; |
|---|
| 35 | |
|---|
| 36 | void gdb() |
|---|
| 37 | { |
|---|
| 38 | static int gdb_pid; |
|---|
| 39 | char spid[20]; |
|---|
| 40 | snprintf(spid, 19, "%i", getpid()); |
|---|
| 41 | spid[19] = 0; |
|---|
| 42 | if (gdb_pid) { |
|---|
| 43 | TRACEF("gdb already started"); |
|---|
| 44 | exit(1); |
|---|
| 45 | } |
|---|
| 46 | gdb_pid = fork(); |
|---|
| 47 | if (gdb_pid == 0) { |
|---|
| 48 | // define _argv and open comment |
|---|
| 49 | // execlp("gdb", "gdb", "-q",_argv[0], spid, NULL); |
|---|
| 50 | } else { |
|---|
| 51 | waitpid(gdb_pid, NULL, 0); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void sighandler(int x) |
|---|
| 57 | { |
|---|
| 58 | gdb(); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | void signal_init() |
|---|
| 62 | { |
|---|
| 63 | signal(SIGSEGV,sighandler); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | void trace_time() |
|---|
| 67 | { |
|---|
| 68 | time_t time_cur; |
|---|
| 69 | time(&time_cur); |
|---|
| 70 | struct timeval now_tv,uptime={0,0},passed_tv={0,0}; |
|---|
| 71 | |
|---|
| 72 | struct timezone tz; |
|---|
| 73 | |
|---|
| 74 | gettimeofday(&now_tv, &tz); |
|---|
| 75 | if (!start_tv.tv_sec) { |
|---|
| 76 | start_tv = now_tv; |
|---|
| 77 | struct tm* now_tm; |
|---|
| 78 | now_tm = localtime(&time_cur); |
|---|
| 79 | fprintf(stderr,"time=%04d-%02d-%02d %02d:%02d:%02d.%06d\n", |
|---|
| 80 | now_tm->tm_year+1900, now_tm->tm_mon+1, now_tm->tm_mday, |
|---|
| 81 | now_tm->tm_hour,now_tm->tm_min,now_tm->tm_sec, (int)now_tv.tv_usec); |
|---|
| 82 | } |
|---|
| 83 | time_cur = now_tv.tv_sec; |
|---|
| 84 | timersub(&now_tv,&start_tv,&uptime); |
|---|
| 85 | if (timerisset(&prev_tv)) timersub(&now_tv,&prev_tv,&passed_tv); |
|---|
| 86 | |
|---|
| 87 | //if ( passed > 0.01 ) |
|---|
| 88 | if ( !timerisset(&passed_tv) || passed_tv.tv_usec > 10000) |
|---|
| 89 | { |
|---|
| 90 | fprintf(stderr,"uptime=%d.%06d s ",(int)uptime.tv_sec,(int)uptime.tv_usec); |
|---|
| 91 | fprintf(stderr,"+%d.%06d ",(int)passed_tv.tv_sec,(int)passed_tv.tv_usec); |
|---|
| 92 | fprintf(stderr,"\n"); |
|---|
| 93 | prev_tv = now_tv; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|