根因:C++
libprotobuf 編譯加
-fvisibility=hidden 或嵌套定義(如
KlineItem::TradeSectionV2 或定義於另一
.proto/另一粒
.so)時,ELF
.dynsym 唔會導出全局
*_descriptor() 符號;但
每一個編譯入庫嘅 .proto 都必然將完整 wire 編碼之 FileDescriptorProto 原文存喺 ELF .rodata 段,且已由
KlineItem::descriptor() 內部指針直接連結!
招式 1A(最快・喺你現有 localPB C++ harness 加 6 行,零符號查找):
既然你已有
KlineItem 嘅
const google::protobuf::Descriptor* item_desc,直接經欄位反查
enum_type()(繞過 ELF symbol visibility):
const auto* f = item_desc->FindFieldByName("trade_section_v2"); // 或 FindFieldByNumber(...)
if (f && f->enum_type()) {
const auto* ed = f->enum_type();
fprintf(stderr, "ENUM %s (%s):\n", ed->full_name().c_str(), ed->file()->name().c_str());
for (int i = 0; i < ed->value_count(); ++i)
fprintf(stderr, " %d = %s\n", ed->value(i)->number(), ed->value(i)->name().c_str());
}
招式 1B(零依賴純 Python 離線 .rodata FileDescriptorProto 掃描器 — 本席已實測 PASS):
直接對
/opt/FTNN 目錄做唯讀檔案掃描(唔碰運行中進程、唔需安裝 python-protobuf),一次過抽出全部隱藏
.proto 嘅
EnumDescriptorProto 與欄位映射(同時可順手抽出
FTCmdQot 之 type 1/2/3 message schema):
#!/usr/bin/env python3
"""Read-only offline ELF/.so FileDescriptorProto wire carver (zero-ptrace, zero-protobuf-dep).
Scans ELF files (e.g. /opt/FTNN/**/lib*.so, /opt/FTNN/FTNN) for embedded .proto descriptors
and extracts enum definitions (e.g. TradeSectionV2 values 12, 20) & field->type mappings.
Usage: python3 carve_pb_enums.py /opt/FTNN > /tmp/pb_enums_dump.txt
"""
import os, re, sys
def read_varint(buf, pos, end):
val, shift = 0, 0
while pos < end:
b = buf[pos]; pos += 1
val |= (b & 0x7F) << shift
if not (b & 0x80):
return val, pos
shift += 7
if shift > 63:
return None, end
return None, end
def parse_msg(buf, pos, end):
fields = []
while pos < end:
tag, pos = read_varint(buf, pos, end)
if tag is None or tag == 0:
return None
fn, wt = tag >> 3, tag & 7
if fn == 0 or fn > 536870911:
return None
if wt == 0:
v, pos = read_varint(buf, pos, end)
if v is None: return None
fields.append((fn, wt, v))
elif wt == 1:
if pos + 8 > end: return None
fields.append((fn, wt, buf[pos:pos+8])); pos += 8
elif wt == 2:
ln, pos = read_varint(buf, pos, end)
if ln is None or pos + ln > end: return None
fields.append((fn, wt, buf[pos:pos+ln])); pos += ln
elif wt == 5:
if pos + 4 > end: return None
fields.append((fn, wt, buf[pos:pos+4])); pos += 4
else:
return None
return fields
def parse_enum(raw):
f = parse_msg(raw, 0, len(raw))
if not f: return None
ename, vals = "", []
for fn, wt, v in f:
if fn == 1 and wt == 2:
ename = v.decode("utf-8", "replace")
elif fn == 2 and wt == 2:
vf = parse_msg(v, 0, len(v))
if vf:
vn, vnum = "", None
for vfn, vwt, vv in vf:
if vfn == 1 and vwt == 2: vn = vv.decode("utf-8", "replace")
elif vfn == 2 and vwt == 0:
vnum = vv if vv < (1 << 31) else vv - (1 << 64)
if vn and vnum is not None:
vals.append((vnum, vn))
return (ename, vals) if ename and vals else None
def walk_msg_type(prefix, raw, out_enums, out_fields):
f = parse_msg(raw, 0, len(raw))
if not f: return
mname = ""
for fn, wt, v in f:
if fn == 1 and wt == 2:
mname = v.decode("utf-8", "replace")
full = f"{prefix}.{mname}" if prefix else mname
for fn, wt, v in f:
if fn == 2 and wt == 2: # FieldDescriptorProto
ff = parse_msg(v, 0, len(v)) or []
fname, fnum, ftype = "", None, ""
for ffn, fwt, fv in ff:
if ffn == 1 and fwt == 2: fname = fv.decode("utf-8", "replace")
elif ffn == 3 and fwt == 0: fnum = fv
elif ffn == 6 and fwt == 2: ftype = fv.decode("utf-8", "replace")
if fname and ("section" in fname.lower() or "exright" in fname.lower() or "kline" in full.lower() or "qot" in full.lower()):
out_fields.append((full, fnum, fname, ftype))
elif fn == 4 and wt == 2: # nested_type
walk_msg_type(full, v, out_enums, out_fields)
elif fn == 5 and wt == 2: # enum_type
e = parse_enum(v)
if e: out_enums.append((f"{full}.{e[0]}", e[1]))
def scan_file(path):
try:
data = open(path, "rb").read()
except OSError:
return
# FileDescriptorProto starts with tag 0x0a <varint_len> "<name>.proto"
for m in re.finditer(rb"\x0a([\x05-\x7f])([A-Za-z0-9_./-]{4,120}\.proto)", data):
decl_len = m.group(1)[0]
pname = m.group(2)
if decl_len != len(pname):
continue
start = m.start()
# Try finding end boundary (C-string/table boundary or valid wire parse up to 64KB)
max_end = min(len(data), start + 65536)
best = None
pos = start
last_good = start
while pos < max_end:
tag, np = read_varint(data, pos, max_end)
if tag is None or tag == 0 or (tag & 7) not in (0, 1, 2, 5) or (tag >> 3) > 20:
break
wt = tag & 7
if wt == 0:
v, np = read_varint(data, np, max_end)
if v is None: break
elif wt == 1: np += 8
elif wt == 5: np += 4
elif wt == 2:
ln, np2 = read_varint(data, np, max_end)
if ln is None or np2 + ln > max_end: break
np = np2 + ln
if np > max_end: break
pos = last_good = np
if last_good > start + len(pname) + 4:
f = parse_msg(data, start, last_good)
if not f: continue
pkg, enums, fields = "", [], []
for fn, wt, v in f:
if fn == 2 and wt == 2: pkg = v.decode("utf-8", "replace")
for fn, wt, v in f:
if fn == 4 and wt == 2: walk_msg_type(pkg, v, enums, fields)
elif fn == 5 and wt == 2:
e = parse_enum(v)
if e: enums.append((f"{pkg}.{e[0]}" if pkg else e[0], e[1]))
if enums or fields:
print(f"=== {path} :: {pname.decode('utf-8','replace')} (pkg={pkg}, {last_good-start} B) ===")
for ef, evs in enums:
print(f" ENUM {ef}: {evs}")
for mf, fnum, fname, ftype in fields:
print(f" FIELD {mf} #{fnum} {fname} -> {ftype}")
if __name__ == "__main__":
target = sys.argv[1] if len(sys.argv) > 1 else "."
if os.path.isfile(target):
scan_file(target)
else:
for r, _, fs in os.walk(target):
for fn in sorted(fs):
fp = os.path.join(r, fn)
if fn.endswith(".so") or ".so." in fn or fn in ("FTNN", "FutuOpenD"):
scan_file(fp)
1. 點解 hp volume 會有小數:(a) 前復權含股息調整(field4 = 5 = FORWARD_INCLUDE_DIVI)或拆合股因子回算歷史成交量時會產生非整數股數;(b) 美股碎股(fractional shares)/指數或特定品種單位(手/萬股/張)換算。你「唔四捨五入、保留原值」100% 正確——建議 schema 同時落盤 vol_int64_raw 同 vol_hp_decimal_str(Python 用 decimal.Decimal 精確解析,禁轉 float 失精度)。
2. 同源雙欄自洽校驗(毋須等 UI 先可機械驗 90%):同一粒 KlineItem 若同時帶 close/high/low/open (int64) 與 hp_* 字串,直接喺全部 20 個符號逐根驗 Decimal(int64_val) / Decimal('1e9') == Decimal(hp_str)(及成交額 / 1e3);一旦 20 符號全數精確相等(或固定比例),縮放因子即獲 protobuf 同源雙欄數學證實。
3. 零重啟 UI 逐根對照(同復權模式對齊):切勿攞未復權現價去比 field4=5 歷史復權 K 線!對現有 Xvfb 顯示面做單幀唯讀截圖(例如 xwd -display :<D> -root -silent | convert xwd:- /tmp/ui_chk.png,唔影響 PID 1032118),確認圖表右上角復權標籤同為「前復權」,即可同屏核對該分鐘 K 線 OHLCV 與圖頂 Header 數值。