1 #include "lightsymbols.h" 2 3 LightSymbol::PLightSymbol LightSymbol::lsFrames[1000]; 4 HANDLE LightSymbol::handleFrames[1000]; 5 SZ* LightSymbol::fileNames; 6 bool LightSymbol::busted = false; 7 8 9 LightSymbol::LightSymbol(const char* sym, int fileId, int lineNumber) { 10 while (busted) { 11 busted = busted; 12 } 13 this->sym = sym; 14 this->fileId = fileId; 15 this->lineNumber = lineNumber; 16 17 LightSymbol** container = getThreadFrameContainer(); 18 19 parentFrame = *container; 20 *container = this; // shortcut for get+set current frame 21 } 22 23 LightSymbol::~LightSymbol() { 24 25 // assert if (GetCurrentFrame() != this) { 26 27 SetCurrentFrame(parentFrame); 28 } 29 30 bool LightSymbol::GetCallStack(char* sz, int len, const char* separator) { 31 LightSymbol* ls = GetCurrentFrame(); 32 if (ls == 0) { 33 return false; 34 } else { 35 return ls->GetCallStackCore(sz, len, separator); 36 } 37 } 38 39 LightSymbol** LightSymbol::getThreadFrameContainer() { 40 //pthread_t t = pthread_self(); 41 HANDLE h = (HANDLE)GetCurrentThreadId(); // f, keep handle so I don't have to recompie tyhe whole app; update toi DWORD one I really need changes in header file 42 int i = 0; 43 while (handleFrames[i] != h && handleFrames[i] != NULL && i < 1000 - 1) { 44 i++; 45 } 46 if (handleFrames[i] == h) { 47 return &lsFrames[i]; 48 } 49 handleFrames[i] = h; 50 return &lsFrames[i]; 51 } 52 53 bool LightSymbol::GetCallStackCore(char* sz, int len, const char* separator) const { 54 if (busted) { 55 return false; 56 } 57 if (fileNames == NULL) { // f multithreading synchr 58 FILE* log = fopen("d:\\edisonn\\log.txt", "wt"); 59 60 if (log) { fprintf(log, "build\n");fflush(log); } 61 62 char szLine[10000]; 63 FILE* file = fopen(getenv(LIGHT_SYMBOLS_FILE), "rt"); 64 if (file == NULL) { 65 busted = true; 66 return false; 67 } 68 69 const char* trimed; 70 71 // count number of lines 72 int id; 73 int entries = 0; 74 while (true) { 75 id = -1; 76 if (fscanf(file, "%i", &id) == 0) break; 77 if (id == -1) break; 78 if (entries <= id + 1) { 79 entries = id + 1; 80 } 81 *szLine = '\0'; 82 fgets(szLine, 10000, file); 83 trimed = trim(szLine); 84 } 85 86 fclose(file); 87 file = fopen(getenv(LIGHT_SYMBOLS_FILE), "rt"); 88 if (file == NULL) { 89 busted = true; 90 return false; // f this 91 } 92 93 if (log) { fprintf(log, "entries: %i\n", entries);fflush(log); } 94 95 SZ* __fileNames = new SZ[entries]; 96 97 while (true) { 98 id = -1; 99 if (fscanf(file, "%i", &id) == 0) break; 100 if (id == -1) break; 101 *szLine = '\0'; 102 fgets(szLine, 10000, file); 103 trimed = trim(szLine); 104 105 if (log) { fprintf(log, "%i, %s", id, trimed); } 106 107 // ass u me the file is correct 108 109 __fileNames[id] = new char[strlen(trimed) + 1]; 110 if (log) { fprintf(log, " - ");fflush(log); } 111 strcpy(__fileNames[id], trimed); 112 if (log) { fprintf(log, " _ \n");fflush(log); } 113 } 114 fclose(file); 115 fileNames = __fileNames; 116 if (log) { fclose(log); } 117 } 118 119 const LightSymbol* ls = this; 120 char* szOut = sz; 121 // f security 122 while (ls != NULL && len > 1000) { 123 sprintf(szOut, "%s, %s:%i%s", ls->sym, fileNames[ls->fileId], ls->lineNumber, separator); 124 while (*szOut && len > 0) { 125 szOut++; 126 len--; 127 } 128 ls = ls->parentFrame; 129 } 130 131 int more = 0; 132 while (ls != NULL) { 133 ls = ls->parentFrame; 134 } 135 136 if (more > 0) { 137 sprintf(szOut, " ... %i more frames. allocate more memory!", more); 138 } 139 140 return true; 141 } 142 143 LightSymbol* LightSymbol::GetCurrentFrame() { 144 return *getThreadFrameContainer(); 145 } 146 147 void LightSymbol::SetCurrentFrame(LightSymbol* ls) { 148 *getThreadFrameContainer() = ls; 149 } 150 151 const char* LightSymbol::trim(char* sz) { 152 if (sz == NULL) return NULL; 153 154 while (*sz == ' ' || *sz == '\t' || *sz == '\r' || *sz == '\n' || *sz == ',') 155 sz++; 156 157 if (*sz == '\0') return sz; 158 159 int len = strlen(sz); 160 char* start = sz; 161 sz = sz + (len - 1); 162 163 while (sz >= start && (*sz == ' ' || *sz == '\t' || *sz == '\r' || *sz == '\n' || *sz == ',')) { 164 *sz = '\0'; 165 sz--; 166 } 167 168 return start; 169 } 170