extractdwrflnfo.lpr 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. {
  2. This file is part of the chelinfo library.
  3. Copyright (c) 2008 by Anton Rzheshevski
  4. Dwarf LineInfo Extractor
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. 2008, Anton Rzheshevski aka Cheb:
  13. Like dr. Frankenshtein I sewn this library together
  14. from the dead meat of the the FPC RTL modules
  15. lineinfo.pp and lnfodwrf.pp.
  16. These (as of Jan. 2008 / FPC 2.2.0) both didn't work
  17. and had several limitations (e.g. inability to be used
  18. from a DLL)
  19. }
  20. {note: DON'T FORGET to compile your program with the -gw key
  21. Lazarus: you must type it in
  22. Project -> Compiler Options -> Other -> User parameters
  23. }
  24. {$mode delphi}
  25. {$longstrings on}
  26. {$ifndef unix}
  27. {$apptype console}
  28. {$endif}
  29. //extracts the line info in the dwarf format from the executable
  30. program extractdwrflnfo;
  31. uses
  32. SysUtils, Classes, un_xtrctdwrflnfo, zstream;
  33. var
  34. _dwarf: pointer;
  35. DwarfSize, CompressedDwarfSize: QWord;
  36. base_addr: QWord;
  37. f: TFileStream;
  38. CS: TCompressionStream;
  39. dllname, iname: ansistring;
  40. begin
  41. if Paramcount = 0 then
  42. begin
  43. WriteLn('Usage: ' + ExtractFileName(GetModuleName(0)) + ' <executable name>');
  44. exit;
  45. end;
  46. dllname:= ParamStr(1);
  47. WriteLn('Extracting Dwarf line info from ', dllname);
  48. try
  49. iname:= DlnNameByExename(dllname);
  50. if ExtractDwarfLineInfo(dllname, _dwarf, DwarfSize, base_addr) then begin
  51. f:= TFileStream.Create(iname , fmCreate);
  52. CS:= TCompressionStream.Create(clMax, f);
  53. CS.Write(dwarfsize, sizeof(dwarfsize)); // 8 bytes (QWORD)
  54. CS.Write(base_addr, sizeof(base_addr)); // 8 bytes (QWORD)
  55. CS.Write(_dwarf^, dwarfsize);
  56. CS.Free;
  57. CompressedDwarfSize := f.Size;
  58. f.free;
  59. WriteLn('Ok, saved ', CompressedDwarfSize, ' bytes to ', iname);
  60. end
  61. else begin
  62. if FileExists(iname) then DeleteFile(iname);
  63. WriteLn('Error: ' + ExtractDwarfLineInfoError);
  64. end;
  65. except
  66. WriteLn((ExceptObject as Exception).Message);
  67. end;
  68. end.