gppc386.pp 5.5 KB

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