pymobiledevice3 5.0.4__py3-none-any.whl → 7.0.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. misc/understanding_idevice_protocol_layers.md +10 -5
  2. pymobiledevice3/__main__.py +171 -46
  3. pymobiledevice3/_version.py +2 -2
  4. pymobiledevice3/bonjour.py +22 -21
  5. pymobiledevice3/cli/activation.py +24 -22
  6. pymobiledevice3/cli/afc.py +49 -41
  7. pymobiledevice3/cli/amfi.py +13 -18
  8. pymobiledevice3/cli/apps.py +71 -65
  9. pymobiledevice3/cli/backup.py +134 -93
  10. pymobiledevice3/cli/bonjour.py +31 -29
  11. pymobiledevice3/cli/cli_common.py +175 -232
  12. pymobiledevice3/cli/companion_proxy.py +12 -12
  13. pymobiledevice3/cli/crash.py +95 -52
  14. pymobiledevice3/cli/developer/__init__.py +62 -0
  15. pymobiledevice3/cli/developer/accessibility/__init__.py +65 -0
  16. pymobiledevice3/cli/developer/accessibility/settings.py +43 -0
  17. pymobiledevice3/cli/developer/arbitration.py +50 -0
  18. pymobiledevice3/cli/developer/condition.py +33 -0
  19. pymobiledevice3/cli/developer/core_device.py +294 -0
  20. pymobiledevice3/cli/developer/debugserver.py +244 -0
  21. pymobiledevice3/cli/developer/dvt/__init__.py +438 -0
  22. pymobiledevice3/cli/developer/dvt/core_profile_session.py +295 -0
  23. pymobiledevice3/cli/developer/dvt/simulate_location.py +56 -0
  24. pymobiledevice3/cli/developer/dvt/sysmon/__init__.py +69 -0
  25. pymobiledevice3/cli/developer/dvt/sysmon/process.py +188 -0
  26. pymobiledevice3/cli/developer/fetch_symbols.py +108 -0
  27. pymobiledevice3/cli/developer/simulate_location.py +51 -0
  28. pymobiledevice3/cli/diagnostics/__init__.py +75 -0
  29. pymobiledevice3/cli/diagnostics/battery.py +47 -0
  30. pymobiledevice3/cli/idam.py +42 -0
  31. pymobiledevice3/cli/lockdown.py +70 -75
  32. pymobiledevice3/cli/mounter.py +99 -57
  33. pymobiledevice3/cli/notification.py +38 -26
  34. pymobiledevice3/cli/pcap.py +36 -20
  35. pymobiledevice3/cli/power_assertion.py +15 -16
  36. pymobiledevice3/cli/processes.py +11 -17
  37. pymobiledevice3/cli/profile.py +120 -75
  38. pymobiledevice3/cli/provision.py +27 -26
  39. pymobiledevice3/cli/remote.py +109 -100
  40. pymobiledevice3/cli/restore.py +134 -129
  41. pymobiledevice3/cli/springboard.py +50 -50
  42. pymobiledevice3/cli/syslog.py +145 -65
  43. pymobiledevice3/cli/usbmux.py +66 -27
  44. pymobiledevice3/cli/version.py +2 -5
  45. pymobiledevice3/cli/webinspector.py +232 -156
  46. pymobiledevice3/exceptions.py +6 -2
  47. pymobiledevice3/lockdown.py +5 -1
  48. pymobiledevice3/lockdown_service_provider.py +5 -0
  49. pymobiledevice3/remote/remote_service_discovery.py +18 -10
  50. pymobiledevice3/restore/device.py +28 -4
  51. pymobiledevice3/restore/restore.py +2 -2
  52. pymobiledevice3/service_connection.py +15 -12
  53. pymobiledevice3/services/afc.py +731 -220
  54. pymobiledevice3/services/device_link.py +45 -31
  55. pymobiledevice3/services/idam.py +20 -0
  56. pymobiledevice3/services/lockdown_service.py +12 -9
  57. pymobiledevice3/services/mobile_config.py +1 -0
  58. pymobiledevice3/services/mobilebackup2.py +6 -3
  59. pymobiledevice3/services/os_trace.py +97 -55
  60. pymobiledevice3/services/remote_fetch_symbols.py +13 -8
  61. pymobiledevice3/services/screenshot.py +2 -2
  62. pymobiledevice3/services/web_protocol/alert.py +8 -8
  63. pymobiledevice3/services/web_protocol/automation_session.py +87 -79
  64. pymobiledevice3/services/web_protocol/cdp_screencast.py +2 -1
  65. pymobiledevice3/services/web_protocol/driver.py +71 -70
  66. pymobiledevice3/services/web_protocol/element.py +58 -56
  67. pymobiledevice3/services/web_protocol/selenium_api.py +47 -47
  68. pymobiledevice3/services/web_protocol/session_protocol.py +3 -2
  69. pymobiledevice3/services/web_protocol/switch_to.py +23 -19
  70. pymobiledevice3/services/webinspector.py +42 -67
  71. {pymobiledevice3-5.0.4.dist-info → pymobiledevice3-7.0.6.dist-info}/METADATA +5 -3
  72. {pymobiledevice3-5.0.4.dist-info → pymobiledevice3-7.0.6.dist-info}/RECORD +76 -61
  73. pymobiledevice3/cli/completions.py +0 -50
  74. pymobiledevice3/cli/developer.py +0 -1539
  75. pymobiledevice3/cli/diagnostics.py +0 -110
  76. {pymobiledevice3-5.0.4.dist-info → pymobiledevice3-7.0.6.dist-info}/WHEEL +0 -0
  77. {pymobiledevice3-5.0.4.dist-info → pymobiledevice3-7.0.6.dist-info}/entry_points.txt +0 -0
  78. {pymobiledevice3-5.0.4.dist-info → pymobiledevice3-7.0.6.dist-info}/licenses/LICENSE +0 -0
  79. {pymobiledevice3-5.0.4.dist-info → pymobiledevice3-7.0.6.dist-info}/top_level.txt +0 -0
@@ -1,1539 +0,0 @@
1
- import asyncio
2
- import contextlib
3
- import json
4
- import logging
5
- import os
6
- import plistlib
7
- import posixpath
8
- import shlex
9
- import signal
10
- import struct
11
- import subprocess
12
- import sys
13
- import time
14
- from collections import namedtuple
15
- from dataclasses import asdict
16
- from datetime import datetime
17
- from pathlib import Path
18
- from typing import IO, Optional
19
-
20
- import click
21
- from click.exceptions import MissingParameter, UsageError
22
- from packaging.version import Version
23
- from plumbum import local
24
- from pykdebugparser.pykdebugparser import PyKdebugParser
25
-
26
- import pymobiledevice3
27
- from pymobiledevice3.cli.cli_common import (
28
- BASED_INT,
29
- Command,
30
- RSDCommand,
31
- default_json_encoder,
32
- print_json,
33
- user_requested_colored_output,
34
- )
35
- from pymobiledevice3.exceptions import (
36
- DeviceAlreadyInUseError,
37
- DvtDirListError,
38
- ExtractingStackshotError,
39
- RSDRequiredError,
40
- UnrecognizedSelectorError,
41
- )
42
- from pymobiledevice3.lockdown import LockdownClient, create_using_usbmux
43
- from pymobiledevice3.lockdown_service_provider import LockdownServiceProvider
44
- from pymobiledevice3.osu.os_utils import get_os_utils
45
- from pymobiledevice3.remote.core_device.app_service import AppServiceService
46
- from pymobiledevice3.remote.core_device.device_info import DeviceInfoService
47
- from pymobiledevice3.remote.core_device.diagnostics_service import DiagnosticsServiceService
48
- from pymobiledevice3.remote.core_device.file_service import APPLE_DOMAIN_DICT, FileServiceService
49
- from pymobiledevice3.remote.remote_service_discovery import RemoteServiceDiscoveryService
50
- from pymobiledevice3.services.accessibilityaudit import AccessibilityAudit
51
- from pymobiledevice3.services.crash_reports import CrashReportsManager
52
- from pymobiledevice3.services.debugserver_applist import DebugServerAppList
53
- from pymobiledevice3.services.device_arbitration import DtDeviceArbitration
54
- from pymobiledevice3.services.dtfetchsymbols import DtFetchSymbols
55
- from pymobiledevice3.services.dvt.dvt_secure_socket_proxy import DvtSecureSocketProxyService
56
- from pymobiledevice3.services.dvt.instruments.activity_trace_tap import ActivityTraceTap, decode_message_format
57
- from pymobiledevice3.services.dvt.instruments.application_listing import ApplicationListing
58
- from pymobiledevice3.services.dvt.instruments.condition_inducer import ConditionInducer
59
- from pymobiledevice3.services.dvt.instruments.core_profile_session_tap import CoreProfileSessionTap
60
- from pymobiledevice3.services.dvt.instruments.device_info import DeviceInfo
61
- from pymobiledevice3.services.dvt.instruments.energy_monitor import EnergyMonitor
62
- from pymobiledevice3.services.dvt.instruments.graphics import Graphics
63
- from pymobiledevice3.services.dvt.instruments.location_simulation import LocationSimulation
64
- from pymobiledevice3.services.dvt.instruments.network_monitor import ConnectionDetectionEvent, NetworkMonitor
65
- from pymobiledevice3.services.dvt.instruments.notifications import Notifications
66
- from pymobiledevice3.services.dvt.instruments.process_control import ProcessControl
67
- from pymobiledevice3.services.dvt.instruments.screenshot import Screenshot
68
- from pymobiledevice3.services.dvt.instruments.sysmontap import Sysmontap
69
- from pymobiledevice3.services.dvt.testmanaged.xcuitest import XCUITestService
70
- from pymobiledevice3.services.installation_proxy import InstallationProxyService
71
- from pymobiledevice3.services.remote_fetch_symbols import RemoteFetchSymbolsService
72
- from pymobiledevice3.services.remote_server import RemoteServer
73
- from pymobiledevice3.services.screenshot import ScreenshotService
74
- from pymobiledevice3.services.simulate_location import DtSimulateLocation
75
- from pymobiledevice3.tcp_forwarder import LockdownTcpForwarder
76
- from pymobiledevice3.utils import try_decode
77
-
78
- OSUTILS = get_os_utils()
79
- BSC_SUBCLASS = 0x40C
80
- BSC_CLASS = 0x4
81
- VFS_AND_TRACES_SET = {0x03010000, 0x07FF0000}
82
- DEBUGSERVER_CONNECTION_STEPS = """
83
- Follow the following connections steps from LLDB:
84
-
85
- (lldb) platform select remote-ios
86
- (lldb) target create /path/to/local/application.app
87
- (lldb) script lldb.target.module[0].SetPlatformFileSpec(lldb.SBFileSpec('/private/var/containers/Bundle/Application/<APP-UUID>/application.app'))
88
- (lldb) process connect connect://[{host}]:{port} <-- ACTUAL CONNECTION DETAILS!
89
- (lldb) process launch
90
- """
91
-
92
- MatchedProcessByPid = namedtuple("MatchedProcess", "name pid")
93
-
94
- logger = logging.getLogger(__name__)
95
-
96
-
97
- @click.group()
98
- def cli() -> None:
99
- pass
100
-
101
-
102
- @cli.group()
103
- def developer() -> None:
104
- """
105
- Perform developer operations (Requires enable of Developer-Mode)
106
-
107
- These options require the DeveloperDiskImage.dmg to be mounted on the device prior
108
- to execution. You can achieve this using:
109
-
110
- pymobiledevice3 mounter mount
111
-
112
- Also, starting at iOS 17.0, a tunnel must be created to the device for the services
113
- to be accessible. Therefore, every CLI command is retried with a `--tunnel` option
114
- for implicitly accessing tunneld when necessary
115
- """
116
- pass
117
-
118
-
119
- @developer.command("shell", cls=Command)
120
- @click.argument("service")
121
- @click.option("-r", "--remove-ssl-context", is_flag=True)
122
- def developer_shell(service_provider: LockdownClient, service, remove_ssl_context):
123
- """Launch developer IPython shell (used for pymobiledevice3 R&D)"""
124
- with RemoteServer(service_provider, service, remove_ssl_context) as service:
125
- service.shell()
126
-
127
-
128
- @developer.group()
129
- def dvt() -> None:
130
- """Access advanced instrumentation APIs"""
131
- pass
132
-
133
-
134
- @dvt.command("proclist", cls=Command)
135
- def proclist(service_provider: LockdownClient) -> None:
136
- """Show process list"""
137
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
138
- processes = DeviceInfo(dvt).proclist()
139
- for process in processes:
140
- if "startDate" in process:
141
- process["startDate"] = str(process["startDate"])
142
-
143
- print_json(processes)
144
-
145
-
146
- @dvt.command("is-running-pid", cls=Command)
147
- @click.argument("pid", type=click.INT)
148
- def is_running_pid(service_provider: LockdownClient, pid: int) -> None:
149
- """Simple check if PID is running"""
150
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
151
- print_json(DeviceInfo(dvt).is_running_pid(pid))
152
-
153
-
154
- @dvt.command("memlimitoff", cls=Command)
155
- @click.argument("pid", type=click.INT)
156
- def memlimitoff(service_provider: LockdownServiceProvider, pid: int) -> None:
157
- """Disable process memory limit"""
158
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
159
- ProcessControl(dvt).disable_memory_limit_for_pid(pid)
160
-
161
-
162
- @dvt.command("applist", cls=Command)
163
- def applist(service_provider: LockdownServiceProvider) -> None:
164
- """Show application list"""
165
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
166
- apps = ApplicationListing(dvt).applist()
167
- print_json(apps)
168
-
169
-
170
- @dvt.command("signal", cls=Command)
171
- @click.argument("pid", type=click.INT)
172
- @click.argument("sig", type=click.INT, required=False)
173
- @click.option("-s", "--signal-name", type=click.Choice([s.name for s in signal.Signals]))
174
- def send_signal(service_provider, pid, sig, signal_name) -> None:
175
- """Send a signal to process by its PID"""
176
- if not sig and not signal_name:
177
- raise MissingParameter(param_type="argument|option", param_hint="'SIG|SIGNAL-NAME'")
178
- if sig and signal_name:
179
- raise UsageError(message="Cannot give SIG and SIGNAL-NAME together")
180
- sig = sig or signal.Signals[signal_name].value
181
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
182
- ProcessControl(dvt).signal(pid, sig)
183
-
184
-
185
- @dvt.command("kill", cls=Command)
186
- @click.argument("pid", type=click.INT)
187
- def kill(service_provider: LockdownClient, pid) -> None:
188
- """Kill a process by its pid."""
189
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
190
- ProcessControl(dvt).kill(pid)
191
-
192
-
193
- @dvt.command(cls=Command)
194
- @click.argument("app_bundle_identifier")
195
- def process_id_for_bundle_id(service_provider: LockdownServiceProvider, app_bundle_identifier: str) -> None:
196
- """Get PID of a bundle identifier (only returns a valid value if its running)."""
197
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
198
- print(ProcessControl(dvt).process_identifier_for_bundle_identifier(app_bundle_identifier))
199
-
200
-
201
- def get_matching_processes(
202
- service_provider: LockdownServiceProvider, name: Optional[str] = None, bundle_identifier: Optional[str] = None
203
- ) -> list[MatchedProcessByPid]:
204
- result = []
205
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
206
- device_info = DeviceInfo(dvt)
207
- for process in device_info.proclist():
208
- current_name = process.get("name")
209
- current_bundle_identifier = process.get("bundleIdentifier", "")
210
- pid = process["pid"]
211
- if (bundle_identifier is not None and bundle_identifier in current_bundle_identifier) or (
212
- name is not None and name in current_name
213
- ):
214
- result.append(MatchedProcessByPid(name=current_name, pid=pid))
215
- return result
216
-
217
-
218
- @dvt.command("pkill", cls=Command)
219
- @click.argument("expression")
220
- @click.option("--bundle", is_flag=True, help="Treat given expression as a bundle-identifier instead of a process name")
221
- def pkill(service_provider: LockdownServiceProvider, expression: str, bundle: False) -> None:
222
- """kill all processes containing `expression` in their name."""
223
- matching_name = expression if not bundle else None
224
- matching_bundle_identifier = expression if bundle else None
225
- matching_processes = get_matching_processes(
226
- service_provider, name=matching_name, bundle_identifier=matching_bundle_identifier
227
- )
228
-
229
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
230
- process_control = ProcessControl(dvt)
231
-
232
- for process in matching_processes:
233
- logger.info(f"killing {process.name}({process.pid})")
234
- process_control.kill(process.pid)
235
-
236
-
237
- @dvt.command("launch", cls=Command)
238
- @click.argument("arguments", type=click.STRING)
239
- @click.option(
240
- "--kill-existing/--no-kill-existing", default=True, help="Whether to kill an existing instance of this process"
241
- )
242
- @click.option("--suspended", is_flag=True, help="Same as WaitForDebugger")
243
- @click.option(
244
- "--env",
245
- multiple=True,
246
- type=click.Tuple((str, str)),
247
- help="Environment variables to pass to process given as a list of key value",
248
- )
249
- @click.option("--stream", is_flag=True)
250
- def launch(
251
- service_provider: LockdownClient, arguments: str, kill_existing: bool, suspended: bool, env: tuple, stream: bool
252
- ) -> None:
253
- """Launch a process."""
254
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
255
- parsed_arguments = shlex.split(arguments)
256
- process_control = ProcessControl(dvt)
257
- pid = process_control.launch(
258
- bundle_id=parsed_arguments[0],
259
- arguments=parsed_arguments[1:],
260
- kill_existing=kill_existing,
261
- start_suspended=suspended,
262
- environment=dict(env),
263
- )
264
- print(f"Process launched with pid {pid}")
265
- while stream:
266
- for output_received in process_control:
267
- logging.getLogger(f"PID:{output_received.pid}").info(output_received.message.strip())
268
-
269
-
270
- @dvt.command("shell", cls=Command)
271
- def dvt_shell(service_provider: LockdownClient):
272
- """Launch developer shell (used for pymobiledevice3 R&D)"""
273
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
274
- dvt.shell()
275
-
276
-
277
- def show_dirlist(device_info: DeviceInfo, dirname, recursive=False):
278
- try:
279
- filenames = device_info.ls(dirname)
280
- except DvtDirListError:
281
- return
282
-
283
- for filename in filenames:
284
- filename = posixpath.join(dirname, filename)
285
- print(filename)
286
- if recursive:
287
- show_dirlist(device_info, filename, recursive=recursive)
288
-
289
-
290
- @dvt.command("ls", cls=Command)
291
- @click.argument("path", type=click.Path(exists=False, readable=False))
292
- @click.option("-r", "--recursive", is_flag=True)
293
- def ls(service_provider: LockdownClient, path, recursive):
294
- """List directory"""
295
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
296
- show_dirlist(DeviceInfo(dvt), path, recursive=recursive)
297
-
298
-
299
- @dvt.command("device-information", cls=Command)
300
- def device_information(service_provider: LockdownClient):
301
- """Print system information"""
302
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
303
- device_info = DeviceInfo(dvt)
304
- info = {
305
- "hardware": device_info.hardware_information(),
306
- "network": device_info.network_information(),
307
- "kernel-name": device_info.mach_kernel_name(),
308
- "kpep-database": device_info.kpep_database(),
309
- }
310
- with contextlib.suppress(UnrecognizedSelectorError):
311
- info["system"] = device_info.system_information()
312
- print_json(info)
313
-
314
-
315
- @dvt.command("netstat", cls=Command)
316
- def netstat(service_provider: LockdownClient):
317
- """Print information about current network activity."""
318
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt, NetworkMonitor(dvt) as monitor:
319
- for event in monitor:
320
- if isinstance(event, ConnectionDetectionEvent):
321
- logger.info(
322
- f"Connection detected: {event.local_address.data.address}:{event.local_address.port} -> "
323
- f"{event.remote_address.data.address}:{event.remote_address.port}"
324
- )
325
-
326
-
327
- @dvt.command("screenshot", cls=Command)
328
- @click.argument("out", type=click.File("wb"))
329
- def dvt_screenshot(service_provider: LockdownClient, out):
330
- """Take device screenshot"""
331
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
332
- out.write(Screenshot(dvt).get_screenshot())
333
-
334
-
335
- @dvt.command("xcuitest", cls=Command)
336
- @click.argument("bundle-id")
337
- def xcuitest(service_provider: LockdownClient, bundle_id: str) -> None:
338
- """\b
339
- Start XCUITest
340
-
341
- Usage example:
342
- python3 -m pymobiledevice3 developer dvt xcuitest com.facebook.WebDriverAgentRunner.xctrunner
343
- """
344
- XCUITestService(service_provider).run(bundle_id)
345
-
346
-
347
- @dvt.group("sysmon")
348
- def sysmon():
349
- """System monitor options."""
350
-
351
-
352
- @sysmon.group("process")
353
- def sysmon_process():
354
- """Process monitor options."""
355
-
356
-
357
- @sysmon_process.command("monitor", cls=Command)
358
- @click.argument("threshold", type=click.FLOAT)
359
- def sysmon_process_monitor(service_provider: LockdownClient, threshold):
360
- """monitor all most consuming processes by given cpuUsage threshold."""
361
-
362
- Process = namedtuple("process", "pid name cpuUsage physFootprint")
363
-
364
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt, Sysmontap(dvt) as sysmon:
365
- for process_snapshot in sysmon.iter_processes():
366
- entries = []
367
- for process in process_snapshot:
368
- if (process["cpuUsage"] is not None) and (process["cpuUsage"] >= threshold):
369
- entries.append(
370
- Process(
371
- pid=process["pid"],
372
- name=process["name"],
373
- cpuUsage=process["cpuUsage"],
374
- physFootprint=process["physFootprint"],
375
- )
376
- )
377
-
378
- logger.info(entries)
379
-
380
-
381
- @sysmon_process.command("single", cls=Command)
382
- @click.option("-a", "--attributes", multiple=True, help="filter processes by given attribute value given as key=value")
383
- def sysmon_process_single(service_provider: LockdownClient, attributes: list[str]):
384
- """show a single snapshot of currently running processes."""
385
-
386
- count = 0
387
-
388
- result = []
389
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
390
- device_info = DeviceInfo(dvt)
391
-
392
- with Sysmontap(dvt) as sysmon:
393
- for process_snapshot in sysmon.iter_processes():
394
- count += 1
395
-
396
- if count < 2:
397
- # first sample doesn't contain an initialized value for cpuUsage
398
- continue
399
-
400
- for process in process_snapshot:
401
- skip = False
402
- if attributes is not None:
403
- for filter_attr in attributes:
404
- filter_attr, filter_value = filter_attr.split("=")
405
- if str(process[filter_attr]) != filter_value:
406
- skip = True
407
- break
408
-
409
- if skip:
410
- continue
411
-
412
- # adding "artificially" the execName field
413
- process["execName"] = device_info.execname_for_pid(process["pid"])
414
- result.append(process)
415
-
416
- # exit after single snapshot
417
- break
418
- print_json(result)
419
-
420
-
421
- @sysmon.command("system", cls=Command)
422
- @click.option("-f", "--fields", help='field names splitted by ",".')
423
- def sysmon_system(service_provider: LockdownClient, fields):
424
- """show current system stats."""
425
-
426
- if fields is not None:
427
- fields = fields.split(",")
428
-
429
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
430
- sysmontap = Sysmontap(dvt)
431
- with sysmontap as sysmon:
432
- system = None
433
- system_usage = None
434
- system_usage_seen = False # Tracks if the first occurrence of SystemCPUUsage
435
-
436
- for row in sysmon:
437
- if "System" in row and system is None:
438
- system = sysmon.system_attributes_cls(*row["System"])
439
-
440
- if "SystemCPUUsage" in row:
441
- if system_usage_seen:
442
- system_usage = {
443
- **row["SystemCPUUsage"],
444
- **{
445
- "CPUCount": row["CPUCount"],
446
- "EnabledCPUs": row["EnabledCPUs"],
447
- },
448
- }
449
- else: # Ignore the first occurrence because first occurrence always gives a incorrect value - 100 or 0
450
- system_usage_seen = True
451
-
452
- if system and system_usage:
453
- break
454
-
455
- attrs_dict = {**asdict(system), **system_usage}
456
- for name, value in attrs_dict.items():
457
- if (fields is None) or (name in fields):
458
- print(f"{name}: {value}")
459
-
460
-
461
- @dvt.group("core-profile-session")
462
- def core_profile_session():
463
- """Access tailspin features"""
464
-
465
-
466
- bsc_filter = click.option("--bsc/--no-bsc", default=False, help="Whether to print BSC events or not.")
467
- class_filter = click.option(
468
- "-cf", "--class-filters", multiple=True, type=BASED_INT, help="Events class filter. Omit for all."
469
- )
470
- subclass_filter = click.option(
471
- "-sf", "--subclass-filters", multiple=True, type=BASED_INT, help="Events subclass filter. Omit for all."
472
- )
473
-
474
-
475
- def parse_filters(subclasses: list[int], classes: list[int]):
476
- if not subclasses and not classes:
477
- return None
478
- parsed = set()
479
- for subclass in subclasses:
480
- if subclass == BSC_SUBCLASS:
481
- parsed |= VFS_AND_TRACES_SET
482
- parsed.add(subclass << 16)
483
- for class_ in classes:
484
- if class_ == BSC_CLASS:
485
- parsed |= VFS_AND_TRACES_SET
486
- parsed.add((class_ << 24) | 0x00FF0000)
487
- return parsed
488
-
489
-
490
- @core_profile_session.command("live", cls=Command)
491
- @click.option("-c", "--count", type=click.INT, default=-1, help="Number of events to print. Omit to endless sniff.")
492
- @bsc_filter
493
- @class_filter
494
- @subclass_filter
495
- @click.option("--tid", type=click.INT, default=None, help="Thread ID to filter. Omit for all.")
496
- @click.option("--timestamp/--no-timestamp", default=True, help="Whether to print timestamp or not.")
497
- @click.option("--event-name/--no-event-name", default=True, help="Whether to print event name or not.")
498
- @click.option("--func-qual/--no-func-qual", default=True, help="Whether to print function qualifier or not.")
499
- @click.option("--show-tid/--no-show-tid", default=True, help="Whether to print thread id or not.")
500
- @click.option("--process-name/--no-process-name", default=True, help="Whether to print process name or not.")
501
- @click.option("--args/--no-args", default=True, help="Whether to print event arguments or not.")
502
- def live_profile_session(
503
- service_provider: LockdownClient,
504
- count,
505
- bsc,
506
- class_filters,
507
- subclass_filters,
508
- tid,
509
- timestamp,
510
- event_name,
511
- func_qual,
512
- show_tid,
513
- process_name,
514
- args,
515
- ):
516
- """Print kevents received from the device in real time."""
517
-
518
- parser = PyKdebugParser()
519
- parser.filter_class = class_filters
520
- if bsc:
521
- subclass_filters = [*list(subclass_filters), BSC_SUBCLASS]
522
- parser.filter_subclass = subclass_filters
523
- filters = parse_filters(subclass_filters, class_filters)
524
- parser.filter_tid = tid
525
- parser.show_timestamp = timestamp
526
- parser.show_name = event_name
527
- parser.show_func_qual = func_qual
528
- parser.show_tid = show_tid
529
- parser.show_process = process_name
530
- parser.show_args = args
531
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
532
- trace_codes_map = DeviceInfo(dvt).trace_codes()
533
- time_config = CoreProfileSessionTap.get_time_config(dvt)
534
- parser.numer = time_config["numer"]
535
- parser.denom = time_config["denom"]
536
- parser.mach_absolute_time = time_config["mach_absolute_time"]
537
- parser.usecs_since_epoch = time_config["usecs_since_epoch"]
538
- parser.timezone = time_config["timezone"]
539
- with CoreProfileSessionTap(dvt, time_config, filters) as tap:
540
- for i, event in enumerate(parser.formatted_kevents(tap.get_kdbuf_stream(), trace_codes_map)):
541
- print(event)
542
- if i == count:
543
- break
544
-
545
-
546
- @core_profile_session.command("save", cls=Command)
547
- @click.argument("out", type=click.File("wb"))
548
- @bsc_filter
549
- @class_filter
550
- @subclass_filter
551
- def save_profile_session(service_provider: LockdownClient, out, bsc, class_filters, subclass_filters):
552
- """Dump core profiling information."""
553
- if bsc:
554
- subclass_filters = [*list(subclass_filters), BSC_SUBCLASS]
555
- filters = parse_filters(subclass_filters, class_filters)
556
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt, CoreProfileSessionTap(dvt, {}, filters) as tap:
557
- tap.dump(out)
558
-
559
-
560
- @core_profile_session.command("stackshot", cls=Command)
561
- @click.option("--out", type=click.File("w"), default=None)
562
- def stackshot(service_provider: LockdownClient, out):
563
- """Dump stackshot information."""
564
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt, CoreProfileSessionTap(dvt, {}) as tap:
565
- try:
566
- data = tap.get_stackshot()
567
- except ExtractingStackshotError:
568
- logger.exception("Extracting stackshot failed")
569
- return
570
-
571
- if out is not None:
572
- json.dump(data, out, indent=4, default=default_json_encoder)
573
- else:
574
- print_json(data)
575
-
576
-
577
- @core_profile_session.command("parse-live", cls=Command)
578
- @click.option("-c", "--count", type=click.INT, default=-1, help="Number of events to print. Omit to endless sniff.")
579
- @click.option("--tid", type=click.INT, default=None, help="Thread ID to filter. Omit for all.")
580
- @click.option("--show-tid/--no-show-tid", default=False, help="Whether to print thread id or not.")
581
- @bsc_filter
582
- @class_filter
583
- @subclass_filter
584
- @click.option("--process", default=None, help="Process ID / name to filter. Omit for all.")
585
- def parse_live_profile_session(
586
- service_provider: LockdownClient, count, tid, show_tid, bsc, class_filters, subclass_filters, process
587
- ):
588
- """Print traces (syscalls, thread events, etc.) received from the device in real time."""
589
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
590
- print("Receiving time information")
591
- time_config = CoreProfileSessionTap.get_time_config(dvt)
592
- parser = PyKdebugParser()
593
- parser.filter_class = list(class_filters)
594
- if bsc:
595
- subclass_filters = [*list(subclass_filters), BSC_SUBCLASS]
596
- parser.filter_subclass = subclass_filters
597
- filters = parse_filters(subclass_filters, class_filters)
598
- parser.numer = time_config["numer"]
599
- parser.denom = time_config["denom"]
600
- parser.mach_absolute_time = time_config["mach_absolute_time"]
601
- parser.usecs_since_epoch = time_config["usecs_since_epoch"]
602
- parser.timezone = time_config["timezone"]
603
- parser.filter_tid = tid
604
- parser.filter_process = process
605
- parser.show_tid = show_tid
606
- parser.color = user_requested_colored_output()
607
-
608
- with CoreProfileSessionTap(dvt, time_config, filters) as tap:
609
- if show_tid:
610
- print("{:^32}|{:^11}|{:^33}| Event".format("Time", "Thread", "Process"))
611
- else:
612
- print("{:^32}|{:^33}| Event".format("Time", "Process"))
613
-
614
- for i, trace in enumerate(parser.formatted_traces(tap.get_kdbuf_stream())):
615
- print(trace, flush=True)
616
- if i == count:
617
- break
618
-
619
-
620
- def get_image_name(dsc_uuid_map, image_uuid, current_dsc_map):
621
- if not current_dsc_map:
622
- for dsc_mapping in dsc_uuid_map.values():
623
- if image_uuid in dsc_mapping:
624
- current_dsc_map.update(dsc_mapping)
625
-
626
- return current_dsc_map.get(image_uuid, image_uuid)
627
-
628
-
629
- def format_callstack(callstack, dsc_uuid_map, current_dsc_map):
630
- lines = callstack.splitlines()
631
- for i, line in enumerate(lines[1:]):
632
- if ":" in line:
633
- uuid = line.split(":")[0].strip()
634
- lines[i + 1] = line.replace(uuid, get_image_name(dsc_uuid_map, uuid, current_dsc_map))
635
- return "\n".join(lines)
636
-
637
-
638
- @core_profile_session.command("callstacks-live", cls=Command)
639
- @click.option("-c", "--count", type=click.INT, default=-1, help="Number of events to print. Omit to endless sniff.")
640
- @click.option("--process", default=None, help="Process to filter. Omit for all.")
641
- @click.option("--tid", type=click.INT, default=None, help="Thread ID to filter. Omit for all.")
642
- @click.option("--show-tid/--no-show-tid", default=False, help="Whether to print thread id or not.")
643
- def callstacks_live_profile_session(service_provider: LockdownClient, count, process, tid, show_tid):
644
- """Print callstacks received from the device in real time."""
645
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
646
- print("Receiving time information")
647
- time_config = CoreProfileSessionTap.get_time_config(dvt)
648
- parser = PyKdebugParser()
649
- parser.numer = time_config["numer"]
650
- parser.denom = time_config["denom"]
651
- parser.mach_absolute_time = time_config["mach_absolute_time"]
652
- parser.usecs_since_epoch = time_config["usecs_since_epoch"]
653
- parser.timezone = time_config["timezone"]
654
- parser.filter_tid = tid
655
- parser.filter_process = process
656
- parser.color = user_requested_colored_output()
657
- parser.show_tid = show_tid
658
-
659
- with open(os.path.join(pymobiledevice3.__path__[0], "resources", "dsc_uuid_map.json")) as fd:
660
- dsc_uuid_map = json.load(fd)
661
-
662
- current_dsc_map = {}
663
- with CoreProfileSessionTap(dvt, time_config) as tap:
664
- for i, callstack in enumerate(parser.formatted_callstacks(tap.get_kdbuf_stream())):
665
- print(format_callstack(callstack, dsc_uuid_map, current_dsc_map))
666
- if i == count:
667
- break
668
-
669
-
670
- @dvt.command("trace-codes", cls=Command)
671
- def dvt_trace_codes(service_provider: LockdownClient):
672
- """Print KDebug trace codes."""
673
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
674
- device_info = DeviceInfo(dvt)
675
- print_json({hex(k): v for k, v in device_info.trace_codes().items()})
676
-
677
-
678
- @dvt.command("name-for-uid", cls=Command)
679
- @click.argument("uid", type=click.INT)
680
- def dvt_name_for_uid(service_provider: LockdownClient, uid):
681
- """Print the assiciated username for the given uid."""
682
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
683
- device_info = DeviceInfo(dvt)
684
- print(device_info.name_for_uid(uid))
685
-
686
-
687
- @dvt.command("name-for-gid", cls=Command)
688
- @click.argument("gid", type=click.INT)
689
- def dvt_name_for_gid(service_provider: LockdownClient, gid):
690
- """Print the assiciated group name for the given gid."""
691
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
692
- device_info = DeviceInfo(dvt)
693
- print(device_info.name_for_gid(gid))
694
-
695
-
696
- @dvt.command("oslog", cls=Command)
697
- @click.option("--pid", type=click.INT)
698
- def dvt_oslog(service_provider: LockdownClient, pid):
699
- """Sniff device oslog (not very stable, but includes more data and normal syslog)"""
700
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt, ActivityTraceTap(dvt) as tap:
701
- for message in tap:
702
- message_pid = message.process
703
- # without message_type maybe signpost have event_type
704
- message_type = (
705
- message.message_type
706
- if hasattr(message, "message_type")
707
- else message.event_type
708
- if hasattr(message, "event_type")
709
- else "unknown"
710
- )
711
- sender_image_path = message.sender_image_path
712
- image_name = os.path.basename(sender_image_path)
713
- subsystem = message.subsystem
714
- category = message.category
715
- timestamp = datetime.now()
716
-
717
- if pid is not None and message_pid != pid:
718
- continue
719
-
720
- formatted_message = decode_message_format(message.message) if message.message else message.name
721
-
722
- if user_requested_colored_output():
723
- timestamp = click.style(str(timestamp), bold=True)
724
- message_pid = click.style(str(message_pid), "magenta")
725
- subsystem = click.style(subsystem, "green")
726
- category = click.style(category, "green")
727
- image_name = click.style(image_name, "yellow")
728
- message_type = click.style(message_type, "cyan")
729
-
730
- print(
731
- f"[{timestamp}][{subsystem}][{category}][{message_pid}][{image_name}] "
732
- f"<{message_type}>: {formatted_message}"
733
- )
734
-
735
-
736
- @dvt.command("energy", cls=Command)
737
- @click.argument("pid-list", nargs=-1)
738
- def dvt_energy(service_provider: LockdownClient, pid_list):
739
- """Monitor the energy consumption for given PIDs"""
740
-
741
- if len(pid_list) == 0:
742
- logger.error("pid_list must not be empty")
743
- return
744
-
745
- pid_list = [int(pid) for pid in pid_list]
746
-
747
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt, EnergyMonitor(dvt, pid_list) as energy_monitor:
748
- for telemetry in energy_monitor:
749
- logger.info(telemetry)
750
-
751
-
752
- @dvt.command("notifications", cls=Command)
753
- def dvt_notifications(service_provider: LockdownClient):
754
- """Monitor memory and app notifications"""
755
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt, Notifications(dvt) as notifications:
756
- for notification in notifications:
757
- logger.info(notification)
758
-
759
-
760
- @dvt.command("graphics", cls=Command)
761
- def dvt_graphics(service_provider: LockdownClient):
762
- """Monitor graphics-related information"""
763
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt, Graphics(dvt) as graphics:
764
- for stats in graphics:
765
- logger.info(stats)
766
-
767
-
768
- @developer.group("fetch-symbols")
769
- def fetch_symbols():
770
- """Download the DSC (and dyld) from the device"""
771
- pass
772
-
773
-
774
- async def fetch_symbols_list_task(service_provider: LockdownServiceProvider) -> None:
775
- if Version(service_provider.product_version) < Version("17.0"):
776
- print_json(DtFetchSymbols(service_provider).list_files())
777
- else:
778
- if not isinstance(service_provider, RemoteServiceDiscoveryService):
779
- raise RSDRequiredError(service_provider.identifier)
780
-
781
- async with RemoteFetchSymbolsService(service_provider) as fetch_symbols:
782
- print_json([f.file_path for f in await fetch_symbols.get_dsc_file_list()])
783
-
784
-
785
- @fetch_symbols.command("list", cls=Command)
786
- def fetch_symbols_list(service_provider: LockdownServiceProvider) -> None:
787
- """list of files to be downloaded"""
788
- asyncio.run(fetch_symbols_list_task(service_provider), debug=True)
789
-
790
-
791
- async def fetch_symbols_download_task(service_provider: LockdownServiceProvider, out: str) -> None:
792
- out = Path(out)
793
- out.mkdir(parents=True, exist_ok=True)
794
-
795
- if Version(service_provider.product_version) < Version("17.0"):
796
- fetch_symbols = DtFetchSymbols(service_provider)
797
- files = fetch_symbols.list_files()
798
-
799
- downloaded_files = set()
800
-
801
- for i, file in enumerate(files):
802
- if file.startswith("/"):
803
- # trim root to allow relative download
804
- file = file[1:]
805
- file = out / file
806
-
807
- if file not in downloaded_files:
808
- # first time the file was seen in list, means we can safely remove any old copy if any
809
- file.unlink(missing_ok=True)
810
-
811
- downloaded_files.add(file)
812
- file.parent.mkdir(parents=True, exist_ok=True)
813
- with open(file, "ab") as f:
814
- # same file may appear twice, so we'll need to append data into it
815
- logger.info(f"writing to: {file}")
816
- fetch_symbols.get_file(i, f)
817
- else:
818
- if not isinstance(service_provider, RemoteServiceDiscoveryService):
819
- raise RSDRequiredError(service_provider.identifier)
820
- async with RemoteFetchSymbolsService(service_provider) as fetch_symbols:
821
- await fetch_symbols.download(out)
822
-
823
-
824
- @fetch_symbols.command("download", cls=Command)
825
- @click.argument("out", type=click.Path(dir_okay=True, file_okay=False))
826
- def fetch_symbols_download(service_provider: LockdownServiceProvider, out: str) -> None:
827
- """download the linker and dyld cache to a specified directory"""
828
- asyncio.run(fetch_symbols_download_task(service_provider, out), debug=True)
829
-
830
-
831
- @developer.group("simulate-location")
832
- def simulate_location():
833
- """Simulate device location by given input"""
834
- pass
835
-
836
-
837
- @simulate_location.command("clear", cls=Command)
838
- def simulate_location_clear(service_provider: LockdownClient):
839
- """clear simulated location"""
840
- DtSimulateLocation(service_provider).clear()
841
-
842
-
843
- @simulate_location.command("set", cls=Command)
844
- @click.argument("latitude", type=click.FLOAT)
845
- @click.argument("longitude", type=click.FLOAT)
846
- def simulate_location_set(service_provider: LockdownClient, latitude, longitude):
847
- """
848
- set a simulated location.
849
- try:
850
- ... set -- 40.690008 -74.045843 for liberty island
851
- """
852
- DtSimulateLocation(service_provider).set(latitude, longitude)
853
-
854
-
855
- @simulate_location.command("play", cls=Command)
856
- @click.argument("filename", type=click.Path(exists=True, file_okay=True, dir_okay=False))
857
- @click.argument("timing_randomness_range", type=click.INT)
858
- @click.option("--disable-sleep", is_flag=True, default=False)
859
- def simulate_location_play(service_provider: LockdownClient, filename, timing_randomness_range, disable_sleep):
860
- """play a .gpx file"""
861
- DtSimulateLocation(service_provider).play_gpx_file(filename, timing_randomness_range, disable_sleep=disable_sleep)
862
-
863
-
864
- @developer.group("accessibility")
865
- def accessibility():
866
- """Interact with accessibility-related features"""
867
- pass
868
-
869
-
870
- @accessibility.command("run-audit", cls=Command)
871
- @click.argument("test_types", nargs=-1)
872
- def accessibility_run_audit(service_provider: LockdownServiceProvider, test_types):
873
- """runs accessibility audit tests"""
874
- param = list(test_types)
875
- audit_issues = AccessibilityAudit(service_provider).run_audit(param)
876
- print_json([audit_issue.json() for audit_issue in audit_issues], False)
877
-
878
-
879
- @accessibility.command("supported-audit-types", cls=Command)
880
- def accessibility_supported_audit_types(service_provider: LockdownServiceProvider):
881
- """lists supported accessibility audit test types"""
882
- print_json(AccessibilityAudit(service_provider).supported_audits_types())
883
-
884
-
885
- @accessibility.command("capabilities", cls=Command)
886
- def accessibility_capabilities(service_provider: LockdownClient):
887
- """display accessibility capabilities"""
888
- print_json(AccessibilityAudit(service_provider).capabilities)
889
-
890
-
891
- @accessibility.group("settings")
892
- def accessibility_settings():
893
- """accessibility settings."""
894
- pass
895
-
896
-
897
- @accessibility_settings.command("show", cls=Command)
898
- def accessibility_settings_show(service_provider: LockdownClient):
899
- """show current settings"""
900
- for setting in AccessibilityAudit(service_provider).settings:
901
- print(setting)
902
-
903
-
904
- @accessibility_settings.command("set", cls=Command)
905
- @click.argument("setting")
906
- @click.argument("value")
907
- def accessibility_settings_set(service_provider: LockdownClient, setting, value):
908
- """
909
- change current settings
910
-
911
- in order to list all available use the "show" command
912
- """
913
- service = AccessibilityAudit(service_provider)
914
- service.set_setting(setting, eval(value))
915
- OSUTILS.wait_return()
916
-
917
-
918
- @accessibility_settings.command("reset", cls=Command)
919
- def accessibility_settings_reset(service_provider: LockdownClient):
920
- """
921
- reset accessibility settings to default
922
- """
923
- service = AccessibilityAudit(service_provider)
924
- service.reset_settings()
925
-
926
-
927
- @accessibility.command("shell", cls=Command)
928
- def accessibility_shell(service_provider: LockdownClient):
929
- """start and ipython accessibility shell"""
930
- AccessibilityAudit(service_provider).shell()
931
-
932
-
933
- @accessibility.command("notifications", cls=Command)
934
- def accessibility_notifications(service_provider: LockdownClient):
935
- """show notifications"""
936
-
937
- service = AccessibilityAudit(service_provider)
938
- for event in service.iter_events():
939
- if event.name in (
940
- "hostAppStateChanged:",
941
- "hostInspectorCurrentElementChanged:",
942
- ):
943
- for focus_item in event.data:
944
- logger.info(focus_item)
945
-
946
-
947
- @accessibility.command("list-items", cls=Command)
948
- def accessibility_list_items(service_provider: LockdownClient):
949
- """List elements available in the currently shown menu."""
950
-
951
- elements = []
952
- with AccessibilityAudit(service_provider) as service:
953
- for element in service.iter_elements():
954
- elements.append(element.to_dict())
955
- print_json(elements)
956
-
957
-
958
- @developer.group("condition")
959
- def condition():
960
- """Force a predefined condition"""
961
- pass
962
-
963
-
964
- @condition.command("list", cls=Command)
965
- def condition_list(service_provider: LockdownServiceProvider) -> None:
966
- """list all available conditions"""
967
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
968
- print_json(ConditionInducer(dvt).list())
969
-
970
-
971
- @condition.command("clear", cls=Command)
972
- def condition_clear(service_provider: LockdownClient):
973
- """clear current condition"""
974
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
975
- ConditionInducer(dvt).clear()
976
-
977
-
978
- @condition.command("set", cls=Command)
979
- @click.argument("profile_identifier")
980
- def condition_set(service_provider: LockdownClient, profile_identifier):
981
- """set a specific condition"""
982
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
983
- ConditionInducer(dvt).set(profile_identifier)
984
- OSUTILS.wait_return()
985
-
986
-
987
- @developer.command(cls=Command)
988
- @click.argument("out", type=click.File("wb"))
989
- def screenshot(service_provider: LockdownClient, out):
990
- """Take a screenshot in PNG format"""
991
- out.write(ScreenshotService(lockdown=service_provider).take_screenshot())
992
-
993
-
994
- @developer.group("debugserver")
995
- def debugserver():
996
- """Interact with debugserver"""
997
- pass
998
-
999
-
1000
- @debugserver.command("applist", cls=Command)
1001
- def debugserver_applist(service_provider: LockdownClient):
1002
- """Get applist xml"""
1003
- print_json(DebugServerAppList(service_provider).get())
1004
-
1005
-
1006
- @debugserver.command("start-server", cls=Command)
1007
- @click.argument("local_port", type=click.INT, required=False)
1008
- def debugserver_start_server(service_provider: LockdownClient, local_port: Optional[int] = None):
1009
- """
1010
- if local_port is provided, start a debugserver at remote listening on a given port locally.
1011
- if local_port is not provided and iOS version >= 17.0 then just print the connect string
1012
-
1013
- Please note the connection must be done soon afterward using your own lldb client.
1014
- This can be done using the following commands within lldb shell.
1015
- """
1016
-
1017
- if Version(service_provider.product_version) < Version("17.0"):
1018
- service_name = "com.apple.debugserver.DVTSecureSocketProxy"
1019
- else:
1020
- service_name = "com.apple.internal.dt.remote.debugproxy"
1021
-
1022
- if local_port is not None:
1023
- print(DEBUGSERVER_CONNECTION_STEPS.format(host="127.0.0.1", port=local_port))
1024
- print("Started port forwarding. Press Ctrl-C to close this shell when done")
1025
- sys.stdout.flush()
1026
- LockdownTcpForwarder(service_provider, local_port, service_name).start()
1027
- elif Version(service_provider.product_version) >= Version("17.0"):
1028
- if not isinstance(service_provider, RemoteServiceDiscoveryService):
1029
- raise RSDRequiredError(service_provider.identifier)
1030
- debugserver_port = service_provider.get_service_port(service_name)
1031
- print(DEBUGSERVER_CONNECTION_STEPS.format(host=service_provider.service.address[0], port=debugserver_port))
1032
- else:
1033
- print("local_port is required for iOS < 17.0")
1034
-
1035
-
1036
- @debugserver.command("lldb", cls=RSDCommand)
1037
- @click.argument("xcodeproj_path", type=click.Path(exists=True, file_okay=False, dir_okay=True))
1038
- @click.option("--configuration", default="Debug", help="Usually Release/Debug")
1039
- @click.option("--lldb-command", default="lldb")
1040
- @click.option("--launch", is_flag=True, default=False, help="Launch the app after connecting to lldb")
1041
- @click.option("breakpoints", "-b", "--break", multiple=True, help="Add multiple startup breakpoints")
1042
- @click.option("user_commands", "--command", "-c", multiple=True, help="Additional commands to run at startup")
1043
- def debugserver_lldb(
1044
- service_provider: LockdownServiceProvider,
1045
- xcodeproj_path: str,
1046
- configuration: str,
1047
- lldb_command: str,
1048
- launch: bool,
1049
- breakpoints: tuple[str],
1050
- user_commands: tuple[str],
1051
- ) -> None:
1052
- """
1053
- Automate lldb launch for a given xcodeproj.
1054
-
1055
- \b
1056
- This will:
1057
- - Build the given xcodeproj
1058
- - Install it
1059
- - Start a debugserver attached to it
1060
- - Place breakpoints if given any
1061
- - Launch the application if requested
1062
- - Execute any additional commands if requested
1063
- - Switch to lldb shell
1064
- """
1065
- if Version(service_provider.product_version) < Version("17.0"):
1066
- logger.error("lldb is only supported on iOS >= 17.0")
1067
- return
1068
-
1069
- commands = []
1070
- xcodeproj_path = Path(xcodeproj_path)
1071
- with local.cwd(xcodeproj_path.parent):
1072
- logger.info(f"Building {xcodeproj_path} for {configuration} configuration")
1073
- local["xcodebuild"]["-configuration", configuration, "build"]()
1074
- local_app = next(iter(Path(f"build/{configuration}-iphoneos").glob("*.app")))
1075
- logger.info(f"Using app: {local_app}")
1076
-
1077
- info_plist_path = local_app / "Info.plist"
1078
- info_plist = plistlib.loads(info_plist_path.read_bytes())
1079
- bundle_identifier = info_plist["CFBundleIdentifier"]
1080
- logger.info(f"Bundle identifier: {bundle_identifier}")
1081
-
1082
- commands.append("platform select remote-ios")
1083
- commands.append(f'target create "{local_app.absolute()}"')
1084
-
1085
- with InstallationProxyService(create_using_usbmux()) as installation_proxy:
1086
- logger.info("Installing app")
1087
- installation_proxy.install_from_local(local_app)
1088
- remote_path = installation_proxy.get_apps(bundle_identifiers=[bundle_identifier])[bundle_identifier]["Path"]
1089
- logger.info(f"Remote path: {remote_path}")
1090
-
1091
- commands.append(f'script lldb.target.module[0].SetPlatformFileSpec(lldb.SBFileSpec("{remote_path}"))')
1092
-
1093
- debugserver_port = service_provider.get_service_port("com.apple.internal.dt.remote.debugproxy")
1094
-
1095
- # Add connection and launch commands
1096
- commands.append(f"process connect connect://[{service_provider.service.address[0]}]:{debugserver_port}")
1097
-
1098
- for bp in breakpoints:
1099
- commands.append(f'breakpoint set -n "{bp}"')
1100
-
1101
- if launch:
1102
- commands.append("process launch")
1103
-
1104
- # Add user commands
1105
- commands += user_commands
1106
-
1107
- logger.info("Starting lldb with automated setup and connection")
1108
-
1109
- # Works only on unix-based systems, so keep these imports here
1110
- import fcntl
1111
- import pty
1112
- import select as select_module
1113
- import termios
1114
- import tty
1115
-
1116
- master, slave = pty.openpty()
1117
-
1118
- process = None # Initialize process variable for signal handler
1119
-
1120
- # Copy terminal size from the current terminal to PTY
1121
- def resize_pty() -> None:
1122
- """Update PTY size to match current terminal size"""
1123
- size = struct.unpack(
1124
- "HHHH", fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, struct.pack("HHHH", 0, 0, 0, 0))
1125
- )
1126
- fcntl.ioctl(master, termios.TIOCSWINSZ, struct.pack("HHHH", *size))
1127
- # Send SIGWINCH to the child process to notify it of the resize
1128
- if process is not None and process.poll() is None:
1129
- process.send_signal(signal.SIGWINCH)
1130
-
1131
- # Initial resize
1132
- resize_pty()
1133
-
1134
- # Set up signal handler for window resize
1135
- def handle_sigwinch(signum, frame):
1136
- resize_pty()
1137
-
1138
- old_sigwinch_handler = signal.signal(signal.SIGWINCH, handle_sigwinch)
1139
-
1140
- # Save original terminal settings
1141
- old_tty = termios.tcgetattr(sys.stdin)
1142
-
1143
- try:
1144
- # Set TERM environment variable to enable colors
1145
- env = os.environ.copy()
1146
- env["TERM"] = os.environ.get("TERM", "xterm-256color")
1147
-
1148
- process = subprocess.Popen([lldb_command], stdin=slave, stdout=slave, stderr=slave, env=env)
1149
- os.close(slave)
1150
-
1151
- # Put terminal in raw mode for proper interaction
1152
- tty.setraw(sys.stdin.fileno())
1153
- # Send all commands through stdin
1154
- for command in commands:
1155
- os.write(master, (command + "\n").encode())
1156
-
1157
- # Now redirect stdin from the terminal to lldb so user can interact
1158
- while True:
1159
- rlist, _, _ = select_module.select([sys.stdin, master], [], [])
1160
-
1161
- if sys.stdin in rlist:
1162
- # User typed something
1163
- data = os.read(sys.stdin.fileno(), 1024)
1164
- if not data:
1165
- break
1166
- os.write(master, data)
1167
-
1168
- if master in rlist:
1169
- # lldb has output
1170
- try:
1171
- data = os.read(master, 1024)
1172
- if not data:
1173
- break
1174
- os.write(sys.stdout.fileno(), data)
1175
- except OSError:
1176
- break
1177
- except (KeyboardInterrupt, OSError):
1178
- pass
1179
- finally:
1180
- # Restore terminal settings
1181
- termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_tty)
1182
- # Restore original SIGWINCH handler
1183
- signal.signal(signal.SIGWINCH, old_sigwinch_handler)
1184
- os.close(master)
1185
- if process is not None:
1186
- process.terminate()
1187
- process.wait()
1188
-
1189
-
1190
- @developer.group("arbitration")
1191
- def arbitration():
1192
- """Mark/Unmark device as "in-use" """
1193
- pass
1194
-
1195
-
1196
- @arbitration.command("version", cls=Command)
1197
- def version(service_provider: LockdownClient):
1198
- """get arbitration version"""
1199
- with DtDeviceArbitration(service_provider) as device_arbitration:
1200
- print_json(device_arbitration.version)
1201
-
1202
-
1203
- @arbitration.command("check-in", cls=Command)
1204
- @click.argument("hostname")
1205
- @click.option("-f", "--force", default=False, is_flag=True)
1206
- def check_in(service_provider: LockdownClient, hostname, force):
1207
- """owner check-in"""
1208
- with DtDeviceArbitration(service_provider) as device_arbitration:
1209
- try:
1210
- device_arbitration.check_in(hostname, force=force)
1211
- OSUTILS.wait_return()
1212
- except DeviceAlreadyInUseError as e:
1213
- logger.error(e.message)
1214
-
1215
-
1216
- @arbitration.command("check-out", cls=Command)
1217
- def check_out(service_provider: LockdownClient):
1218
- """owner check-out"""
1219
- with DtDeviceArbitration(service_provider) as device_arbitration:
1220
- device_arbitration.check_out()
1221
-
1222
-
1223
- @dvt.command("har", cls=Command)
1224
- def dvt_har(service_provider: LockdownClient):
1225
- """
1226
- Enable har-logging
1227
-
1228
- For more information, please read:
1229
- https://github.com/doronz88/harlogger?tab=readme-ov-file#enable-http-instrumentation-method
1230
- """
1231
- with DvtSecureSocketProxyService(lockdown=service_provider) as dvt:
1232
- print("> Press Ctrl-C to abort")
1233
- with ActivityTraceTap(dvt, enable_http_archive_logging=True) as tap:
1234
- while True:
1235
- tap.channel.receive_message()
1236
-
1237
-
1238
- @dvt.group("simulate-location")
1239
- def dvt_simulate_location():
1240
- """Simulate device location by given input"""
1241
- pass
1242
-
1243
-
1244
- @dvt_simulate_location.command("clear", cls=Command)
1245
- def dvt_simulate_location_clear(service_provider: LockdownClient):
1246
- """Clear currently simulated location"""
1247
- with DvtSecureSocketProxyService(service_provider) as dvt:
1248
- LocationSimulation(dvt).clear()
1249
-
1250
-
1251
- @dvt_simulate_location.command("set", cls=Command)
1252
- @click.argument("latitude", type=click.FLOAT)
1253
- @click.argument("longitude", type=click.FLOAT)
1254
- def dvt_simulate_location_set(service_provider: LockdownClient, latitude, longitude):
1255
- """
1256
- Set a simulated location.
1257
- For example:
1258
- ... set -- 40.690008 -74.045843 for liberty island
1259
- """
1260
- with DvtSecureSocketProxyService(service_provider) as dvt:
1261
- LocationSimulation(dvt).set(latitude, longitude)
1262
- OSUTILS.wait_return()
1263
-
1264
-
1265
- @dvt_simulate_location.command("play", cls=Command)
1266
- @click.argument("filename", type=click.Path(exists=True, file_okay=True, dir_okay=False))
1267
- @click.argument("timing_randomness_range", type=click.INT, default=0)
1268
- @click.option("--disable-sleep", is_flag=True, default=False)
1269
- def dvt_simulate_location_play(
1270
- service_provider: LockdownClient, filename: str, timing_randomness_range: int, disable_sleep: bool
1271
- ) -> None:
1272
- """Simulate inputs from a given .gpx file"""
1273
- with DvtSecureSocketProxyService(service_provider) as dvt:
1274
- LocationSimulation(dvt).play_gpx_file(
1275
- filename, disable_sleep=disable_sleep, timing_randomness_range=timing_randomness_range
1276
- )
1277
- OSUTILS.wait_return()
1278
-
1279
-
1280
- @developer.group()
1281
- def core_device() -> None:
1282
- """Access features exposed by the DeveloperDiskImage"""
1283
- pass
1284
-
1285
-
1286
- async def core_device_list_directory_task(
1287
- service_provider: RemoteServiceDiscoveryService, domain: str, path: str, identifier: str
1288
- ) -> None:
1289
- async with FileServiceService(service_provider, APPLE_DOMAIN_DICT[domain], identifier) as file_service:
1290
- print_json(await file_service.retrieve_directory_list(path))
1291
-
1292
-
1293
- @core_device.command("list-directory", cls=RSDCommand)
1294
- @click.argument("domain", type=click.Choice(APPLE_DOMAIN_DICT.keys()))
1295
- @click.argument("path")
1296
- @click.option("--identifier", default="")
1297
- def core_device_list_directory(
1298
- service_provider: RemoteServiceDiscoveryService, domain: str, path: str, identifier: str
1299
- ) -> None:
1300
- """List directory at given domain-path"""
1301
- asyncio.run(core_device_list_directory_task(service_provider, domain, path, identifier))
1302
-
1303
-
1304
- async def core_device_read_file_task(
1305
- service_provider: RemoteServiceDiscoveryService, domain: str, path: str, identifier: str, output: Optional[IO]
1306
- ) -> None:
1307
- async with FileServiceService(service_provider, APPLE_DOMAIN_DICT[domain], identifier) as file_service:
1308
- buf = await file_service.retrieve_file(path)
1309
- if output is not None:
1310
- output.write(buf)
1311
- else:
1312
- print(try_decode(buf))
1313
-
1314
-
1315
- @core_device.command("read-file", cls=RSDCommand)
1316
- @click.argument("domain", type=click.Choice(APPLE_DOMAIN_DICT.keys()))
1317
- @click.argument("path")
1318
- @click.option("--identifier", default="")
1319
- @click.option("-o", "--output", type=click.File("wb"))
1320
- def core_device_read_file(
1321
- service_provider: RemoteServiceDiscoveryService, domain: str, path: str, identifier: str, output: Optional[IO]
1322
- ) -> None:
1323
- """Read file from given domain-path"""
1324
- asyncio.run(core_device_read_file_task(service_provider, domain, path, identifier, output))
1325
-
1326
-
1327
- async def core_device_propose_empty_file_task(
1328
- service_provider: RemoteServiceDiscoveryService,
1329
- domain: str,
1330
- path: str,
1331
- identifier: str,
1332
- file_permissions: int,
1333
- uid: int,
1334
- gid: int,
1335
- creation_time: int,
1336
- last_modification_time: int,
1337
- ) -> None:
1338
- async with FileServiceService(service_provider, APPLE_DOMAIN_DICT[domain], identifier) as file_service:
1339
- await file_service.propose_empty_file(path, file_permissions, uid, gid, creation_time, last_modification_time)
1340
-
1341
-
1342
- @core_device.command("propose-empty-file", cls=RSDCommand)
1343
- @click.argument("domain", type=click.Choice(APPLE_DOMAIN_DICT.keys()))
1344
- @click.argument("path")
1345
- @click.option("--identifier", default="")
1346
- @click.option("--file-permissions", type=click.INT, default=0o644)
1347
- @click.option("--uid", type=click.INT, default=501)
1348
- @click.option("--gid", type=click.INT, default=501)
1349
- @click.option("--creation-time", type=click.INT, default=time.time())
1350
- @click.option("--last-modification-time", type=click.INT, default=time.time())
1351
- def core_device_propose_empty_file(
1352
- service_provider: RemoteServiceDiscoveryService,
1353
- domain: str,
1354
- path: str,
1355
- identifier: str,
1356
- file_permissions: int,
1357
- uid: int,
1358
- gid: int,
1359
- creation_time: int,
1360
- last_modification_time: int,
1361
- ) -> None:
1362
- """Write an empty file to given domain-path"""
1363
- asyncio.run(
1364
- core_device_propose_empty_file_task(
1365
- service_provider,
1366
- domain,
1367
- path,
1368
- identifier,
1369
- file_permissions,
1370
- uid,
1371
- gid,
1372
- creation_time,
1373
- last_modification_time,
1374
- )
1375
- )
1376
-
1377
-
1378
- async def core_device_list_launch_application_task(
1379
- service_provider: RemoteServiceDiscoveryService,
1380
- bundle_identifier: str,
1381
- argument: list[str],
1382
- kill_existing: bool,
1383
- suspended: bool,
1384
- env: list[tuple[str, str]],
1385
- ) -> None:
1386
- async with AppServiceService(service_provider) as app_service:
1387
- print_json(
1388
- await app_service.launch_application(bundle_identifier, argument, kill_existing, suspended, dict(env))
1389
- )
1390
-
1391
-
1392
- @core_device.command("launch-application", cls=RSDCommand)
1393
- @click.argument("bundle_identifier")
1394
- @click.argument("argument", nargs=-1)
1395
- @click.option(
1396
- "--kill-existing/--no-kill-existing", default=True, help="Whether to kill an existing instance of this process"
1397
- )
1398
- @click.option("--suspended", is_flag=True, help="Same as WaitForDebugger")
1399
- @click.option(
1400
- "--env",
1401
- multiple=True,
1402
- type=click.Tuple((str, str)),
1403
- help="Environment variables to pass to process given as a list of key value",
1404
- )
1405
- def core_device_launch_application(
1406
- service_provider: RemoteServiceDiscoveryService,
1407
- bundle_identifier: str,
1408
- argument: tuple[str],
1409
- kill_existing: bool,
1410
- suspended: bool,
1411
- env: list[tuple[str, str]],
1412
- ) -> None:
1413
- """Launch application"""
1414
- asyncio.run(
1415
- core_device_list_launch_application_task(
1416
- service_provider, bundle_identifier, list(argument), kill_existing, suspended, env
1417
- )
1418
- )
1419
-
1420
-
1421
- async def core_device_list_processes_task(service_provider: RemoteServiceDiscoveryService) -> None:
1422
- async with AppServiceService(service_provider) as app_service:
1423
- print_json(await app_service.list_processes())
1424
-
1425
-
1426
- @core_device.command("list-processes", cls=RSDCommand)
1427
- def core_device_list_processes(service_provider: RemoteServiceDiscoveryService) -> None:
1428
- """Get process list"""
1429
- asyncio.run(core_device_list_processes_task(service_provider))
1430
-
1431
-
1432
- async def core_device_uninstall_app_task(
1433
- service_provider: RemoteServiceDiscoveryService, bundle_identifier: str
1434
- ) -> None:
1435
- async with AppServiceService(service_provider) as app_service:
1436
- await app_service.uninstall_app(bundle_identifier)
1437
-
1438
-
1439
- @core_device.command("uninstall", cls=RSDCommand)
1440
- @click.argument("bundle_identifier")
1441
- def core_device_uninstall_app(service_provider: RemoteServiceDiscoveryService, bundle_identifier: str) -> None:
1442
- """Uninstall application"""
1443
- asyncio.run(core_device_uninstall_app_task(service_provider, bundle_identifier))
1444
-
1445
-
1446
- async def core_device_send_signal_to_process_task(
1447
- service_provider: RemoteServiceDiscoveryService, pid: int, signal: int
1448
- ) -> None:
1449
- async with AppServiceService(service_provider) as app_service:
1450
- print_json(await app_service.send_signal_to_process(pid, signal))
1451
-
1452
-
1453
- @core_device.command("send-signal-to-process", cls=RSDCommand)
1454
- @click.argument("pid", type=click.INT)
1455
- @click.argument("signal", type=click.INT)
1456
- def core_device_send_signal_to_process(service_provider: RemoteServiceDiscoveryService, pid: int, signal: int) -> None:
1457
- """Send signal to process"""
1458
- asyncio.run(core_device_send_signal_to_process_task(service_provider, pid, signal))
1459
-
1460
-
1461
- async def core_device_get_device_info_task(service_provider: RemoteServiceDiscoveryService) -> None:
1462
- async with DeviceInfoService(service_provider) as app_service:
1463
- print_json(await app_service.get_device_info())
1464
-
1465
-
1466
- @core_device.command("get-device-info", cls=RSDCommand)
1467
- def core_device_get_device_info(service_provider: RemoteServiceDiscoveryService) -> None:
1468
- """Get device information"""
1469
- asyncio.run(core_device_get_device_info_task(service_provider))
1470
-
1471
-
1472
- async def core_device_get_display_info_task(service_provider: RemoteServiceDiscoveryService) -> None:
1473
- async with DeviceInfoService(service_provider) as app_service:
1474
- print_json(await app_service.get_display_info())
1475
-
1476
-
1477
- @core_device.command("get-display-info", cls=RSDCommand)
1478
- def core_device_get_display_info(service_provider: RemoteServiceDiscoveryService) -> None:
1479
- """Get display information"""
1480
- asyncio.run(core_device_get_display_info_task(service_provider))
1481
-
1482
-
1483
- async def core_device_query_mobilegestalt_task(service_provider: RemoteServiceDiscoveryService, key: list[str]) -> None:
1484
- """Query MobileGestalt"""
1485
- async with DeviceInfoService(service_provider) as app_service:
1486
- print_json(await app_service.query_mobilegestalt(key))
1487
-
1488
-
1489
- @core_device.command("query-mobilegestalt", cls=RSDCommand)
1490
- @click.argument("key", nargs=-1, type=click.STRING)
1491
- def core_device_query_mobilegestalt(service_provider: RemoteServiceDiscoveryService, key: tuple[str]) -> None:
1492
- """Query MobileGestalt"""
1493
- asyncio.run(core_device_query_mobilegestalt_task(service_provider, list(key)))
1494
-
1495
-
1496
- async def core_device_get_lockstate_task(service_provider: RemoteServiceDiscoveryService) -> None:
1497
- async with DeviceInfoService(service_provider) as app_service:
1498
- print_json(await app_service.get_lockstate())
1499
-
1500
-
1501
- @core_device.command("get-lockstate", cls=RSDCommand)
1502
- def core_device_get_lockstate(service_provider: RemoteServiceDiscoveryService) -> None:
1503
- """Get lockstate"""
1504
- asyncio.run(core_device_get_lockstate_task(service_provider))
1505
-
1506
-
1507
- async def core_device_list_apps_task(service_provider: RemoteServiceDiscoveryService) -> None:
1508
- async with AppServiceService(service_provider) as app_service:
1509
- print_json(await app_service.list_apps())
1510
-
1511
-
1512
- @core_device.command("list-apps", cls=RSDCommand)
1513
- def core_device_list_apps(service_provider: RemoteServiceDiscoveryService) -> None:
1514
- """Get application list"""
1515
- asyncio.run(core_device_list_apps_task(service_provider))
1516
-
1517
-
1518
- async def core_device_sysdiagnose_task(service_provider: RemoteServiceDiscoveryService, output: str) -> None:
1519
- output = Path(output)
1520
- async with DiagnosticsServiceService(service_provider) as service:
1521
- response = await service.capture_sysdiagnose(False)
1522
- logger.info(f"Operation response: {response}")
1523
- if output.is_dir():
1524
- output /= response.preferred_filename
1525
- logger.info(f"Downloading sysdiagnose to: {output}")
1526
-
1527
- # get the file over lockdownd which is WAYYY faster
1528
- lockdown = create_using_usbmux(service_provider.udid)
1529
- with CrashReportsManager(lockdown) as crash_reports_manager:
1530
- crash_reports_manager.afc.pull(
1531
- posixpath.join(f"/DiagnosticLogs/sysdiagnose/{response.preferred_filename}"), output
1532
- )
1533
-
1534
-
1535
- @core_device.command("sysdiagnose", cls=RSDCommand)
1536
- @click.argument("output", type=click.Path(dir_okay=True, file_okay=True, exists=True))
1537
- def core_device_sysdiagnose(service_provider: RemoteServiceDiscoveryService, output: str) -> None:
1538
- """Execute sysdiagnose and fetch the output file"""
1539
- asyncio.run(core_device_sysdiagnose_task(service_provider, output))