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
|
unit DebugAttachDialog;
{$mode objfpc}{$H+}
{$ifdef darwin}
{$modeswitch ObjectiveC1}
{$endif}
interface
uses
Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls, ComCtrls,
LCLType, LazFileUtils, DbgIntfDebuggerBase,
LazarusIDEStrConsts, BaseDebugManager, Debugger;
type
{$IFDEF darwin}
TMyDummyObcCClass = objcclass(NSObject)
// dummy class to get rid of FPC messages unit objcbase not used
b: BOOL;
end;
{$ENDIF}
{ TDebugAttachDialogForm }
TDebugAttachDialogForm = class(TForm)
btnRefresh: TButton;
btnAttach: TButton;
btnCancel: TButton;
labelRunningProcesses: TLabel;
lvProcesses: TListView;
procedure btnRefreshClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure lvProcessesColumnClick(Sender: TObject; Column: TListColumn);
procedure lvProcessesData(Sender: TObject; Item: TListItem);
procedure lvProcessesDblClick(Sender: TObject);
procedure lvProcessesKeyDown(Sender: TObject; var Key: Word;
{%H-}Shift: TShiftState);
procedure lvProcessesSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
private
FPidString: string;
FList: TRunningProcessInfoList;
// Must return chosen process id as string in PidString and mrOk as result
// on success.
function ChooseProcess(AList: TRunningProcessInfoList; out PidString: string): TModalResult;
public
FSortColumn: Integer;
FSortBackward: Boolean;
end;
var
DebugAttachDialogForm: TDebugAttachDialogForm;
// Ask user for Process ID to attach to and returns it in a string form.
function GetPidForAttach: string;
implementation
{$ifdef windows}
uses
Windows
{$ifndef WIN9XPLATFORM}
,JwaTlHelp32
{$endif};
// Enumerate running processes.
// Result must be always set: True if enumeration supported or False otherwise.
// If AList is not nil it must be filled with TRunningProcessInfo items.
function EnumerateProcesses(AList: TRunningProcessInfoList): boolean;
{$ifndef WIN9XPLATFORM}
var
hShot: HANDLE;
pe: tagPROCESSENTRY32W;
item: TRunningProcessInfo;
{$endif}
begin
{$ifdef WIN9XPLATFORM}
Result := False;
{$else}
Result := True; // we can enumerate processes
if not Assigned(AList) then
Exit;
hShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if hShot = INVALID_HANDLE_VALUE then
Exit;
try
FillByte(pe{%H-}, SizeOf(pe), 0);
pe.dwSize := SizeOf(pe);
if Process32FirstW(hShot, pe) then
repeat
item := TRunningProcessInfo.Create(pe.th32ProcessID, pe.szExeFile);
AList.Add(item);
until not Process32NextW(hShot, pe);
finally
CloseHandle(hShot);
end;
{$endif}
end;
{$else}
{$ifdef linux}
uses
LazUTF8Classes;
function EnumerateProcesses(AList: TRunningProcessInfoList): boolean;
function GetProcName(Pid: THandle): String;
var
S: TStream;
Sz: Integer;
begin
S := TFileStreamUTF8.Create('/proc/' + IntToStr(Pid) + '/cmdline', fmOpenRead or fmShareDenyNone);
try
SetLength(Result, 255);
Sz := S.Read(Result[1], 255);
SetLength(Result, Sz);
finally
S.Free;
end;
end;
var
Rec: TSearchRec;
ProcName: String;
Pid: THandle;
Code: Integer;
item: TRunningProcessInfo;
begin
Result := True;
if not Assigned(AList) then
Exit;
if FindFirstUTF8('/proc/*', faDirectory, Rec) = 0 then
begin
repeat
Val(Rec.Name, Pid, Code);
if (Code = 0) then
begin
ProcName := GetProcName(Pid);
item := TRunningProcessInfo.Create(Pid, ProcName);
AList.Add(item);
end;
until FindNextUTF8(Rec) <> 0;
end;
FindCloseUTF8(Rec);
end;
{$else}
{$ifdef darwin}
uses
MacOSAll, CocoaAll;
function CFStringToStr(AString: CFStringRef; Encoding: CFStringEncoding = kCFStringEncodingUTF8): String;
var
Str: Pointer;
StrSize: CFIndex;
StrRange: CFRange;
begin
if AString = nil then
begin
Result := '';
Exit;
end;
// Try the quick way first
Str := CFStringGetCStringPtr(AString, Encoding);
if Str <> nil then
Result := PChar(Str)
else
begin
// if that doesn't work this will
StrRange.location := 0;
StrRange.length := CFStringGetLength(AString);
StrSize:=0;
CFStringGetBytes(AString, StrRange, Encoding,
Ord('?'), False, nil, 0, StrSize);
SetLength(Result, StrSize);
if StrSize > 0 then
CFStringGetBytes(AString, StrRange, Encoding,
Ord('?'), False, @Result[1], StrSize, StrSize);
end;
end;
function EnumerateProcesses(AList: TRunningProcessInfoList): boolean;
var
Arr: NSArray;
App: NSRunningApplication;
I: Integer;
item: TRunningProcessInfo;
begin
Result := True; // we can enumerate processes
if not Assigned(AList) then
Exit;
// If it is not possible to get the process-list from the debugger,
// use NSRunningApplication as fallback method. This list is not complete,
// though. But better then nothing.
Arr := NSWorkspace.sharedWorkspace.runningApplications;
for I := 0 to Arr.count - 1 do
begin
App := NSRunningApplication(Arr.objectAtIndex(I));
item := TRunningProcessInfo.Create(App.processIdentifier, CFStringToStr(CFStringRef(App.localizedName)));
AList.Add(item);
end;
end;
{$else}
function EnumerateProcesses(AList: TRunningProcessInfoList): boolean;
begin
Result := False;
end;
{$endif}
{$endif}
{$endif}
function GetPidForAttach: string;
var
ProcessList: TRunningProcessInfoList;
begin
Result := '';
ProcessList := TRunningProcessInfoList.Create(True);
try
// Check if we can enumerate processes.
if not DebugBoss.FillProcessList(ProcessList) then
if not EnumerateProcesses(ProcessList) then
begin
// If we can't just ask PID as string.
InputQuery(rsAttachTo, rsEnterPID, Result);
Exit;
end;
// Enumerate.
DebugAttachDialogForm := TDebugAttachDialogForm.Create(nil);
try
if DebugAttachDialogForm.ChooseProcess(ProcessList, Result) <> mrOK then
Result := '';
finally
FreeAndNil(DebugAttachDialogForm);
end;
finally
FreeAndNil(ProcessList);
end;
end;
{$R *.lfm}
{ TDebugAttachDialogForm }
procedure TDebugAttachDialogForm.lvProcessesData(Sender: TObject;
Item: TListItem);
var
info: TRunningProcessInfo;
begin
if Item.Index <> -1 then
begin
info := TRunningProcessInfo(FList.Items[Item.Index]);
Item.Caption := info.ImageName;
Item.SubItems.Add(IntToStr(info.PID));
end;
end;
procedure TDebugAttachDialogForm.lvProcessesDblClick(Sender: TObject);
begin
if lvProcesses.ItemIndex <> -1 then
ModalResult := mrOK;
end;
procedure TDebugAttachDialogForm.lvProcessesKeyDown(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
case Key of
VK_RETURN:
ModalResult := mrOK;
VK_ESCAPE:
ModalResult := mrCancel;
end;
end;
procedure TDebugAttachDialogForm.lvProcessesSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
var
info: TRunningProcessInfo;
begin
if (Item.Index <> -1) And Selected then
begin
info := TRunningProcessInfo(FList.Items[Item.Index]);
FPidString := IntToStr(info.PID);
btnAttach.Enabled := True;
end;
end;
function CompareListItems(Item1, Item2: Pointer): Integer;
begin
case DebugAttachDialogForm.FSortColumn of
0: Result := AnsiStrComp(pchar(TRunningProcessInfo(Item1).ImageName),
pchar(TRunningProcessInfo(Item2).ImageName));
1: Result := integer(int64(TRunningProcessInfo(Item1).PID) -
int64(TRunningProcessInfo(Item2).PID));
else Result := 0;
end;
if DebugAttachDialogForm.FSortBackward then
Result := -Result;
end;
procedure TDebugAttachDialogForm.lvProcessesColumnClick(Sender: TObject;
Column: TListColumn);
begin
if FSortColumn = Column.Index then
FSortBackward := not FSortBackward
else
FSortBackward := False;
FSortColumn := Column.Index;
if FSortColumn >= 0 then
FList.Sort(@CompareListItems);
lvProcesses.Items.Clear;
lvProcesses.Items.Count := FList.Count;
end;
procedure TDebugAttachDialogForm.btnRefreshClick(Sender: TObject);
begin
lvProcesses.Items.Clear;
FSortColumn := -1;
FList.Clear;
if not DebugBoss.FillProcessList(FList)
then
EnumerateProcesses(FList);
lvProcesses.Items.Count := FList.Count;
end;
procedure TDebugAttachDialogForm.FormCreate(Sender: TObject);
begin
Caption:=rsAttachTo;
labelRunningProcesses.Caption:=lisDADRunningProcesses;
lvProcesses.Column[0].Caption:=lisDADImageName;
lvProcesses.Column[1].Caption:=lisDADPID;
btnRefresh.Caption:=dlgUnitDepRefresh;
btnAttach.Caption:=lisDADAttach;
btnCancel.Caption:=lisCancel;
end;
function TDebugAttachDialogForm.ChooseProcess(AList: TRunningProcessInfoList;
out PidString: string): TModalResult;
begin
FPidString := '';
FList := AList;
FSortColumn := -1;
lvProcesses.Items.Count := AList.Count;
Result := ShowModal;
if Result = mrOK then
PidString := FPidString;
end;
end.
|