1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
|
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
Functions to parse GDB stacktraces, dismangle FPC identifiers and
find the stacktrace identifiers in Pascal sources.
ToDo:
- unit names with underscores
- unit names with points
- procs starting with underscore
}
unit CodetoolGDBTracer;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LazLoggerBase, KeywordFuncLists, contnrs;
type
TCTGDBMangledItemKind = (
cgmiNone,
cgmiUnknown, // '??'
cgmiProgram, // program, e.g. P$identifier
cgmiCompiler, // compiler function, like start, _start, PASCALMAIN
cgmiUnit, // unit, e.g. SYSTEM
cgmiStructure, // class, procedure
cgmiParameter, // function parameter name
cgmiParameterType, // function parameter type
cgmiResultType, // result type
cgmiError // syntax error
);
TCTGDBMangledItemTypes = set of TCTGDBMangledItemKind;
{ TCTGDBMangledItem }
TCTGDBMangledItem = class
public
Kind: TCTGDBMangledItemKind;
Identifier: string;
function AsString: string;
end;
TCTArrayOfGDBMangledItems = array of TCTGDBMangledItem;
{ TCTGDBTraceLine }
TCTGDBTraceLine = class
public
LineNumber: integer; // 1 based
Source: string;
Depth: integer;
MangledIdentifier: string;
GDBAddress: string; // e.g. '0x007489de'
GDBFilename: string;
GDBLine, GDBCol: integer;
MangledItems: TCTArrayOfGDBMangledItems;
Error: string;
ErrorCol: integer;
constructor Create;
destructor Destroy; override;
procedure WriteToStream(Indent: integer; s: TStream);
procedure Parse;
end;
{ TCTGDBTracer }
TCTGDBTracer = class
private
fLines: TObjectList; // list of TCTGDBTraceLine
FTraceText: string;
fCurP: PChar;
fLineStart: PChar;
fLineNumber: integer;
function GetCurrentLine: string;
procedure AddUnknownLine;
function GetLineCount: integer;
function GetLines(Index: integer): TCTGDBTraceLine;
procedure SetTraceText(AValue: string);
procedure SkipLine;
procedure TraceToLines;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
property TraceText: string read FTraceText write SetTraceText;
property LineCount: integer read GetLineCount;
property Lines[Index: integer]: TCTGDBTraceLine read GetLines;
function AsString: string;
procedure WriteToStream(s: TStream);
procedure ParseLines;
end;
procedure DemangleGDBIdentifier(Mangled: string;
var MangledItems: TCTArrayOfGDBMangledItems);
function dbgs(k: TCTGDBMangledItemKind): string; overload;
function dbgs(Mangles: TCTArrayOfGDBMangledItems): string; overload;
implementation
procedure DemangleGDBIdentifier(Mangled: string;
var MangledItems: TCTArrayOfGDBMangledItems);
{ examples:
fpc_raiseexception
SYSUTILS_RUNERRORTOEXCEPT$LONGINT$POINTER$POINTER
??
EXTTOOLEDITDLG_TEXTERNALTOOLMENUITEMS_$__LOAD$TCONFIGSTORAGE$$TMODALRESULT
PASCALMAIN
SYSTEM_FPC_SYSTEMMAIN$LONGINT$PPCHAR$PPCHAR
P$TESTSTACKTRACE1_TMAINCLASS_$_TSUBCLASS_$__RAISESOMETHING$ANSISTRING
start
}
procedure Add(Kind: TCTGDBMangledItemKind; const Identifier: string);
var
l: Integer;
Item: TCTGDBMangledItem;
begin
Item:=TCTGDBMangledItem.Create;
Item.Kind:=Kind;
Item.Identifier:=Identifier;
l:=length(MangledItems);
SetLength(MangledItems,l+1);
MangledItems[l]:=Item;
end;
var
p: PChar;
StartP: PChar;
function Extract: string;
begin
Result:=copy(Mangled,StartP-PChar(Mangled)+1,p-StartP);
end;
procedure ExpectedButFound(const Expected: string);
begin
Add(cgmiError,'expected '+Expected+', but found '
+dbgstr(p^)+' after "'+dbgstr(Mangled,1,p-PChar(Mangled))+'"');
end;
function ReadIdentifier: boolean;
begin
if not (p^ in ['a'..'z','A'..'Z']) then begin
ExpectedButFound('identifier');
exit(false);
end;
StartP:=p;
while p^ in ['a'..'z','A'..'Z'] do inc(p);
Result:=true;
end;
var
Identifier: String;
begin
SetLength(MangledItems,0);
if Mangled='' then exit;
p:=PChar(Mangled);
if p^='?' then begin
// for example: ??
Add(cgmiUnknown,Mangled);
exit;
end;
if (p^='P') and (p^='$') then begin
// program, for example: P$TESTSTACKTRACE1
inc(p,2);
StartP:=p;
while p^ in ['a'..'z','A'..'Z'] do inc(p);
Add(cgmiProgram,Extract);
end else if p^ in ['A'..'Z','a'..'z'] then begin
StartP:=p;
while p^ in ['a'..'z','A'..'Z'] do inc(p);
Identifier:=Extract;
if p^=#0 then begin
// Compiler function, e.g. start, _start, PASCALMAIN
Add(cgmiCompiler,Identifier);
exit;
end;
// unit, for example SYSTEM
Add(cgmiUnit,Identifier);
end else begin
// syntax error
Add(cgmiError,'unknown format');
exit;
end;
repeat
// read sub identifiers
if p^='_' then begin
inc(p);
if (p^='$') then begin
// for example _$_ or _$__
inc(p);
if p^<>'_' then begin
ExpectedButFound('_');
exit;
end;
inc(p);
if p^='_' then begin
// _$__
inc(p);
end;
end;
end else if p^='$' then begin
// parameters, p1$p2$p3
if MangledItems[length(MangledItems)-1].Kind<>cgmiStructure then begin
ExpectedButFound('_');
exit;
end;
repeat
inc(p);
// read parameter type
if not ReadIdentifier then exit;
Add(cgmiParameterType,Extract);
until (p^<>'$') or (not IsIdentStartChar[p[1]]);
if (p^='$') and (p[1]='$') then begin
// function result type
inc(p,2);
if not ReadIdentifier then exit;
Add(cgmiResultType,Extract);
end;
continue;
end else if p^=#0 then begin
exit;
end else begin
ExpectedButFound('_');
exit;
end;
if not ReadIdentifier then exit;
Add(cgmiStructure,Extract);
until false;
end;
function dbgs(k: TCTGDBMangledItemKind): string;
begin
case k of
cgmiNone: Result:='None';
cgmiUnknown: Result:='Unknown';
cgmiProgram: Result:='Program';
cgmiCompiler: Result:='Compiler';
cgmiUnit: Result:='Unit';
cgmiStructure: Result:='Structure';
cgmiParameter: Result:='Parameter';
cgmiParameterType: Result:='ParameterType';
cgmiResultType: Result:='ResultType';
cgmiError: Result:='Error';
else Result:='?';
end;
end;
function dbgs(Mangles: TCTArrayOfGDBMangledItems): string;
var
i: Integer;
begin
Result:='[';
for i:=0 to length(Mangles)-1 do begin
if i>0 then Result+=',';
Result+=Mangles[i].AsString;
end;
Result+=']';
end;
{ TCTGDBMangledItem }
function TCTGDBMangledItem.AsString: string;
begin
Result:=dbgs(Kind)+':'+Identifier;
end;
{ TCTGDBTraceLine }
constructor TCTGDBTraceLine.Create;
begin
Depth:=-1;
end;
destructor TCTGDBTraceLine.Destroy;
begin
inherited Destroy;
end;
procedure TCTGDBTraceLine.WriteToStream(Indent: integer; s: TStream);
procedure w(const h: string);
begin
if h='' then exit;
s.Write(h[1],length(h));
end;
begin
// source
w(Space(Indent));
w('Source={'+Source+'}');
w(LineEnding);
// error
if Error<>'' then begin
w(Space(Indent));
w('Error(');
w(IntToStr(LineNumber));
if ErrorCol>0 then begin
w(',');
w(IntToStr(ErrorCol));
end;
w('): '+Error);
w(LineEnding);
end;
if MangledIdentifier<>'' then begin
w(Space(Indent));
w('MangledIdentifier='+MangledIdentifier);
w(LineEnding);
if length(MangledItems)>0 then begin
w(Space(Indent));
w('Demangled='+dbgs(MangledItems));
w(LineEnding);
end;
end;
if GDBFilename<>'' then begin
w(Space(Indent));
w('GDB Source position='+GDBFilename+'('+IntToStr(GDBLine)+','+IntToStr(GDBCol)+')');
w(LineEnding);
end;
end;
procedure TCTGDBTraceLine.Parse;
{ Examples:
#0 0x00020e16 in fpc_raiseexception ()
#2 0x00024e48 in SYSTEM_HANDLEERRORADDRFRAME$LONGINT$POINTER$POINTER ()
#3 0xbffff548 in ?? ()
#9 0x00011124 in PASCALMAIN ()
#0 DOHANDLEMOUSEACTION (this=0x14afae00, ANACTIONLIST=0x14a96af8,ANINFO=...) at synedit.pp:3000
}
var
p: PChar;
i: Integer;
StartP: PChar;
Level: Integer;
procedure UnexpectedError(Expected: string);
begin
ErrorCol:=p-PChar(Source)+1;
Error:='expected ' +Expected+', but found '+DbgStr(p^);
end;
begin
Depth:=-1;
MangledIdentifier:='';
GDBAddress:='';
GDBFilename:='';
GDBLine:=0;
GDBCol:=0;
if Source='' then begin
Error:='Empty line';
exit;
end;
p:=PChar(Source);
// read #
while p^ in [' ',#9] do inc(p);
if p^<>'#' then begin
UnexpectedError('#');
exit;
end;
inc(p);
// read stack depth number (decimal)
if not (p^ in ['0'..'9']) then begin
UnexpectedError('decimal number');
exit;
end;
i:=0;
while p^ in ['0'..'9'] do begin
if i>100000 then begin
UnexpectedError('short decimal number');
exit;
end;
i:=i*10+ord(p^)-ord('0');
inc(p);
end;
Depth:=i;
// skip space
while p^ in [' ',#9] do inc(p);
if (p^='0') and (p[1]='x') then begin
// format: <hexnumber> in <mangledidentifier> ()
StartP:=p;
inc(p,2);
while IsHexNumberChar[p^] do inc(p);
GDBAddress:=copy(Source,StartP-PChar(Source)+1,p-StartP);
// skip space
while p^ in [' ',#9] do inc(p);
// read 'in'
if (p^<>'i') or (p[1]<>'n') then begin
UnexpectedError('in');
exit;
end;
inc(p,2);
// skip space
while p^ in [' ',#9] do inc(p);
end;
if (p^ in ['A'..'Z','a'..'z','_','?','$']) then begin
// format: <gdbidentifier> (<parameter list>) at gdbfilename:gdbline
// format: <mangledidentifier> ()
// read identifier
StartP:=p;
while p^ in ['a'..'z','A'..'Z','0'..'9','_','?','$'] do inc(p);
MangledIdentifier:=copy(Source,StartP-PChar(Source)+1,p-StartP);
// skip space
while p^ in [' ',#9] do inc(p);
if p^='(' then begin
// read parameters
Level:=0;
repeat
case p^ of
'(':
begin
inc(Level);
inc(p);
end;
')':
begin
inc(p);
dec(Level);
if Level=0 then break;
end;
#0:
exit;
else
inc(p);
end;
until false;
// skip space
while p^ in [' ',#9] do inc(p);
end;
// read 'at'
if (p^='a') and (p[1]='t') then begin
inc(p,2);
// skip space
while p^ in [' ',#9] do inc(p);
// read gdbfilename:gdbline
StartP:=p;
while not (p^ in [#0,':']) do inc(p);
GDBFilename:=copy(Source,StartP-PChar(Source)+1,p-StartP);
if p^=#0 then exit;
inc(p);
StartP:=p;
while p^ in ['0'..'9'] do inc(p);
GDBLine:=StrToIntDef(copy(Source,StartP-PChar(Source)+1,p-StartP),0);
end else if (p^='f') and (p[1]='r') and (p[2]='o') and (p[3]='m') then begin
// from <filename>
end else if p^=#0 then begin
// no source position => mangled
DemangleGDBIdentifier(MangledIdentifier,MangledItems);
end else begin
UnexpectedError('at');
exit;
end;
end else begin
// unknown format
UnexpectedError('hexnumber');
exit;
end;
end;
{ TCTGDBTracer }
procedure TCTGDBTracer.SetTraceText(AValue: string);
begin
if FTraceText=AValue then Exit;
Clear;
FTraceText:=AValue;
TraceToLines;
end;
function TCTGDBTracer.GetLineCount: integer;
begin
Result:=fLines.Count;
end;
function TCTGDBTracer.GetLines(Index: integer): TCTGDBTraceLine;
begin
Result:=TCTGDBTraceLine(fLines[Index]);
end;
procedure TCTGDBTracer.SkipLine;
var
c: Char;
begin
while not (fCurP^ in [#0,#10,#13]) do inc(fCurP);
repeat
c:=fCurP^;
if not (c in [#10,#13]) then break;
inc(fCurP);
inc(fLineNumber);
if (fCurP^ in [#10,#13]) and (c<>fCurP^) then
inc(fCurP);
until false;
fLineStart:=fCurP;
end;
function TCTGDBTracer.GetCurrentLine: string;
var
LineEnd: PChar;
begin
LineEnd:=fCurP;
while not (LineEnd^ in [#0,#10,#13]) do inc(LineEnd);
Result:=copy(FTraceText,fLineStart-PChar(FTraceText)+1,LineEnd-fLineStart);
end;
procedure TCTGDBTracer.AddUnknownLine;
var
Line: TCTGDBTraceLine;
begin
Line:=TCTGDBTraceLine.Create;
Line.LineNumber:=fLineNumber;
Line.Source:=GetCurrentLine;
Line.Error:='invalid format';
fLines.Add(Line);
SkipLine;
end;
procedure TCTGDBTracer.TraceToLines;
{ Example:
#0 0x00020e16 in fpc_raiseexception ()
#1 0x0004cb37 in SYSUTILS_RUNERRORTOEXCEPT$LONGINT$POINTER$POINTER ()
#3 0xbffff548 in ?? ()
#4 0x007489de in EXTTOOLEDITDLG_TEXTERNALTOOLMENUITEMS_$__LOAD$TCONFIGSTORAGE$$TMODALRESULT ()
#7 0x0007e620 in MAIN_TMAINIDE_$__LOADGLOBALOPTIONS ()
#9 0x00011124 in PASCALMAIN ()
#10 0x0002f416 in SYSTEM_FPC_SYSTEMMAIN$LONGINT$PPCHAR$PPCHAR ()
#0 0x0001136d in P$TESTSTACKTRACE1_TMAINCLASS_$_TSUBCLASS_$__RAISESOMETHING$ANSISTRING ()
#5 0x0001114a in start ()
~"#0 DOHANDLEMOUSEACTION (this=0x14afae00, ANACTIONLIST=0x14a96af8,
ANINFO=...) at synedit.pp:3000\n"
~"#1 0x00aea3e9 in FINDANDHANDLEMOUSEACTION (this=0x14afae00,
ABUTTON=MBLEFT, ASHIFT=..., X=233, Y=241, ACCOUNT=CCSINGLE, ADIR=CDDOWN,
ANAC
TIONRESULT=..., AWHEELDELTA=0) at synedit.pp:3307\n"
~"#3 0x005e083b in DOMOUSEDOWN (this=0x14afae00, MESSAGE=...,
BUTTON=MBLEFT, SHIFT=...) at include/control.inc:2135\n"
~"#5 0x0040d096 in DISPATCH (this=0xeebf6d4, MESSAGE=0) at
../inc/objpas.inc:592\n"
~"#8 0x00af3b76 in WNDPROC (this=0x14afae00, MSG=...) at synedit.pp:5740\n"
~"#11 0x7673fd72 in ?? () from C:\\Windows\\system32\\user32.dll\n"
~"#20 0x0040358f in main () at lazarus.pp:128\n"
}
procedure ReadLine;
var
StartP: PChar;
Line: String;
CopyStartP: PChar;
StartLineNumber: Integer;
NewLine: TCTGDBTraceLine;
c: Char;
procedure ConcatLine;
begin
if fCurP=CopyStartP then exit;
Line+=copy(FTraceText,CopyStartP-PChar(FTraceText)+1,fCurP-CopyStartP);
end;
begin
while (fCurP^ in [' ',#9]) do inc(fCurP);
if fCurP^='~' then begin
// quoted format ~"<line>"
inc(fCurP);
if fCurP^<>'"' then begin
AddUnknownLine;
exit;
end;
inc(fCurP);
StartP:=fCurP;
Line:='';
CopyStartP:=StartP;
StartLineNumber:=fLineNumber;
repeat
case fCurP^ of
#0:
begin
// missing closing quote
fCurP:=StartP;
fLineNumber:=StartLineNumber;
AddUnknownLine;
exit;
end;
#10,#13:
begin
ConcatLine;
repeat
c:=fCurP^;
if not (c in [#10,#13]) then break;
inc(fLineNumber);
inc(fCurP);
if (fCurP^ in [#10,#13]) and (c<>fCurP^) then
inc(fCurP);
until false;
CopyStartP:=fCurP;
end;
'\':
begin
ConcatLine;
inc(fCurP);
case fCurP^ of
'n': // ignore line breaks
inc(fCurP);
#0:
break;
else
Line+=fCurP^;
end;
CopyStartP:=fCurP;
end;
'"':
begin
ConcatLine;
inc(fCurP);
break;
end;
else
inc(fCurP);
end;
until false;
NewLine:=TCTGDBTraceLine.Create;
NewLine.LineNumber:=StartLineNumber;
NewLine.Source:=Line;
fLines.Add(NewLine);
// skip the rest of the line
SkipLine;
exit;
end else if fCurP^='#' then begin
// non quoted format
NewLine:=TCTGDBTraceLine.Create;
NewLine.LineNumber:=fLineNumber;
NewLine.Source:=GetCurrentLine;
fLines.Add(NewLine);
// skip the rest of the line
SkipLine;
exit;
end;
// unknown format
AddUnknownLine;
end;
begin
fLines.Clear;
if FTraceText='' then exit;
fLineNumber:=1;
fCurP:=PChar(FTraceText);
fLineStart:=fCurP;
while fCurP^<>#0 do
ReadLine;
end;
procedure TCTGDBTracer.ParseLines;
var
i: Integer;
Line: TCTGDBTraceLine;
begin
for i:=0 to LineCount-1 do begin
Line:=Lines[i];
if Line.Error<>'' then continue;
Line.Parse;
end;
end;
constructor TCTGDBTracer.Create;
begin
fLines:=TObjectList.Create(true);
end;
destructor TCTGDBTracer.Destroy;
begin
Clear;
FreeAndNil(fLines);
inherited Destroy;
end;
procedure TCTGDBTracer.Clear;
begin
fLines.Clear;
end;
function TCTGDBTracer.AsString: string;
var
ms: TMemoryStream;
begin
ms:=TMemoryStream.Create;
try
WriteToStream(ms);
ms.Position:=0;
SetLength(Result,ms.Size);
if Result<>'' then
ms.Read(Result[1],length(Result));
finally
ms.Free;
end;
end;
procedure TCTGDBTracer.WriteToStream(s: TStream);
procedure w(const h: string);
begin
if h='' then exit;
s.Write(h[1],length(h));
end;
var
i: Integer;
Line: TCTGDBTraceLine;
begin
for i:=0 to LineCount-1 do begin
Line:=Lines[i];
w(IntToStr(i+1)+'/'+IntToStr(LineCount)+': ');
Line.WriteToStream(2,s);
w(LineEnding);
end;
end;
end.
|