-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_hopper_utils.py
More file actions
177 lines (133 loc) · 4.05 KB
/
_hopper_utils.py
File metadata and controls
177 lines (133 loc) · 4.05 KB
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
from __future__ import annotations
import json
import shutil
import subprocess
from functools import lru_cache
from pathlib import Path
from typing import Any
SUBSYSTEM_TOKENS: tuple[tuple[str, str], ...] = (
("tokio", "async-runtime"),
("axum", "http-routing"),
("hyper", "http-core"),
("serde", "serialization"),
("tracing", "observability"),
("rustls", "tls"),
("ring", "crypto"),
("sqlx", "database"),
("reqwest", "http-client"),
("tower", "middleware"),
("std::", "std"),
)
CALL_TYPE_NAMES: dict[int, str] = {
0: "none",
1: "unknown",
2: "direct",
3: "objc",
}
def load_json(path: str | Path) -> Any:
return json.loads(Path(path).read_text(encoding="utf-8"))
def write_json(path: str | Path, payload: Any) -> None:
Path(path).write_text(f"{json.dumps(payload, indent=2)}\n", encoding="utf-8")
def write_text(path: str | Path, content: str) -> None:
Path(path).write_text(content, encoding="utf-8")
@lru_cache(maxsize=4096)
def demangle_rust_symbol(name: str) -> str:
if not name:
return ""
if shutil.which("rustfilt") is None:
return name
try:
completed = subprocess.run(
["rustfilt", name],
capture_output=True,
check=False,
text=True,
)
except OSError:
return name
if completed.returncode != 0:
return name
return completed.stdout.strip() or name
def guess_subsystem(name: str) -> str:
normalized = name.lower()
for token, subsystem in SUBSYSTEM_TOKENS:
if token in normalized:
return subsystem
return "application-or-unknown"
def sanitize_id(value: object) -> str:
text = str(value)
return "".join(character if character.isalnum() else "_" for character in text)
def sanitize_label(value: object) -> str:
return str(value or "unknown").replace('"', "'")
def short_label(value: object, limit: int = 36) -> str:
text = sanitize_label(value)
if len(text) <= limit:
return text
return f"{text[: limit - 3]}..."
def default_output_path(
executable_path: str | None,
suffix: str,
fallback_name: str,
) -> Path:
if executable_path:
return Path(f"{executable_path}{suffix}")
return Path.home() / fallback_name
def to_hex(value: object) -> str | None:
try:
return f"0x{int(value):x}"
except (TypeError, ValueError):
return None
def parse_address(value: object) -> int:
if isinstance(value, int):
return value
text = str(value).strip().lower()
if text.startswith("0x"):
return int(text, 16)
return int(text, 10)
def parse_color(value: object) -> int:
if isinstance(value, int):
return value
text = str(value).strip().lower()
if text.startswith("#"):
text = text[1:]
if text.startswith("0x"):
text = text[2:]
return int(text, 16)
def call_type_name(value: object) -> str:
try:
return CALL_TYPE_NAMES.get(int(value), f"unknown-{value}")
except (TypeError, ValueError):
return "unknown"
def ensure_document_ready(document: Any) -> None:
try:
if document.backgroundProcessActive():
document.waitForBackgroundProcessToEnd()
except AttributeError:
return
def iter_document_procedures(document: Any) -> list[Any]:
procedures: list[Any] = []
for segment in document.getSegmentsList():
try:
count = int(segment.getProcedureCount())
except Exception:
continue
for index in range(count):
try:
procedure = segment.getProcedureAtIndex(index)
except Exception:
continue
if procedure is not None:
procedures.append(procedure)
return procedures
def tag_names(owner: Any) -> list[str]:
try:
tags = owner.getTagList()
except Exception:
return []
names: list[str] = []
for tag in tags:
try:
names.append(str(tag.getName()))
except Exception:
continue
return names