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
|
(* This program 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, 3 or any later version
of the License (at your option).
This program 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*)
unit LldbHelper;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, math, DbgIntfBaseTypes, strutils;
function LastPos(ASearch, AString: string): Integer;
function StrStartsWith(AString, AStart: string; ACheckStartNotEmpty: Boolean = False): Boolean;
function StrContains(AString, AFind: string): Boolean;
function StrMatches(AString: string; const AFind: array of string): Boolean;
function StrMatches(AString: string; const AFind: array of string; out AGapsContent: TStringArray): Boolean;
function ParseThreadLocation(AnInput: String; out AnId: Integer;
out AnIsCurrent: Boolean; out AName: String; out AnAddr: TDBGPtr;
out AFuncName: String; out AnArgs: TStringList; out AFile: String;
out ALine: Integer; out AReminder: String): Boolean;
function ParseFrameLocation(AnInput: String; out AnId: Integer;
out AnIsCurrent: Boolean; out AnAddr: TDBGPtr; out AFuncName: String;
out AnArgs: TStringList; out AFile: String; out ALine: Integer;
out AReminder: String): Boolean;
function ParseNewFrameLocation(AnInput: String; out AnId: Integer;
out AnIsCurrent: Boolean; out AnAddr, AnStack, AnFrame: TDBGPtr; out AFuncName: String;
out AnArgs: TStringList; out AFile, AFullFile: String; out ALine: Integer;
out AReminder: String): Boolean;
function ParseNewThreadLocation(AnInput: String; out AnId: Integer;
out AnIsCurrent: Boolean; out AName: String; out AnAddr, AnStack, AnFrame: TDBGPtr;
out AFuncName: String; out AnArgs: TStringList; out AFile, AFullFile: String;
out ALine: Integer; out AReminder: String): Boolean;
implementation
function LastPos(ASearch, AString: string): Integer;
var
i: Integer;
begin
i := pos(ASearch, AString);
Result := i;
while i > 0 do begin
Result := i;
i := PosEx(ASearch, AString, i + 1);
end;
end;
function StrStartsWith(AString, AStart: string; ACheckStartNotEmpty: Boolean
): Boolean;
begin
Result := ( (not ACheckStartNotEmpty) or (AStart <> '') ) and (LeftStr(AString, Length(AStart)) = AStart);
end;
function StrContains(AString, AFind: string): Boolean;
begin
Result := pos(AFind, AString) > 0;
end;
function StrMatches(AString: string; const AFind: array of string): Boolean;
var
Dummy: TStringArray;
begin
Result := StrMatches(AString, AFind, Dummy);
end;
function StrMatches(AString: string; const AFind: array of string; out
AGapsContent: TStringArray): Boolean;
var
FindIdx, FindLen, j, j2, ResIdx: Integer;
OpenEnd: Boolean;
begin
FindLen := Length(AFind);
if FindLen = 0 then begin
Result := False;
AGapsContent := nil;
exit;
end;
SetLength(AGapsContent, FindLen - 1);
Result := StrStartsWith(AString, AFind[0]);
if not Result then
exit;
Delete(AString, 1, Length(AFind[0]));
OpenEnd := AFind[FindLen - 1] = '';
if OpenEnd then
dec(FindLen);
FindIdx := 1;
ResIdx := 0;
while (FindIdx < FindLen) do begin
if AFind[FindIdx] = '' then begin
// empty string, match as far ahead as possible
inc(FindIdx);
j := LastPos(AFind[FindIdx], AString) - 1;
end
else
j := pos(AFind[FindIdx], AString) - 1;
Result := j >= 0;
if not Result then
exit;
AGapsContent[ResIdx] := copy(AString, 1, j);
Delete(AString, 1, j + Length(AFind[FindIdx]));
inc(FindIdx);
inc(ResIdx);
end;
if OpenEnd then begin
AGapsContent[ResIdx] := AString;
inc(ResIdx);
end
else
Result := AString = '';
SetLength(AGapsContent, ResIdx);
end;
(* Examples
"* thread #1: tid = 0x1a1c, 0x0042951d project1.exe`FORMCREATE(this=0x00151060, SENDER=0x00151060) at unit1.pas:59, stop reason = breakpoint 2.1"
" thread #2: tid = 0x16ac, 0x7700eb6c ntdll.dll`NtDelayExecution + 12"
" * frame #0: 0x7700eb6c ntdll.dll`NtDelayExecution + 12"
" frame #1: 0x767a5f5b KernelBase.dll`SleepEx + 155"
" frame #3: 0x004521c9 project1.exe"
" frame #7: 0x761a8654 kernel32.dll`BaseThreadInitThunk + 36"
" frame #2: 0x048fa158"
" frame #1: 0x0041ab6f project1.exe`DOCREATE(this=0x04a91060) at customform.inc:939"
" frame #5: 0x00402a42 project1.exe`main at project1.lpr:19"
*)
procedure ParseLocation(AnInput: String; out AnAddr: TDBGPtr; out AFuncName: String;
out AnArgs: TStringList; out AFile: String; out ALine: Integer; out AReminder: String);
var
found: TStringArray;
i, j, k: Integer;
begin
if pos(' ', AnInput) = 0 then begin
AnAddr := StrToInt64Def(AnInput, 0);
AnInput := '';
end
else
if StrMatches(AnInput, [''{addr}, ' '{remainder}, ''], found) then begin
AnAddr := StrToInt64Def(found[0], 0);
AnInput := found[1];
end
else
AnAddr := 0;
AnArgs := nil;
AFile := '';
ALine := 0;
AReminder := '';
if StrMatches(AnInput, [''{exe}, '`'{remainder}, ''], found) then begin
AnInput := found[1];
i := pos(' ', AnInput);
j := pos('(', AnInput);
k := pos(')', AnInput);
if ((i = 0) or (i > j)) and (j > 1) and (k > j) then begin
AFuncName := Copy(AnInput, 1, j-1);
AnArgs := TStringList.Create;
AnArgs.CommaText := copy(AnInput, j+1, k-j-1);
AnInput := Copy(AnInput, k+1, Length(AnInput));
end
else begin
i := Max(i, pos(', ', AnInput));
if i = 0 then i := Length(AnInput) + 1;
AFuncName := Copy(AnInput, 1, i-1);
AnInput := Copy(AnInput, i, Length(AnInput));
end;
if StrStartsWith(AnInput, ' + ') and (Length(AnInput) >= 4) and (AnInput[4] in ['0'..'9']) then begin
i := 4;
while (Length(AnInput) > i) and (AnInput[i+1] in ['0'..'9']) do inc(i);
delete(AnInput, 1, i);
end;
if StrMatches(AnInput, [' at ', ':', ''], found) then begin
AFile := found[0];
i := pos(', ', found[1]);
if i = 0 then i := Length(found[1]) + 1;
ALine := StrToIntDef(copy(found[1], 1, i-1), 0);
AReminder := copy(found[1], i, Length(found[1]));
end
else
AReminder := AnInput;
end
else begin
AFuncName := AnInput;
end;
end;
function ParseThreadLocation(AnInput: String; out AnId: Integer; out
AnIsCurrent: Boolean; out AName: String; out AnAddr: TDBGPtr; out
AFuncName: String; out AnArgs: TStringList; out AFile: String; out
ALine: Integer; out AReminder: String): Boolean;
var
found: TStringArray;
begin
Result := False;
AnIsCurrent := (Length(AnInput) > 1) and (AnInput[1] = '*');
if AnIsCurrent then AnInput[1] := ' ';
if not StrMatches(AnInput, [' thread #'{id}, ': '{}, ''], found) then begin
AnId := -1;
AName := '';
ParseLocation('', AnAddr, AFuncName, AnArgs, AFile, ALine, AReminder);
exit;
end;
AnId := StrToIntDef(found[0], -1);
AnInput := found[1];
Result := True;
if StrMatches(AnInput, ['tid = '{tid}, ', '{}, ''], found) then begin
AName := found[0];
AnInput := found[1];
end
else
AName := '';
ParseLocation(AnInput, AnAddr, AFuncName, AnArgs, AFile, ALine, AReminder);
end;
function ParseFrameLocation(AnInput: String; out AnId: Integer; out
AnIsCurrent: Boolean; out AnAddr: TDBGPtr; out AFuncName: String; out
AnArgs: TStringList; out AFile: String; out ALine: Integer; out
AReminder: String): Boolean;
var
found: TStringArray;
begin
Result := False;
AnIsCurrent := (Length(AnInput) > 3) and (AnInput[3] = '*');
if AnIsCurrent then AnInput[3] := ' ';
if not StrMatches(AnInput, [' frame #'{id}, ': '{}, ''], found) then begin
AnId := -1;
ParseLocation('', AnAddr, AFuncName, AnArgs, AFile, ALine, AReminder);
exit;
end;
AnId := StrToIntDef(found[0], -1);
AnInput := found[1];
Result := True;
ParseLocation(AnInput, AnAddr, AFuncName, AnArgs, AFile, ALine, AReminder);
end;
function ParseFrameOrThread(AnInput: String; out AnAddr, AnStack, AnFrame: TDBGPtr;
out AFuncName: String; out AnArgs: TStringList; out AFile, AFullFile: String;
out ALine: Integer; out AReminder: String): Boolean;
var
found: TStringArray;
i, j, k: SizeInt;
begin
Result := False;
if not StrMatches(AnInput, [''{addr}, ', ' {sp}, ', ' {fp},
' &&//FULL: '{fullfile}, ' &&//SHORT: '{file},' &&//LINE: '{line},
' &&//MOD: '{mod},' &&//FUNC: '{func}, '',' <<&&//FRAME', ''
], found) then begin
AnAddr := 0;
AFile := '';
AFullFile := '';
ALine := -1;
AFuncName := '';
AReminder := '';
AnArgs := nil;
exit;
end;
AnAddr := StrToInt64Def(found[0], 0);
AnStack := StrToInt64Def(found[1], 0);
AnFrame := StrToInt64Def(found[2], 0);
AFullFile := found[3];
AFile := found[4];
ALine := StrToIntDef(found[5], -1);
AFuncName := found[7];
AnArgs := nil;
AReminder := found[8];
if AFuncName = '' then begin
AFuncName := '<'+found[6]+'>';
end
else begin
AnInput := AFuncName;
i := pos(' ', AnInput);
j := pos('(', AnInput);
k := pos(')', AnInput);
if ((i = 0) or (i > j)) and (j > 1) and (k > j) then begin
AFuncName := Copy(AnInput, 1, j-1);
AnArgs := TStringList.Create;
AnArgs.CommaText := copy(AnInput, j+1, k-j-1);
AnInput := Copy(AnInput, k+1, Length(AnInput));
end;
end;
Result := True;
end;
function ParseNewFrameLocation(AnInput: String; out AnId: Integer; out
AnIsCurrent: Boolean; out AnAddr, AnStack, AnFrame: TDBGPtr; out
AFuncName: String; out AnArgs: TStringList; out AFile, AFullFile: String; out
ALine: Integer; out AReminder: String): Boolean;
var
found: TStringArray;
begin
AnIsCurrent := (Length(AnInput) > 3) and (AnInput[3] = '*');
if AnIsCurrent then AnInput[3] := ' ';
if StrMatches(AnInput, [' frame #'{id}, ': '{}, ''], found) then begin
AnId := StrToIntDef(found[0], -1);
AnInput := found[1];
end
else begin
AnId := -1;
AnInput := '';
end;
Result := ParseFrameOrThread(AnInput, AnAddr, AnStack, AnFrame,
AFuncName, AnArgs, AFile, AFullFile, ALine, AReminder);
end;
function ParseNewThreadLocation(AnInput: String; out AnId: Integer; out
AnIsCurrent: Boolean; out AName: String; out AnAddr, AnStack,
AnFrame: TDBGPtr; out AFuncName: String; out AnArgs: TStringList; out AFile,
AFullFile: String; out ALine: Integer; out AReminder: String): Boolean;
var
found: TStringArray;
begin
AnIsCurrent := (Length(AnInput) > 1) and (AnInput[1] = '*');
if AnIsCurrent then AnInput[1] := ' ';
if StrMatches(AnInput, [' thread #'{id}, ': tid='{tid}, ': '{}, ''], found) then begin
AnId := StrToIntDef(found[0], -1);
AName := found[1];
AnInput := found[2];
end
else begin
AnId := -1;
AName := '';
AnInput := '';
end;
Result := ParseFrameOrThread(AnInput, AnAddr, AnStack, AnFrame,
AFuncName, AnArgs, AFile, AFullFile, ALine, AReminder);
end;
end.
|