From 8f2e4f051012bedb5178569aa57696311ade2567 Mon Sep 17 00:00:00 2001 From: Arnaud Bouchez Date: Tue, 5 Sep 2023 12:08:20 +0200 Subject: [PATCH] introducing the map2mab tool --- src/mormot.commit.inc | 2 +- src/tools/README.md | 13 +++++ src/tools/map2mapb/map2mab.dpr | 104 +++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/tools/map2mapb/map2mab.dpr diff --git a/src/mormot.commit.inc b/src/mormot.commit.inc index 81aa02f1d..3d8bdd646 100644 --- a/src/mormot.commit.inc +++ b/src/mormot.commit.inc @@ -1 +1 @@ -'2.1.5827' +'2.1.5828' diff --git a/src/tools/README.md b/src/tools/README.md index 36ff2f157..1804397a5 100644 --- a/src/tools/README.md +++ b/src/tools/README.md @@ -26,3 +26,16 @@ Angelize (`agl`) tool is able to run one or several executables as daemon/servic - A WatchDog can check the availibility of a service on regular basis - Can redirect the console output, restart on problem, notify issues - Command line switches are available for status listing or main actions + +### map2mab + +Command-Line Tool to Generate `.mab` files from existing `.map` files +- if some `.map` file name is specified (you can use wild chars), it will +process all those `.map` files, then create the corresponding `.mab` files +- if some `.exe`/`.dll` file name is specified (you can use wild chars), will +process all matching `.exe`/`.dll` files with an associated `.map` file, and will +create the `.mab` files, then embedd the `.mab` content to the `.exe`/`.dll` +- if no file name is specified, will process `*.map` into `*.mab` from the +current directory +- with FPC, will use DWARF debugging information instead of the `.map` file + diff --git a/src/tools/map2mapb/map2mab.dpr b/src/tools/map2mapb/map2mab.dpr new file mode 100644 index 000000000..6d195c7a4 --- /dev/null +++ b/src/tools/map2mapb/map2mab.dpr @@ -0,0 +1,104 @@ +/// Command Line .map to .mab Conversion Tool +// - this program is a part of the Open Source Synopse mORMot framework 2, +// licensed under a MPL/GPL/LGPL three license - see LICENSE.md +program map2mab; + +{ + ***************************************************************************** + + Command-Line Tool to Generate .mab files from existing .map files + - if some .map file name is specified (you can use wild chars), it will + process all those .map files, then create the corresponding .mab files + - if some .exe/.dll file name is specified (you can use wild chars), will + process all matching .exe/.dll files with an associated .map file, and will + create the .mab files, then embedd the .mab content to the .exe/.dll + - if no file name is specified, will process *.map into *.mab from the + current directory + - with FPC, will use DWARF debugging information instead of the `.map` file + + ***************************************************************************** +} + +{$I ..\..\mormot.defines.inc} + +{$ifdef OSWINDOWS} + {$apptype console} + {$R ..\..\mormot.win.default.manifest.res} +{$endif OSWINDOWS} + +uses + {$I ..\..\mormot.uses.inc} + classes, + sysutils, + mormot.core.base in '..\..\core\mormot.core.base.pas', + mormot.core.os in '..\..\core\mormot.core.os.pas', + mormot.core.text in '..\..\core\mormot.core.text.pas', + mormot.core.log in '..\..\core\mormot.core.log.pas'; + +procedure Process(const FileName: TFileName); +var + SR: TSearchRec; + Path, FN: TFileName; + Ext, Count: integer; + AllOk: boolean; +begin + AllOk := True; + Ext := GetFileNameExtIndex(FileName, 'map,dbg,exe,dll,ocx,bpl'); + if (Ext >= 0) and + (FindFirst(FileName, faAnyFile, SR) = 0) then + try + Path := ExtractFilePath(FileName); + repeat + FN := Path + SR.Name; + if SearchRecValidFile(SR) then + try + // generate the mab content, maybe into the executable itself + with TDebugFile.Create(FN, {MabCreate=}true) do + try + Count := length(Symbols); + if not HasDebugInfo then + begin + WriteLn('Error: no Debug Info found on ', FN); + AllOk := False; + end + else if Ext > 1 then // has debug info and is not a map/dbg + SaveToExe(FN); // embedd into the executable + finally + Free; + end; + // ensure the generated mab content is actually readable + with TDebugFile.Create(FN, {MabCreate=}false) do + try + if Count <> length(Symbols) then + raise ESynLogException.Create('Invalid .mab content'); + finally + Free; + end; + except + on E: Exception do + begin + // ignore any problem here: just print it and process next file + WriteLn('Error: ', E.ClassName, ' ', E.Message); + AllOk := False; + end; + end; + until FindNext(SR) <> 0; + finally + FindClose(SR); + end + else + begin + WriteLn('Error: cant find any file to process matching: ', FileName); + ExitCode := 2; + end; + if not AllOk then + ExitCode := 1; +end; + +begin + if ParamCount > 0 then + Process(ParamStr(1)) + else + Process('*.map'); +end. +