|
| 1 | +"""Remote RenderDoc server commands: connect, list, capture.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import dataclasses |
| 6 | +import json |
| 7 | +import time |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +import click |
| 12 | + |
| 13 | +from rdc.discover import find_renderdoc |
| 14 | +from rdc.remote_core import ( |
| 15 | + build_conn_url, |
| 16 | + connect_remote_server, |
| 17 | + enumerate_remote_targets, |
| 18 | + parse_url, |
| 19 | + remote_capture, |
| 20 | + warn_if_public, |
| 21 | +) |
| 22 | +from rdc.remote_state import ( |
| 23 | + RemoteServerState, |
| 24 | + load_latest_remote_state, |
| 25 | + save_remote_state, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def _require_renderdoc() -> Any: |
| 30 | + """Find and return the renderdoc module, or exit with error.""" |
| 31 | + rd = find_renderdoc() |
| 32 | + if rd is None: |
| 33 | + click.echo("error: renderdoc module not found", err=True) |
| 34 | + raise SystemExit(1) |
| 35 | + return rd |
| 36 | + |
| 37 | + |
| 38 | +def _resolve_url(url: str | None) -> tuple[str, int]: |
| 39 | + """Resolve host/port from --url flag or saved state.""" |
| 40 | + if url: |
| 41 | + try: |
| 42 | + return parse_url(url) |
| 43 | + except ValueError as exc: |
| 44 | + click.echo(f"error: {exc}", err=True) |
| 45 | + raise SystemExit(1) from None |
| 46 | + state = load_latest_remote_state() |
| 47 | + if state is None: |
| 48 | + click.echo("error: no remote connection (run 'rdc remote connect' first)", err=True) |
| 49 | + raise SystemExit(1) |
| 50 | + return state.host, state.port |
| 51 | + |
| 52 | + |
| 53 | +def _check_public_ip(host: str) -> None: |
| 54 | + """Emit warning to stderr if host appears to be a public IP.""" |
| 55 | + warning = warn_if_public(host) |
| 56 | + if warning: |
| 57 | + click.echo(warning, err=True) |
| 58 | + |
| 59 | + |
| 60 | +@click.group("remote") |
| 61 | +def remote_group() -> None: |
| 62 | + """Remote RenderDoc server commands.""" |
| 63 | + |
| 64 | + |
| 65 | +@remote_group.command("connect") |
| 66 | +@click.argument("url") |
| 67 | +@click.option("--json", "as_json", is_flag=True, help="Output as JSON.") |
| 68 | +def remote_connect_cmd(url: str, as_json: bool) -> None: |
| 69 | + """Connect to a remote RenderDoc server.""" |
| 70 | + try: |
| 71 | + host, port = parse_url(url) |
| 72 | + except ValueError as exc: |
| 73 | + click.echo(f"error: {exc}", err=True) |
| 74 | + raise SystemExit(1) from None |
| 75 | + _check_public_ip(host) |
| 76 | + rd = _require_renderdoc() |
| 77 | + |
| 78 | + conn_url = build_conn_url(host, port) |
| 79 | + try: |
| 80 | + remote = connect_remote_server(rd, conn_url) |
| 81 | + except RuntimeError as exc: |
| 82 | + click.echo(f"error: {exc}", err=True) |
| 83 | + raise SystemExit(1) from None |
| 84 | + |
| 85 | + try: |
| 86 | + remote.Ping() |
| 87 | + save_remote_state(RemoteServerState(host=host, port=port, connected_at=time.time())) |
| 88 | + finally: |
| 89 | + remote.ShutdownConnection() |
| 90 | + |
| 91 | + if as_json: |
| 92 | + click.echo(json.dumps({"host": host, "port": port})) |
| 93 | + else: |
| 94 | + click.echo(f"connected: {host}:{port}") |
| 95 | + |
| 96 | + |
| 97 | +@remote_group.command("list") |
| 98 | +@click.option("--url", default=None, help="Override saved remote (host:port).") |
| 99 | +@click.option("--json", "as_json", is_flag=True, help="Output as JSON.") |
| 100 | +def remote_list_cmd(url: str | None, as_json: bool) -> None: |
| 101 | + """List capturable applications on a remote host.""" |
| 102 | + host, port = _resolve_url(url) |
| 103 | + _check_public_ip(host) |
| 104 | + rd = _require_renderdoc() |
| 105 | + |
| 106 | + conn_url = build_conn_url(host, port) |
| 107 | + idents = enumerate_remote_targets(rd, conn_url) |
| 108 | + |
| 109 | + targets: list[dict[str, Any]] = [] |
| 110 | + for ident in idents: |
| 111 | + tc = rd.CreateTargetControl(conn_url, ident, "rdc-cli", False) |
| 112 | + if tc is None: |
| 113 | + targets.append({"ident": ident, "target": "unknown", "pid": 0, "api": "unknown"}) |
| 114 | + continue |
| 115 | + try: |
| 116 | + targets.append( |
| 117 | + { |
| 118 | + "ident": ident, |
| 119 | + "target": tc.GetTarget(), |
| 120 | + "pid": tc.GetPID(), |
| 121 | + "api": tc.GetAPI(), |
| 122 | + } |
| 123 | + ) |
| 124 | + finally: |
| 125 | + tc.Shutdown() |
| 126 | + |
| 127 | + if as_json: |
| 128 | + click.echo(json.dumps({"targets": targets})) |
| 129 | + else: |
| 130 | + if not targets: |
| 131 | + click.echo("no targets found") |
| 132 | + for t in targets: |
| 133 | + click.echo(f"ident={t['ident']} target={t['target']} pid={t['pid']} api={t['api']}") |
| 134 | + |
| 135 | + |
| 136 | +@remote_group.command("capture") |
| 137 | +@click.argument("app") |
| 138 | +@click.option( |
| 139 | + "-o", "--output", required=True, type=click.Path(path_type=Path), help="Local output path." |
| 140 | +) |
| 141 | +@click.option("--url", default=None, help="Override saved remote (host:port).") |
| 142 | +@click.option("--args", "app_args", default="", help="Arguments for remote app.") |
| 143 | +@click.option("--workdir", default="", help="Remote working directory.") |
| 144 | +@click.option("--frame", type=int, default=None, help="Queue capture at frame N.") |
| 145 | +@click.option("--timeout", type=float, default=60.0, help="Capture timeout in seconds.") |
| 146 | +@click.option("--api-validation", is_flag=True, help="Enable API validation.") |
| 147 | +@click.option("--callstacks", is_flag=True, help="Capture callstacks.") |
| 148 | +@click.option("--hook-children", is_flag=True, help="Hook child processes.") |
| 149 | +@click.option("--ref-all-resources", is_flag=True, help="Reference all resources.") |
| 150 | +@click.option("--soft-memory-limit", type=int, default=None, help="Soft memory limit (MB).") |
| 151 | +@click.option("--json", "as_json", is_flag=True, help="Output as JSON.") |
| 152 | +def remote_capture_cmd( |
| 153 | + app: str, |
| 154 | + output: Path, |
| 155 | + url: str | None, |
| 156 | + app_args: str, |
| 157 | + workdir: str, |
| 158 | + frame: int | None, |
| 159 | + timeout: float, |
| 160 | + api_validation: bool, |
| 161 | + callstacks: bool, |
| 162 | + hook_children: bool, |
| 163 | + ref_all_resources: bool, |
| 164 | + soft_memory_limit: int | None, |
| 165 | + as_json: bool, |
| 166 | +) -> None: |
| 167 | + """Capture on a remote host and transfer to local.""" |
| 168 | + host, port = _resolve_url(url) |
| 169 | + _check_public_ip(host) |
| 170 | + rd = _require_renderdoc() |
| 171 | + |
| 172 | + opts: dict[str, Any] = {} |
| 173 | + if api_validation: |
| 174 | + opts["api_validation"] = True |
| 175 | + if callstacks: |
| 176 | + opts["callstacks"] = True |
| 177 | + if hook_children: |
| 178 | + opts["hook_children"] = True |
| 179 | + if ref_all_resources: |
| 180 | + opts["ref_all_resources"] = True |
| 181 | + if soft_memory_limit is not None: |
| 182 | + opts["soft_memory_limit"] = soft_memory_limit |
| 183 | + |
| 184 | + conn_url = build_conn_url(host, port) |
| 185 | + try: |
| 186 | + remote = connect_remote_server(rd, conn_url) |
| 187 | + except RuntimeError as exc: |
| 188 | + click.echo(f"error: {exc}", err=True) |
| 189 | + raise SystemExit(1) from None |
| 190 | + |
| 191 | + try: |
| 192 | + result = remote_capture( |
| 193 | + rd, |
| 194 | + remote, |
| 195 | + conn_url, |
| 196 | + app, |
| 197 | + args=app_args, |
| 198 | + workdir=workdir, |
| 199 | + output=str(output), |
| 200 | + opts=opts, |
| 201 | + frame=frame, |
| 202 | + timeout=timeout, |
| 203 | + ) |
| 204 | + finally: |
| 205 | + remote.ShutdownConnection() |
| 206 | + |
| 207 | + if as_json: |
| 208 | + click.echo(json.dumps(dataclasses.asdict(result))) |
| 209 | + if not result.success: |
| 210 | + raise SystemExit(1) |
| 211 | + return |
| 212 | + |
| 213 | + if not result.success: |
| 214 | + click.echo(f"error: {result.error}", err=True) |
| 215 | + raise SystemExit(1) |
| 216 | + |
| 217 | + click.echo(result.path) |
| 218 | + click.echo(f"next: rdc open {result.path}", err=True) |
0 commit comments