00001
00002
00003 #include "general.h"
00004
00006
00007 LOStream *LSys::_pSys=&_Error;
00008
00009 char *LSys::sInfo=GRN"[I]"NOR" ",*LSys::sInfoEnd="\n",
00010 *LSys::sWarning=YEL"[W]"NOR" ",
00011 *LSys::sWarningEnd="\n",
00012 *LSys::sError=RED"[E]"NOR" ",*LSys::sErrorEnd="\n",
00013 *LSys::sFatal=HIR"[F]"NOR" ",*LSys::sFatalEnd="\n";
00014 #ifdef _DEBUG
00015 char *LSys::sDebug=HIC"[D]"NOR" ",*LSys::sDebugEnd="\n";
00016 #endif
00017
00018 LFDStream _IO(0,1);
00019 LFDStream _Error(-1,2);
00020 LFDStream _Null;
00021
00022 int LOStream::SafeWrite(const char *data, int size, int nretry, long udel)
00023 {
00024 int retval, retry, nwritten;
00025
00026 retry=nwritten=0;
00027 l_retry:
00028 retval=Write(data+nwritten, size-nwritten);
00029 if(retval<0 && (nretry==-1 || ++retry < nretry)) {
00030 SYSERROR("SafeWrite: Failed to write "<<size-nwritten<<
00031 " bytes to file, retrying...");
00032 udelay(udel);
00033 goto l_retry;
00034 }
00035 else if(retval < size-nwritten){
00036 nwritten+=retval;
00037 if(nretry==-1 || ++retry < nretry) {
00038 ERROR("SafeWrite: Written "<<nwritten<<" out of "<<size<<
00039 " bytes, trying to write remaining...");
00040 goto l_retry;
00041 }
00042 }
00043 return retval;
00044 }
00046
00047 unsigned UpdateCRC(unsigned crc, unsigned char *buf, int len)
00048 {
00049 static unsigned _CRCTable[256]=
00050 {
00051 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
00052 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
00053 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
00054 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
00055 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
00056 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
00057 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
00058 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
00059 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
00060 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
00061 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
00062 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
00063 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
00064 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
00065 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
00066 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
00067 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
00068 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
00069 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
00070 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
00071 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
00072 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
00073 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
00074 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
00075 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
00076 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
00077 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
00078 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
00079 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
00080 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
00081 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
00082 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
00083 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
00084 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
00085 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
00086 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
00087 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
00088 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
00089 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
00090 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
00091 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
00092 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
00093 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
00094 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
00095 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
00096 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
00097 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
00098 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
00099 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
00100 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
00101 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
00102 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
00103 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
00104 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
00105 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
00106 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
00107 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
00108 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
00109 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
00110 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
00111 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
00112 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
00113 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
00114 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
00115 };
00116 unsigned c=crc^0xFFFFFFFF;
00117 int n;
00118
00119 for (n = 0; n < len; n++)
00120 c = _CRCTable[(c ^ buf[n]) & 0xff] ^ (c >> 8);
00121 return c ^ 0xFFFFFFFF;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 LOStream & LOStream::operator <<(void *p)
00186 {
00187 (*this << '^').FmtOutput((long)p,Hex|NoPrefix|ZeroFill);
00188 return *this;
00189 }
00190
00191 void LOStream::FmtOutput(long l, unsigned format, int )
00192 {
00193 if((format & Hex) && (format & ZeroFill))
00194 Printf("%08X", l);
00195 else Printf("%d",l);
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 }
00227
00228 #include <stdio.h>
00229 void LOStream::FmtOutput(double d)
00230 {
00231 Printf("%.14g",d);
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 }
00253 int LOStream::vPrintf(const char *fmt, va_list ap)
00254 {
00255 char outbuf[LIStream::BUFLEN];
00256 int l;
00257 l=vsprintf(outbuf, fmt, ap);
00258 Write(outbuf, l);
00259 return l;
00260 }
00261
00262
00263
00264
00265 LIStream &LIStream::operator >>(int &a)
00266 {
00267 char inbuf[100];
00268 ReadWord(inbuf,BUFLEN);
00269 StrScanInt(inbuf,a);
00270 return *this;
00271 }
00272
00273 LIStream &LIStream::operator >>(double &a)
00274 {
00275 char inbuf[100];
00276 ReadWord(inbuf,BUFLEN);
00277 StrScanDouble(inbuf,a);
00278 return *this;
00279 }
00280
00281 LIStream &LIStream::operator >>(char *a)
00282 { ReadWord(a,BUFLEN); return *this;}
00283
00285
00286 const char *StrCaseStr(const char *s1, const char *s2)
00287 {
00288 ASSERT(s1!=NULL && s2!=NULL);
00289 int sl1, sl2, i;
00290 sl1=StrLen(s1); sl2=StrLen(s2);
00291
00292 for(i=0;i<=sl1-sl2;i++) if(StrNCaseCmp(s1+i, s2, sl2)==0) return s1+i;
00293 return NULL;
00294 }
00295 const char *StrSkipSpaces(const char *str)
00296 {
00297 ASSERT(str!=NULL);
00298 const char *ret=str;
00299 while(IsSpace(*ret)) ret++;
00300 return ret;
00301 }
00302 void StrSnipSpaces(char *str)
00303 {
00304 char *ret;
00305 for(ret=str+StrLen(str)-1; ret>=str && IsSpace(*ret); ret--);
00306 ret[1]='\0';
00307 }
00308
00309 bool StrScanInt(const char *s, int &i)
00310 {
00311 return sscanf(s,"%d",&i)==1;
00312 }
00313 bool StrScanLong(const char *s, long &i)
00314 {
00315 return sscanf(s,"%ld",&i)==1;
00316 }
00317 bool StrScanDouble(const char *s, double &d)
00318 {
00319 return sscanf(s,"%lf",&d)==1;
00320 }
00321 #ifndef NO_VSNPRINTF
00322 int StrNPrintf(char *str, int size, const char *fmt...)
00323 {
00324 int ret;
00325 va_list ap;
00326 va_start(ap, fmt);
00327 ret=vsnprintf(str,size, fmt, ap);
00328 va_end(ap);
00329 return ret;
00330 }
00331 #endif
00332 int StrPrintf(char *str, const char *fmt...)
00333 {
00334 int ret;
00335 va_list ap;
00336 va_start(ap, fmt);
00337 ret=vsprintf(str, fmt, ap);
00338 va_end(ap);
00339 return ret;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 const unsigned _CharTable[256] =
00360 {
00361 0, 0, 0, 0,
00362 0, 0, 0, 0,
00363 0, _cS|_cP, _cS|_cP, 0,
00364 _cS, _cS, 0, 0,
00365 0, 0, 0, 0,
00366 0, 0, 0, 0,
00367 0, 0, 0, 0,
00368 0, 0, 0, 0,
00369 _cS|_cP, _cP, _cP, _cP,
00370 _cP, _cP, _cP, _cP,
00371 _cP, _cP, _cP, _cP,
00372 _cP, _cP, _cP, _cP,
00373 _cN|_cX|_cP, _cN|_cX|_cP,
00374 _cN|_cX|_cP, _cN|_cX|_cP,
00375 _cN|_cX|_cP, _cN|_cX|_cP,
00376 _cN|_cX|_cP, _cN|_cX|_cP,
00377 _cN|_cX|_cP, _cN|_cX|_cP, _cP, _cP,
00378 _cP, _cP, _cP, _cP,
00379 _cP, _cU|_cX|_cP, _cU|_cX|_cP, _cU|_cX|_cP,
00380 _cU|_cX|_cP, _cU|_cX|_cP,
00381 _cU|_cX|_cP, _cU|_cP,
00382 _cU|_cP, _cU|_cP, _cU|_cP, _cU|_cP,
00383 _cU|_cP, _cU|_cP, _cU|_cP, _cU|_cP,
00384 _cU|_cP, _cU|_cP, _cU|_cP, _cU|_cP,
00385 _cU|_cP, _cU|_cP, _cU|_cP, _cU|_cP,
00386 _cU|_cP, _cU|_cP, _cU|_cP, _cP,
00387 _cP, _cP, _cP, _cP,
00388 _cP, _cL|_cX|_cP, _cL|_cX|_cP, _cL|_cX|_cP,
00389 _cL|_cX|_cP, _cL|_cX|_cP,
00390 _cL|_cX|_cP, _cL|_cP,
00391 _cL|_cP, _cL|_cP, _cL|_cP, _cL|_cP,
00392 _cL|_cP, _cL|_cP, _cL|_cP, _cL|_cP,
00393 _cL|_cP, _cL|_cP, _cL|_cP, _cL|_cP,
00394 _cL|_cP, _cL|_cP, _cL|_cP, _cL|_cP,
00395 _cL|_cP, _cL|_cP, _cL|_cP, _cP,
00396 _cP, _cP, _cP, 0,
00397 0, 0, 0, 0,
00398 0, 0, 0, 0,
00399 0, 0, 0, 0,
00400 0, 0, 0, 0,
00401 0, 0, 0, 0,
00402 0, 0, 0, 0,
00403 0, 0, 0, 0,
00404 0, 0, 0, 0,
00405 0, 0, 0, 0,
00406 0, 0, 0, 0,
00407 0, 0, 0, 0,
00408 0, 0, 0, 0,
00409 0, 0, 0, 0,
00410 0, 0, 0, 0,
00411 0, 0, 0, 0,
00412 0, 0, 0, 0,
00413 0, 0, 0, 0,
00414 0, 0, 0, 0,
00415 0, 0, 0, 0,
00416 0, 0, 0, 0,
00417 0, 0, 0, 0,
00418 0, 0, 0, 0,
00419 0, 0, 0, 0,
00420 0, 0, 0, 0,
00421 0, 0, 0, 0,
00422 0, 0, 0, 0,
00423 0, 0, 0, 0,
00424 0, 0, 0, 0,
00425 0, 0, 0, 0,
00426 0, 0, 0, 0,
00427 0, 0, 0, 0,
00428 0, 0, 0, 0,
00429 };
00430
00431
00433
00434 #ifdef _DEBUG
00435
00436 #undef new
00437 #include <malloc.h>
00438
00439 static const unsigned MEM_HEAD =0xA1A3A5A7;
00440 static const unsigned MEM_START=0xC1C3C5C7;
00441 static const unsigned MEM_END =0xF1F3F5F7;
00442 static const char MEM_NEW='?';
00443 static const char MEM_DEL='!';
00444
00445 struct MemBlk
00446 {
00447 unsigned m_bHead;
00448 unsigned m_nSize;
00449 unsigned m_nLine;
00450 enum MemType m_bType;
00451 char m_cFile[0x20];
00452 MemBlk *m_pNext;
00453 unsigned m_bStart;
00454 unsigned char m_Data[sizeof(unsigned)];
00455 };
00456
00457 static LOStream &operator <<(LOStream &os, MemBlk &bl)
00458 {
00459 os << "[" << (bl.m_bType==M_Array?"Array": bl.m_bType==M_Obj? "Object":
00460 bl.m_bType==M_Alloc?"Alloc":"Unknown")
00461 << " size:" << bl.m_nSize << ", file: " << bl.m_cFile
00462 << ", line: " << bl.m_nLine << "]";
00463 return os;
00464 }
00465
00466 inline unsigned WB(unsigned u)
00467 { return (u+sizeof(unsigned)-1)&~(sizeof(unsigned)-1); }
00468
00469 static MemBlk *m_pFirstBlk=0;
00470 struct MemBlk *MemBlkPtr(void *pmem)
00471 {
00472 return (struct MemBlk *)(((char *)pmem)-sizeof(MemBlk)+sizeof(unsigned));
00473 }
00474
00475 static void MemValid(struct MemBlk *p, const char *file, int line)
00476 {
00477 if(p->m_bHead!=MEM_HEAD)
00478 {
00479 if((char)p->m_bHead==MEM_NEW)
00480 FATAL("Not a block (inside another new block?) ("
00481 << file<<','<<line<<")");
00482 if((char)p->m_bHead==MEM_DEL)
00483 FATAL("Not a block (already deleted?) ("
00484 << file<<','<<line<<")");
00485 FATAL("Not a block (variables on stack?)("
00486 << file<<','<<line<<")");
00487 }
00488 if(p->m_bStart!=MEM_START)
00489 WARNING("Subscript underflow" << *p<<"("<<
00490 file<<','<<line<<")");
00491 if(*(unsigned *)(p->m_Data+WB(p->m_nSize))!=MEM_END)
00492 WARNING("Subscript overflow" << *p<<"("<<
00493 file<<','<<line<<")");
00494 }
00495
00496 void MemChk(const char *file, unsigned line)
00497 {
00498 MemBlk **q;
00499 for(q=&m_pFirstBlk; *q!=0; q=&((*q)->m_pNext)) MemValid(*q, file, line);
00500 }
00501 void MemDump(const char *file, unsigned line)
00502 {
00503 MemBlk **q;
00504 DUMP("Begin memory block dumping");
00505 for(q=&m_pFirstBlk; *q!=0; q=&((*q)->m_pNext)) {
00506 MemValid(*q, file, line);
00507 *LSys::_pSys << **q << '\n';
00508 }
00509 DUMP("End memory block dumping");
00510 }
00511
00512
00513 void *MemAlloc(size_t size, const char *file, unsigned line, enum MemType mt)
00514 {
00515 MemChk(file, line);
00516 MemBlk *p=(MemBlk *)malloc(sizeof(struct MemBlk)+WB(size));
00517
00518 ASSERTMSG(p!=0, "Out of memory: File " << file << ", Line " << line);
00519 p->m_bType=mt;
00520 p->m_nSize=size;
00521 p->m_nLine=line;
00522 StrNCpy(p->m_cFile, file, sizeof(p->m_cFile)-1);
00523 p->m_cFile[sizeof(p->m_cFile)-1]='\0';
00524 p->m_bHead=MEM_HEAD;
00525 p->m_bStart=MEM_START;
00526
00527 *(unsigned *)(p->m_Data+WB(p->m_nSize))=MEM_END;
00528 StrMemSet(p->m_Data,MEM_NEW,size);
00529 p->m_pNext=m_pFirstBlk;
00530 m_pFirstBlk=p;
00531 return p->m_Data;
00532 }
00533
00534 void MemFree(void *pp, const char *file, unsigned line, enum MemType mt)
00535 {
00536 MemChk(file, line);
00537 if(pp==0) return;
00538 MemBlk *p=MemBlkPtr(pp);
00539
00540
00541 MemBlk **q;
00542 MemValid(p, file, line);
00543 if(mt!=p->m_bType)
00544 DUMP("Memory type inconsistant during MemFree("<<
00545 file<<','<<line<<")"<<*p);
00546 for(q=&m_pFirstBlk; *q!=0; q=&((*q)->m_pNext))
00547 {
00548 if(*q==p)
00549 {
00550 *q=p->m_pNext;
00551 StrMemSet(p, MEM_DEL, sizeof(MemBlk)+WB(p->m_nSize));
00552 free(p);
00553 return;
00554 }
00555 else MemValid(*q, file, line);
00556 }
00557 NOTREACHEDMSG("Block not found during MemFree("<<
00558 file<<','<<line<<")" << *p);
00559 }
00560
00561 void *MemRealloc(void *p, size_t s, const char *file, unsigned line,
00562 enum MemType mt)
00563 {
00564 MemChk(file, line);
00565 if(p==0) return MemAlloc(s,file,line,mt);
00566
00567 void *pmem;
00568 MemBlk *pb=MemBlkPtr(p);
00569
00570 MemValid(pb, file, line);
00571
00572
00573 if(s > pb->m_nSize)
00574 {
00575 pmem=MemAlloc(s, file, line, mt);
00576 StrMemCpy(pmem, p, pb->m_nSize);
00577 MemFree(p,file,line,M_Alloc);
00578 return pmem;
00579 }
00580 else if(s < pb->m_nSize) {
00581 pb=(MemBlk *)realloc(pb,sizeof(struct MemBlk)+WB(s));
00582 ASSERTMSG(pb!=0, "Out of memory: File " << file << ", Line " << line);
00583
00584 pb->m_bType=mt;
00585 pb->m_nSize=s;
00586 pb->m_nLine=line;
00587 StrNCpy(pb->m_cFile, file, sizeof(pb->m_cFile)-1);
00588 pb->m_cFile[sizeof(pb->m_cFile)-1]='\0';
00589
00590
00591
00592 *(unsigned *)(pb->m_Data+WB(pb->m_nSize))=MEM_END;
00593 }
00594 return pb->m_Data;
00595 }
00596
00597
00598 static void MemInit()
00599 {
00600 }
00601
00602 static void MemCleanup()
00603 {
00604 struct MemBlk *p;
00605 while((p=m_pFirstBlk)!=0)
00606 {
00607 DUMP("Block not free" << *p);
00608 MemFree(p->m_Data,"<cleanup>",0,p->m_bType);
00609 }
00610 }
00611
00612 int LSys::m_Init=0;
00613
00614 static void iDUMP(const char *s)
00615 {
00616 write(2,LSys::sDebug, strlen(LSys::sDebug));
00617 write(2,s,strlen(s));
00618 write(2,"\n",1);
00619 fsync(2);
00620 }
00621
00622 static const char *print_siginfo(pid_t pid, siginfo_t *siginfo)
00623 {
00624 static char printbuf[200];
00625 const char *signame;
00626 const char *si_code_name;
00627 static char info[100];
00628
00629 si_code_name=0;
00630 info[0]=0;
00631
00632 switch (siginfo->si_code) {
00633 case SI_USER:
00634 si_code_name="(SI_USER) kill, sigsend or raise";
00635 sprintf(info, "uid=%d, pid=%d",
00636 (int)siginfo->si_uid,(int)siginfo->si_pid);
00637 break;
00638
00639 case SI_QUEUE:
00640 si_code_name="(SI_QUEUE) sigqueue";
00641 sprintf(info, "uid=%d, pid=%d",
00642 (int)siginfo->si_uid, (int)siginfo->si_pid);
00643 break;
00644 case SI_TIMER:
00645 si_code_name="(SI_TIMER) timer expired";
00646 break;
00647 case SI_MESGQ:
00648 si_code_name="(SI_MESGQ) mesq state changed";
00649 break;
00650 case SI_ASYNCIO:
00651 si_code_name="(SI_ASYNCIO) AIO completed";
00652 break;
00653
00654 }
00655 switch (siginfo->si_signo) {
00656 case SIGHUP: signame="SIGHUP"; break;
00657 case SIGINT: signame="SIGINT"; break;
00658 case SIGQUIT: signame="SIGQUIT"; break;
00659 case SIGILL: signame="SIGILL";
00660 sprintf(info, "addr=^%X", (unsigned)(siginfo->si_addr));
00661 switch (siginfo->si_code) {
00662 case ILL_ILLOPC: si_code_name="(ILL_ILLOPC) illegal opcode";
00663 break;
00664 case ILL_ILLOPN: si_code_name="(ILL_ILLOPN) illegal operand";
00665 break;
00666 case ILL_ILLADR: si_code_name="(ILL_ILLADR) illegal addressing mode";
00667 break;
00668 case ILL_ILLTRP: si_code_name="(ILL_ILLTRP) illegal trap";
00669 break;
00670 case ILL_PRVOPC: si_code_name="(ILL_PRVOPC) privileged opcode";
00671 break;
00672 case ILL_PRVREG: si_code_name="(ILL_PRVREG) privileged register";
00673 break;
00674 case ILL_COPROC: si_code_name="(ILL_COPROC) coprocessor error";
00675 break;
00676 case ILL_BADSTK: si_code_name="(ILL_BADSTK) internal stack error";
00677 break;
00678 }
00679 break;
00680 case SIGTRAP: signame="SIGTRAP"; break;
00681 case SIGABRT: signame="SIGABRT"; break;
00682 case SIGFPE: signame="SIGFPE";
00683 sprintf(info, "addr=^%X", (unsigned)(siginfo->si_addr));
00684 switch (siginfo->si_code) {
00685 case FPE_INTDIV:
00686 si_code_name="(FPE_INTDIV) integer divide by zero";
00687 break;
00688 case FPE_INTOVF:
00689 si_code_name="(FPE_INTOVF) integer overflow";
00690 break;
00691 case FPE_FLTDIV:
00692 si_code_name="(FPE_FLTDIV) floating point divide by zero";
00693 break;
00694 case FPE_FLTOVF:
00695 si_code_name="(FPE_FLTOVF) floating point overflow";
00696 break;
00697 case FPE_FLTUND:
00698 si_code_name="(FPE_FLTUND) floating point underflow";
00699 break;
00700 case FPE_FLTRES:
00701 si_code_name="(FPE_FLTRES) floating point inexact result";
00702 break;
00703 case FPE_FLTINV:
00704 si_code_name="(FPE_FLTINV) floating point invalid operation";
00705 break;
00706 case FPE_FLTSUB:
00707 si_code_name="(FPE_FLTSUB) subscript out of range";
00708 break;
00709 }
00710 break;
00711 case SIGKILL: signame="SIGKILL"; break;
00712 case SIGBUS: signame="SIGBUS";
00713 sprintf(info, "addr=^%X", (unsigned)(siginfo->si_addr));
00714 switch (siginfo->si_code) {
00715 case BUS_ADRALN:
00716 si_code_name="(BUS_ADRALN) invalid address alignment";
00717 break;
00718 case BUS_ADRERR:
00719 si_code_name="(BUS_ADRERR) non-existant physical address";
00720 break;
00721 case BUS_OBJERR:
00722 si_code_name="(BUS_OBJERR) object specific hardware error";
00723 break;
00724 }
00725 break;
00726 case SIGSEGV: signame="SIGSEGV";
00727 sprintf(info, "addr=^%X", (unsigned)(siginfo->si_addr));
00728 switch (siginfo->si_code) {
00729 case SEGV_MAPERR:
00730 si_code_name="(SEGV_MAPERR) address not mapped to object";
00731 break;
00732 case SEGV_ACCERR:
00733 si_code_name="(SEGV_ACCERR) invalid permissions for mapped object";
00734 break;
00735 }
00736 break;
00737 case SIGSYS: signame="SIGSYS"; break;
00738 case SIGPIPE: signame="SIGPIPE"; break;
00739 case SIGALRM: signame="SIGALRM"; break;
00740 case SIGTERM: signame="SIGTERM"; break;
00741 case SIGCHLD: signame="SIGCHLD";
00742 sprintf(info, "pid=%d, status=%x", (int)siginfo->si_pid,
00743 (int)siginfo->si_status);
00744 switch (siginfo->si_code) {
00745 case CLD_EXITED:
00746
00747 return 0;
00748
00749
00750 case CLD_KILLED:
00751 si_code_name="(CLD_KILLED) child was killed";
00752 break;
00753 case CLD_DUMPED:
00754 si_code_name="(CLD_DUMPED) child terminated abnormally";
00755 break;
00756 case CLD_TRAPPED:
00757 si_code_name="(CLD_TRAPPED) traced child has trapped";
00758 break;
00759 case CLD_STOPPED:
00760 si_code_name="(CLD_STOPPED) child has stopped";
00761 break;
00762 case CLD_CONTINUED:
00763 si_code_name="(CLD_CONTINUED) stopped child has continued";
00764 break;
00765 }
00766 break;
00767 case SIGUSR1: signame="SIGUSR1"; break;
00768 case SIGUSR2: signame="SIGUSR2"; break;
00769 default:
00770 signame="SIG???";
00771 }
00772 if(si_code_name==0)
00773 {
00774 static char si_code_buf[20];
00775 sprintf(si_code_buf, "(%d) Unknown si_code", siginfo->si_code);
00776 si_code_name=si_code_buf;
00777 }
00778 if(info[0])
00779 sprintf(printbuf, HIR"[%d]%s%s[%s](%s)"NOR"\n", (int)pid,signame,
00780 si_code_name, info, strerror(siginfo->si_errno));
00781 else sprintf(printbuf, HIR"[%d]%s%s(%s)"NOR"\n", (int)pid,signame,
00782 si_code_name, strerror(siginfo->si_errno));
00783 return printbuf;
00784 }
00785
00786 void LSys::sig_handler(int, siginfo_t *sig, void *)
00787 {
00788 const char *str=print_siginfo(getpid(), sig);
00789 if(str) {
00790 write(2, str, strlen(str));
00791 fsync(2);
00792 }
00793
00794 switch(sig->si_signo) {
00795
00796 case SIGSEGV:
00797 case SIGBUS:
00798 case SIGILL:
00799 _exit(1);
00800
00801 case SIGCHLD:
00802 default:;
00803 }
00804 }
00805
00806 LSys::LSys()
00807 {
00808 if(!m_Init)
00809 {
00810 struct sigaction sa;
00811 sa.sa_flags=SA_SIGINFO;
00812 sa.sa_sigaction=sig_handler;
00813
00814
00815
00816
00817 if(sigaction(SIGFPE, &sa, 0)!=0) iDUMP("Error in sigaction(SIGFPE)");
00818 if(sigaction(SIGBUS, &sa, 0)!=0) iDUMP("Error in sigaction(SIGBUS)");
00819 if(sigaction(SIGSEGV, &sa, 0)!=0) iDUMP("Error in sigaction(SIGSEGV)");
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831 MemInit();
00832 iDUMP("Program starting...");
00833 }
00834 m_Init++;
00835 }
00836
00837 #include <sys/time.h>
00838 #include <sys/resource.h>
00839 LSys::~LSys()
00840 {
00841 if(!--m_Init)
00842 {
00843 struct rusage ru;
00844 MemCleanup();
00845
00846 getrusage(RUSAGE_SELF,&ru);
00847 DUMP("CPU time spent(in seconds):"<<
00848 ru.ru_utime.tv_sec+1e-6*ru.ru_utime.tv_usec);
00849 }
00850 }
00851
00852 void *operator new[](size_t size) throw(bad_alloc)
00853 {
00854 return MemAlloc(size, "<new[]>", 0, M_Array);
00855 }
00856 void *operator new[](size_t size, const char *file, unsigned line)
00857 throw(bad_alloc)
00858 {
00859 return MemAlloc(size, file, line, M_Array);
00860 }
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872 void operator delete[](void *pmem) throw()
00873 {
00874 if(pmem!=0) MemFree(pmem,"<delete[]>",0, M_Array);
00875 }
00876
00877 void *operator new(size_t size)throw(bad_alloc)
00878 {
00879 return MemAlloc(size, "<new>", 0, M_Obj);
00880 }
00881 void *operator new(size_t size, const char *file, unsigned line)
00882 throw(bad_alloc)
00883 {
00884 return MemAlloc(size, file, line, M_Obj);
00885 }
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895 void operator delete(void *pmem)throw()
00896 {
00897 if(pmem!=0) MemFree(pmem,"<delete>",0,M_Obj);
00898 }
00899
00900 #endif //_DEBUG
00901
00902 #ifdef GENERAL_TEST
00903 #define new new(__FILE__,__LINE__)
00904
00905 int main()
00906 {
00907 {
00908 _IO << "=*= Message system\n";
00909 INFO("This is a test message");
00910 WARNING("This is a test warning");
00911 ERROR("This is a test error");
00912
00913 DUMP("This is a test debug dump");
00914 }
00915
00916 {
00917 char str[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
00918 _IO << "=*= CRC checking\n";
00919 _IO << "CRC of " << str << " is "
00920 << CRC((unsigned char *)str, sizeof(str)) << '\n';
00921 }
00922 {
00923 double data[10]={12.5,2,13,1,331,4,31,21,35,12};
00924 int index[10];
00925 _IO << "=*=QuickSort test\n";
00926 _IO << "[ ";
00927 for(int i=0;i<10;i++)
00928 _IO << data[i] << ' ';
00929 _IO << "];\n";
00930 IndexQuickSort(data,index, 10);
00931 _IO << "[ ";
00932 for(int i=0;i<10;i++)
00933 _IO << index[i] << ' ';
00934 _IO << "];\n";
00935 }
00936 {
00937 char strbuf[200];
00938 double sec= 231342;
00939 _IO << "=*=LTime demo\n";
00940 _IO << "PrettyPrint of "<<sec<<" time seconds:\n";
00941 LTime::PrettyPrint(strbuf, sizeof(strbuf), sec);
00942 _IO << strbuf << '\n';
00943 sec=3892740.233;
00944 _IO << "PrettyPrint of "<<sec<<" time seconds:\n";
00945 LTime::PrettyPrint(strbuf, sizeof(strbuf), sec);
00946 _IO << strbuf << '\n';
00947 }
00948
00949
00950
00951
00952 return 0;
00953 }
00954
00955 #endif //_TEST