gdbcon.pp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 s[i]:='/';
  54. end;
  55. constructor TGDBController.Init;
  56. begin
  57. inherited init;
  58. end;
  59. destructor TGDBController.Done;
  60. begin
  61. if assigned(progname) then
  62. strdispose(progname);
  63. if assigned(progargs) then
  64. strdispose(progargs);
  65. inherited done;
  66. end;
  67. procedure TGDBController.Command(const s:string);
  68. begin
  69. CommandBegin(s);
  70. gdboutputbuf.reset;
  71. gdberrorbuf.reset;
  72. inc(in_command);
  73. gdb_command(s);
  74. dec(in_command);
  75. {
  76. What is that for ?? PM
  77. I had to comment it because
  78. it resets the debuggere after each command !!
  79. Maybe it can happen on errors ??
  80. if in_command<0 then
  81. begin
  82. in_command:=0;
  83. inc(in_command);
  84. Reset;
  85. dec(in_command);
  86. end; }
  87. CommandEnd(s);
  88. end;
  89. procedure TGDBController.CommandBegin(const s:string);
  90. begin
  91. end;
  92. procedure TGDBController.CommandEnd(const s:string);
  93. begin
  94. end;
  95. function TGDBController.LoadFile(var fn:string):boolean;
  96. var
  97. cmd : string;
  98. begin
  99. getdir(0,cmd);
  100. UnixDir(cmd);
  101. cmd:='cd '+cmd;
  102. Command(cmd);
  103. GDB__Init;
  104. UnixDir(fn);
  105. if assigned(progname) then
  106. strdispose(progname);
  107. getmem(progname,length(fn)+1);
  108. strpcopy(progname,fn);
  109. if fn<>'' then
  110. Command('file '+fn);
  111. LoadFile:=true;
  112. end;
  113. procedure TGDBController.SetArgs(const s : string);
  114. begin
  115. if assigned(progargs) then
  116. strdispose(progargs);
  117. getmem(progargs,length(s)+1);
  118. strpcopy(progargs,s);
  119. command('set args '+s);
  120. end;
  121. procedure TGDBController.Reset;
  122. begin
  123. call_reset:=false;
  124. { DeleteBreakPoints(); }
  125. if debuggee_started then
  126. begin
  127. reset_command:=true;
  128. BreakSession;
  129. Command('kill');
  130. reset_command:=false;
  131. debuggee_started:=false;
  132. end;
  133. end;
  134. procedure TGDBController.StartTrace;
  135. begin
  136. Command('tbreak PASCALMAIN');
  137. Run;
  138. end;
  139. procedure TGDBController.Run;
  140. begin
  141. Command('run');
  142. inc(init_count);
  143. end;
  144. procedure TGDBController.TraceStep;
  145. begin
  146. Command('step');
  147. end;
  148. procedure TGDBController.TraceNext;
  149. begin
  150. Command('next');
  151. end;
  152. procedure TGDBController.TraceStepI;
  153. begin
  154. Command('stepi');
  155. end;
  156. procedure TGDBController.TraceNextI;
  157. begin
  158. Command('nexti');
  159. end;
  160. procedure TGDBController.Continue;
  161. begin
  162. Command('continue');
  163. end;
  164. procedure TGDBController.ClearSymbols;
  165. begin
  166. if debuggee_started then
  167. Reset;
  168. if init_count>0 then
  169. Command('file');
  170. end;
  171. procedure BufWrite(Buf : pchar);
  172. var p,pe : pchar;
  173. begin
  174. p:=buf;
  175. While assigned(p) do
  176. begin
  177. pe:=strscan(p,#10);
  178. if pe<>nil then
  179. pe^:=#0;
  180. Writeln(p);
  181. { restore for dispose }
  182. if pe<>nil then
  183. pe^:=#10;
  184. if pe=nil then
  185. p:=nil
  186. else
  187. begin
  188. p:=pe;
  189. inc(p);
  190. end;
  191. end;
  192. end;
  193. function TGDBController.GetOutput : Pchar;
  194. begin
  195. GetOutput:=gdboutputbuf.buf;
  196. end;
  197. function TGDBController.GetError : Pchar;
  198. var p : pchar;
  199. begin
  200. p:=gdberrorbuf.buf;
  201. if (p^=#0) and got_error then
  202. GetError:=pchar(longint(gdboutputbuf.buf)+gdboutputbuf.idx)
  203. else
  204. GetError:=p;
  205. end;
  206. procedure TGDBController.WriteErrorBuf;
  207. begin
  208. BufWrite(gdberrorbuf.buf);
  209. end;
  210. procedure TGDBController.WriteOutputBuf;
  211. begin
  212. BufWrite(gdboutputbuf.buf);
  213. end;
  214. end.
  215. {
  216. $Log$
  217. Revision 1.3 2001-04-08 11:43:39 peter
  218. * merged changes from fixes branch
  219. Revision 1.2 2000/07/13 11:33:15 michael
  220. + removed logs
  221. }