gppc386.pp 6.8 KB

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