gppc386.pp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. {$mode objfpc}
  19. { Use ansitrings for long PATH variables }
  20. {$H+}
  21. program fpc_with_gdb;
  22. {
  23. This program uses several files :
  24. -- 'gdb4fpc.ini' contains the standard breakpoints (see below)
  25. -- 'gdb.fpc' is an optional file that can contain any other
  26. instruction that GDB should do before starting.
  27. Note that if gdb.fpc is present, no "run" command is
  28. inserted if gdb4fpc.ini is found
  29. but it can be inserted in gdb.fpc itself.
  30. Use EXTDEBUG conditional to get debug information.
  31. }
  32. uses
  33. sysutils,
  34. dos;
  35. const
  36. {$ifdef Unix}
  37. GDBExeName : String = 'gdbpas';
  38. GDBAltExeName = 'gdb';
  39. GDBIniName = '.gdbinit';
  40. DefaultCompilerName = 'ppc386';
  41. PathSep=':';
  42. DirSep = '/';
  43. {$else}
  44. GDBExeName : String = 'gdbpas.exe';
  45. GDBAltExeName = 'gdb.exe';
  46. GDBIniName = 'gdb.ini';
  47. DefaultCompilerName = 'ppc386.exe';
  48. PathSep=';';
  49. DirSep = '\';
  50. {$endif not linux}
  51. { If you add a gdb.fpc file in a given directory }
  52. { GDB will read it; this allows you to add }
  53. { special tests in specific directories PM }
  54. FpcGDBIniName = 'gdb.fpc';
  55. GDBIniTempName : string = 'gdb4fpc.ini';
  56. { Dos/Windows GDB still need forward slashes }
  57. procedure AdaptToGDB(var filename : string);
  58. var
  59. i : longint;
  60. begin
  61. for i:=1 to length(filename) do
  62. if filename[i]='\' then
  63. filename[i]:='/';
  64. end;
  65. var
  66. fpcgdbini : text;
  67. CompilerName : String;
  68. Dir,Name,Ext : ShortString;
  69. GDBError,GDBExitCode,i : longint;
  70. begin
  71. fsplit(paramstr(0),Dir,Name,Ext);
  72. if (length(Name)>3) and (UpCase(Name[1])='G') then
  73. CompilerName:=Copy(Name,2,255)+Ext
  74. else
  75. CompilerName:=DefaultCompilerName;
  76. CompilerName:=filesearch(CompilerName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  77. { support for info functions directly : used in makefiles }
  78. if (paramcount=1) and (pos('-i',Paramstr(1))=1) then
  79. begin
  80. Exec(CompilerName,Paramstr(1));
  81. exit;
  82. end;
  83. {$ifdef EXTDEBUG}
  84. writeln(stderr,'Using compiler "',CompilerName,'"');
  85. flush(stderr);
  86. {$endif}
  87. if fsearch(GDBIniTempName,'.')<>'' then
  88. begin
  89. Assign(fpcgdbini,GDBIniTempName);
  90. {$ifdef EXTDEBUG}
  91. writeln(stderr,'Erasing file "',GDBIniTempName,'"');
  92. flush(stderr);
  93. {$endif}
  94. erase(fpcgdbini);
  95. end;
  96. GDBIniTempName:=fexpand('.'+DirSep+GDBIniTempName);
  97. Assign(fpcgdbini,GdbIniTempName);
  98. {$ifdef EXTDEBUG}
  99. writeln(stderr,'Creating file "',GDBIniTempName,'"');
  100. flush(stderr);
  101. {$endif}
  102. Rewrite(fpcgdbini);
  103. Writeln(fpcgdbini,'set language pascal');
  104. Write(fpcgdbini,'set args');
  105. { this will not work correctly if there are " or '' inside the command line :( }
  106. for i:=1 to Paramcount do
  107. begin
  108. if pos(' ',Paramstr(i))>0 then
  109. Write(fpcgdbini,' "'+ParamStr(i)+'"')
  110. else
  111. Write(fpcgdbini,' '+ParamStr(i));
  112. end;
  113. Writeln(fpcgdbini);
  114. Writeln(fpcgdbini,'b SYSTEM_EXIT');
  115. Writeln(fpcgdbini,'cond 1 EXITCODE <> 0');
  116. Writeln(fpcgdbini,'set $_exitcode := -1');
  117. { b INTERNALERROR sometimes fails ... Don't know why. PM 2010-08-28 }
  118. Writeln(fpcgdbini,'info fun INTERNALERROR');
  119. Writeln(fpcgdbini,'b INTERNALERROR');
  120. Writeln(fpcgdbini,'b GENERATEERROR');
  121. Writeln(fpcgdbini,'b HANDLEERRORADDRFRAME');
  122. { This one will fail unless sysutils unit is also loaded }
  123. Writeln(fpcgdbini,'b RUNERRORTOEXCEPT');
  124. if fsearch(FpcGDBIniName,'./')<>'' then
  125. begin
  126. Writeln(fpcgdbini,'source '+FpcGDBIniName);
  127. end
  128. else
  129. Writeln(fpcgdbini,'run');
  130. Writeln(fpcgdbini,'if ($_exitcode = -1)');
  131. Writeln(fpcgdbini,' echo Program not completed');
  132. Writeln(fpcgdbini,'else');
  133. Writeln(fpcgdbini,' quit');
  134. Writeln(fpcgdbini,'end');
  135. Close(fpcgdbini);
  136. {$ifdef EXTDEBUG}
  137. writeln(stderr,'Closing file "',GDBIniTempName,'"');
  138. flush(stderr);
  139. {$endif}
  140. GDBExeName:=filesearch(GDBExeName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  141. if GDBExeName='' then
  142. GDBExeName:=filesearch(GDBAltExeName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  143. AdaptToGDB(CompilerName);
  144. AdaptToGDB(GDBIniTempName);
  145. {$ifdef EXTDEBUG}
  146. Writeln(stderr,'Starting ',GDBExeName,
  147. {$ifdef win32}
  148. '--nw '+
  149. {$endif win32}
  150. '--nx --command='+GDBIniTempName+' '+CompilerName);
  151. flush(stderr);
  152. {$endif}
  153. DosError:=0;
  154. Exec(GDBExeName,
  155. {$ifdef win32}
  156. '--nw '+
  157. {$endif win32}
  158. '--nx --command='+GDBIniTempName+' '+CompilerName);
  159. GDBError:=DosError;
  160. GDBExitCode:=DosExitCode;
  161. if (GDBError<>0) or (GDBExitCode<>0) then
  162. begin
  163. Writeln('Error running GDB');
  164. if (GDBError<>0) then
  165. Writeln('DosError = ',GDBError);
  166. if (GDBExitCode<>0) then
  167. Writeln('DosExitCode = ',GDBExitCode);
  168. if GDBExitCode<>0 then
  169. RunError(GDBExitCode)
  170. else
  171. RunError(GDBError);
  172. end
  173. else
  174. Erase(fpcgdbini);
  175. end.