gppc386.pp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. {
  2. Copyright (c) 2000-2002 by Pierre Muller
  3. This program allows to run the Makefiles
  4. with the compiler running inside GDB
  5. GDB only stops if there is something special
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************}
  18. program fpc_with_gdb;
  19. {
  20. This program uses several files :
  21. -- 'gdb4fpc.ini' contains the standard breakpoints (see below)
  22. -- 'gdb.fpc' is an optional file that can contain any other
  23. instruction that GDB should do before starting.
  24. Note that if gdb.fpc is present, no "run" command is
  25. inserted if gdb4fpc.ini is found
  26. but it can be inserted in gdb.fpc itself.
  27. Use EXTDEBUG conditional to get debug information.
  28. }
  29. uses
  30. dos;
  31. const
  32. {$ifdef Unix}
  33. GDBExeName : String = 'gdbpas';
  34. GDBIniName = '.gdbinit';
  35. DefaultCompilerName = 'ppc386';
  36. PathSep=':';
  37. DirSep = '/';
  38. {$else}
  39. GDBExeName : String = 'gdbpas.exe';
  40. GDBIniName = 'gdb.ini';
  41. DefaultCompilerName = 'ppc386.exe';
  42. PathSep=';';
  43. DirSep = '\';
  44. {$endif not linux}
  45. { If you add a gdb.fpc file in a given directory }
  46. { GDB will read it; this allows you to add }
  47. { special tests in specific directories PM }
  48. FpcGDBIniName = 'gdb.fpc';
  49. GDBIniTempName : string = 'gdb4fpc.ini';
  50. var
  51. fpcgdbini : text;
  52. CompilerName,Dir,Name,Ext : String;
  53. GDBError,GDBExitCode,i : longint;
  54. begin
  55. fsplit(paramstr(0),Dir,Name,Ext);
  56. if (length(Name)>3) and (UpCase(Name[1])='G') then
  57. CompilerName:=Copy(Name,2,255)+Ext
  58. else
  59. CompilerName:=DefaultCompilerName;
  60. CompilerName:=fsearch(CompilerName,Dir+PathSep+GetEnv('PATH'));
  61. { support for info functions directly : used in makefiles }
  62. if (paramcount=1) and (pos('-i',Paramstr(1))=1) then
  63. begin
  64. Exec(CompilerName,Paramstr(1));
  65. exit;
  66. end;
  67. {$ifdef EXTDEBUG}
  68. writeln(stderr,'Using compiler "',CompilerName,'"');
  69. flush(stderr);
  70. {$endif}
  71. if fsearch(GDBIniTempName,'.')<>'' then
  72. begin
  73. Assign(fpcgdbini,GDBIniTempName);
  74. {$ifdef EXTDEBUG}
  75. writeln(stderr,'Erasing file "',GDBIniTempName,'"');
  76. flush(stderr);
  77. {$endif}
  78. erase(fpcgdbini);
  79. end;
  80. GDBIniTempName:=fexpand('.'+DirSep+GDBIniTempName);
  81. Assign(fpcgdbini,GdbIniTempName);
  82. {$ifdef EXTDEBUG}
  83. writeln(stderr,'Creating file "',GDBIniTempName,'"');
  84. flush(stderr);
  85. {$endif}
  86. Rewrite(fpcgdbini);
  87. Writeln(fpcgdbini,'set language pascal');
  88. Write(fpcgdbini,'set args');
  89. { this will not work correctly if there are " or '' inside the command line :( }
  90. for i:=1 to Paramcount do
  91. begin
  92. if pos(' ',Paramstr(i))>0 then
  93. Write(fpcgdbini,' "'+ParamStr(i)+'"')
  94. else
  95. Write(fpcgdbini,' '+ParamStr(i));
  96. end;
  97. Writeln(fpcgdbini);
  98. Writeln(fpcgdbini,'b SYSTEM_EXIT');
  99. Writeln(fpcgdbini,'cond 1 EXITCODE <> 0');
  100. Writeln(fpcgdbini,'set $_exitcode := -1');
  101. { b INTERNALERROR sometimes fails ... Don't know why. PM 2010-08-28 }
  102. Writeln(fpcgdbini,'info fun INTERNALERROR');
  103. Writeln(fpcgdbini,'b INTERNALERROR');
  104. Writeln(fpcgdbini,'b HANDLEERRORADDRFRAME');
  105. { This one will fail unless sysutils unit is also loaded }
  106. Writeln(fpcgdbini,'b RUNERRORTOEXCEPT');
  107. if fsearch(FpcGDBIniName,'./')<>'' then
  108. begin
  109. Writeln(fpcgdbini,'source '+FpcGDBIniName);
  110. end
  111. else
  112. Writeln(fpcgdbini,'run');
  113. Writeln(fpcgdbini,'if ($_exitcode = -1)');
  114. Writeln(fpcgdbini,' echo Program not completed');
  115. Writeln(fpcgdbini,'else');
  116. Writeln(fpcgdbini,' quit');
  117. Writeln(fpcgdbini,'end');
  118. Close(fpcgdbini);
  119. {$ifdef EXTDEBUG}
  120. writeln(stderr,'Closing file "',GDBIniTempName,'"');
  121. flush(stderr);
  122. {$endif}
  123. GDBExeName:=fsearch(GDBExeName,Dir+PathSep+GetEnv('PATH'));
  124. {$ifdef EXTDEBUG}
  125. Writeln(stderr,'Starting ',GDBExeName,
  126. {$ifdef win32}
  127. '--nw '+
  128. {$endif win32}
  129. '--nx --command='+GDBIniTempName+' '+CompilerName);
  130. flush(stderr);
  131. {$endif}
  132. DosError:=0;
  133. Exec(GDBExeName,
  134. {$ifdef win32}
  135. '--nw '+
  136. {$endif win32}
  137. '--nx --command='+GDBIniTempName+' '+CompilerName);
  138. GDBError:=DosError;
  139. GDBExitCode:=DosExitCode;
  140. if (GDBError<>0) or (GDBExitCode<>0) then
  141. begin
  142. Writeln('Error running GDB');
  143. if (GDBError<>0) then
  144. Writeln('DosError = ',GDBError);
  145. if (GDBExitCode<>0) then
  146. Writeln('DosExitCode = ',GDBExitCode);
  147. if GDBExitCode<>0 then
  148. RunError(GDBExitCode)
  149. else
  150. RunError(GDBError);
  151. end
  152. else
  153. Erase(fpcgdbini);
  154. end.