gdbmiint.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. {
  2. Copyright (c) 2015 by Nikolay Nikolov
  3. Copyright (c) 1998 by Peter Vreman
  4. This is a replacement for GDBInt, implemented on top of GDB/MI,
  5. instead of LibGDB. This allows integration of GDB/MI support in the
  6. text mode IDE.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit gdbmiint;
  14. {$MODE fpc}{$H-}
  15. {$I globdir.inc}
  16. interface
  17. uses
  18. gdbmiwrap;
  19. type
  20. CORE_ADDR = gdbmiwrap.CORE_ADDR;
  21. PPFrameEntry = ^PFrameEntry;
  22. PFrameEntry = ^TFrameEntry;
  23. TFrameEntry = object
  24. private
  25. procedure Reset;
  26. procedure Clear;
  27. public
  28. file_name: PChar;
  29. function_name: PChar;
  30. args: PChar;
  31. line_number: LongInt;
  32. address: CORE_ADDR;
  33. level : longint;
  34. constructor Init;
  35. destructor Done;
  36. end;
  37. TGDBBuffer = object
  38. private
  39. buf: PChar;
  40. size, idx: LongInt;
  41. procedure Resize(nsize: LongInt);
  42. procedure Append(p: PChar);
  43. procedure LAppend(p: PChar; len: LongInt);
  44. public
  45. constructor Init;
  46. destructor Done;
  47. procedure Reset;
  48. end;
  49. TGDBInterface = object
  50. private
  51. user_screen_shown: Boolean;
  52. {$ifdef GDB_RAW_OUTPUT}
  53. output_raw : boolean;
  54. {$endif GDB_RAW_OUTPUT}
  55. protected
  56. GDB: TGDBWrapper;
  57. procedure i_gdb_command(const S: string);
  58. procedure WaitForProgramStop;
  59. procedure ProcessResponse;
  60. public
  61. GDBErrorBuf: TGDBBuffer;
  62. GDBOutputBuf: TGDBBuffer;
  63. {$ifdef GDB_RAW_OUTPUT}
  64. { Separate Raw buffer to display everything inside GDB Window
  65. but without parsing it twice }
  66. GDBRawBuf: TGDBBuffer;
  67. {$endif GDB_RAW_OUTPUT}
  68. got_error: Boolean;
  69. reset_command: Boolean;
  70. Debuggee_started: Boolean;
  71. init_count : longint;
  72. { frames and frame info while recording a frame }
  73. frames: PPFrameEntry;
  74. frame_count: LongInt;
  75. command_level: LongInt;
  76. signal_name: PChar;
  77. signal_string: PChar;
  78. current_pc: CORE_ADDR;
  79. switch_to_user: Boolean;
  80. { init }
  81. constructor Init;
  82. destructor Done;
  83. { from gdbcon }
  84. function GetOutput: PChar;
  85. function GetError: PChar;
  86. {$ifdef DEBUG}
  87. function GetRaw: PChar;
  88. {$endif DEBUG}
  89. { Lowlevel }
  90. procedure Set_debuggee_started;
  91. function error: Boolean;
  92. function error_num: LongInt;
  93. function get_current_frame: PtrInt;
  94. function set_current_frame(level: LongInt): Boolean;
  95. procedure clear_frames;
  96. { Highlevel }
  97. procedure DebuggerScreen;
  98. procedure UserScreen;
  99. procedure FlushAll; virtual;
  100. function Query(question: PChar; args: PChar): LongInt; virtual;
  101. { Hooks }
  102. function DoSelectSourceline(const fn: string; line, BreakIndex: longint): Boolean;virtual;
  103. procedure DoStartSession; virtual;
  104. procedure DoBreakSession; virtual;
  105. procedure DoEndSession(code: LongInt); virtual;
  106. procedure DoUserSignal; virtual;
  107. procedure DoDebuggerScreen; virtual;
  108. procedure DoUserScreen; virtual;
  109. function AllowQuit: Boolean; virtual;
  110. end;
  111. const
  112. use_gdb_file: Boolean = False;
  113. var
  114. gdb_file: Text;
  115. function GDBVersion: string;
  116. function inferior_pid : longint;
  117. {$ifdef windows}
  118. { We need to do some path conversions if we are using Cygwin GDB }
  119. var
  120. using_cygwin_gdb : boolean;
  121. {$endif windows}
  122. implementation
  123. uses
  124. strings;
  125. constructor TFrameEntry.Init;
  126. begin
  127. Reset;
  128. end;
  129. destructor TFrameEntry.Done;
  130. begin
  131. Clear;
  132. end;
  133. procedure TFrameEntry.Reset;
  134. begin
  135. file_name := nil;
  136. function_name := nil;
  137. args := nil;
  138. line_number := 0;
  139. address := 0;
  140. level := 0;
  141. end;
  142. procedure TFrameEntry.Clear;
  143. begin
  144. if Assigned(file_name) then
  145. StrDispose(file_name);
  146. if Assigned(function_name) then
  147. StrDispose(function_name);
  148. if Assigned(args) then
  149. StrDispose(args);
  150. Reset;
  151. end;
  152. const
  153. BlockSize = 2048;
  154. constructor TGDBBuffer.Init;
  155. begin
  156. buf := nil;
  157. size := 0;
  158. Resize(BlockSize);
  159. Reset;
  160. end;
  161. destructor TGDBBuffer.Done;
  162. begin
  163. if Assigned(buf) then
  164. FreeMem(buf, size);
  165. end;
  166. procedure TGDBBuffer.Reset;
  167. begin
  168. idx := 0;
  169. buf[0] := #0;
  170. end;
  171. procedure TGDBBuffer.Resize(nsize: LongInt);
  172. var
  173. np: PChar;
  174. begin
  175. nsize := ((nsize + BlockSize - 1) div BlockSize) * BlockSize;
  176. GetMem(np, nsize);
  177. if Assigned(buf) then
  178. begin
  179. Move(buf^, np^, size);
  180. FreeMem(buf, size);
  181. end;
  182. buf := np;
  183. size := nsize;
  184. end;
  185. procedure TGDBBuffer.Append(p: PChar);
  186. var
  187. len: LongInt;
  188. begin
  189. if not Assigned(p) then
  190. exit;
  191. len := StrLen(p);
  192. LAppend(p, len);
  193. end;
  194. procedure TGDBBuffer.LAppend(p: PChar; len: LongInt);
  195. begin
  196. if not Assigned(p) then
  197. exit;
  198. if (len + idx + 1) > size then
  199. Resize(len + idx + 1);
  200. Move(p^, buf[idx], len);
  201. Inc(idx, len);
  202. buf[idx] := #0;
  203. end;
  204. constructor TGDBInterface.Init;
  205. begin
  206. GDBErrorBuf.Init;
  207. GDBOutputBuf.Init;
  208. GDB := TGDBWrapper.Create;
  209. command_level := 0;
  210. Debuggee_started:=false;
  211. init_count:=0;
  212. {$ifdef GDB_RAW_OUTPUT}
  213. output_raw:=true;
  214. GDBRawBuf.Init;
  215. {$endif GDB_RAW_OUTPUT}
  216. { other standard commands used for fpc debugging }
  217. i_gdb_command('-gdb-set print demangle off');
  218. i_gdb_command('-gdb-set gnutarget auto');
  219. i_gdb_command('-gdb-set language auto');
  220. i_gdb_command('-gdb-set print vtbl on');
  221. i_gdb_command('-gdb-set print object on');
  222. i_gdb_command('-gdb-set print null-stop');
  223. end;
  224. destructor TGDBInterface.Done;
  225. begin
  226. clear_frames;
  227. GDB.Free;
  228. GDBErrorBuf.Done;
  229. GDBOutputBuf.Done;
  230. {$ifdef GDB_RAW_OUTPUT}
  231. GDBRawBuf.Done;
  232. {$endif GDB_RAW_OUTPUT}
  233. end;
  234. function TGDBInterface.GetOutput: PChar;
  235. begin
  236. GetOutput := GDBOutputBuf.buf;
  237. end;
  238. {$ifdef GDB_RAW_OUTPUT}
  239. function TGDBInterface.GetRaw: PChar;
  240. begin
  241. GetRaw := GDBRawBuf.buf;
  242. end;
  243. {$endif GDB_RAW_OUTPUT}
  244. function TGDBInterface.GetError: PChar;
  245. var
  246. p: PChar;
  247. begin
  248. p := GDBErrorBuf.buf;
  249. if (p^=#0) and got_error then
  250. GetError := PChar(PtrInt(GDBOutputBuf.buf) + GDBOutputBuf.idx)
  251. else
  252. GetError := p;
  253. end;
  254. procedure TGDBInterface.Set_debuggee_started;
  255. begin
  256. if not Debuggee_started then
  257. begin
  258. inc(init_count);
  259. Debuggee_started:=true;
  260. end;
  261. end;
  262. procedure TGDBInterface.i_gdb_command(const S: string);
  263. var
  264. I: LongInt;
  265. begin
  266. Inc(command_level);
  267. got_error := False;
  268. GDB.Command(S);
  269. {$ifdef GDB_RAW_OUTPUT}
  270. if output_raw then
  271. for I := 0 to GDB.RawResponse.Count - 1 do
  272. GDBRawBuf.Append(PChar(GDB.RawResponse[I]));
  273. {$endif GDB_RAW_OUTPUT}
  274. for I := 0 to GDB.ConsoleStream.Count - 1 do
  275. GDBOutputBuf.Append(PChar(GDB.ConsoleStream[I]));
  276. if GDB.ResultRecord.AsyncClass='error' then
  277. begin
  278. got_error := True;
  279. if Assigned(GDB.ResultRecord.Parameters['msg']) then
  280. GDBErrorBuf.Append(PChar(GDB.ResultRecord.Parameters['msg'].AsString));
  281. end;
  282. ProcessResponse;
  283. Dec(command_level);
  284. end;
  285. procedure TGDBInterface.WaitForProgramStop;
  286. label
  287. Ignore;
  288. var
  289. StopReason: string;
  290. FileName: string = '';
  291. LineNumber: LongInt = 0;
  292. Addr: CORE_ADDR;
  293. BreakpointNo: LongInt;
  294. ExitCode: LongInt;
  295. begin
  296. Ignore:
  297. GDB.WaitForProgramStop;
  298. if not GDB.Alive then
  299. begin
  300. DebuggerScreen;
  301. current_pc := 0;
  302. Debuggee_started := False;
  303. exit;
  304. end;
  305. ProcessResponse;
  306. StopReason := GDB.ExecAsyncOutput.Parameters['reason'].AsString;
  307. case StopReason of
  308. 'watchpoint-scope':
  309. begin
  310. { A watchpoint has gone out of scope (e.g. if it was a local variable). TODO: should we stop
  311. the program and notify the user or maybe silently disable it in the breakpoint list and
  312. continue execution? The libgdb.a version of the debugger just silently ignores this case.
  313. We have: GDB.ExecAsyncOutput.Parameters['wpnum'].AsLongInt }
  314. i_gdb_command('-exec-continue');
  315. if not GDB.ResultRecord.Success then
  316. begin
  317. DebuggerScreen;
  318. got_error := True;
  319. exit;
  320. end;
  321. goto Ignore;
  322. end;
  323. 'signal-received':
  324. begin
  325. { TODO: maybe show information to the user about the signal
  326. we have:
  327. GDB.ExecAsyncOutput.Parameters['signal-name'].AsString (e.g. 'SIGTERM')
  328. GDB.ExecAsyncOutput.PArameters['signal-meaning'].AsString (e.g. 'Terminated')
  329. }
  330. i_gdb_command('-exec-continue');
  331. if not GDB.ResultRecord.Success then
  332. begin
  333. DebuggerScreen;
  334. got_error := True;
  335. exit;
  336. end;
  337. goto Ignore;
  338. end;
  339. 'breakpoint-hit',
  340. 'watchpoint-trigger',
  341. 'access-watchpoint-trigger',
  342. 'read-watchpoint-trigger',
  343. 'end-stepping-range',
  344. 'function-finished':
  345. begin
  346. if StopReason = 'breakpoint-hit' then
  347. BreakpointNo := GDB.ExecAsyncOutput.Parameters['bkptno'].AsLongInt
  348. else if StopReason = 'watchpoint-trigger' then
  349. BreakpointNo := GDB.ExecAsyncOutput.Parameters['wpt'].AsTuple['number'].AsLongInt
  350. else if StopReason = 'access-watchpoint-trigger' then
  351. BreakpointNo := GDB.ExecAsyncOutput.Parameters['hw-awpt'].AsTuple['number'].AsLongInt
  352. else if StopReason = 'read-watchpoint-trigger' then
  353. BreakpointNo := GDB.ExecAsyncOutput.Parameters['hw-rwpt'].AsTuple['number'].AsLongInt
  354. else
  355. BreakpointNo := 0;
  356. Addr := GDB.ExecAsyncOutput.Parameters['frame'].AsTuple['addr'].AsCoreAddr;
  357. if Assigned(GDB.ExecAsyncOutput.Parameters['frame'].AsTuple['fullname']) then
  358. FileName := GDB.ExecAsyncOutput.Parameters['frame'].AsTuple['fullname'].AsString;
  359. if Assigned(GDB.ExecAsyncOutput.Parameters['frame'].AsTuple['line']) then
  360. LineNumber := GDB.ExecAsyncOutput.Parameters['frame'].AsTuple['line'].AsLongInt;
  361. { this kills GDB.ExecAsyncOutput, because it may execute other gdb commands, so
  362. make sure we have read all parameters that we need to local variables before that }
  363. DebuggerScreen;
  364. set_debuggee_started;
  365. current_pc := Addr;
  366. if not DoSelectSourceLine(FileName, LineNumber, BreakpointNo) then
  367. begin
  368. UserScreen;
  369. i_gdb_command('-exec-continue');
  370. if not GDB.ResultRecord.Success then
  371. begin
  372. DebuggerScreen;
  373. got_error := True;
  374. exit;
  375. end;
  376. goto Ignore;
  377. end;
  378. end;
  379. 'exited-signalled':
  380. begin
  381. DebuggerScreen;
  382. current_pc := 0;
  383. Debuggee_started := False;
  384. { TODO: maybe show information to the user about the signal
  385. we have:
  386. GDB.ExecAsyncOutput.Parameters['signal-name'].AsString (e.g. 'SIGTERM')
  387. GDB.ExecAsyncOutput.PArameters['signal-meaning'].AsString (e.g. 'Terminated')
  388. }
  389. DoEndSession(1);
  390. end;
  391. 'exited':
  392. begin
  393. ExitCode := LongInt(GDB.ExecAsyncOutput.Parameters['exit-code'].AsLongWord);
  394. DebuggerScreen;
  395. current_pc := 0;
  396. Debuggee_started := False;
  397. DoEndSession(ExitCode);
  398. end;
  399. 'exited-normally':
  400. begin
  401. DebuggerScreen;
  402. current_pc := 0;
  403. Debuggee_started := False;
  404. DoEndSession(0);
  405. end;
  406. end;
  407. end;
  408. procedure TGDBInterface.ProcessResponse;
  409. //var
  410. // NAO: TGDBMI_AsyncOutput;
  411. // Code: LongInt;
  412. begin
  413. // for NAO in GDB.NotifyAsyncOutput do
  414. // begin
  415. // if NAO.AsyncClass = 'breakpoint-created' then
  416. // begin
  417. // Writeln('BREAKPOINT created!');
  418. // Val(NAO.Parameters['bkpt'].AsTuple['number'].AsString, last_breakpoint_number, Code);
  419. // Writeln('last_breakpoint_number=', last_breakpoint_number);
  420. // end;
  421. // end;
  422. end;
  423. function TGDBInterface.error: Boolean;
  424. begin
  425. error := got_error or not GDB.Alive;
  426. end;
  427. function TGDBInterface.error_num: LongInt;
  428. begin
  429. error_num := 0; { TODO }
  430. end;
  431. function TGDBInterface.get_current_frame: PtrInt;
  432. begin
  433. i_gdb_command('-stack-info-frame');
  434. if GDB.ResultRecord.Success then
  435. get_current_frame := GDB.ResultRecord.Parameters['frame'].AsTuple['level'].AsLongInt
  436. else
  437. get_current_frame := 0;
  438. end;
  439. function TGDBInterface.set_current_frame(level: LongInt): Boolean;
  440. var
  441. s: string;
  442. begin
  443. str(level,s);
  444. { Note: according to the gdb docs, '-stack-select-frame' is deprecated in favor of passing the '--frame' option to every command }
  445. i_gdb_command('-stack-select-frame '+s);
  446. set_current_frame := GDB.ResultRecord.Success;
  447. end;
  448. procedure TGDBInterface.clear_frames;
  449. var
  450. I: LongInt;
  451. begin
  452. for I := 0 to frame_count - 1 do
  453. Dispose(frames[I], Done);
  454. if Assigned(frames) then
  455. begin
  456. FreeMem(frames, SizeOf(Pointer) * frame_count);
  457. frames := nil;
  458. end;
  459. frame_count := 0;
  460. end;
  461. procedure TGDBInterface.DebuggerScreen;
  462. begin
  463. if user_screen_shown then
  464. DoDebuggerScreen;
  465. user_screen_shown := False;
  466. end;
  467. procedure TGDBInterface.UserScreen;
  468. begin
  469. if switch_to_user then
  470. begin
  471. if not user_screen_shown then
  472. DoUserScreen;
  473. user_screen_shown := True;
  474. end;
  475. end;
  476. procedure TGDBInterface.FlushAll;
  477. begin
  478. end;
  479. function TGDBInterface.Query(question: PChar; args: PChar): LongInt;
  480. begin
  481. Query := 0;
  482. end;
  483. function TGDBInterface.DoSelectSourceline(const fn: string; line, BreakIndex: LongInt): Boolean;
  484. begin
  485. end;
  486. procedure TGDBInterface.DoStartSession;
  487. begin
  488. end;
  489. procedure TGDBInterface.DoBreakSession;
  490. begin
  491. end;
  492. procedure TGDBInterface.DoEndSession(code: LongInt);
  493. begin
  494. end;
  495. procedure TGDBInterface.DoUserSignal;
  496. begin
  497. end;
  498. procedure TGDBInterface.DoDebuggerScreen;
  499. begin
  500. end;
  501. procedure TGDBInterface.DoUserScreen;
  502. begin
  503. end;
  504. function TGDBInterface.AllowQuit: Boolean;
  505. begin
  506. AllowQuit := True;
  507. end;
  508. function inferior_pid : longint;
  509. begin
  510. inferior_pid:=0; {inferior_ptid.pid; }
  511. end;
  512. var
  513. CachedGDBVersion: string;
  514. function GDBVersion: string;
  515. var
  516. GDB: TGDBWrapper;
  517. {$ifdef windows}
  518. i : longint;
  519. line :string;
  520. {$endif windows}
  521. begin
  522. if CachedGDBVersion <> '' then
  523. begin
  524. GDBVersion := CachedGDBVersion;
  525. exit;
  526. end;
  527. GDBVersion := '';
  528. GDB := TGDBWrapper.Create;
  529. GDB.Command('-gdb-version');
  530. if GDB.ConsoleStream.Count > 0 then
  531. GDBVersion := GDB.ConsoleStream[0];
  532. if (GDBVersion <> '') and (GDBVersion[Length(GDBVersion)]=#10) then
  533. Delete(GDBVersion, Length(GDBVersion), 1);
  534. {$ifdef windows}
  535. i:=0;
  536. using_cygwin_gdb:=false;
  537. while i < GDB.ConsoleStream.Count do
  538. begin
  539. line:=GDB.ConsoleStream[i];
  540. if pos('This GDB was configured',line) > 0 then
  541. using_cygwin_gdb:=pos('cygwin',line) > 0;
  542. inc(i);
  543. end;
  544. {$endif windows}
  545. GDB.Free;
  546. CachedGDBVersion := GDBVersion;
  547. if GDBVersion = '' then
  548. GDBVersion := 'GDB missing or does not work';
  549. end;
  550. begin
  551. CachedGDBVersion := '';
  552. end.