dclib-debug.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. /***************************************************************************
  2. * *
  3. * _____ ____ *
  4. * | __ \ / __ \ _ _ _____ *
  5. * | | \ \ / / \_\ | | | | _ \ *
  6. * | | \ \| | | | | | |_| | *
  7. * | | | || | | | | | ___/ *
  8. * | | / /| | __ | | | | _ \ *
  9. * | |__/ / \ \__/ / | |___| | |_| | *
  10. * |_____/ \____/ |_____|_|_____/ *
  11. * *
  12. * Wiimms source code library *
  13. * *
  14. ***************************************************************************
  15. * *
  16. * Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de> *
  17. * *
  18. ***************************************************************************
  19. * *
  20. * This library is free software; you can redistribute it and/or modify *
  21. * it under the terms of the GNU General Public License as published by *
  22. * the Free Software Foundation; either version 2 of the License, or *
  23. * (at your option) any later version. *
  24. * *
  25. * This library is distributed in the hope that it will be useful, *
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  28. * GNU General Public License for more details. *
  29. * *
  30. * See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
  31. * *
  32. ***************************************************************************/
  33. #define DCLIB_DEBUG_C 1
  34. #include "dclib-debug.h"
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <stdarg.h>
  38. #include <string.h>
  39. #include <time.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include "dclib-basics.h"
  43. #include "dclib-file.h"
  44. #include "dclib-xdump.h"
  45. //
  46. ///////////////////////////////////////////////////////////////////////////////
  47. /////////////// error messages ///////////////
  48. ///////////////////////////////////////////////////////////////////////////////
  49. ccp TRACE_PREFIX = "";
  50. FILE *TRACE_FILE = 0;
  51. FILE *MEM_LOG_FILE = 0;
  52. ccp (*GetErrorNameHook)( int stat, ccp ret_not_found ) = 0;
  53. ccp (*GetErrorTextHook)( int stat, ccp ret_not_found ) = 0;
  54. ccp GENERIC_ERROR_MESSAGE = "generic err";
  55. ///////////////////////////////////////////////////////////////////////////////
  56. ccp GetErrorName
  57. (
  58. int stat, // error status, err := abs(stat)
  59. ccp ret_not_found // not NULL: return this, if no message is found
  60. // GENERIC_ERROR_MESSAGE: return a generic message
  61. )
  62. {
  63. if (GetErrorNameHook)
  64. {
  65. ccp res = GetErrorNameHook(stat,0);
  66. if ( res && *res )
  67. return res;
  68. }
  69. if ( stat < 0 )
  70. stat = -stat;
  71. switch(stat)
  72. {
  73. case ERR_OK: return "OK";
  74. case ERR_DIFFER: return "DIFFER";
  75. case ERR_NOTHING_TO_DO: return "NOTHING TO DO";
  76. case ERR_SOURCE_FOUND: return "SOURCE FOUND";
  77. case ERR_NO_SOURCE_FOUND: return "NO SOURCE";
  78. case ERR_JOB_IGNORED: return "JOB IGNORED";
  79. case ERR_SUBJOB_WARNING: return "SUB JOB WARNINGS";
  80. case ERR_NOT_EXISTS: return "NOT EXISTS";
  81. case ERR_WARNING: return "WARNING";
  82. case ERR_WRONG_FILE_TYPE: return "WRONG FILE TYPE";
  83. case ERR_INVALID_FILE: return "INVALID FILE";
  84. case ERR_INVALID_VERSION: return "INVALID VERSION";
  85. case ERR_INVALID_DATA: return "INVALID DATA";
  86. case ERR_ENCODING: return "ENCODING FAILED";
  87. case ERR_DECODING: return "DECODING FAILED";
  88. case ERR_ALREADY_EXISTS: return "FILE ALREADY EXISTS";
  89. case ERR_SUBJOB_FAILED: return "SUB JOB FAILED";
  90. case ERR_CANT_REMOVE: return "CAN'T REMOVE FILE";
  91. case ERR_CANT_RENAME: return "CAN'T RENAME FILE";
  92. case ERR_CANT_CLOSE: return "CAN'T CLOSE FILE";
  93. case ERR_CANT_CONNECT: return "CAN'T CONNECT";
  94. case ERR_CANT_OPEN: return "CAN'T OPEN FILE";
  95. case ERR_CANT_APPEND: return "CAN'T APPEND FILE";
  96. case ERR_CANT_CREATE: return "CAN'T CREATE FILE";
  97. case ERR_CANT_CREATE_DIR: return "CAN'T CREATE DIRECTORY";
  98. case ERR_READ_FAILED: return "READ FILE FAILED";
  99. case ERR_REMOVE_FAILED: return "REMOVE FILE FAILED";
  100. case ERR_WRITE_FAILED: return "WRITE FILE FAILED";
  101. case ERR_DATABASE: return "DATABASE ACCESS FAILED";
  102. case ERR_MISSING_PARAM: return "MISSING PARAMETERS";
  103. case ERR_SEMANTIC: return "SEMANTIC ERROR";
  104. case ERR_SYNTAX: return "SYNTAX ERROR";
  105. case ERR_INTERRUPT: return "INTERRUPT";
  106. case ERR_ERROR: return "ERROR";
  107. case ERR_NOT_IMPLEMENTED: return "NOT IMPLEMENTED YET";
  108. case ERR_INTERNAL: return "INTERNAL ERROR";
  109. case ERR_OUT_OF_MEMORY: return "OUT OF MEMORY";
  110. case ERR_FATAL: return "FATAL ERROR";
  111. }
  112. if ( ret_not_found != GENERIC_ERROR_MESSAGE )
  113. return ret_not_found;
  114. //--- generic messages
  115. if (GetErrorNameHook)
  116. {
  117. ccp res = GetErrorNameHook(stat,GENERIC_ERROR_MESSAGE);
  118. if ( res && *res )
  119. return res;
  120. }
  121. if ( stat >= ERU_WARN_00 & stat <= ERU_WARN_XX )
  122. {
  123. const uint buf_size = 20;
  124. char *buf = GetCircBuf(buf_size);
  125. snprintf(buf,buf_size,"USER WARNING #%02u", stat - ERU_WARN_00 );
  126. return buf;
  127. }
  128. if ( stat >= ERU_ERROR1_00 & stat <= ERU_ERROR1_XX )
  129. {
  130. const uint buf_size = 20;
  131. char *buf = GetCircBuf(buf_size);
  132. snprintf(buf,buf_size,"USER ERROR #%02u", stat - ERU_ERROR1_00 );
  133. return buf;
  134. }
  135. if ( stat >= ERU_ERROR2_00 & stat <= ERU_ERROR2_XX )
  136. {
  137. const uint buf_size = 20;
  138. char *buf = GetCircBuf(buf_size);
  139. snprintf(buf,buf_size,"USER ERROR #%02u",
  140. stat - ERU_ERROR2_00 + ERU_ERROR1_XX+1 - ERU_ERROR1_00 );
  141. return buf;
  142. }
  143. if ( stat >= ERU_FATAL_00 & stat <= ERU_FATAL_XX )
  144. {
  145. const uint buf_size = 24;
  146. char *buf = GetCircBuf(buf_size);
  147. snprintf(buf,buf_size,"USER FATAL ERROR #%02u", stat - ERU_FATAL_00 );
  148. return buf;
  149. }
  150. ccp res = GetErrorName(stat+1,"");
  151. return res && *res ? res : "?";
  152. }
  153. ///////////////////////////////////////////////////////////////////////////////
  154. ccp GetErrorText
  155. (
  156. int stat, // error status, err := abs(stat)
  157. ccp ret_not_found // not NULL: return this, if no message is found
  158. // GENERIC_ERROR_MESSAGE: return a generic message
  159. )
  160. {
  161. if (GetErrorTextHook)
  162. {
  163. ccp res = GetErrorTextHook(stat,0);
  164. if (res)
  165. return res;
  166. }
  167. if ( stat < 0 )
  168. stat = -stat;
  169. switch(stat)
  170. {
  171. case ERR_OK: return "Ok";
  172. case ERR_DIFFER: return "Files differ";
  173. case ERR_NOTHING_TO_DO: return "Nothing to do";
  174. case ERR_SOURCE_FOUND: return "Source found";
  175. case ERR_NO_SOURCE_FOUND: return "No source found";
  176. case ERR_JOB_IGNORED: return "Job ignored";
  177. case ERR_SUBJOB_WARNING: return "Sub job had warnings";
  178. case ERR_NOT_EXISTS: return "File does not exists";
  179. case ERR_WARNING: return "Unspecific warning";
  180. case ERR_WRONG_FILE_TYPE: return "Wrong type of file";
  181. case ERR_INVALID_FILE: return "Invalid file";
  182. case ERR_INVALID_VERSION: return "Invalid file version";
  183. case ERR_INVALID_DATA: return "Invalid data";
  184. case ERR_ENCODING: return "Encoding data failed";
  185. case ERR_DECODING: return "Decoding data failed";
  186. case ERR_ALREADY_EXISTS: return "File already exists";
  187. case ERR_SUBJOB_FAILED: return "Sub job failed";
  188. case ERR_CANT_REMOVE: return "Can't remove file";
  189. case ERR_CANT_RENAME: return "Can't rename file";
  190. case ERR_CANT_CLOSE: return "Can't close file";
  191. case ERR_CANT_CONNECT: return "Can't connect";
  192. case ERR_CANT_OPEN: return "Can't open file";
  193. case ERR_CANT_APPEND: return "Can't open file for appending";
  194. case ERR_CANT_CREATE: return "Can't create file";
  195. case ERR_CANT_CREATE_DIR: return "Can't create directory";
  196. case ERR_READ_FAILED: return "Reading from file failed";
  197. case ERR_REMOVE_FAILED: return "Removing a file failed";
  198. case ERR_WRITE_FAILED: return "Writing to file failed";
  199. case ERR_DATABASE: return "Access to database failed";
  200. case ERR_MISSING_PARAM: return "Missing at least one parameter";
  201. case ERR_SEMANTIC: return "Semantic error";
  202. case ERR_SYNTAX: return "Syntax error";
  203. case ERR_INTERRUPT: return "Program interrupted by user";
  204. case ERR_ERROR: return "Unspecific error";
  205. case ERR_NOT_IMPLEMENTED: return "Not implemented yet";
  206. case ERR_INTERNAL: return "Internal error";
  207. case ERR_OUT_OF_MEMORY: return "Allocation of dynamic memory failed";
  208. case ERR_FATAL: return "Unspecific fatal error";
  209. }
  210. if ( ret_not_found != GENERIC_ERROR_MESSAGE )
  211. return ret_not_found;
  212. //--- generic messages
  213. if (GetErrorTextHook)
  214. {
  215. ccp res = GetErrorTextHook(stat,GENERIC_ERROR_MESSAGE);
  216. if ( res && *res )
  217. return res;
  218. }
  219. if ( stat >= ERU_WARN_00 & stat <= ERU_WARN_XX )
  220. {
  221. const uint buf_size = 20;
  222. char *buf = GetCircBuf(buf_size);
  223. snprintf(buf,buf_size,"USER WARNING #%02u", stat - ERU_WARN_00 );
  224. return buf;
  225. }
  226. if ( stat >= ERU_ERROR1_00 & stat <= ERU_ERROR1_XX )
  227. {
  228. const uint buf_size = 20;
  229. char *buf = GetCircBuf(buf_size);
  230. snprintf(buf,buf_size,"USER ERROR #%02u", stat - ERU_ERROR1_00 );
  231. return buf;
  232. }
  233. if ( stat >= ERU_ERROR2_00 & stat <= ERU_ERROR2_XX )
  234. {
  235. const uint buf_size = 20;
  236. char *buf = GetCircBuf(buf_size);
  237. snprintf(buf,buf_size,"USER ERROR #%02u",
  238. stat - ERU_ERROR2_00 + ERU_ERROR1_XX+1 - ERU_ERROR1_00 );
  239. return buf;
  240. }
  241. if ( stat >= ERU_FATAL_00 & stat <= ERU_FATAL_XX )
  242. {
  243. const uint buf_size = 24;
  244. char *buf = GetCircBuf(buf_size);
  245. snprintf(buf,buf_size,"USER FATAL ERROR #%02u", stat - ERU_FATAL_00 );
  246. return buf;
  247. }
  248. ccp res = GetErrorText(stat+1,0);
  249. return res && *res ? res : "?";
  250. }
  251. ///////////////////////////////////////////////////////////////////////////////
  252. ///////////////////////////////////////////////////////////////////////////////
  253. void ListErrorCodes
  254. (
  255. FILE * f, // valid output stream
  256. int indent, // indent of output
  257. bool all // true: print all user reserved messages too.
  258. )
  259. {
  260. if (!f)
  261. return;
  262. indent = NormalizeIndent(indent);
  263. fprintf(f,"\n%*sList of error messages:\n\n",indent,"");
  264. ccp p2 = all ? 0 : "";
  265. uint i, fw_name = 4, fw_text = 7;
  266. for ( i = 0; i < ERR__N; i++ )
  267. {
  268. ccp name = GetErrorName(i,p2);
  269. if (name)
  270. {
  271. const uint len = strlen(name);
  272. if ( fw_name < len )
  273. fw_name = len;
  274. }
  275. ccp text = GetErrorText(i,p2);
  276. {
  277. const uint len = strlen(text);
  278. if ( fw_text < len )
  279. fw_text = len;
  280. }
  281. }
  282. fprintf(f,
  283. "%*s err | %-*s | %-*s\n"
  284. "%*s-----+%.*s+%.*s\n",
  285. indent, "",
  286. fw_name, "name",
  287. fw_text, "message",
  288. indent, "",
  289. fw_name+2, Minus300,
  290. fw_text+2, Minus300 );
  291. for ( i = 0; i < ERR__N; i++ )
  292. {
  293. ccp name = GetErrorName(i,p2);
  294. ccp text = GetErrorText(i,p2);
  295. if ( name && *name || text && *text )
  296. fprintf(f,"%*s%4u | %-*s | %-*s\n",
  297. indent, "", i,
  298. fw_name, name ? name : "-",
  299. fw_text, text ? text : "-" );
  300. if ( i == ERR_OK || i == ERR_WARNING || i == ERR_ERROR )
  301. fprintf(f,"%*s-----+%.*s+%.*s\n",
  302. indent, "",
  303. fw_name+2, Minus300,
  304. fw_text+2, Minus300 );
  305. }
  306. fprintf(f,"%*s-----+%.*s+%.*s\n\n",
  307. indent, "",
  308. fw_name+2, Minus300,
  309. fw_text+2, Minus300 );
  310. }
  311. //
  312. ///////////////////////////////////////////////////////////////////////////////
  313. /////////////// error handling ///////////////
  314. ///////////////////////////////////////////////////////////////////////////////
  315. #ifdef EXTENDED_ERRORS
  316. ProgInfo_t ProgInfo = { .error_level = ERRLEV_EXTENDED };
  317. #else
  318. ProgInfo_t ProgInfo = { .error_level = ERRLEV_MINIMAL };
  319. #endif
  320. ///////////////////////////////////////////////////////////////////////////////
  321. enumError PrintError ( ccp func, ccp file, uint line,
  322. int syserr, enumError err_code, ccp format, ... )
  323. {
  324. va_list arg;
  325. va_start(arg,format);
  326. const int stat = PrintErrorArg(func,file,line,syserr,err_code,format,arg);
  327. va_end(arg);
  328. return stat;
  329. }
  330. ///////////////////////////////////////////////////////////////////////////////
  331. enumError PrintErrorFile ( ccp func, ccp file, uint line, struct File_t *F,
  332. int syserr, enumError err_code, ccp format, ... )
  333. {
  334. va_list arg;
  335. va_start(arg,format);
  336. const int stat = PrintErrorArg(func,file,line,syserr,err_code,format,arg);
  337. va_end(arg);
  338. return F ? RegisterFileError(F,stat) : stat;
  339. }
  340. ///////////////////////////////////////////////////////////////////////////////
  341. enumError PrintErrorArg ( ccp func, ccp file, unsigned int line,
  342. int syserr, enumError err_code, ccp format, va_list arg )
  343. {
  344. if ( err_code > ERR_OK )
  345. {
  346. ProgInfo.error_count++;
  347. ProgInfo.last_error = err_code;
  348. if ( ProgInfo.max_error < err_code )
  349. ProgInfo.max_error = err_code;
  350. }
  351. fflush(stdout);
  352. if (!stdwrn)
  353. {
  354. SetupStdMsg();
  355. if (!stdwrn)
  356. {
  357. stdwrn = stderr;
  358. if (!stdwrn)
  359. return err_code;
  360. }
  361. }
  362. char msg[1000];
  363. if (!ProgInfo.progname)
  364. ProgInfo.progname = "?";
  365. const int plen = strlen(ProgInfo.progname)+2;
  366. if ( !format && err_code > ERR_NOT_IMPLEMENTED )
  367. format = "Program is aborted immediately!";
  368. if (format)
  369. {
  370. vsnprintf(msg,sizeof(msg),format,arg);
  371. msg[sizeof(msg)-2] = 0;
  372. const int mlen = strlen(msg);
  373. if ( mlen > 0 && msg[mlen-1] != '\n' )
  374. {
  375. msg[mlen] = '\n';
  376. msg[mlen+1] = 0;
  377. }
  378. }
  379. else
  380. StringCat2S(msg,sizeof(msg),GetErrorText(err_code,0),"\n");
  381. ccp prefix = err_code == ERR_OK ? "" : err_code <= ERR_WARNING ? "! " : "!! ";
  382. const int fw = GetTermWidth(80,40) - 1;
  383. #ifdef DEBUG
  384. TRACE("%s%s #%d [%s] in %s() @ %s#%d\n",
  385. prefix, err_code <= ERR_WARNING ? "WARNING" : "ERROR",
  386. err_code, GetErrorName(err_code,0), func, file, line );
  387. TRACE("%s%*s%s",prefix,plen,"",msg);
  388. if (syserr)
  389. TRACE("!! %s(), ERRNO=%d: %s\n",__FUNCTION__,syserr,strerror(syserr));
  390. fflush(TRACE_FILE);
  391. #endif
  392. ccp coln, col1, col0;
  393. if (IsFileColorized(stdwrn))
  394. {
  395. coln = GetTextMode(1,TTM_COL_INFO);
  396. col1 = GetTextMode(1, err_code > ERR_WARNING ? TTM_COL_WARN : TTM_COL_HINT );
  397. col0 = TermTextModeReset;
  398. }
  399. else
  400. coln = col1 = col0 = EmptyString;
  401. if ( err_code >= ERR_NOT_IMPLEMENTED
  402. || ProgInfo.error_level >= ERRLEV_HEADING && err_code > ERR_WARNING )
  403. {
  404. if ( err_code >= ERR_NOT_IMPLEMENTED || ProgInfo.error_level >= ERRLEV_EXTENDED )
  405. {
  406. if ( err_code > ERR_WARNING )
  407. fprintf(stdwrn,"%s%s%s:%s ERROR #%d [%s] in %s() @ %s#%d%s\n",
  408. prefix, coln, ProgInfo.progname, col1, err_code,
  409. GetErrorName(err_code,0), func, file, line, col0 );
  410. else
  411. fprintf(stdwrn,"%s%s%s:%s WARNING in %s() @ %s#%d%s\n",
  412. prefix, coln, ProgInfo.progname, col1, func, file, line, col0 );
  413. }
  414. else
  415. {
  416. if ( err_code > ERR_WARNING )
  417. fprintf(stdwrn,"%s%s%s:%s ERROR #%d [%s]%s\n",
  418. prefix, coln, ProgInfo.progname, col1, err_code,
  419. GetErrorName(err_code,0), col0 );
  420. else
  421. fprintf(stdwrn,"%s%s%s:%s WARNING%s\n",
  422. prefix, coln, ProgInfo.progname, col1, col0 );
  423. }
  424. if ( ProgInfo.error_level >= ERRLEV_ANNOTATE )
  425. {
  426. #ifdef SVN_NEXT
  427. fprintf(stdwrn,"%s -> %s/%s?annotate=%d#l%d\n",
  428. prefix, URI_VIEWVC, file, SVN_NEXT, line );
  429. #elif defined(REVISION_NEXT)
  430. fprintf(stdwrn,"%s -> %s/%s?annotate=%d#l%d\n",
  431. prefix, URI_VIEWVC, file, REVISION_NEXT, line );
  432. #endif
  433. }
  434. fputs(prefix,stdwrn);
  435. PutLines(stdwrn,plen,fw,0,prefix,msg,0);
  436. }
  437. else
  438. {
  439. fprintf(stdwrn,"%s%s%s:%s",prefix,coln,ProgInfo.progname,col0);
  440. PutLines(stdwrn,plen,fw,strlen(ProgInfo.progname)+1,prefix,msg,0);
  441. }
  442. if (syserr)
  443. {
  444. fprintf(stdwrn,"%s%*s-> ",prefix,plen,"");
  445. snprintf(msg,sizeof(msg),"%s [%d]",strerror(syserr),syserr);
  446. PutLines(stdwrn,plen+3,fw,plen+3,prefix,msg,0);
  447. }
  448. fflush(stdwrn);
  449. if ( err_code > ERR_NOT_IMPLEMENTED )
  450. exit(err_code);
  451. return err_code;
  452. }
  453. ///////////////////////////////////////////////////////////////////////////////
  454. enumError PrintErrorStat ( enumError err, int verbose, ccp cmdname )
  455. {
  456. if ( verbose > 0 && err >= ERR_WARNING
  457. || verbose > 1 && err
  458. || err == ERR_NOT_IMPLEMENTED )
  459. {
  460. if (!stdwrn)
  461. {
  462. SetupStdMsg();
  463. if (!stdwrn)
  464. {
  465. stdwrn = stderr;
  466. if (!stdwrn)
  467. return err;
  468. }
  469. }
  470. fprintf(stdwrn,"%s: Command '%s' returns with status #%d [%s]\n",
  471. ProgInfo.progname, cmdname, err, GetErrorName(err,0) );
  472. }
  473. return err;
  474. }
  475. ///////////////////////////////////////////////////////////////////////////////
  476. bool mark_used ( ccp name, ... )
  477. {
  478. return true;
  479. }
  480. ///////////////////////////////////////////////////////////////////////////////
  481. #if HAVE_PRINT
  482. #undef PRINT_TIME
  483. void PRINT_TIME ( time_t time, ccp title )
  484. {
  485. struct tm utc, loc;
  486. gmtime_r(&time,&utc);
  487. localtime_r(&time,&loc);
  488. PRINT( "%s: UTC= %4u-%02u-%02u %2u:%02u:%02u dst=%d"
  489. " LOCAL= %4u-%02u-%02u %2u:%02u:%02u dst=%d\n",
  490. title ? title : "TIME",
  491. utc.tm_year+1900, utc.tm_mon+1, utc.tm_mday,
  492. utc.tm_hour, utc.tm_min, utc.tm_sec, utc.tm_isdst,
  493. loc.tm_year+1900, loc.tm_mon+1, loc.tm_mday,
  494. loc.tm_hour, loc.tm_min, loc.tm_sec, loc.tm_isdst );
  495. }
  496. #endif
  497. //
  498. ///////////////////////////////////////////////////////////////////////////////
  499. /////////////// hexdump ///////////////
  500. ///////////////////////////////////////////////////////////////////////////////
  501. uint HexDump16 ( FILE * f, int indent, u64 addr,
  502. const void * data, size_t count )
  503. {
  504. // return the number of printed lines
  505. return HexDump(f,indent,addr,4,16,data,count);
  506. }
  507. //-----------------------------------------------------------------------------
  508. uint HexDump20 ( FILE * f, int indent, u64 addr,
  509. const void * data, size_t count )
  510. {
  511. // return the number of printed lines
  512. return HexDump(f,indent,addr,4,20,data,count);
  513. }
  514. //-----------------------------------------------------------------------------
  515. uint HexDump ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
  516. const void * p_data, size_t count )
  517. {
  518. // return the number of printed lines
  519. if ( !f || !p_data || !count )
  520. return 0;
  521. XDump_t xd;
  522. InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
  523. const int stat = XDump(&xd,p_data,count,true);
  524. return stat < 0 ? 0 : stat;
  525. }
  526. ///////////////////////////////////////////////////////////////////////////////
  527. uint HexDump16BE2 ( FILE * f, int indent, u64 addr,
  528. const void * data, size_t count )
  529. {
  530. // return the number of printed lines
  531. return HexDumpBE2(f,indent,addr,4,16,data,count);
  532. }
  533. //-----------------------------------------------------------------------------
  534. uint HexDump20BE2 ( FILE * f, int indent, u64 addr,
  535. const void * data, size_t count )
  536. {
  537. // return the number of printed lines
  538. return HexDumpBE2(f,indent,addr,4,20,data,count);
  539. }
  540. //-----------------------------------------------------------------------------
  541. uint HexDumpBE2 ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
  542. const void * p_data, size_t count )
  543. {
  544. // return the number of printed lines
  545. if ( !f || !p_data || !count )
  546. return 0;
  547. XDump_t xd;
  548. InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
  549. xd.format = XDUMPF_INT_2;
  550. xd.endian = DC_BIG_ENDIAN;
  551. const int stat = XDump(&xd,p_data,count,true);
  552. return stat < 0 ? 0 : stat;
  553. }
  554. ///////////////////////////////////////////////////////////////////////////////
  555. uint HexDump16LE2 ( FILE * f, int indent, u64 addr,
  556. const void * data, size_t count )
  557. {
  558. // return the number of printed lines
  559. return HexDumpLE2(f,indent,addr,4,16,data,count);
  560. }
  561. //-----------------------------------------------------------------------------
  562. uint HexDump20LE2 ( FILE * f, int indent, u64 addr,
  563. const void * data, size_t count )
  564. {
  565. // return the number of printed lines
  566. return HexDumpLE2(f,indent,addr,4,20,data,count);
  567. }
  568. //-----------------------------------------------------------------------------
  569. uint HexDumpLE2 ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
  570. const void * p_data, size_t count )
  571. {
  572. // return the number of printed lines
  573. if ( !f || !p_data || !count )
  574. return 0;
  575. XDump_t xd;
  576. InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
  577. xd.format = XDUMPF_INT_2;
  578. xd.endian = DC_LITTLE_ENDIAN;
  579. const int stat = XDump(&xd,p_data,count,true);
  580. return stat < 0 ? 0 : stat;
  581. }
  582. ///////////////////////////////////////////////////////////////////////////////
  583. uint HexDump16BE4 ( FILE * f, int indent, u64 addr,
  584. const void * data, size_t count )
  585. {
  586. // return the number of printed lines
  587. return HexDumpBE4(f,indent,addr,4,16,data,count);
  588. }
  589. //-----------------------------------------------------------------------------
  590. uint HexDump20BE4 ( FILE * f, int indent, u64 addr,
  591. const void * data, size_t count )
  592. {
  593. // return the number of printed lines
  594. return HexDumpBE4(f,indent,addr,4,20,data,count);
  595. }
  596. //-----------------------------------------------------------------------------
  597. uint HexDumpBE4 ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
  598. const void * p_data, size_t count )
  599. {
  600. // return the number of printed lines
  601. if ( !f || !p_data || !count )
  602. return 0;
  603. XDump_t xd;
  604. InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
  605. xd.format = XDUMPF_INT_4;
  606. xd.endian = DC_BIG_ENDIAN;
  607. const int stat = XDump(&xd,p_data,count,true);
  608. return stat < 0 ? 0 : stat;
  609. }
  610. ///////////////////////////////////////////////////////////////////////////////
  611. uint HexDump16LE4 ( FILE * f, int indent, u64 addr,
  612. const void * data, size_t count )
  613. {
  614. // return the number of printed lines
  615. return HexDumpLE4(f,indent,addr,4,16,data,count);
  616. }
  617. //-----------------------------------------------------------------------------
  618. uint HexDump20LE4 ( FILE * f, int indent, u64 addr,
  619. const void * data, size_t count )
  620. {
  621. // return the number of printed lines
  622. return HexDumpLE4(f,indent,addr,4,20,data,count);
  623. }
  624. //-----------------------------------------------------------------------------
  625. uint HexDumpLE4 ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
  626. const void * p_data, size_t count )
  627. {
  628. // return the number of printed lines
  629. if ( !f || !p_data || !count )
  630. return 0;
  631. XDump_t xd;
  632. InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
  633. xd.format = XDUMPF_INT_4;
  634. xd.endian = DC_LITTLE_ENDIAN;
  635. const int stat = XDump(&xd,p_data,count,true);
  636. return stat < 0 ? 0 : stat;
  637. }
  638. ///////////////////////////////////////////////////////////////////////////////
  639. uint HexDump016 ( FILE * f, int indent, u64 addr,
  640. const void * data, size_t count )
  641. {
  642. // return the number of printed lines
  643. return HexDump0(f,indent,addr,4,16,data,count);
  644. }
  645. //-----------------------------------------------------------------------------
  646. uint HexDump0 ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
  647. const void * p_data, size_t count )
  648. {
  649. // return the number of printed lines
  650. if ( !f || !p_data || !count )
  651. return 0;
  652. XDump_t xd;
  653. InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
  654. xd.mode_ignore = true;
  655. const int stat = XDump(&xd,p_data,count,true);
  656. return stat < 0 ? 0 : stat;
  657. }
  658. ///////////////////////////////////////////////////////////////////////////////
  659. void HexDiff16 ( FILE * f, int indent, u64 addr,
  660. const void * data1, size_t count1,
  661. const void * data2, size_t count2 )
  662. {
  663. HexDiff(f,indent,addr,4,16,data1,count1,data2,count2);
  664. }
  665. //-----------------------------------------------------------------------------
  666. void HexDiff ( FILE * f, int indent, u64 addr, int addr_fw, int row_len,
  667. const void * p_data1, size_t count1,
  668. const void * p_data2, size_t count2 )
  669. {
  670. if (!f)
  671. return;
  672. XDump_t xd;
  673. InitializeXDumpEx(&xd,f,indent,addr,addr_fw,row_len);
  674. XDiff(&xd, p_data1,count1,true, p_data2,count2,true, 0,false );
  675. return;
  676. }
  677. //
  678. ///////////////////////////////////////////////////////////////////////////////
  679. /////////////// trace functions ///////////////
  680. ///////////////////////////////////////////////////////////////////////////////
  681. static void trace_helper ( int print_stderr, ccp format, va_list arg )
  682. {
  683. static uint last_day = 0;
  684. DayTime_t dt = GetDayTime(false);
  685. if (!TRACE_FILE)
  686. {
  687. char extend[16];
  688. if (ProgInfo.multi_processing)
  689. snprintf(extend,sizeof(extend),"-%u",getpid());
  690. else
  691. *extend = 0;
  692. char fname[1000];
  693. if ( ProgInfo.progname && *ProgInfo.progname && *ProgInfo.progname != '?' )
  694. snprintf( fname, sizeof(fname), "_trace-%s%s.tmp", ProgInfo.progname, extend );
  695. else
  696. snprintf( fname, sizeof(fname), "_trace-%s.tmp", extend );
  697. TRACE_FILE = fopen(fname,"wb");
  698. if (TRACE_FILE)
  699. {
  700. fcntl(fileno(TRACE_FILE),F_SETFD,FD_CLOEXEC);
  701. fprintf(TRACE_FILE,"\n#\f\n#OPEN %s, pid=%d\n\n",
  702. PrintTimeByFormat("%F %T %z",dt.time), getpid() );
  703. }
  704. else
  705. TRACE_FILE = stderr;
  706. last_day = dt.day;
  707. }
  708. char pre[200];
  709. if ( last_day != dt.day )
  710. {
  711. SetupTimezone(true);
  712. dt = GetDayTime(false);
  713. last_day = dt.day;
  714. snprintf(pre,sizeof(pre),
  715. "\n#DAY: %s, pid=%d\n%02u:%02u:%02u.%03u ",
  716. PrintTimeByFormat("%F %T %z",dt.time), getpid(),
  717. dt.hour, dt.min, dt.sec, dt.usec/1000 );
  718. }
  719. else
  720. snprintf(pre,sizeof(pre),"%02u:%02u:%02u.%03u ",
  721. dt.hour, dt.min, dt.sec, dt.usec/1000 );
  722. if ( print_stderr || TRACE_FILE == stderr )
  723. {
  724. fflush(stdout);
  725. fputs(pre,stderr);
  726. fputs(TRACE_PREFIX,stderr);
  727. va_list arg2;
  728. va_copy(arg2,arg);
  729. vfprintf(stderr,format,arg2);
  730. va_end(arg2);
  731. fflush(stderr);
  732. }
  733. if ( TRACE_FILE && TRACE_FILE != stderr )
  734. {
  735. fputs(pre,TRACE_FILE);
  736. fputs(TRACE_PREFIX,TRACE_FILE);
  737. vfprintf(TRACE_FILE,format,arg);
  738. fflush(TRACE_FILE);
  739. }
  740. }
  741. ///////////////////////////////////////////////////////////////////////////////
  742. #undef TRACE_ARG_FUNC
  743. void TRACE_ARG_FUNC ( ccp format, va_list arg )
  744. {
  745. trace_helper(0,format,arg);
  746. }
  747. ///////////////////////////////////////////////////////////////////////////////
  748. #undef TRACE_FUNC
  749. void TRACE_FUNC ( ccp format, ... )
  750. {
  751. va_list arg;
  752. va_start(arg,format);
  753. trace_helper(0,format,arg);
  754. va_end(arg);
  755. }
  756. ///////////////////////////////////////////////////////////////////////////////
  757. #undef PRINT_ARG_FUNC
  758. void PRINT_ARG_FUNC ( ccp format, va_list arg )
  759. {
  760. trace_helper(1,format,arg);
  761. }
  762. ///////////////////////////////////////////////////////////////////////////////
  763. #undef PRINT_FUNC
  764. void PRINT_FUNC ( ccp format, ... )
  765. {
  766. va_list arg;
  767. va_start(arg,format);
  768. trace_helper(1,format,arg);
  769. va_end(arg);
  770. }
  771. ///////////////////////////////////////////////////////////////////////////////
  772. #undef BINGO_FUNC
  773. void BINGO_FUNC ( ccp func, int line, ccp src )
  774. {
  775. #if defined(DEBUG) && defined(TEST)
  776. if (colerr)
  777. PRINT_FUNC("%sBINGO!%s %s() #%d @ %s\n",
  778. colerr->magenta, colerr->reset, func, line, src );
  779. else
  780. PRINT_FUNC("BINGO! %s() #%d @ %s\n", func, line, src );
  781. #else
  782. if (colerr)
  783. fprintf(stderr,"%sBINGO!%s %s() #%d @ %s\n",
  784. colerr->magenta, colerr->reset, func, line, src );
  785. else
  786. fprintf(stderr,"BINGO! %s() #%d @ %s\n", func, line, src );
  787. #endif
  788. }
  789. ///////////////////////////////////////////////////////////////////////////////
  790. #undef WAIT_ARG_FUNC
  791. void WAIT_ARG_FUNC ( ccp format, va_list arg )
  792. {
  793. if ( format && *format )
  794. trace_helper(1,format,arg);
  795. PRINT_FUNC(">>>>>> PRESS RETURN: ");
  796. getchar();
  797. }
  798. ///////////////////////////////////////////////////////////////////////////////
  799. #undef WAIT_FUNC
  800. void WAIT_FUNC ( ccp format, ... )
  801. {
  802. va_list arg;
  803. va_start(arg,format);
  804. WAIT_ARG_FUNC(format,arg);
  805. va_end(arg);
  806. }
  807. //
  808. ///////////////////////////////////////////////////////////////////////////////
  809. /////////////// alloc/free system ///////////////
  810. ///////////////////////////////////////////////////////////////////////////////
  811. #if TRACE_ALLOC_MODE > 2
  812. #define MEM_FILLER_SIZE 8
  813. static u8 mem_filler[MEM_FILLER_SIZE];
  814. #else
  815. #define MEM_FILLER_SIZE 0
  816. #endif
  817. #if TRACE_ALLOC_MODE > 1
  818. #define MPARAM ccp func, ccp file, uint line,
  819. #define MCALL func,file,line,
  820. #define PRINT_OOM(...) \
  821. PrintError(func,file,line,0,ERR_OUT_OF_MEMORY,__VA_ARGS__)
  822. #else
  823. #define MPARAM
  824. #define MCALL
  825. #define PRINT_OOM(...) \
  826. PrintError(__FUNCTION__,__FILE__,__LINE__,0,ERR_OUT_OF_MEMORY,__VA_ARGS__)
  827. #endif
  828. ///////////////////////////////////////////////////////////////////////////////
  829. void dclib_free ( void * ptr )
  830. {
  831. free(ptr);
  832. }
  833. ///////////////////////////////////////////////////////////////////////////////
  834. ///////////////////////////////////////////////////////////////////////////////
  835. void * dclib_xcalloc ( size_t nmemb, size_t size )
  836. {
  837. void * res = calloc(nmemb,size);
  838. if (!res)
  839. PrintError(__FUNCTION__,__FILE__,__LINE__,0,ERR_OUT_OF_MEMORY,
  840. "Out of memory while calloc() %zu bytes (%zu*%zu=0x%zx)\n",
  841. nmemb*size, nmemb, size, nmemb*size );
  842. return res;
  843. }
  844. ///////////////////////////////////////////////////////////////////////////////
  845. void * dclib_xmalloc ( size_t size )
  846. {
  847. void * res = malloc(size);
  848. if (!res)
  849. PrintError(__FUNCTION__,__FILE__,__LINE__,0,ERR_OUT_OF_MEMORY,
  850. "Out of memory while malloc() %zu bytes (0x%zx)\n",
  851. size, size );
  852. return res;
  853. }
  854. ///////////////////////////////////////////////////////////////////////////////
  855. void * dclib_xrealloc ( void * ptr, size_t size )
  856. {
  857. void * res = realloc(ptr,size);
  858. if ( !res && size )
  859. PrintError(__FUNCTION__,__FILE__,__LINE__,0,ERR_OUT_OF_MEMORY,
  860. "Out of memory while realloc() %zu bytes (0x%zx)\n",
  861. size, size );
  862. return res;
  863. }
  864. ///////////////////////////////////////////////////////////////////////////////
  865. char * dclib_xstrdup ( ccp src )
  866. {
  867. char * res = strdup(src?src:"");
  868. if (!res)
  869. {
  870. const uint size = src ? strlen(src)+1 : 0;
  871. PrintError(__FUNCTION__,__FILE__,__LINE__,0,ERR_OUT_OF_MEMORY,
  872. "Out of memory while strdup() %u bytes (0x%x)\n",
  873. size, size );
  874. }
  875. return res;
  876. }
  877. ///////////////////////////////////////////////////////////////////////////////
  878. ///////////////////////////////////////////////////////////////////////////////
  879. void * dclib_calloc ( MPARAM size_t nmemb, size_t size )
  880. {
  881. void * res = calloc(nmemb,size);
  882. if (!res)
  883. PRINT_OOM("Out of memory while calloc() %zu bytes (%zu*%zu=0x%zx)\n",
  884. nmemb*size, nmemb, size, nmemb*size );
  885. return res;
  886. }
  887. ///////////////////////////////////////////////////////////////////////////////
  888. void * dclib_malloc ( MPARAM size_t size )
  889. {
  890. void * res = malloc(size);
  891. if (!res)
  892. PRINT_OOM("Out of memory while malloc() %zu bytes (0x%zx)\n",
  893. size, size );
  894. return res;
  895. }
  896. ///////////////////////////////////////////////////////////////////////////////
  897. void * dclib_realloc ( MPARAM void * ptr, size_t size )
  898. {
  899. void * res = realloc(ptr,size);
  900. if ( !res && size )
  901. PRINT_OOM("Out of memory while realloc() %zu bytes (0x%zx)\n",
  902. size, size );
  903. return res;
  904. }
  905. ///////////////////////////////////////////////////////////////////////////////
  906. ///////////////////////////////////////////////////////////////////////////////
  907. char * dclib_strdup ( MPARAM ccp src )
  908. {
  909. char * res = strdup(src?src:"");
  910. if (!res)
  911. {
  912. const uint size = src ? strlen(src)+1 : 0;
  913. PRINT_OOM("Out of memory while strdup() %u bytes (0x%x)\n",
  914. size, size );
  915. }
  916. return res;
  917. }
  918. ///////////////////////////////////////////////////////////////////////////////
  919. char * dclib_strdup2 ( MPARAM ccp src1, ccp src2 )
  920. {
  921. const uint len1 = src1 ? strlen(src1) : 0;
  922. const uint len2 = src2 ? strlen(src2) : 0;
  923. char * res = dclib_malloc( MCALL len1+len2+1 );
  924. if (len1)
  925. memcpy(res,src1,len1);
  926. if (len2)
  927. memcpy(res+len1,src2,len2);
  928. res[len1+len2] = 0;
  929. return res;
  930. }
  931. ///////////////////////////////////////////////////////////////////////////////
  932. char * dclib_strdup3 ( MPARAM ccp src1, ccp src2, ccp src3 )
  933. {
  934. const uint len1 = src1 ? strlen(src1) : 0;
  935. const uint len2 = src2 ? strlen(src2) : 0;
  936. const uint len3 = src3 ? strlen(src3) : 0;
  937. char * res = dclib_malloc( MCALL len1+len2+len3+1 );
  938. char * dest = res;
  939. if (len1)
  940. {
  941. memcpy(dest,src1,len1);
  942. dest += len1;
  943. }
  944. if (len2)
  945. {
  946. memcpy(dest,src2,len2);
  947. dest += len2;
  948. }
  949. if (len3)
  950. {
  951. memcpy(dest,src3,len3);
  952. dest += len3;
  953. }
  954. *dest = 0;
  955. return res;
  956. }
  957. ///////////////////////////////////////////////////////////////////////////////
  958. void * dclib_memdup ( MPARAM const void * src, size_t copylen )
  959. {
  960. char * dest = dclib_malloc( MCALL copylen+1);
  961. memcpy(dest,src,copylen);
  962. dest[copylen] = 0;
  963. return dest;
  964. }
  965. ///////////////////////////////////////////////////////////////////////////////
  966. void * dclib_memdup2 ( MPARAM cvp src1, size_t len1, cvp src2, size_t len2 )
  967. {
  968. char *res = dclib_malloc( MCALL len1+len2+1);
  969. char *dest = res;
  970. if (len1)
  971. {
  972. memcpy(dest,src1,len1);
  973. dest += len1;
  974. }
  975. if (len2)
  976. {
  977. memcpy(dest,src2,len2);
  978. dest += len2;
  979. }
  980. *dest = 0;
  981. return res;
  982. }
  983. ///////////////////////////////////////////////////////////////////////////////
  984. void * dclib_memdup3
  985. ( MPARAM cvp src1, size_t len1, cvp src2, size_t len2, cvp src3, size_t len3 )
  986. {
  987. char *res = dclib_malloc( MCALL len1+len2+len3+1);
  988. char *dest = res;
  989. if (len1)
  990. {
  991. memcpy(dest,src1,len1);
  992. dest += len1;
  993. }
  994. if (len2)
  995. {
  996. memcpy(dest,src2,len2);
  997. dest += len2;
  998. }
  999. if (len3)
  1000. {
  1001. memcpy(dest,src3,len3);
  1002. dest += len3;
  1003. }
  1004. *dest = 0;
  1005. return res;
  1006. }
  1007. ///////////////////////////////////////////////////////////////////////////////
  1008. void * dclib_allocdup ( MPARAM const void * src, size_t copylen )
  1009. {
  1010. char * dest = dclib_malloc( MCALL copylen);
  1011. memcpy(dest,src,copylen);
  1012. return dest;
  1013. }
  1014. ///////////////////////////////////////////////////////////////////////////////
  1015. char * dclib_printdup ( MPARAM ccp format, ... )
  1016. {
  1017. char buf[2000];
  1018. va_list arg;
  1019. va_start(arg,format);
  1020. const int stat = vsnprintf(buf,sizeof(buf),format,arg) + 1;
  1021. va_end(arg);
  1022. char * dest = dclib_malloc( MCALL stat );
  1023. if ( stat <= sizeof(buf) )
  1024. memcpy(dest,buf,stat);
  1025. else
  1026. {
  1027. va_start(arg,format);
  1028. vsnprintf(dest,stat,format,arg);
  1029. va_end(arg);
  1030. }
  1031. return dest;
  1032. }
  1033. //
  1034. ///////////////////////////////////////////////////////////////////////////////
  1035. ///////////////////////////////////////////////////////////////////////////////
  1036. #if TRACE_ALLOC_MODE > 2
  1037. ///////////////////////////////////////////////////////////////////////////////
  1038. // [[mem_info_t]]
  1039. typedef struct mem_info_t
  1040. {
  1041. ccp func;
  1042. ccp file;
  1043. uint line;
  1044. u8 * data;
  1045. uint size;
  1046. bool filler;
  1047. uint seqnum;
  1048. } mem_info_t;
  1049. //-----------------------------------------------------------------------------
  1050. static mem_info_t * mem_list = 0;
  1051. static uint mem_used = 0;
  1052. static uint mem_size = 0;
  1053. static uint mem_seqnum = 0;
  1054. ///////////////////////////////////////////////////////////////////////////////
  1055. static FILE * OpenMemLog()
  1056. {
  1057. static bool done = false;
  1058. if (!done)
  1059. {
  1060. done = true;
  1061. char fname[500];
  1062. if ( ProgInfo.progname && *ProgInfo.progname && *ProgInfo.progname != '?' )
  1063. snprintf(fname,sizeof(fname),"_trace_alloc-%s-%u.tmp",ProgInfo.progname,getpid());
  1064. else
  1065. snprintf(fname,sizeof(fname),"_trace_alloc-%u.tmp",getpid());
  1066. MEM_LOG_FILE = fopen(fname,"ab");
  1067. if (MEM_LOG_FILE)
  1068. {
  1069. fcntl(fileno(MEM_LOG_FILE),F_SETFD,FD_CLOEXEC);
  1070. fprintf(MEM_LOG_FILE,"#\f\n# OPEN FILE %s\n",
  1071. PrintTimeByFormat("%F %T %z",time(0)));
  1072. fflush(MEM_LOG_FILE);
  1073. }
  1074. }
  1075. return MEM_LOG_FILE;
  1076. }
  1077. ///////////////////////////////////////////////////////////////////////////////
  1078. static void MemLog
  1079. ( ccp func, ccp file, uint line, ccp info, const u8 * ptr, bool no_stderr )
  1080. {
  1081. static uint line_no = 0;
  1082. if (!no_stderr)
  1083. {
  1084. fprintf(stderr,"MEM-ERR[%s,%p] %s() @ %s#%u\n",
  1085. info, ptr, func, file, line );
  1086. fflush(stderr);
  1087. }
  1088. if (OpenMemLog())
  1089. {
  1090. if (!line_no)
  1091. fputs("#index addr type function line file\n",MEM_LOG_FILE);
  1092. fprintf(MEM_LOG_FILE,"%07u %p %s %s %u %s\n",
  1093. line_no, ptr, info, func, line, file );
  1094. fflush(MEM_LOG_FILE);
  1095. }
  1096. line_no++;
  1097. }
  1098. ///////////////////////////////////////////////////////////////////////////////
  1099. static void MemLogItem
  1100. ( ccp func, ccp file, uint line, ccp info, const mem_info_t * mi )
  1101. {
  1102. if (func)
  1103. fprintf(stderr,"MEM-ERR[%s] %s() @ %s#%u\n",
  1104. info, func, file, line );
  1105. fprintf(stderr,"MEM-ERR[%s,%u] %s() @ %s#%u\n",
  1106. info, mi->seqnum, mi->func, mi->file, mi->line );
  1107. fflush(stderr);
  1108. if (OpenMemLog())
  1109. {
  1110. if (func)
  1111. fprintf(MEM_LOG_FILE,"[%s] %s() @ %s#%u\n",
  1112. info, func, file, line );
  1113. fprintf(MEM_LOG_FILE,"[%s,%u] %s() @ %s#%u\n",
  1114. info, mi->seqnum, mi->func, mi->file, mi->line );
  1115. if (mi->filler)
  1116. {
  1117. HexDump16( MEM_LOG_FILE, 10, 0,
  1118. mi->data - MEM_FILLER_SIZE, 2*MEM_FILLER_SIZE );
  1119. HexDump16( MEM_LOG_FILE, 10, mi->size + MEM_FILLER_SIZE,
  1120. mi->data + mi->size - MEM_FILLER_SIZE, 2 * MEM_FILLER_SIZE );
  1121. }
  1122. fputc('\n',MEM_LOG_FILE);
  1123. fflush(MEM_LOG_FILE);
  1124. }
  1125. }
  1126. ///////////////////////////////////////////////////////////////////////////////
  1127. uint FindMemInfoHelper ( u8 * data, uint size )
  1128. {
  1129. int beg = 0;
  1130. int end = mem_used - 1;
  1131. while ( beg <= end )
  1132. {
  1133. uint idx = (beg+end)/2;
  1134. mem_info_t * mi = mem_list + idx;
  1135. if ( data < mi->data )
  1136. end = idx - 1 ;
  1137. else if ( data > mi->data )
  1138. beg = idx + 1;
  1139. else if ( size < mi->size )
  1140. end = idx - 1 ;
  1141. else if ( size > mi->size )
  1142. beg = idx + 1;
  1143. else
  1144. {
  1145. noTRACE("FindMemInfoHelper(%llx,%llx) FOUND=%d/%d/%d\n",
  1146. (u64)off, (u64)size, idx, mem_used, mem_size );
  1147. return idx;
  1148. }
  1149. }
  1150. noTRACE("FindStringFieldHelper(%llx,%llx) failed=%d/%d/%d\n",
  1151. (u64)off, (u64)size, beg, mem_used, mem_size );
  1152. return beg;
  1153. }
  1154. ///////////////////////////////////////////////////////////////////////////////
  1155. ///////////////////////////////////////////////////////////////////////////////
  1156. void InitializeTraceAlloc()
  1157. {
  1158. static bool done = false;
  1159. if (done)
  1160. return;
  1161. done = true;
  1162. OpenMemLog();
  1163. memset(mem_filler,0xdc,sizeof(mem_filler));
  1164. mem_filler[MEM_FILLER_SIZE/2] = 0xcd;
  1165. mem_filler[MEM_FILLER_SIZE/3] = 0xcd;
  1166. }
  1167. ///////////////////////////////////////////////////////////////////////////////
  1168. int CheckTraceAlloc ( ccp func, ccp file, uint line )
  1169. {
  1170. int count = 0;
  1171. mem_info_t *ptr, *end = mem_list + mem_used;
  1172. for ( ptr = mem_list; ptr < end; ptr++ )
  1173. {
  1174. if (!ptr->filler)
  1175. continue;
  1176. if (memcmp(ptr->data-MEM_FILLER_SIZE,mem_filler,sizeof(mem_filler)))
  1177. {
  1178. MemLogItem(0,0,0,"BEG",ptr);
  1179. memcpy(ptr->data-MEM_FILLER_SIZE,mem_filler,sizeof(mem_filler));
  1180. count++;
  1181. }
  1182. if (memcmp(ptr->data+ptr->size,mem_filler,sizeof(mem_filler)))
  1183. {
  1184. MemLogItem(0,0,0,"END",ptr);
  1185. memcpy(ptr->data+ptr->size,mem_filler,sizeof(mem_filler));
  1186. count++;
  1187. }
  1188. }
  1189. if (count)
  1190. {
  1191. fprintf(stderr,"MEM-ERR: %u errors found -> %s() @ %s#%u\n",
  1192. count, func, file, line );
  1193. fflush(stderr);
  1194. if (OpenMemLog())
  1195. {
  1196. fprintf(MEM_LOG_FILE,"--- %u errors found -> %s() @ %s#%u\n\n",
  1197. count, func, file, line );
  1198. fflush(MEM_LOG_FILE);
  1199. }
  1200. }
  1201. return count;
  1202. }
  1203. ///////////////////////////////////////////////////////////////////////////////
  1204. void DumpTraceAlloc ( ccp func, ccp file, uint line, FILE * f )
  1205. {
  1206. CheckTraceAlloc(func,file,line);
  1207. if (!f)
  1208. return;
  1209. fprintf(f,"---- MEM-DUMP [N=%u] -----\n",mem_used);
  1210. mem_info_t *ptr, *end = mem_list + mem_used;
  1211. for ( ptr = mem_list; ptr < end; ptr++ )
  1212. {
  1213. ccp file = strrchr(ptr->file,'/');
  1214. file = file ? file+1 : ptr->file;
  1215. fprintf(f,"%6u %p %6u %-20.20s %5u %-.25s\n",
  1216. ptr->seqnum, ptr->data, ptr->size,
  1217. ptr->func, ptr->line, file );
  1218. }
  1219. fflush(stderr);
  1220. }
  1221. ///////////////////////////////////////////////////////////////////////////////
  1222. mem_info_t * RegisterAlloc
  1223. ( ccp func, ccp file, uint line, cvp data, uint size, bool filler )
  1224. {
  1225. if (!mem_seqnum)
  1226. InitializeTraceAlloc();
  1227. if (filler)
  1228. {
  1229. memcpy((u8*)data-MEM_FILLER_SIZE,mem_filler,MEM_FILLER_SIZE);
  1230. memcpy((u8*)data+size,mem_filler,MEM_FILLER_SIZE);
  1231. }
  1232. uint idx = FindMemInfoHelper((u8*)data,size);
  1233. ASSERT( mem_used <= mem_size );
  1234. if ( mem_used == mem_size )
  1235. {
  1236. mem_size += 1000 + mem_size/2;
  1237. const uint alloc_size = mem_size * sizeof(*mem_list);
  1238. mem_list = realloc(mem_list,alloc_size);
  1239. if (!mem_list)
  1240. PRINT_OOM("Out of memory while RegisterAlloc() %u bytes (0x%x)\n",
  1241. alloc_size, alloc_size );
  1242. }
  1243. DASSERT( idx <= mem_used );
  1244. mem_info_t * mi = mem_list + idx;
  1245. memmove(mi+1,mi,(mem_used-idx)*sizeof(*mi));
  1246. mem_used++;
  1247. mi->func = func;
  1248. mi->file = file;
  1249. mi->line = line;
  1250. mi->data = (u8*)data;
  1251. mi->size = size;
  1252. mi->filler = filler;
  1253. mi->seqnum = mem_seqnum++;
  1254. #if TRACE_ALLOC_MODE > 3
  1255. MemLog(func,file,line,"ALLOC",data,true);
  1256. #endif
  1257. return mi;
  1258. }
  1259. //
  1260. ///////////////////////////////////////////////////////////////////////////////
  1261. #endif // TRACE_ALLOC_MODE > 2
  1262. ///////////////////////////////////////////////////////////////////////////////
  1263. ///////////////////////////////////////////////////////////////////////////////
  1264. uint trace_test_alloc
  1265. ( ccp func, ccp file, uint line, const void *p_data, bool hexdump )
  1266. {
  1267. #if TRACE_ALLOC_MODE > 2
  1268. if (!p_data)
  1269. return 0;
  1270. const u8 *data = p_data;
  1271. int beg = 0;
  1272. int end = mem_used - 1;
  1273. while ( beg <= end )
  1274. {
  1275. uint idx = (beg+end)/2;
  1276. mem_info_t * mi = mem_list + idx;
  1277. if ( data < mi->data )
  1278. end = idx - 1 ;
  1279. else if ( data > mi->data )
  1280. beg = idx + 1;
  1281. else
  1282. {
  1283. #if TRACE_ALLOC_MODE > 3
  1284. MemLog(func,file,line,"TEST",data,true);
  1285. #endif
  1286. uint stat = 0;
  1287. if (memcmp(data-MEM_FILLER_SIZE,mem_filler,MEM_FILLER_SIZE))
  1288. {
  1289. MemLogItem(func,file,line,"BEG",mi);
  1290. stat = 1;
  1291. HexDump16(stderr,0,0,data-MEM_FILLER_SIZE,16);
  1292. }
  1293. if (memcmp(data+mi->size,mem_filler,MEM_FILLER_SIZE))
  1294. {
  1295. MemLogItem(func,file,line,"END",mi);
  1296. stat |= 2;
  1297. HexDump16(stderr,0,mi->size+sizeof(mem_filler),data+mi->size,16);
  1298. }
  1299. return stat;
  1300. }
  1301. }
  1302. MemLog(func,file,line,"NOT FOUND",data,false);
  1303. return 4;
  1304. #else
  1305. return 0;
  1306. #endif
  1307. }
  1308. ///////////////////////////////////////////////////////////////////////////////
  1309. static void * UnregisterAlloc ( ccp func, ccp file, uint line, u8 * data )
  1310. {
  1311. #if TRACE_ALLOC_MODE > 2
  1312. if (!data)
  1313. return 0;
  1314. int beg = 0;
  1315. int end = mem_used - 1;
  1316. while ( beg <= end )
  1317. {
  1318. uint idx = (beg+end)/2;
  1319. mem_info_t * mi = mem_list + idx;
  1320. if ( data < mi->data )
  1321. end = idx - 1 ;
  1322. else if ( data > mi->data )
  1323. beg = idx + 1;
  1324. else
  1325. {
  1326. #if TRACE_ALLOC_MODE > 3
  1327. MemLog(func,file,line,"FREE",data,true);
  1328. #endif
  1329. if (mi->filler)
  1330. {
  1331. if (memcmp(data-MEM_FILLER_SIZE,mem_filler,MEM_FILLER_SIZE))
  1332. MemLogItem(func,file,line,"BEG",mi);
  1333. if (memcmp(data+mi->size,mem_filler,MEM_FILLER_SIZE))
  1334. MemLogItem(func,file,line,"END",mi);
  1335. data -= MEM_FILLER_SIZE;
  1336. }
  1337. memmove(mi,mi+1,(--mem_used-idx)*sizeof(*mi));
  1338. return data;
  1339. }
  1340. }
  1341. MemLog(func,file,line,"NOT FOUND",data,false);
  1342. #endif
  1343. return data;
  1344. }
  1345. ///////////////////////////////////////////////////////////////////////////////
  1346. void trace_free ( ccp func, ccp file, uint line, void * ptr )
  1347. {
  1348. free(UnregisterAlloc(func,file,line,ptr));
  1349. }
  1350. ///////////////////////////////////////////////////////////////////////////////
  1351. void * trace_malloc ( ccp func, ccp file, uint line, size_t size )
  1352. {
  1353. u8 * res = malloc( size + 2 * MEM_FILLER_SIZE );
  1354. if (!res)
  1355. PRINT_OOM("Out of memory while allocate %zu+%u bytes (0x%zx)\n",
  1356. size, 2 * MEM_FILLER_SIZE, size + 2 * MEM_FILLER_SIZE );
  1357. res += MEM_FILLER_SIZE;
  1358. #if TRACE_ALLOC_MODE > 2
  1359. RegisterAlloc(func,file,line,res,size,true);
  1360. #endif
  1361. return res;
  1362. }
  1363. ///////////////////////////////////////////////////////////////////////////////
  1364. void * trace_calloc ( ccp func, ccp file, uint line, size_t nmemb, size_t size )
  1365. {
  1366. uint total_size = nmemb * size;
  1367. void * res = trace_malloc(func,file,line,total_size);
  1368. memset(res,0,total_size);
  1369. return res;
  1370. }
  1371. ///////////////////////////////////////////////////////////////////////////////
  1372. void * trace_realloc ( ccp func, ccp file, uint line, void *ptr, size_t size )
  1373. {
  1374. ptr = UnregisterAlloc(func,file,line,ptr);
  1375. u8 * res = realloc( ptr, size + 2 * MEM_FILLER_SIZE );
  1376. if (!res)
  1377. PRINT_OOM("Out of memory while re allocate %zu+%u bytes (0x%zx)\n",
  1378. size, 2 * MEM_FILLER_SIZE, size + 2 * MEM_FILLER_SIZE );
  1379. res += MEM_FILLER_SIZE;
  1380. #if TRACE_ALLOC_MODE > 2
  1381. RegisterAlloc(func,file,line,res,size,true);
  1382. #endif
  1383. return res;
  1384. }
  1385. ///////////////////////////////////////////////////////////////////////////////
  1386. char * trace_strdup ( ccp func, ccp file, uint line, ccp src )
  1387. {
  1388. const uint len = src ? strlen(src)+1 : 0;
  1389. char * res = trace_malloc(func,file,line,len);
  1390. memcpy(res,src,len);
  1391. return res;
  1392. }
  1393. ///////////////////////////////////////////////////////////////////////////////
  1394. char * trace_strdup2 ( ccp func, ccp file, uint line, ccp src1, ccp src2 )
  1395. {
  1396. const uint len1 = src1 ? strlen(src1) : 0;
  1397. const uint len2 = src2 ? strlen(src2) : 0;
  1398. char * res = trace_malloc(func,file,line,len1+len2+1);
  1399. if (len1)
  1400. memcpy(res,src1,len1);
  1401. if (len2)
  1402. memcpy(res+len1,src2,len2);
  1403. res[len1+len2] = 0;
  1404. return res;
  1405. }
  1406. ///////////////////////////////////////////////////////////////////////////////
  1407. char * trace_strdup3 ( ccp func, ccp file, uint line, ccp src1, ccp src2, ccp src3 )
  1408. {
  1409. const uint len1 = src1 ? strlen(src1) : 0;
  1410. const uint len2 = src2 ? strlen(src2) : 0;
  1411. const uint len3 = src3 ? strlen(src3) : 0;
  1412. char * res = trace_malloc(func,file,line,len1+len2+len3+1);
  1413. char * dest = res;
  1414. if (len1)
  1415. {
  1416. memcpy(dest,src1,len1);
  1417. dest += len1;
  1418. }
  1419. if (len2)
  1420. {
  1421. memcpy(dest,src2,len2);
  1422. dest += len2;
  1423. }
  1424. if (len3)
  1425. {
  1426. memcpy(dest,src3,len3);
  1427. dest += len3;
  1428. }
  1429. *dest = 0;
  1430. return res;
  1431. }
  1432. ///////////////////////////////////////////////////////////////////////////////
  1433. void * trace_memdup
  1434. ( ccp func, ccp file, uint line, const void * src, size_t copylen )
  1435. {
  1436. char * dest = trace_malloc(func,file,line,copylen+1);
  1437. memcpy(dest,src,copylen);
  1438. dest[copylen] = 0;
  1439. return dest;
  1440. }
  1441. ///////////////////////////////////////////////////////////////////////////////
  1442. void * trace_memdup2 ( ccp func, ccp file, uint line,
  1443. cvp src1, size_t len1, cvp src2, size_t len2 )
  1444. {
  1445. char *res = trace_malloc(func,file,line,len1+len2+1);
  1446. char *dest = res;
  1447. if (len1)
  1448. {
  1449. memcpy(dest,src1,len1);
  1450. dest += len1;
  1451. }
  1452. if (len2)
  1453. {
  1454. memcpy(dest,src2,len2);
  1455. dest += len2;
  1456. }
  1457. *dest = 0;
  1458. return res;
  1459. }
  1460. ///////////////////////////////////////////////////////////////////////////////
  1461. void * trace_memdup3 ( ccp func, ccp file, uint line,
  1462. cvp src1, size_t len1, cvp src2, size_t len2, cvp src3, size_t len3 )
  1463. {
  1464. char *res = trace_malloc(func,file,line,len1+len2+len3+1);
  1465. char *dest = res;
  1466. if (len1)
  1467. {
  1468. memcpy(dest,src1,len1);
  1469. dest += len1;
  1470. }
  1471. if (len2)
  1472. {
  1473. memcpy(dest,src2,len2);
  1474. dest += len2;
  1475. }
  1476. if (len3)
  1477. {
  1478. memcpy(dest,src3,len3);
  1479. dest += len3;
  1480. }
  1481. *dest = 0;
  1482. return res;
  1483. }
  1484. ///////////////////////////////////////////////////////////////////////////////
  1485. void * trace_allocdup
  1486. ( ccp func, ccp file, uint line, const void * src, size_t copylen )
  1487. {
  1488. char * dest = trace_malloc(func,file,line,copylen);
  1489. memcpy(dest,src,copylen);
  1490. return dest;
  1491. }
  1492. ///////////////////////////////////////////////////////////////////////////////
  1493. char * trace_printdup
  1494. ( ccp func, ccp file, uint line, ccp format, ... )
  1495. {
  1496. char buf[2000];
  1497. va_list arg;
  1498. va_start(arg,format);
  1499. const int stat = vsnprintf(buf,sizeof(buf),format,arg) + 1;
  1500. va_end(arg);
  1501. char * dest = trace_malloc(func,file,line,stat);
  1502. if ( stat <= sizeof(buf) )
  1503. memcpy(dest,buf,stat);
  1504. else
  1505. {
  1506. va_start(arg,format);
  1507. vsnprintf(dest,stat,format,arg);
  1508. va_end(arg);
  1509. }
  1510. return dest;
  1511. }
  1512. //
  1513. ///////////////////////////////////////////////////////////////////////////////
  1514. /////////////// mem check ///////////////
  1515. ///////////////////////////////////////////////////////////////////////////////
  1516. static u8 mem_check_buf[0x100];
  1517. static const void * mem_check_ptr = 0;
  1518. static uint mem_check_size = 0;
  1519. ///////////////////////////////////////////////////////////////////////////////
  1520. void MemCheckSetup ( const void * ptr, uint size )
  1521. {
  1522. if ( ptr && size > 0 )
  1523. {
  1524. mem_check_ptr = ptr;
  1525. if ( size > sizeof(mem_check_buf) )
  1526. {
  1527. ERROR0(ERR_WARNING,
  1528. "MemCheckSetup(), max watch size = %zx (<%x)",
  1529. sizeof(mem_check_buf), size );
  1530. size = sizeof(mem_check_buf);
  1531. }
  1532. mem_check_size = size;
  1533. memcpy(mem_check_buf,ptr,size);
  1534. }
  1535. else
  1536. mem_check_size = 0;
  1537. }
  1538. ///////////////////////////////////////////////////////////////////////////////
  1539. void MemCheck ( ccp func, ccp file, uint line )
  1540. {
  1541. if (!mem_check_size)
  1542. return;
  1543. if ( memcmp(mem_check_buf,mem_check_ptr,mem_check_size) )
  1544. {
  1545. fprintf(stderr,"--- MemCheck: %p should be:\n",mem_check_ptr);
  1546. HexDump16(stderr,0,0,mem_check_buf,mem_check_size);
  1547. fprintf(stderr,"--- MemCheck: ... but is:\n");
  1548. HexDump16(stderr,0,0,mem_check_ptr,mem_check_size);
  1549. TRACE("--- MemCheck: %p should be:\n",mem_check_ptr);
  1550. TRACE_HEXDUMP16(0,0,mem_check_buf,mem_check_size);
  1551. TRACE("--- MemCheck: ... but is:\n");
  1552. TRACE_HEXDUMP16(0,0,mem_check_ptr,mem_check_size);
  1553. PrintError(func,file,line,0,ERR_FATAL,"MemCheck() failed!\n");
  1554. }
  1555. else
  1556. TRACE("MemCheck OK: %s @ %s#%u\n",func,file,line);
  1557. }
  1558. //
  1559. ///////////////////////////////////////////////////////////////////////////////
  1560. /////////////// END ///////////////
  1561. ///////////////////////////////////////////////////////////////////////////////