gppc386.pp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. FullCompilerName : String;
  69. {$ifdef linux}
  70. argv0 : pchar;
  71. {$endif}
  72. Dir,Name,Ext : ShortString;
  73. GDBError,GDBExitCode,i : longint;
  74. begin
  75. fsplit(paramstr(0),Dir,Name,Ext);
  76. {$ifdef linux}
  77. argv0:=argv[0];
  78. if (argv0 <> '') then
  79. fsplit(argv0,Dir,Name,Ext);
  80. {$endif}
  81. if (length(Name)>3) and (UpCase(Name[1])='G') then
  82. CompilerName:=Copy(Name,2,255)+Ext
  83. else
  84. begin
  85. if (Name+ext = DefaultCompilerName) then
  86. begin
  87. writeln(stderr,'Avoiding infinite recursion with ',Name+Ext,' binary');
  88. halt(1);
  89. end;
  90. CompilerName:=DefaultCompilerName;
  91. end;
  92. FullCompilerName:=filesearch(CompilerName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  93. if FullCompilerName='' then
  94. begin
  95. writeln(stderr,'Unable to find ',CompilerName,' binary');
  96. halt(2);
  97. end;
  98. { support for info functions directly : used in makefiles }
  99. if (paramcount=1) and (pos('-i',Paramstr(1))=1) then
  100. begin
  101. Exec(FullCompilerName,Paramstr(1));
  102. exit;
  103. end;
  104. {$ifdef EXTDEBUG}
  105. writeln(stderr,'Using compiler "',FullCompilerName,'"');
  106. flush(stderr);
  107. {$endif}
  108. if fsearch(GDBIniTempName,'.')<>'' then
  109. begin
  110. Assign(fpcgdbini,GDBIniTempName);
  111. {$ifdef EXTDEBUG}
  112. writeln(stderr,'Erasing file "',GDBIniTempName,'"');
  113. flush(stderr);
  114. {$endif}
  115. erase(fpcgdbini);
  116. end;
  117. GDBIniTempName:=fexpand('.'+DirSep+GDBIniTempName);
  118. Assign(fpcgdbini,GdbIniTempName);
  119. {$ifdef EXTDEBUG}
  120. writeln(stderr,'Creating file "',GDBIniTempName,'"');
  121. flush(stderr);
  122. {$endif}
  123. Rewrite(fpcgdbini);
  124. Writeln(fpcgdbini,'set language pascal');
  125. Write(fpcgdbini,'set args');
  126. { this will not work correctly if there are " or '' inside the command line :( }
  127. for i:=1 to Paramcount do
  128. begin
  129. if pos(' ',Paramstr(i))>0 then
  130. Write(fpcgdbini,' "'+ParamStr(i)+'"')
  131. else
  132. Write(fpcgdbini,' '+ParamStr(i));
  133. end;
  134. Writeln(fpcgdbini);
  135. Writeln(fpcgdbini,'b SYSTEM_EXIT');
  136. Writeln(fpcgdbini,'cond 1 EXITCODE <> 0');
  137. Writeln(fpcgdbini,'set $_exitcode := -1');
  138. { b INTERNALERROR sometimes fails ... Don't know why. PM 2010-08-28 }
  139. Writeln(fpcgdbini,'info fun INTERNALERROR');
  140. Writeln(fpcgdbini,'b INTERNALERROR');
  141. Writeln(fpcgdbini,'b GENERATEERROR');
  142. Writeln(fpcgdbini,'b HANDLEERRORADDRFRAME');
  143. { This one will fail unless sysutils unit is also loaded }
  144. Writeln(fpcgdbini,'b RUNERRORTOEXCEPT');
  145. if fsearch(FpcGDBIniName,'./')<>'' then
  146. begin
  147. Writeln(fpcgdbini,'source '+FpcGDBIniName);
  148. end
  149. else
  150. Writeln(fpcgdbini,'run');
  151. Writeln(fpcgdbini,'if ($_exitcode = -1)');
  152. Writeln(fpcgdbini,' echo Program not completed');
  153. Writeln(fpcgdbini,'else');
  154. Writeln(fpcgdbini,' quit');
  155. Writeln(fpcgdbini,'end');
  156. Close(fpcgdbini);
  157. {$ifdef EXTDEBUG}
  158. writeln(stderr,'Closing file "',GDBIniTempName,'"');
  159. flush(stderr);
  160. {$endif}
  161. GDBExeName:=filesearch(GDBExeName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  162. if GDBExeName='' then
  163. GDBExeName:=filesearch(GDBAltExeName,Dir+PathSep+GetEnvironmentVariable('PATH'));
  164. if GDBExeName='' then
  165. begin
  166. writeln('Unable to find ',GDBExeName,' and ',GDBAltExeName);
  167. halt(3);
  168. end;
  169. AdaptToGDB(CompilerName);
  170. AdaptToGDB(GDBIniTempName);
  171. {$ifdef EXTDEBUG}
  172. Writeln(stderr,'Starting ',GDBExeName,
  173. {$ifdef win32}
  174. '--nw '+
  175. {$endif win32}
  176. '--nx --command='+GDBIniTempName+' '+FullCompilerName);
  177. flush(stderr);
  178. {$endif}
  179. DosError:=0;
  180. Exec(GDBExeName,
  181. {$ifdef win32}
  182. '--nw '+
  183. {$endif win32}
  184. '--nx --command='+GDBIniTempName+' '+FullCompilerName);
  185. GDBError:=DosError;
  186. GDBExitCode:=DosExitCode;
  187. if (GDBError<>0) or (GDBExitCode<>0) then
  188. begin
  189. Writeln('Error running GDB');
  190. if (GDBError<>0) then
  191. Writeln('DosError = ',GDBError);
  192. if (GDBExitCode<>0) then
  193. Writeln('DosExitCode = ',GDBExitCode);
  194. if GDBExitCode<>0 then
  195. RunError(GDBExitCode)
  196. else
  197. RunError(GDBError);
  198. end
  199. else
  200. Erase(fpcgdbini);
  201. end.