gdbcon.pp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Peter Vreman
  4. Lowlevel GDB interface which communicates directly with libgdb
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit GDBCon;
  12. interface
  13. uses
  14. GDBInt;
  15. type
  16. PGDBController=^TGDBController;
  17. TGDBController=object(TGDBInterface)
  18. progname : pchar;
  19. progargs : pchar;
  20. in_command,
  21. init_count : longint;
  22. constructor Init;
  23. destructor Done;
  24. procedure CommandBegin(const s:string);virtual;
  25. procedure Command(const s:string);
  26. procedure CommandEnd(const s:string);virtual;
  27. procedure Reset;virtual;
  28. { tracing }
  29. procedure StartTrace;
  30. procedure Run;virtual;
  31. procedure TraceStep;virtual;
  32. procedure TraceNext;virtual;
  33. procedure TraceStepI;virtual;
  34. procedure TraceNextI;virtual;
  35. procedure Continue;virtual;
  36. { needed for dos because newlines are only #10 (PM) }
  37. procedure WriteErrorBuf;
  38. procedure WriteOutputBuf;
  39. function GetOutput : Pchar;
  40. function GetError : Pchar;
  41. function LoadFile(var fn:string):boolean;
  42. procedure SetArgs(const s : string);
  43. procedure ClearSymbols;
  44. end;
  45. procedure UnixDir(var s : string);
  46. implementation
  47. uses
  48. strings;
  49. procedure UnixDir(var s : string);
  50. var i : longint;
  51. begin
  52. for i:=1 to length(s) do
  53. if s[i]='\' then
  54. {$ifdef win32}
  55. { Don't touch at '\ ' used to escapes spaces in windows file names PM }
  56. if (i=length(s)) or (s[i+1]<>' ') then
  57. {$endif win32}
  58. s[i]:='/';
  59. end;
  60. constructor TGDBController.Init;
  61. begin
  62. inherited init;
  63. end;
  64. destructor TGDBController.Done;
  65. begin
  66. if assigned(progname) then
  67. strdispose(progname);
  68. if assigned(progargs) then
  69. strdispose(progargs);
  70. inherited done;
  71. end;
  72. procedure TGDBController.Command(const s:string);
  73. begin
  74. CommandBegin(s);
  75. gdboutputbuf.reset;
  76. gdberrorbuf.reset;
  77. inc(in_command);
  78. gdb_command(s);
  79. dec(in_command);
  80. {
  81. What is that for ?? PM
  82. I had to comment it because
  83. it resets the debuggere after each command !!
  84. Maybe it can happen on errors ??
  85. if in_command<0 then
  86. begin
  87. in_command:=0;
  88. inc(in_command);
  89. Reset;
  90. dec(in_command);
  91. end; }
  92. CommandEnd(s);
  93. end;
  94. procedure TGDBController.CommandBegin(const s:string);
  95. begin
  96. end;
  97. procedure TGDBController.CommandEnd(const s:string);
  98. begin
  99. end;
  100. function TGDBController.LoadFile(var fn:string):boolean;
  101. var
  102. cmd : string;
  103. begin
  104. getdir(0,cmd);
  105. UnixDir(cmd);
  106. cmd:='cd '+cmd;
  107. Command(cmd);
  108. GDB__Init;
  109. UnixDir(fn);
  110. if assigned(progname) then
  111. strdispose(progname);
  112. getmem(progname,length(fn)+1);
  113. strpcopy(progname,fn);
  114. if fn<>'' then
  115. Command('file '+fn);
  116. LoadFile:=true;
  117. end;
  118. procedure TGDBController.SetArgs(const s : string);
  119. begin
  120. if assigned(progargs) then
  121. strdispose(progargs);
  122. getmem(progargs,length(s)+1);
  123. strpcopy(progargs,s);
  124. command('set args '+s);
  125. end;
  126. procedure TGDBController.Reset;
  127. begin
  128. call_reset:=false;
  129. { DeleteBreakPoints(); }
  130. if debuggee_started then
  131. begin
  132. reset_command:=true;
  133. BreakSession;
  134. Command('kill');
  135. reset_command:=false;
  136. debuggee_started:=false;
  137. end;
  138. end;
  139. procedure TGDBController.StartTrace;
  140. begin
  141. Command('tbreak PASCALMAIN');
  142. Run;
  143. end;
  144. procedure TGDBController.Run;
  145. begin
  146. Command('run');
  147. inc(init_count);
  148. end;
  149. procedure TGDBController.TraceStep;
  150. begin
  151. Command('step');
  152. end;
  153. procedure TGDBController.TraceNext;
  154. begin
  155. Command('next');
  156. end;
  157. procedure TGDBController.TraceStepI;
  158. begin
  159. Command('stepi');
  160. end;
  161. procedure TGDBController.TraceNextI;
  162. begin
  163. Command('nexti');
  164. end;
  165. procedure TGDBController.Continue;
  166. begin
  167. Command('continue');
  168. end;
  169. procedure TGDBController.ClearSymbols;
  170. begin
  171. if debuggee_started then
  172. Reset;
  173. if init_count>0 then
  174. Command('file');
  175. end;
  176. procedure BufWrite(Buf : pchar);
  177. var p,pe : pchar;
  178. begin
  179. p:=buf;
  180. While assigned(p) do
  181. begin
  182. pe:=strscan(p,#10);
  183. if pe<>nil then
  184. pe^:=#0;
  185. Writeln(p);
  186. { restore for dispose }
  187. if pe<>nil then
  188. pe^:=#10;
  189. if pe=nil then
  190. p:=nil
  191. else
  192. begin
  193. p:=pe;
  194. inc(p);
  195. end;
  196. end;
  197. end;
  198. function TGDBController.GetOutput : Pchar;
  199. begin
  200. GetOutput:=gdboutputbuf.buf;
  201. end;
  202. function TGDBController.GetError : Pchar;
  203. var p : pchar;
  204. begin
  205. p:=gdberrorbuf.buf;
  206. if (p^=#0) and got_error then
  207. GetError:=pchar(longint(gdboutputbuf.buf)+gdboutputbuf.idx)
  208. else
  209. GetError:=p;
  210. end;
  211. procedure TGDBController.WriteErrorBuf;
  212. begin
  213. BufWrite(gdberrorbuf.buf);
  214. end;
  215. procedure TGDBController.WriteOutputBuf;
  216. begin
  217. BufWrite(gdboutputbuf.buf);
  218. end;
  219. end.
  220. {
  221. $Log$
  222. Revision 1.2 2002-03-26 16:23:14 pierre
  223. * get IDE to work with dirs containing spaces for win32
  224. Revision 1.1 2002/01/29 17:54:49 peter
  225. * splitted to base and extra
  226. Revision 1.3 2001/04/08 11:43:39 peter
  227. * merged changes from fixes branch
  228. Revision 1.2 2000/07/13 11:33:15 michael
  229. + removed logs
  230. }