Skip to content

Commit e953d62

Browse files
committed
feat: Enhance symbol handling by excluding OBJECT types from source navigation
1 parent 3e9f209 commit e953d62

File tree

4 files changed

+155
-32
lines changed

4 files changed

+155
-32
lines changed

src/utils/elf/handleElf.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,36 +96,46 @@ export async function handleElf(context: vscode.ExtensionContext, panel: vscode.
9696
case OPEN_SYMBOL_SOURCE:
9797
// 打开符号对应的源代码文件
9898
const symbol = analyzer.getSymbolWithDebugInfo(message.symbolName);
99-
if (symbol && symbol.sourceFile) {
100-
try {
101-
// 处理相对路径和绝对路径
102-
let filePath = symbol.sourceFile;
103-
if (!path.isAbsolute(filePath)) {
104-
filePath = path.join(projectPath, filePath);
105-
}
106-
107-
// 检查文件是否存在
108-
if (fs.existsSync(filePath)) {
109-
const document = await vscode.workspace.openTextDocument(filePath);
110-
const editor = await vscode.window.showTextDocument(document);
111-
112-
// 跳转到指定行号
113-
if (symbol.sourceLine && symbol.sourceLine > 0) {
114-
const position = new vscode.Position(symbol.sourceLine - 1, 0);
115-
const range = new vscode.Range(position, position);
116-
editor.selection = new vscode.Selection(range.start, range.end);
117-
editor.revealRange(range, vscode.TextEditorRevealType.InCenter);
99+
if (symbol) {
100+
// Check if symbol is a FUNCTION type
101+
if (symbol.type !== 'FUNC') {
102+
vscode.window.showWarningMessage(`符号 ${message.symbolName}${symbol.type} 类型,暂不支持定位源代码位置`);
103+
break;
104+
}
105+
106+
if (symbol.sourceFile) {
107+
try {
108+
// 处理相对路径和绝对路径
109+
let filePath = symbol.sourceFile;
110+
if (!path.isAbsolute(filePath)) {
111+
filePath = path.join(projectPath, filePath);
118112
}
119113

120-
// vscode.window.showInformationMessage(`已打开 ${symbol.name} 的源代码位置`);
121-
} else {
122-
vscode.window.showWarningMessage(`源文件不存在: ${filePath}`);
114+
// 检查文件是否存在
115+
if (fs.existsSync(filePath)) {
116+
const document = await vscode.workspace.openTextDocument(filePath);
117+
const editor = await vscode.window.showTextDocument(document);
118+
119+
// 跳转到指定行号
120+
if (symbol.sourceLine && symbol.sourceLine > 0) {
121+
const position = new vscode.Position(symbol.sourceLine - 1, 0);
122+
const range = new vscode.Range(position, position);
123+
editor.selection = new vscode.Selection(range.start, range.end);
124+
editor.revealRange(range, vscode.TextEditorRevealType.InCenter);
125+
}
126+
127+
// vscode.window.showInformationMessage(`已打开 ${symbol.name} 的源代码位置`);
128+
} else {
129+
vscode.window.showWarningMessage(`源文件不存在: ${filePath}`);
130+
}
131+
} catch (error) {
132+
vscode.window.showErrorMessage(`无法打开源文件: ${error}`);
123133
}
124-
} catch (error) {
125-
vscode.window.showErrorMessage(`无法打开源文件: ${error}`);
134+
} else {
135+
vscode.window.showWarningMessage(`无法找到函数 ${message.symbolName} 的源代码位置`);
126136
}
127137
} else {
128-
vscode.window.showWarningMessage(`无法找到符号 ${message.symbolName} 的源代码位置`);
138+
vscode.window.showWarningMessage(`未找到符号: ${message.symbolName}`);
129139
}
130140
break;
131141

src/utils/elf/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,20 @@ export class ElfAnalyzer {
113113
return null;
114114
}
115115

116-
const debugInfo = this.getSymbolDebugInfo(symbolName);
117-
if (debugInfo) {
118-
return {
119-
...symbol,
120-
sourceFile: debugInfo.sourceFile,
121-
sourceLine: debugInfo.sourceLine
122-
};
116+
// Only provide debug info for FUNCTION symbols
117+
// OBJECT symbols (variables) don't have accurate debug info
118+
if (symbol.type === 'FUNC') {
119+
const debugInfo = this.getSymbolDebugInfo(symbolName);
120+
if (debugInfo) {
121+
return {
122+
...symbol,
123+
sourceFile: debugInfo.sourceFile,
124+
sourceLine: debugInfo.sourceLine
125+
};
126+
}
123127
}
124128

129+
// For OBJECT and other types, return without source info
125130
return symbol;
126131
}
127132
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Test script to verify OBJECT symbols don't have source info
5+
*/
6+
7+
const path = require('path');
8+
const fs = require('fs');
9+
10+
// Load the compiled module
11+
const { ElfAnalyzer } = require('../../../out/utils/elf/index');
12+
13+
console.log('=== Testing OBJECT Symbol Source Info Removal ===\n');
14+
15+
// Find a test ELF file
16+
const testDir = path.join(__dirname, 'test/fixtures');
17+
const elfPath = path.join(testDir, 'test.elf');
18+
19+
if (!fs.existsSync(elfPath)) {
20+
console.log('Test ELF not found, creating a simple analyzer...');
21+
const analyzer = new ElfAnalyzer();
22+
console.log('✓ ElfAnalyzer created without files');
23+
process.exit(0);
24+
}
25+
26+
try {
27+
const analyzer = new ElfAnalyzer(elfPath);
28+
console.log('✓ Loaded ELF file:', elfPath);
29+
30+
// Get all symbols
31+
const allSymbols = analyzer.getAllSymbols();
32+
console.log(`\nFound ${allSymbols.length} total symbols`);
33+
34+
// Separate by type
35+
const functions = allSymbols.filter(s => s.type === 'FUNC');
36+
const objects = allSymbols.filter(s => s.type === 'OBJECT');
37+
38+
console.log(` - ${functions.length} FUNCTION symbols`);
39+
console.log(` - ${objects.length} OBJECT symbols`);
40+
41+
// Test a function symbol
42+
console.log('\n--- Testing FUNCTION Symbol ---');
43+
if (functions.length > 0) {
44+
const func = functions[0];
45+
console.log(`Testing: ${func.name}`);
46+
const funcWithDebug = analyzer.getSymbolWithDebugInfo(func.name);
47+
if (funcWithDebug) {
48+
console.log(` Type: ${funcWithDebug.type}`);
49+
console.log(` Has sourceFile: ${funcWithDebug.sourceFile ? 'YES' : 'NO'}`);
50+
console.log(` Has sourceLine: ${funcWithDebug.sourceLine ? 'YES' : 'NO'}`);
51+
if (funcWithDebug.sourceFile) {
52+
console.log(` Source: ${funcWithDebug.sourceFile}:${funcWithDebug.sourceLine}`);
53+
}
54+
}
55+
}
56+
57+
// Test an object symbol
58+
console.log('\n--- Testing OBJECT Symbol ---');
59+
if (objects.length > 0) {
60+
const obj = objects[0];
61+
console.log(`Testing: ${obj.name}`);
62+
const objWithDebug = analyzer.getSymbolWithDebugInfo(obj.name);
63+
if (objWithDebug) {
64+
console.log(` Type: ${objWithDebug.type}`);
65+
console.log(` Has sourceFile: ${objWithDebug.sourceFile ? 'YES' : 'NO'}`);
66+
console.log(` Has sourceLine: ${objWithDebug.sourceLine ? 'YES' : 'NO'}`);
67+
if (objWithDebug.sourceFile) {
68+
console.log(` ⚠️ WARNING: OBJECT should not have source info!`);
69+
console.log(` Source: ${objWithDebug.sourceFile}:${objWithDebug.sourceLine}`);
70+
} else {
71+
console.log(` ✓ Correctly has no source info (as expected for OBJECT)`);
72+
}
73+
}
74+
}
75+
76+
// Test specific known symbols if they exist
77+
console.log('\n--- Testing Known Symbols ---');
78+
const testSymbols = [
79+
{ name: 'main', expectedType: 'FUNC', shouldHaveSource: true },
80+
{ name: 'global_variable', expectedType: 'OBJECT', shouldHaveSource: false },
81+
{ name: 'global_array', expectedType: 'OBJECT', shouldHaveSource: false }
82+
];
83+
84+
testSymbols.forEach(test => {
85+
const symbol = analyzer.getSymbolWithDebugInfo(test.name);
86+
if (symbol) {
87+
const hasSource = !!(symbol.sourceFile);
88+
const correct = hasSource === test.shouldHaveSource;
89+
const status = correct ? '✓' : '✗';
90+
console.log(`${status} ${test.name} (${symbol.type}): source info = ${hasSource ? 'YES' : 'NO'}`);
91+
}
92+
});
93+
94+
console.log('\n✅ Test completed successfully!');
95+
96+
} catch (error) {
97+
console.error('Error:', error.message);
98+
if (error.message.includes('out of range')) {
99+
console.log('Note: DWARF parsing error, but the main functionality works');
100+
}
101+
process.exit(1);
102+
}

src/vue/analyze/App.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ const handleRowClick = (row: any, column: any, event: Event) => {
112112
113113
const handleRowDblclick = (row: any, column: any, event: Event) => {
114114
console.log('Row double-clicked:', row);
115+
116+
// 如果是 OBJECT 类型的符号,直接返回不处理
117+
if (row.type === 'OBJECT') {
118+
return;
119+
}
120+
115121
const symbolName = row.name;
116122
vscode.postMessage({
117123
eventName: OPEN_SYMBOL_SOURCE,

0 commit comments

Comments
 (0)