1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870 |
- /***************************************************************************
- * *
- * _____ ____ *
- * | __ \ / __ \ _ _ _____ *
- * | | \ \ / / \_\ | | | | _ \ *
- * | | \ \| | | | | | |_| | *
- * | | | || | | | | | ___/ *
- * | | / /| | __ | | | | _ \ *
- * | |__/ / \ \__/ / | |___| | |_| | *
- * |_____/ \____/ |_____|_|_____/ *
- * *
- * Wiimms source code library *
- * *
- ***************************************************************************
- * *
- * Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de> *
- * *
- ***************************************************************************
- * *
- * This library is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- * This library is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
- * *
- ***************************************************************************/
- #define DCLIB_DEBUG_C 1
- #include "dclib-debug.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include "dclib-basics.h"
- #include "dclib-file.h"
- #include "dclib-xdump.h"
- //
- ///////////////////////////////////////////////////////////////////////////////
- /////////////// error messages ///////////////
- ///////////////////////////////////////////////////////////////////////////////
- ccp TRACE_PREFIX = "";
- FILE *TRACE_FILE = 0;
- FILE *MEM_LOG_FILE = 0;
- ccp (*GetErrorNameHook)( int stat, ccp ret_not_found ) = 0;
- ccp (*GetErrorTextHook)( int stat, ccp ret_not_found ) = 0;
- ccp GENERIC_ERROR_MESSAGE = "generic err";
- ///////////////////////////////////////////////////////////////////////////////
- ccp GetErrorName
- (
- int stat, // error status, err := abs(stat)
- ccp ret_not_found // not NULL: return this, if no message is found
- // GENERIC_ERROR_MESSAGE: return a generic message
- )
- {
- if (GetErrorNameHook)
- {
- ccp res = GetErrorNameHook(stat,0);
- if ( res && *res )
- return res;
- }
- if ( stat < 0 )
- stat = -stat;
- switch(stat)
- {
- case ERR_OK: return "OK";
- case ERR_DIFFER: return "DIFFER";
- case ERR_NOTHING_TO_DO: return "NOTHING TO DO";
- case ERR_SOURCE_FOUND: return "SOURCE FOUND";
- case ERR_NO_SOURCE_FOUND: return "NO SOURCE";
- case ERR_JOB_IGNORED: return "JOB IGNORED";
- case ERR_SUBJOB_WARNING: return "SUB JOB WARNINGS";
- case ERR_NOT_EXISTS: return "NOT EXISTS";
- case ERR_WARNING: return "WARNING";
- case ERR_WRONG_FILE_TYPE: return "WRONG FILE TYPE";
- case ERR_INVALID_FILE: return "INVALID FILE";
- case ERR_INVALID_VERSION: return "INVALID VERSION";
- case ERR_INVALID_DATA: return "INVALID DATA";
- case ERR_ENCODING: return "ENCODING FAILED";
- case ERR_DECODING: return "DECODING FAILED";
- case ERR_ALREADY_EXISTS: return "FILE ALREADY EXISTS";
- case ERR_SUBJOB_FAILED: return "SUB JOB FAILED";
- case ERR_CANT_REMOVE: return "CAN'T REMOVE FILE";
- case ERR_CANT_RENAME: return "CAN'T RENAME FILE";
- case ERR_CANT_CLOSE: return "CAN'T CLOSE FILE";
- case ERR_CANT_CONNECT: return "CAN'T CONNECT";
- case ERR_CANT_OPEN: return "CAN'T OPEN FILE";
- case ERR_CANT_APPEND: return "CAN'T APPEND FILE";
- case ERR_CANT_CREATE: return "CAN'T CREATE FILE";
- case ERR_CANT_CREATE_DIR: return "CAN'T CREATE DIRECTORY";
- case ERR_READ_FAILED: return "READ FILE FAILED";
- case ERR_REMOVE_FAILED: return "REMOVE FILE FAILED";
- case ERR_WRITE_FAILED: return "WRITE FILE FAILED";
- case ERR_DATABASE: return "DATABASE ACCESS FAILED";
- case ERR_MISSING_PARAM: return "MISSING PARAMETERS";
- case ERR_SEMANTIC: return "SEMANTIC ERROR";
- case ERR_SYNTAX: return "SYNTAX ERROR";
- case ERR_INTERRUPT: return "INTERRUPT";
- case ERR_ERROR: return "ERROR";
- case ERR_NOT_IMPLEMENTED: return "NOT IMPLEMENTED YET";
- case ERR_INTERNAL: return "INTERNAL ERROR";
- case ERR_OUT_OF_MEMORY: return "OUT OF MEMORY";
- case ERR_FATAL: return "FATAL ERROR";
- }
- if ( ret_not_found != GENERIC_ERROR_MESSAGE )
- return ret_not_found;
- //--- generic messages
- if (GetErrorNameHook)
- {
- ccp res = GetErrorNameHook(stat,GENERIC_ERROR_MESSAGE);
- if ( res && *res )
- return res;
- }
- if ( stat >= ERU_WARN_00 & stat <= ERU_WARN_XX )
- {
- const uint buf_size = 20;
- char *buf = GetCircBuf(buf_size);
- snprintf(buf,buf_size,"USER WARNING #%02u", stat - ERU_WARN_00 );
- return buf;
- }
- if ( stat >= ERU_ERROR1_00 & stat <= ERU_ERROR1_XX )
- {
- const uint buf_size = 20;
- char *buf = GetCircBuf(buf_size);
- snprintf(buf,buf_size,"USER ERROR #%02u", stat - ERU_ERROR1_00 );
- return buf;
- }
- if ( stat >= ERU_ERROR2_00 & stat <= ERU_ERROR2_XX )
- {
- const uint buf_size = 20;
- char *buf = GetCircBuf(buf_size);
- snprintf(buf,buf_size,"USER ERROR #%02u",
- stat - ERU_ERROR2_00 + ERU_ERROR1_XX+1 - ERU_ERROR1_00 );
- return buf;
- }
- if ( stat >= ERU_FATAL_00 & stat <= ERU_FATAL_XX )
- {
- const uint buf_size = 24;
- char *buf = GetCircBuf(buf_size);
- snprintf(buf,buf_size,"USER FATAL ERROR #%02u", stat - ERU_FATAL_00 );
- return buf;
- }
- ccp res = GetErrorName(stat+1,"");
- return res && *res ? res : "?";
- }
- ///////////////////////////////////////////////////////////////////////////////
- ccp GetErrorText
- (
- int stat, // error status, err := abs(stat)
- ccp ret_not_found // not NULL: return this, if no message is found
- // GENERIC_ERROR_MESSAGE: return a generic message
- )
- {
- if (GetErrorTextHook)
- {
- ccp res = GetErrorTextHook(stat,0);
- if (res)
- return res;
- }
- if ( stat < 0 )
- stat = -stat;
- switch(stat)
- {
- case ERR_OK: return "Ok";
- case ERR_DIFFER: return "Files differ";
- case ERR_NOTHING_TO_DO: return "Nothing to do";
- case ERR_SOURCE_FOUND: return "Source found";
- case ERR_NO_SOURCE_FOUND: return "No source found";
- case ERR_JOB_IGNORED: return "Job ignored";
- case ERR_SUBJOB_WARNING: return "Sub job had warnings";
- case ERR_NOT_EXISTS: return "File does not exists";
- case ERR_WARNING: return "Unspecific warning";
- case ERR_WRONG_FILE_TYPE: return "Wrong type of file";
- case ERR_INVALID_FILE: return "Invalid file";
- case ERR_INVALID_VERSION: return "Invalid file version";
- case ERR_INVALID_DATA: return "Invalid data";
- case ERR_ENCODING: return "Encoding data failed";
- case ERR_DECODING: return "Decoding data failed";
- case ERR_ALREADY_EXISTS: return "File already exists";
- case ERR_SUBJOB_FAILED: return "Sub job failed";
- case ERR_CANT_REMOVE: return "Can't remove file";
- case ERR_CANT_RENAME: return "Can't rename file";
- case ERR_CANT_CLOSE: return "Can't close file";
- case ERR_CANT_CONNECT: return "Can't connect";
- case ERR_CANT_OPEN: return "Can't open file";
- case ERR_CANT_APPEND: return "Can't open file for appending";
- case ERR_CANT_CREATE: return "Can't create file";
- case ERR_CANT_CREATE_DIR: return "Can't create directory";
- case ERR_READ_FAILED: return "Reading from file failed";
- case ERR_REMOVE_FAILED: return "Removing a file failed";
- case ERR_WRITE_FAILED: return "Writing to file failed";
- case ERR_DATABASE: return "Access to database failed";
- case ERR_MISSING_PARAM: return "Missing at least one parameter";
- case ERR_SEMANTIC: return "Semantic error";
- case ERR_SYNTAX: return "Syntax error";
- case ERR_INTERRUPT: return "Program interrupted by user";
- case ERR_ERROR: return "Unspecific error";
- case ERR_NOT_IMPLEMENTED: return "Not implemented yet";
- case ERR_INTERNAL: return "Internal error";
- case ERR_OUT_OF_MEMORY: return "Allocation of dynamic memory failed";
- case ERR_FATAL: return "Unspecific fatal error";
- }
- if ( ret_not_found != GENERIC_ERROR_MESSAGE )
- return ret_not_found;
- //--- generic messages
- if (GetErrorTextHook)
- {
- ccp res = GetErrorTextHook(stat,GENERIC_ERROR_MESSAGE);
- if ( res && *res )
- return res;
- }
- if ( stat >= ERU_WARN_00 & stat <= ERU_WARN_XX )
- {
- const uint buf_size = 20;
- char *buf = GetCircBuf(buf_size);
- snprintf(buf,buf_size,"USER WARNING #%02u", stat - ERU_WARN_00 );
- return buf;
- }
- if ( stat >= ERU_ERROR1_00 & stat <= ERU_ERROR1_XX )
- {
- const uint buf_size = 20;
- char *buf = GetCircBuf(buf_size);
- snprintf(buf,buf_size,"USER ERROR #%02u", stat - ERU_ERROR1_00 );
- return buf;
- }
- if ( stat >= ERU_ERROR2_00 & stat <= ERU_ERROR2_XX )
- {
- const uint buf_size = 20;
- char *buf = GetCircBuf(buf_size);
- snprintf(buf,buf_size,"USER ERROR #%02u",
- stat - ERU_ERROR2_00 + ERU_ERROR1_XX+1 - ERU_ERROR1_00 );
- return buf;
- }
- if ( stat >= ERU_FATAL_00 & stat <= ERU_FATAL_XX )
- {
- const uint buf_size = 24;
- char *buf = GetCircBuf(buf_size);
- snprintf(buf,buf_size,"USER FATAL ERROR #%02u", stat - ERU_FATAL_00 );
- return buf;
- }
- ccp res = GetErrorText(stat+1,0);
- return res && *res ? res : "?";
- }
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- void ListErrorCodes
- (
- FILE * f, // valid output stream
- int indent, // indent of output
- bool all // true: print all user reserved messages too.
- )
- {
- if (!f)
- return;
- indent = NormalizeIndent(indent);
- fprintf(f,"\n%*sList of error messages:\n\n",indent,"");
- ccp p2 = all ? 0 : "";
- uint i, fw_name = 4, fw_text = 7;
- for ( i = 0; i < ERR__N; i++ )
- {
- ccp name = GetErrorName(i,p2);
- if (name)
- {
- const uint len = strlen(name);
- if ( fw_name < len )
- fw_name = len;
- }
- ccp text = GetErrorText(i,p2);
- {
- const uint len = strlen(text);
- if ( fw_text < len )
- fw_text = len;
- }
- }
- fprintf(f,
- "%*s err | %-*s | %-*s\n"
- "%*s-----+%.*s+%.*s\n",
- indent, "",
- fw_name, "name",
- fw_text, "message",
- indent, "",
- fw_name+2, Minus300,
- fw_text+2, Minus300 );
- for ( i = 0; i < ERR__N; i++ )
- {
- ccp name = GetErrorName(i,p2);
- ccp text = GetErrorText(i,p2);
- if ( name && *name || text && *text )
- fprintf(f,"%*s%4u | %-*s | %-*s\n",
- indent, "", i,
- fw_name, name ? name : "-",
- fw_text, text ? text : "-" );
- if ( i == ERR_OK || i == ERR_WARNING || i == ERR_ERROR )
- fprintf(f,"%*s-----+%.*s+%.*s\n",
- indent, "",
- fw_name+2, Minus300,
- fw_text+2, Minus300 );
- }
- fprintf(f,"%*s-----+%.*s+%.*s\n\n",
- indent, "",
- fw_name+2, Minus300,
- fw_text+2, Minus300 );
- }
- //
- ///////////////////////////////////////////////////////////////////////////////
- /////////////// error handling ///////////////
- ///////////////////////////////////////////////////////////////////////////////
- #ifdef EXTENDED_ERRORS
- ProgInfo_t ProgInfo = { .error_level = ERRLEV_EXTENDED };
- #else
- ProgInfo_t ProgInfo = { .error_level = ERRLEV_MINIMAL };
- #endif
- ///////////////////////////////////////////////////////////////////////////////
- enumError PrintError ( ccp func, ccp file, uint line,
- int syserr, enumError err_code, ccp format, ... )
- {
- va_list arg;
- va_start(arg,format);
- const int stat = PrintErrorArg(func,file,line,syserr,err_code,format,arg);
- va_end(arg);
- return stat;
- }
- ///////////////////////////////////////////////////////////////////////////////
- enumError PrintErrorFile ( ccp func, ccp file, uint line, struct File_t *F,
- int syserr, enumError err_code, ccp format, ... )
- {
- va_list arg;
- va_start(arg,format);
- const int stat = PrintErrorArg(func,file,line,syserr,err_code,format,arg);
- va_end(arg);
- return F ? RegisterFileError(F,stat) : stat;
- }
- ///////////////////////////////////////////////////////////////////////////////
- enumError PrintErrorArg ( ccp func, ccp file, unsigned int line,
- int syserr, enumError err_code, ccp format, va_list arg )
- {
- if ( err_code > ERR_OK )
- {
- ProgInfo.error_count++;
- ProgInfo.last_error = err_code;
- if ( ProgInfo.max_error < err_code )
- ProgInfo.max_error = err_code;
- }
- fflush(stdout);
- if (!stdwrn)
- {
- SetupStdMsg();
- if (!stdwrn)
- {
- stdwrn = stderr;
- if (!stdwrn)
- return err_code;
- }
- }
- char msg[1000];
- if (!ProgInfo.progname)
- ProgInfo.progname = "?";
- const int plen = strlen(ProgInfo.progname)+2;
- if ( !format && err_code > ERR_NOT_IMPLEMENTED )
- format = "Program is aborted immediately!";
- if (format)
- {
- vsnprintf(msg,sizeof(msg),format,arg);
- msg[sizeof(msg)-2] = 0;
- const int mlen = strlen(msg);
- if ( mlen > 0 && msg[mlen-1] != '\n' )
- {
- msg[mlen] = '\n';
- msg[mlen+1] = 0;
- }
- }
- else
- StringCat2S(msg,sizeof(msg),GetErrorText(err_code,0),"\n");
- ccp prefix = err_code == ERR_OK ? "" : err_code <= ERR_WARNING ? "! " : "!! ";
- const int fw = GetTermWidth(80,40) - 1;
- #ifdef DEBUG
- TRACE("%s%s #%d [%s] in %s() @ %s#%d\n",
- prefix, err_code <= ERR_WARNING ? "WARNING" : "ERROR",
- err_code, GetErrorName(err_code,0), func, file, line );
- TRACE("%s%*s%s",prefix,plen,"",msg);
- if (syserr)
- TRACE("!! %s(), ERRNO=%d: %s\n",__FUNCTION__,syserr,strerror(syserr));
- fflush(TRACE_FILE);
- #endif
- ccp coln, col1, col0;
- if (IsFileColorized(stdwrn))
- {
- coln = GetTextMode(1,TTM_COL_INFO);
- col1 = GetTextMode(1, err_code > ERR_WARNING ? TTM_COL_WARN : TTM_COL_HINT );
- col0 = TermTextModeReset;
- }
- else
- coln = col1 = col0 = EmptyString;
-
- if ( err_code >= ERR_NOT_IMPLEMENTED
- || ProgInfo.error_level >= ERRLEV_HEADING && err_code > ERR_WARNING )
- {
- if ( err_code >= ERR_NOT_IMPLEMENTED || ProgInfo.error_level >= ERRLEV_EXTENDED )
- {
- if ( err_code > ERR_WARNING )
- fprintf(stdwrn,"%s%s%s:%s ERROR #%d [%s] in %s() @ %s#%d%s\n",
- prefix, coln, ProgInfo.progname, col1, err_code,
- GetErrorName(err_code,0), func, file, line, col0 );
- else
- fprintf(stdwrn,"%s%s%s:%s WARNING in %s() @ %s#%d%s\n",
- prefix, coln, ProgInfo.progname, col1, func, file, line, col0 );
- }
- else
- {
- if ( err_code > ERR_WARNING )
- fprintf(stdwrn,"%s%s%s:%s ERROR #%d [%s]%s\n",
- prefix, coln, ProgInfo.progname, col1, err_code,
- GetErrorName(err_code,0), col0 );
- else
- fprintf(stdwrn,"%s%s%s:%s WARNING%s\n",
- prefix, coln, ProgInfo.progname, col1, col0 );
- }
- if ( ProgInfo.error_level >= ERRLEV_ANNOTATE )
- {
- #ifdef SVN_NEXT
- fprintf(stdwrn,"%s -> %s/%s?annotate=%d#l%d\n",
- prefix, URI_VIEWVC, file, SVN_NEXT, line );
- #elif defined(REVISION_NEXT)
- fprintf(stdwrn,"%s -> %s/%s?annotate=%d#l%d\n",
- prefix, URI_VIEWVC, file, REVISION_NEXT, line );
- #endif
- }
- fputs(prefix,stdwrn);
- PutLines(stdwrn,plen,fw,0,prefix,msg,0);
- }
- else
- {
- fprintf(stdwrn,"%s%s%s:%s",prefix,coln,ProgInfo.progname,col0);
- PutLines(stdwrn,plen,fw,strlen(ProgInfo.progname)+1,prefix,msg,0);
- }
- if (syserr)
- {
- fprintf(stdwrn,"%s%*s-> ",prefix,plen,"");
- snprintf(msg,sizeof(msg),"%s [%d]",strerror(syserr),syserr);
- PutLines(stdwrn,plen+3,fw,plen+3,prefix,msg,0);
- }
- fflush(stdwrn);
- if ( err_code > ERR_NOT_IMPLEMENTED )
- exit(err_code);
- return err_code;
- }
- ///////////////////////////////////////////////////////////////////////////////
- enumError PrintErrorStat ( enumError err, int verbose, ccp cmdname )
- {
- if ( verbose > 0 && err >= ERR_WARNING
- || verbose > 1 && err
- || err == ERR_NOT_IMPLEMENTED )
- {
- if (!stdwrn)
- {
- SetupStdMsg();
- if (!stdwrn)
- {
- stdwrn = stderr;
- if (!stdwrn)
- return err;
- }
- }
- fprintf(stdwrn,"%s: Command '%s' returns with status #%d [%s]\n",
- ProgInfo.progname, cmdname, err, GetErrorName(err,0) );
- }
- return err;
- }
- ///////////////////////////////////////////////////////////////////////////////
- bool mark_used ( ccp name, ... )
- {
- return true;
- }
- ///////////////////////////////////////////////////////////////////////////////
- #if HAVE_PRINT
- #undef PRINT_TIME
- void PRINT_TIME ( time_t time, ccp title )
- {
- struct tm utc, loc;
- gmtime_r(&time,&utc);
- localtime_r(&time,&loc);
- PRINT( "%s: UTC= %4u-%02u-%02u %2u:%02u:%02u dst=%d"
- " LOCAL= %4u-%02u-%02u %2u:%02u:%02u dst=%d\n",
- title ? title : "TIME",
- utc.tm_year+1900, utc.tm_mon+1, utc.tm_mday,
- utc.tm_hour, utc.tm_min, utc.tm_sec, utc.tm_isdst,
- loc.tm_year+1900, loc.tm_mon+1, loc.tm_mday,
- loc.tm_hour, loc.tm_min, loc.tm_sec, loc.tm_isdst );
- }
- #endif
- //
- ///////////////////////////////////////////////////////////////////////////////
- /////////////// hexdump ///////////////
- ///////////////////////////////////////////////////////////////////////////////
- uint HexDump16 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDump(f,indent,addr,4,16,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDump20 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDump(f,indent,addr,4,20,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDump ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
- const void * p_data, size_t count )
- {
- // return the number of printed lines
- if ( !f || !p_data || !count )
- return 0;
- XDump_t xd;
- InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
- const int stat = XDump(&xd,p_data,count,true);
- return stat < 0 ? 0 : stat;
- }
- ///////////////////////////////////////////////////////////////////////////////
- uint HexDump16BE2 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDumpBE2(f,indent,addr,4,16,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDump20BE2 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDumpBE2(f,indent,addr,4,20,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDumpBE2 ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
- const void * p_data, size_t count )
- {
- // return the number of printed lines
- if ( !f || !p_data || !count )
- return 0;
- XDump_t xd;
- InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
- xd.format = XDUMPF_INT_2;
- xd.endian = DC_BIG_ENDIAN;
- const int stat = XDump(&xd,p_data,count,true);
- return stat < 0 ? 0 : stat;
- }
- ///////////////////////////////////////////////////////////////////////////////
- uint HexDump16LE2 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDumpLE2(f,indent,addr,4,16,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDump20LE2 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDumpLE2(f,indent,addr,4,20,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDumpLE2 ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
- const void * p_data, size_t count )
- {
- // return the number of printed lines
- if ( !f || !p_data || !count )
- return 0;
- XDump_t xd;
- InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
- xd.format = XDUMPF_INT_2;
- xd.endian = DC_LITTLE_ENDIAN;
- const int stat = XDump(&xd,p_data,count,true);
- return stat < 0 ? 0 : stat;
- }
- ///////////////////////////////////////////////////////////////////////////////
- uint HexDump16BE4 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDumpBE4(f,indent,addr,4,16,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDump20BE4 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDumpBE4(f,indent,addr,4,20,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDumpBE4 ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
- const void * p_data, size_t count )
- {
- // return the number of printed lines
- if ( !f || !p_data || !count )
- return 0;
- XDump_t xd;
- InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
- xd.format = XDUMPF_INT_4;
- xd.endian = DC_BIG_ENDIAN;
- const int stat = XDump(&xd,p_data,count,true);
- return stat < 0 ? 0 : stat;
- }
- ///////////////////////////////////////////////////////////////////////////////
- uint HexDump16LE4 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDumpLE4(f,indent,addr,4,16,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDump20LE4 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDumpLE4(f,indent,addr,4,20,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDumpLE4 ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
- const void * p_data, size_t count )
- {
- // return the number of printed lines
- if ( !f || !p_data || !count )
- return 0;
- XDump_t xd;
- InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
- xd.format = XDUMPF_INT_4;
- xd.endian = DC_LITTLE_ENDIAN;
- const int stat = XDump(&xd,p_data,count,true);
- return stat < 0 ? 0 : stat;
- }
- ///////////////////////////////////////////////////////////////////////////////
- uint HexDump016 ( FILE * f, int indent, u64 addr,
- const void * data, size_t count )
- {
- // return the number of printed lines
- return HexDump0(f,indent,addr,4,16,data,count);
- }
- //-----------------------------------------------------------------------------
- uint HexDump0 ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
- const void * p_data, size_t count )
- {
- // return the number of printed lines
- if ( !f || !p_data || !count )
- return 0;
- XDump_t xd;
- InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
- xd.mode_ignore = true;
- const int stat = XDump(&xd,p_data,count,true);
- return stat < 0 ? 0 : stat;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void HexDiff16 ( FILE * f, int indent, u64 addr,
- const void * data1, size_t count1,
- const void * data2, size_t count2 )
- {
- HexDiff(f,indent,addr,4,16,data1,count1,data2,count2);
- }
- //-----------------------------------------------------------------------------
- void HexDiff ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
- const void * p_data1, size_t count1,
- const void * p_data2, size_t count2 )
- {
- if (!f)
- return;
- XDump_t xd;
- InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
- XDiff(&xd, p_data1,count1,true, p_data2,count2,true, 0,false );
- return;
- }
- //
- ///////////////////////////////////////////////////////////////////////////////
- /////////////// trace functions ///////////////
- ///////////////////////////////////////////////////////////////////////////////
- static void trace_helper ( int print_stderr, ccp format, va_list arg )
- {
- static uint last_day = 0;
- DayTime_t dt = GetDayTime(false);
- if (!TRACE_FILE)
- {
- char extend[16];
- if (ProgInfo.multi_processing)
- snprintf(extend,sizeof(extend),"-%u",getpid());
- else
- *extend = 0;
- char fname[1000];
- if ( ProgInfo.progname && *ProgInfo.progname && *ProgInfo.progname != '?' )
- snprintf( fname, sizeof(fname), "_trace-%s%s.tmp", ProgInfo.progname, extend );
- else
- snprintf( fname, sizeof(fname), "_trace-%s.tmp", extend );
- TRACE_FILE = fopen(fname,"wb");
- if (TRACE_FILE)
- {
- fcntl(fileno(TRACE_FILE),F_SETFD,FD_CLOEXEC);
- fprintf(TRACE_FILE,"\n#\f\n#OPEN %s, pid=%d\n\n",
- PrintTimeByFormat("%F %T %z",dt.time), getpid() );
- }
- else
- TRACE_FILE = stderr;
- last_day = dt.day;
- }
- char pre[200];
- if ( last_day != dt.day )
- {
- SetupTimezone(true);
- dt = GetDayTime(false);
- last_day = dt.day;
- snprintf(pre,sizeof(pre),
- "\n#DAY: %s, pid=%d\n%02u:%02u:%02u.%03u ",
- PrintTimeByFormat("%F %T %z",dt.time), getpid(),
- dt.hour, dt.min, dt.sec, dt.usec/1000 );
- }
- else
- snprintf(pre,sizeof(pre),"%02u:%02u:%02u.%03u ",
- dt.hour, dt.min, dt.sec, dt.usec/1000 );
- if ( print_stderr || TRACE_FILE == stderr )
- {
- fflush(stdout);
- fputs(pre,stderr);
- fputs(TRACE_PREFIX,stderr);
- va_list arg2;
- va_copy(arg2,arg);
- vfprintf(stderr,format,arg2);
- va_end(arg2);
- fflush(stderr);
- }
- if ( TRACE_FILE && TRACE_FILE != stderr )
- {
- fputs(pre,TRACE_FILE);
- fputs(TRACE_PREFIX,TRACE_FILE);
- vfprintf(TRACE_FILE,format,arg);
- fflush(TRACE_FILE);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- #undef TRACE_ARG_FUNC
- void TRACE_ARG_FUNC ( ccp format, va_list arg )
- {
- trace_helper(0,format,arg);
- }
- ///////////////////////////////////////////////////////////////////////////////
- #undef TRACE_FUNC
- void TRACE_FUNC ( ccp format, ... )
- {
- va_list arg;
- va_start(arg,format);
- trace_helper(0,format,arg);
- va_end(arg);
- }
- ///////////////////////////////////////////////////////////////////////////////
- #undef PRINT_ARG_FUNC
- void PRINT_ARG_FUNC ( ccp format, va_list arg )
- {
- trace_helper(1,format,arg);
- }
- ///////////////////////////////////////////////////////////////////////////////
- #undef PRINT_FUNC
- void PRINT_FUNC ( ccp format, ... )
- {
- va_list arg;
- va_start(arg,format);
- trace_helper(1,format,arg);
- va_end(arg);
- }
- ///////////////////////////////////////////////////////////////////////////////
- #undef BINGO_FUNC
- void BINGO_FUNC ( ccp func, int line, ccp src )
- {
- #if defined(DEBUG) && defined(TEST)
- if (colerr)
- PRINT_FUNC("%sBINGO!%s %s() #%d @ %s\n",
- colerr->magenta, colerr->reset, func, line, src );
- else
- PRINT_FUNC("BINGO! %s() #%d @ %s\n", func, line, src );
- #else
- if (colerr)
- fprintf(stderr,"%sBINGO!%s %s() #%d @ %s\n",
- colerr->magenta, colerr->reset, func, line, src );
- else
- fprintf(stderr,"BINGO! %s() #%d @ %s\n", func, line, src );
- #endif
- }
- ///////////////////////////////////////////////////////////////////////////////
- #undef WAIT_ARG_FUNC
- void WAIT_ARG_FUNC ( ccp format, va_list arg )
- {
- if ( format && *format )
- trace_helper(1,format,arg);
- PRINT_FUNC(">>>>>> PRESS RETURN: ");
- getchar();
- }
- ///////////////////////////////////////////////////////////////////////////////
- #undef WAIT_FUNC
- void WAIT_FUNC ( ccp format, ... )
- {
- va_list arg;
- va_start(arg,format);
- WAIT_ARG_FUNC(format,arg);
- va_end(arg);
- }
- //
- ///////////////////////////////////////////////////////////////////////////////
- /////////////// alloc/free system ///////////////
- ///////////////////////////////////////////////////////////////////////////////
- #if TRACE_ALLOC_MODE > 2
- #define MEM_FILLER_SIZE 8
- static u8 mem_filler[MEM_FILLER_SIZE];
- #else
- #define MEM_FILLER_SIZE 0
- #endif
- #if TRACE_ALLOC_MODE > 1
- #define MPARAM ccp func, ccp file, uint line,
- #define MCALL func,file,line,
- #define PRINT_OOM(...) \
- PrintError(func,file,line,0,ERR_OUT_OF_MEMORY,__VA_ARGS__)
- #else
- #define MPARAM
- #define MCALL
- #define PRINT_OOM(...) \
- PrintError(__FUNCTION__,__FILE__,__LINE__,0,ERR_OUT_OF_MEMORY,__VA_ARGS__)
- #endif
- ///////////////////////////////////////////////////////////////////////////////
- void dclib_free ( void * ptr )
- {
- free(ptr);
- }
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- void * dclib_xcalloc ( size_t nmemb, size_t size )
- {
- void * res = calloc(nmemb,size);
- if (!res)
- PrintError(__FUNCTION__,__FILE__,__LINE__,0,ERR_OUT_OF_MEMORY,
- "Out of memory while calloc() %zu bytes (%zu*%zu=0x%zx)\n",
- nmemb*size, nmemb, size, nmemb*size );
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * dclib_xmalloc ( size_t size )
- {
- void * res = malloc(size);
- if (!res)
- PrintError(__FUNCTION__,__FILE__,__LINE__,0,ERR_OUT_OF_MEMORY,
- "Out of memory while malloc() %zu bytes (0x%zx)\n",
- size, size );
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * dclib_xrealloc ( void * ptr, size_t size )
- {
- void * res = realloc(ptr,size);
- if ( !res && size )
- PrintError(__FUNCTION__,__FILE__,__LINE__,0,ERR_OUT_OF_MEMORY,
- "Out of memory while realloc() %zu bytes (0x%zx)\n",
- size, size );
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- char * dclib_xstrdup ( ccp src )
- {
- char * res = strdup(src?src:"");
- if (!res)
- {
- const uint size = src ? strlen(src)+1 : 0;
- PrintError(__FUNCTION__,__FILE__,__LINE__,0,ERR_OUT_OF_MEMORY,
- "Out of memory while strdup() %u bytes (0x%x)\n",
- size, size );
- }
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- void * dclib_calloc ( MPARAM size_t nmemb, size_t size )
- {
- void * res = calloc(nmemb,size);
- if (!res)
- PRINT_OOM("Out of memory while calloc() %zu bytes (%zu*%zu=0x%zx)\n",
- nmemb*size, nmemb, size, nmemb*size );
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * dclib_malloc ( MPARAM size_t size )
- {
- void * res = malloc(size);
- if (!res)
- PRINT_OOM("Out of memory while malloc() %zu bytes (0x%zx)\n",
- size, size );
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * dclib_realloc ( MPARAM void * ptr, size_t size )
- {
- void * res = realloc(ptr,size);
- if ( !res && size )
- PRINT_OOM("Out of memory while realloc() %zu bytes (0x%zx)\n",
- size, size );
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- char * dclib_strdup ( MPARAM ccp src )
- {
- char * res = strdup(src?src:"");
- if (!res)
- {
- const uint size = src ? strlen(src)+1 : 0;
- PRINT_OOM("Out of memory while strdup() %u bytes (0x%x)\n",
- size, size );
- }
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- char * dclib_strdup2 ( MPARAM ccp src1, ccp src2 )
- {
- const uint len1 = src1 ? strlen(src1) : 0;
- const uint len2 = src2 ? strlen(src2) : 0;
- char * res = dclib_malloc( MCALL len1+len2+1 );
- if (len1)
- memcpy(res,src1,len1);
- if (len2)
- memcpy(res+len1,src2,len2);
- res[len1+len2] = 0;
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- char * dclib_strdup3 ( MPARAM ccp src1, ccp src2, ccp src3 )
- {
- const uint len1 = src1 ? strlen(src1) : 0;
- const uint len2 = src2 ? strlen(src2) : 0;
- const uint len3 = src3 ? strlen(src3) : 0;
- char * res = dclib_malloc( MCALL len1+len2+len3+1 );
- char * dest = res;
- if (len1)
- {
- memcpy(dest,src1,len1);
- dest += len1;
- }
- if (len2)
- {
- memcpy(dest,src2,len2);
- dest += len2;
- }
- if (len3)
- {
- memcpy(dest,src3,len3);
- dest += len3;
- }
- *dest = 0;
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * dclib_memdup ( MPARAM const void * src, size_t copylen )
- {
- char * dest = dclib_malloc( MCALL copylen+1);
- memcpy(dest,src,copylen);
- dest[copylen] = 0;
- return dest;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * dclib_memdup2 ( MPARAM cvp src1, size_t len1, cvp src2, size_t len2 )
- {
- char *res = dclib_malloc( MCALL len1+len2+1);
- char *dest = res;
- if (len1)
- {
- memcpy(dest,src1,len1);
- dest += len1;
- }
- if (len2)
- {
- memcpy(dest,src2,len2);
- dest += len2;
- }
- *dest = 0;
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * dclib_memdup3
- ( MPARAM cvp src1, size_t len1, cvp src2, size_t len2, cvp src3, size_t len3 )
- {
- char *res = dclib_malloc( MCALL len1+len2+len3+1);
- char *dest = res;
- if (len1)
- {
- memcpy(dest,src1,len1);
- dest += len1;
- }
- if (len2)
- {
- memcpy(dest,src2,len2);
- dest += len2;
- }
- if (len3)
- {
- memcpy(dest,src3,len3);
- dest += len3;
- }
- *dest = 0;
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * dclib_allocdup ( MPARAM const void * src, size_t copylen )
- {
- char * dest = dclib_malloc( MCALL copylen);
- memcpy(dest,src,copylen);
- return dest;
- }
- ///////////////////////////////////////////////////////////////////////////////
- char * dclib_printdup ( MPARAM ccp format, ... )
- {
- char buf[2000];
- va_list arg;
- va_start(arg,format);
- const int stat = vsnprintf(buf,sizeof(buf),format,arg) + 1;
- va_end(arg);
- char * dest = dclib_malloc( MCALL stat );
- if ( stat <= sizeof(buf) )
- memcpy(dest,buf,stat);
- else
- {
- va_start(arg,format);
- vsnprintf(dest,stat,format,arg);
- va_end(arg);
- }
- return dest;
- }
- //
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- #if TRACE_ALLOC_MODE > 2
- ///////////////////////////////////////////////////////////////////////////////
- // [[mem_info_t]]
- typedef struct mem_info_t
- {
- ccp func;
- ccp file;
- uint line;
- u8 * data;
- uint size;
- bool filler;
- uint seqnum;
- } mem_info_t;
- //-----------------------------------------------------------------------------
- static mem_info_t * mem_list = 0;
- static uint mem_used = 0;
- static uint mem_size = 0;
- static uint mem_seqnum = 0;
- ///////////////////////////////////////////////////////////////////////////////
- static FILE * OpenMemLog()
- {
- static bool done = false;
- if (!done)
- {
- done = true;
- char fname[500];
- if ( ProgInfo.progname && *ProgInfo.progname && *ProgInfo.progname != '?' )
- snprintf(fname,sizeof(fname),"_trace_alloc-%s-%u.tmp",ProgInfo.progname,getpid());
- else
- snprintf(fname,sizeof(fname),"_trace_alloc-%u.tmp",getpid());
- MEM_LOG_FILE = fopen(fname,"ab");
- if (MEM_LOG_FILE)
- {
- fcntl(fileno(MEM_LOG_FILE),F_SETFD,FD_CLOEXEC);
- fprintf(MEM_LOG_FILE,"#\f\n# OPEN FILE %s\n",
- PrintTimeByFormat("%F %T %z",time(0)));
- fflush(MEM_LOG_FILE);
- }
- }
- return MEM_LOG_FILE;
- }
- ///////////////////////////////////////////////////////////////////////////////
- static void MemLog
- ( ccp func, ccp file, uint line, ccp info, const u8 * ptr, bool no_stderr )
- {
- static uint line_no = 0;
- if (!no_stderr)
- {
- fprintf(stderr,"MEM-ERR[%s,%p] %s() @ %s#%u\n",
- info, ptr, func, file, line );
- fflush(stderr);
- }
- if (OpenMemLog())
- {
- if (!line_no)
- fputs("#index addr type function line file\n",MEM_LOG_FILE);
- fprintf(MEM_LOG_FILE,"%07u %p %s %s %u %s\n",
- line_no, ptr, info, func, line, file );
- fflush(MEM_LOG_FILE);
- }
- line_no++;
- }
- ///////////////////////////////////////////////////////////////////////////////
- static void MemLogItem
- ( ccp func, ccp file, uint line, ccp info, const mem_info_t * mi )
- {
- if (func)
- fprintf(stderr,"MEM-ERR[%s] %s() @ %s#%u\n",
- info, func, file, line );
- fprintf(stderr,"MEM-ERR[%s,%u] %s() @ %s#%u\n",
- info, mi->seqnum, mi->func, mi->file, mi->line );
- fflush(stderr);
- if (OpenMemLog())
- {
- if (func)
- fprintf(MEM_LOG_FILE,"[%s] %s() @ %s#%u\n",
- info, func, file, line );
- fprintf(MEM_LOG_FILE,"[%s,%u] %s() @ %s#%u\n",
- info, mi->seqnum, mi->func, mi->file, mi->line );
- if (mi->filler)
- {
- HexDump16( MEM_LOG_FILE, 10, 0,
- mi->data - MEM_FILLER_SIZE, 2*MEM_FILLER_SIZE );
- HexDump16( MEM_LOG_FILE, 10, mi->size + MEM_FILLER_SIZE,
- mi->data + mi->size - MEM_FILLER_SIZE, 2 * MEM_FILLER_SIZE );
- }
- fputc('\n',MEM_LOG_FILE);
- fflush(MEM_LOG_FILE);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- uint FindMemInfoHelper ( u8 * data, uint size )
- {
- int beg = 0;
- int end = mem_used - 1;
- while ( beg <= end )
- {
- uint idx = (beg+end)/2;
- mem_info_t * mi = mem_list + idx;
- if ( data < mi->data )
- end = idx - 1 ;
- else if ( data > mi->data )
- beg = idx + 1;
- else if ( size < mi->size )
- end = idx - 1 ;
- else if ( size > mi->size )
- beg = idx + 1;
- else
- {
- noTRACE("FindMemInfoHelper(%llx,%llx) FOUND=%d/%d/%d\n",
- (u64)off, (u64)size, idx, mem_used, mem_size );
- return idx;
- }
- }
- noTRACE("FindStringFieldHelper(%llx,%llx) failed=%d/%d/%d\n",
- (u64)off, (u64)size, beg, mem_used, mem_size );
- return beg;
- }
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- void InitializeTraceAlloc()
- {
- static bool done = false;
- if (done)
- return;
- done = true;
- OpenMemLog();
- memset(mem_filler,0xdc,sizeof(mem_filler));
- mem_filler[MEM_FILLER_SIZE/2] = 0xcd;
- mem_filler[MEM_FILLER_SIZE/3] = 0xcd;
- }
- ///////////////////////////////////////////////////////////////////////////////
- int CheckTraceAlloc ( ccp func, ccp file, uint line )
- {
- int count = 0;
- mem_info_t *ptr, *end = mem_list + mem_used;
- for ( ptr = mem_list; ptr < end; ptr++ )
- {
- if (!ptr->filler)
- continue;
- if (memcmp(ptr->data-MEM_FILLER_SIZE,mem_filler,sizeof(mem_filler)))
- {
- MemLogItem(0,0,0,"BEG",ptr);
- memcpy(ptr->data-MEM_FILLER_SIZE,mem_filler,sizeof(mem_filler));
- count++;
- }
- if (memcmp(ptr->data+ptr->size,mem_filler,sizeof(mem_filler)))
- {
- MemLogItem(0,0,0,"END",ptr);
- memcpy(ptr->data+ptr->size,mem_filler,sizeof(mem_filler));
- count++;
- }
- }
- if (count)
- {
- fprintf(stderr,"MEM-ERR: %u errors found -> %s() @ %s#%u\n",
- count, func, file, line );
- fflush(stderr);
- if (OpenMemLog())
- {
- fprintf(MEM_LOG_FILE,"--- %u errors found -> %s() @ %s#%u\n\n",
- count, func, file, line );
- fflush(MEM_LOG_FILE);
- }
- }
- return count;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void DumpTraceAlloc ( ccp func, ccp file, uint line, FILE * f )
- {
- CheckTraceAlloc(func,file,line);
- if (!f)
- return;
- fprintf(f,"---- MEM-DUMP [N=%u] -----\n",mem_used);
- mem_info_t *ptr, *end = mem_list + mem_used;
- for ( ptr = mem_list; ptr < end; ptr++ )
- {
- ccp file = strrchr(ptr->file,'/');
- file = file ? file+1 : ptr->file;
- fprintf(f,"%6u %p %6u %-20.20s %5u %-.25s\n",
- ptr->seqnum, ptr->data, ptr->size,
- ptr->func, ptr->line, file );
- }
- fflush(stderr);
- }
- ///////////////////////////////////////////////////////////////////////////////
- mem_info_t * RegisterAlloc
- ( ccp func, ccp file, uint line, cvp data, uint size, bool filler )
- {
- if (!mem_seqnum)
- InitializeTraceAlloc();
- if (filler)
- {
- memcpy((u8*)data-MEM_FILLER_SIZE,mem_filler,MEM_FILLER_SIZE);
- memcpy((u8*)data+size,mem_filler,MEM_FILLER_SIZE);
- }
- uint idx = FindMemInfoHelper((u8*)data,size);
- ASSERT( mem_used <= mem_size );
- if ( mem_used == mem_size )
- {
- mem_size += 1000 + mem_size/2;
- const uint alloc_size = mem_size * sizeof(*mem_list);
- mem_list = realloc(mem_list,alloc_size);
- if (!mem_list)
- PRINT_OOM("Out of memory while RegisterAlloc() %u bytes (0x%x)\n",
- alloc_size, alloc_size );
- }
- DASSERT( idx <= mem_used );
- mem_info_t * mi = mem_list + idx;
- memmove(mi+1,mi,(mem_used-idx)*sizeof(*mi));
- mem_used++;
- mi->func = func;
- mi->file = file;
- mi->line = line;
- mi->data = (u8*)data;
- mi->size = size;
- mi->filler = filler;
- mi->seqnum = mem_seqnum++;
- #if TRACE_ALLOC_MODE > 3
- MemLog(func,file,line,"ALLOC",data,true);
- #endif
- return mi;
- }
- //
- ///////////////////////////////////////////////////////////////////////////////
- #endif // TRACE_ALLOC_MODE > 2
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- uint trace_test_alloc
- ( ccp func, ccp file, uint line, const void *p_data, bool hexdump )
- {
- #if TRACE_ALLOC_MODE > 2
- if (!p_data)
- return 0;
- const u8 *data = p_data;
- int beg = 0;
- int end = mem_used - 1;
- while ( beg <= end )
- {
- uint idx = (beg+end)/2;
- mem_info_t * mi = mem_list + idx;
- if ( data < mi->data )
- end = idx - 1 ;
- else if ( data > mi->data )
- beg = idx + 1;
- else
- {
- #if TRACE_ALLOC_MODE > 3
- MemLog(func,file,line,"TEST",data,true);
- #endif
- uint stat = 0;
- if (memcmp(data-MEM_FILLER_SIZE,mem_filler,MEM_FILLER_SIZE))
- {
- MemLogItem(func,file,line,"BEG",mi);
- stat = 1;
- HexDump16(stderr,0,0,data-MEM_FILLER_SIZE,16);
- }
- if (memcmp(data+mi->size,mem_filler,MEM_FILLER_SIZE))
- {
- MemLogItem(func,file,line,"END",mi);
- stat |= 2;
- HexDump16(stderr,0,mi->size+sizeof(mem_filler),data+mi->size,16);
- }
- return stat;
- }
- }
- MemLog(func,file,line,"NOT FOUND",data,false);
- return 4;
- #else
- return 0;
- #endif
- }
- ///////////////////////////////////////////////////////////////////////////////
- static void * UnregisterAlloc ( ccp func, ccp file, uint line, u8 * data )
- {
- #if TRACE_ALLOC_MODE > 2
- if (!data)
- return 0;
- int beg = 0;
- int end = mem_used - 1;
- while ( beg <= end )
- {
- uint idx = (beg+end)/2;
- mem_info_t * mi = mem_list + idx;
- if ( data < mi->data )
- end = idx - 1 ;
- else if ( data > mi->data )
- beg = idx + 1;
- else
- {
- #if TRACE_ALLOC_MODE > 3
- MemLog(func,file,line,"FREE",data,true);
- #endif
- if (mi->filler)
- {
- if (memcmp(data-MEM_FILLER_SIZE,mem_filler,MEM_FILLER_SIZE))
- MemLogItem(func,file,line,"BEG",mi);
- if (memcmp(data+mi->size,mem_filler,MEM_FILLER_SIZE))
- MemLogItem(func,file,line,"END",mi);
- data -= MEM_FILLER_SIZE;
- }
- memmove(mi,mi+1,(--mem_used-idx)*sizeof(*mi));
- return data;
- }
- }
- MemLog(func,file,line,"NOT FOUND",data,false);
- #endif
- return data;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void trace_free ( ccp func, ccp file, uint line, void * ptr )
- {
- free(UnregisterAlloc(func,file,line,ptr));
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * trace_malloc ( ccp func, ccp file, uint line, size_t size )
- {
- u8 * res = malloc( size + 2 * MEM_FILLER_SIZE );
- if (!res)
- PRINT_OOM("Out of memory while allocate %zu+%u bytes (0x%zx)\n",
- size, 2 * MEM_FILLER_SIZE, size + 2 * MEM_FILLER_SIZE );
- res += MEM_FILLER_SIZE;
- #if TRACE_ALLOC_MODE > 2
- RegisterAlloc(func,file,line,res,size,true);
- #endif
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * trace_calloc ( ccp func, ccp file, uint line, size_t nmemb, size_t size )
- {
- uint total_size = nmemb * size;
- void * res = trace_malloc(func,file,line,total_size);
- memset(res,0,total_size);
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * trace_realloc ( ccp func, ccp file, uint line, void *ptr, size_t size )
- {
- ptr = UnregisterAlloc(func,file,line,ptr);
- u8 * res = realloc( ptr, size + 2 * MEM_FILLER_SIZE );
- if (!res)
- PRINT_OOM("Out of memory while re allocate %zu+%u bytes (0x%zx)\n",
- size, 2 * MEM_FILLER_SIZE, size + 2 * MEM_FILLER_SIZE );
- res += MEM_FILLER_SIZE;
- #if TRACE_ALLOC_MODE > 2
- RegisterAlloc(func,file,line,res,size,true);
- #endif
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- char * trace_strdup ( ccp func, ccp file, uint line, ccp src )
- {
- const uint len = src ? strlen(src)+1 : 0;
- char * res = trace_malloc(func,file,line,len);
- memcpy(res,src,len);
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- char * trace_strdup2 ( ccp func, ccp file, uint line, ccp src1, ccp src2 )
- {
- const uint len1 = src1 ? strlen(src1) : 0;
- const uint len2 = src2 ? strlen(src2) : 0;
- char * res = trace_malloc(func,file,line,len1+len2+1);
- if (len1)
- memcpy(res,src1,len1);
- if (len2)
- memcpy(res+len1,src2,len2);
- res[len1+len2] = 0;
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- char * trace_strdup3 ( ccp func, ccp file, uint line, ccp src1, ccp src2, ccp src3 )
- {
- const uint len1 = src1 ? strlen(src1) : 0;
- const uint len2 = src2 ? strlen(src2) : 0;
- const uint len3 = src3 ? strlen(src3) : 0;
- char * res = trace_malloc(func,file,line,len1+len2+len3+1);
- char * dest = res;
- if (len1)
- {
- memcpy(dest,src1,len1);
- dest += len1;
- }
- if (len2)
- {
- memcpy(dest,src2,len2);
- dest += len2;
- }
- if (len3)
- {
- memcpy(dest,src3,len3);
- dest += len3;
- }
- *dest = 0;
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * trace_memdup
- ( ccp func, ccp file, uint line, const void * src, size_t copylen )
- {
- char * dest = trace_malloc(func,file,line,copylen+1);
- memcpy(dest,src,copylen);
- dest[copylen] = 0;
- return dest;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * trace_memdup2 ( ccp func, ccp file, uint line,
- cvp src1, size_t len1, cvp src2, size_t len2 )
- {
- char *res = trace_malloc(func,file,line,len1+len2+1);
- char *dest = res;
- if (len1)
- {
- memcpy(dest,src1,len1);
- dest += len1;
- }
- if (len2)
- {
- memcpy(dest,src2,len2);
- dest += len2;
- }
- *dest = 0;
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * trace_memdup3 ( ccp func, ccp file, uint line,
- cvp src1, size_t len1, cvp src2, size_t len2, cvp src3, size_t len3 )
- {
- char *res = trace_malloc(func,file,line,len1+len2+len3+1);
- char *dest = res;
- if (len1)
- {
- memcpy(dest,src1,len1);
- dest += len1;
- }
- if (len2)
- {
- memcpy(dest,src2,len2);
- dest += len2;
- }
- if (len3)
- {
- memcpy(dest,src3,len3);
- dest += len3;
- }
- *dest = 0;
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void * trace_allocdup
- ( ccp func, ccp file, uint line, const void * src, size_t copylen )
- {
- char * dest = trace_malloc(func,file,line,copylen);
- memcpy(dest,src,copylen);
- return dest;
- }
- ///////////////////////////////////////////////////////////////////////////////
- char * trace_printdup
- ( ccp func, ccp file, uint line, ccp format, ... )
- {
- char buf[2000];
- va_list arg;
- va_start(arg,format);
- const int stat = vsnprintf(buf,sizeof(buf),format,arg) + 1;
- va_end(arg);
- char * dest = trace_malloc(func,file,line,stat);
- if ( stat <= sizeof(buf) )
- memcpy(dest,buf,stat);
- else
- {
- va_start(arg,format);
- vsnprintf(dest,stat,format,arg);
- va_end(arg);
- }
- return dest;
- }
- //
- ///////////////////////////////////////////////////////////////////////////////
- /////////////// mem check ///////////////
- ///////////////////////////////////////////////////////////////////////////////
- static u8 mem_check_buf[0x100];
- static const void * mem_check_ptr = 0;
- static uint mem_check_size = 0;
- ///////////////////////////////////////////////////////////////////////////////
- void MemCheckSetup ( const void * ptr, uint size )
- {
- if ( ptr && size > 0 )
- {
- mem_check_ptr = ptr;
- if ( size > sizeof(mem_check_buf) )
- {
- ERROR0(ERR_WARNING,
- "MemCheckSetup(), max watch size = %zx (<%x)",
- sizeof(mem_check_buf), size );
- size = sizeof(mem_check_buf);
- }
- mem_check_size = size;
- memcpy(mem_check_buf,ptr,size);
- }
- else
- mem_check_size = 0;
- }
- ///////////////////////////////////////////////////////////////////////////////
- void MemCheck ( ccp func, ccp file, uint line )
- {
- if (!mem_check_size)
- return;
- if ( memcmp(mem_check_buf,mem_check_ptr,mem_check_size) )
- {
- fprintf(stderr,"--- MemCheck: %p should be:\n",mem_check_ptr);
- HexDump16(stderr,0,0,mem_check_buf,mem_check_size);
- fprintf(stderr,"--- MemCheck: ... but is:\n");
- HexDump16(stderr,0,0,mem_check_ptr,mem_check_size);
- TRACE("--- MemCheck: %p should be:\n",mem_check_ptr);
- TRACE_HEXDUMP16(0,0,mem_check_buf,mem_check_size);
- TRACE("--- MemCheck: ... but is:\n");
- TRACE_HEXDUMP16(0,0,mem_check_ptr,mem_check_size);
- PrintError(func,file,line,0,ERR_FATAL,"MemCheck() failed!\n");
- }
- else
- TRACE("MemCheck OK: %s @ %s#%u\n",func,file,line);
- }
- //
- ///////////////////////////////////////////////////////////////////////////////
- /////////////// END ///////////////
- ///////////////////////////////////////////////////////////////////////////////
|