Skip to content

Commit

Permalink
reduce hardcoded arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhiy2001 committed Jan 15, 2024
1 parent fad0a4f commit 2d39d51
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 26 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ Qemu虚拟机运行rCore-Tutorial操作系统,本项目中Qemu开启了gdbstub

## 调试工具实现

TODO: 这一节所列出的代码有**少量**变动,应当更新

### 常用API、GDB命令

#### TreeView命令注册
Expand Down Expand Up @@ -720,15 +722,18 @@ export PATH=$PATH:/home/path/to/riscv64-linux-musl-cross/bin
"-s",
"-S"
],
"userSpaceDebuggeeFolder":"${userHome}/rCore-Tutorial-v3/user/target/riscv64gc-unknown-none-elf/release/",
"KERNEL_IN_BREAKPOINTS_LINE":65, // src/trap/mod.rs中内核入口行号。可能要修改
"KERNEL_OUT_BREAKPOINTS_LINE":124, // src/trap/mod.rs中内核出口行号。可能要修改
"GO_TO_KERNEL_LINE":30, // src/trap/mod.rs中,用于从用户态返回内核的断点行号。在rCore-Tutorial-v3中,这是set_user_trap_entry函数中的stvec::write(TRAMPOLINE as usize, TrapMode::Direct);语句。
"KERNEL_IN_BREAKPOINTS_FILENAME":"src/trap/mod.rs",
"KERNEL_OUT_BREAKPOINTS_FILENAME":"src/trap/mod.rs",
"GO_TO_KERNEL_FILENAME":"src/trap/mod.rs"
},
]
}
```

- 这里解释一下`KERNEL_IN_BREAKPOINTS_LINE``GO_TO_KERNEL_LINE`的区别。以rCore-Tutorial-v3为例,`KERNEL_IN_BREAKPOINTS_LINE`对应`trap_return`函数的断点,而`GO_TO_KERNEL_LINE`对应`trap_return`函数调用的`set_user_trap_entry`子函数的断点。而`set_user_trap_entry`子函数实际上只有一行语句:`stvec::write(TRAMPOLINE as usize, TrapMode::Direct);`。也就是说,`KERNEL_IN_BREAKPOINTS_LINE`指向中断处理例程,而`GO_TO_KERNEL_LINE`精确地指向中断处理例程中的`stvec::write(TRAMPOLINE as usize, TrapMode::Direct);`语句。
1. 为了用eBPF Panel,需要在rCore-Tutorial-v3的根目录下添加一个脚本:

```shell
Expand Down
75 changes: 75 additions & 0 deletions docs/2024-01-14.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
## 用 code-debug 调试 x86 Starry

首先,我们将Makefile中的 `MODE ?= release` 改为 `MODE ?= debug`

其次,将主目录中的Cargo.toml里的`lto = true`注释掉

接着,将`modules/axdriver/build.rs`里的`.align 16`改为`.align 4096`

最后,运行Starry并输出到一个文本文件中:

```
./build_img.sh sdcard
make A=apps/oscomp LOG=debug QEMU_LOG=y ARCH=x86_64 run > output.txt
```

打开`output.txt`并将Qemu的启动参数修改为`launch.json`

```json
//launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "launch",
"name": "Attach to Qemu",
"executable": "${userHome}/Starry-x86/Starry/apps/oscomp/oscomp_x86_64-qemu-q35.elf",
"target": ":1234",//不能和Qemu开放的tcp端口重叠
"remote": true,
"cwd": "${workspaceRoot}",
"valuesFormatting": "parseText",
"gdbpath": "gdb-multiarch",
"showDevDebugOutput":true,
"internalConsoleOptions": "openOnSessionStart",
"printCalls": true,
"stopAtConnect": true,
"qemuPath": "qemu-system-x86_64",
"qemuArgs": [
"-m",
"2G",
"-smp",
"1",
"-machine",
"q35",
"-kernel",
"apps/oscomp/oscomp_x86_64-qemu-q35.elf",
"-device",
"virtio-blk-pci,drive=disk0",
"-drive",
"id=disk0,if=none,format=raw,file=disk.img",
"-device",
"virtio-net-pci,netdev=net0",
"-netdev",
"user,id=net0,hostfwd=tcp::1235-:5555,hostfwd=udp::5555-:5555",
"-nographic",
"-s",
"-S"
],
"userSpaceDebuggeeFolder": "${userHome}/Starry-x86/Starry/testcases/sdcard/bin/",
"KERNEL_IN_BREAKPOINTS_LINE":12, // src/trap/mod.rs中内核入口行号。可能要修改
"KERNEL_OUT_BREAKPOINTS_LINE":770, // src/trap/mod.rs中内核出口行号。可能要修改
"GO_TO_KERNEL_LINE":30, // src/trap/mod.rs中,用于从用户态返回内核的断点行号。在rCore-Tutorial-v3中,这是set_user_trap_entry函数中的stvec::write(TRAMPOLINE as usize, TrapMode::Direct);语句。
"KERNEL_IN_BREAKPOINTS_FILENAME":"/home/oslab/Starry-x86/Starry/modules/axhal/src/arch/x86_64/trap.rs",
"KERNEL_OUT_BREAKPOINTS_FILENAME":"/home/oslab/Starry-x86/Starry/modules/axtask/src/task.rs",
"GO_TO_KERNEL_FILENAME":""
},
]
}
```

一定要debug模式。release模式没有文件名

riscv版本
离谱:debug一定要用lto
Failed to get Stack Trace: PC not saved (from stack-info-depth --thread 1)
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,11 @@
"-S"
]
},
"userSpaceDebuggeeFiles": {
"type": "array",
"description": "userspace executable name which will be debugged",
"default": [
"initproc"
]
"userSpaceDebuggeeFolder": {
"type": "string",
"description": "userspace executable folder which executables inside will be debugged",
"default":
"please_configure_userSpaceDebuggeeFolder"
}
}
}
Expand Down
34 changes: 23 additions & 11 deletions src/frontend/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import * as vscode from "vscode";
import * as os from "os";
import * as ChildProcess from "child_process";

//=========================================================================================
let userDebugFile = "initproc"; //可以修改为其它用户程序名,如matrix
let userConf = {// Default values are suitable for rCore-Tutorial-v3 since it is most commonly used
KERNEL_IN_BREAKPOINTS_LINE:65,
KERNEL_OUT_BREAKPOINTS_LINE:124,
GO_TO_KERNEL_LINE:30,
KERNEL_IN_BREAKPOINTS_FILENAME:"src/trap/mod.rs",
KERNEL_OUT_BREAKPOINTS_FILENAME:"src/trap/mod.rs",
GO_TO_KERNEL_FILENAME:"src/trap/mod.rs",
executable:"",//executable with symbol. absolute path
userSpaceDebuggeeFolder:""//absolute path
}
//========================================================================================

export function activate(context: vscode.ExtensionContext) {

// Only allow a single Panel
Expand Down Expand Up @@ -113,7 +127,7 @@ export function activate(context: vscode.ExtensionContext) {
ChildProcess.exec('nm '+vscode.workspace.workspaceFolders[0].uri.path+'/os/target/riscv64gc-unknown-none-elf/release/'+'os'+' | rustfilt |grep '+selectedText,
(err, stdout, stderr) => {
console.log('stdout: ' + stdout);
panel.webview.postMessage({ command: 'symbol_table_update',text:stdout.split('\n'),program_name:'kernel'});
panel.webview.postMessage({ command: 'symbol_table_update',text:stdout.split('\n'),program_name:"kernel"});
//console.log('stdout: ' + stdout);
if(stderr){
console.log('stderr in registering selected symbol: ' + stderr)
Expand All @@ -137,17 +151,13 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand("code-debug.examineMemoryLocation", examineMemory)
);

//=========================================================================================
const kernelInOutBreakpointArgs = 1;
let userDebugFile = "initproc"; //可以修改为其它用户程序名,如matrix
const your_path_to_core = os.homedir() + "/rCore-Tutorial-v3-eBPF/rCore-Tutorial-v3"; //tag:oscomp2023 org:/rCore-Tutorial-v3
//========================================================================================


const removeDebugFileCmd = vscode.commands.registerCommand("code-debug.removeDebugFile", () => {
// 自定义请求.customRequest函数见/src/mibase.ts
vscode.debug.activeDebugSession?.customRequest("removeDebugFile", {
debugFilepath:
your_path_to_core + "/user/target/riscv64gc-unknown-none-elf/release/"+userDebugFile,
userConf.userSpaceDebuggeeFolder+userDebugFile,
});
// 弹出窗口
vscode.window.showInformationMessage("symbol file "+userDebugFile+" removed");
Expand Down Expand Up @@ -244,7 +254,7 @@ export function activate(context: vscode.ExtensionContext) {
vscode.window.showInformationMessage("will switched to " + userDebugFile + " breakpoints");
vscode.debug.activeDebugSession?.customRequest("addDebugFile", {
debugFilepath:
your_path_to_core + "/user/target/riscv64gc-unknown-none-elf/release/" +userDebugFile,
userConf.userSpaceDebuggeeFolder +userDebugFile,
//tag:oscomp2023 original "/user/target/riscv64gc-unknown-none-elf/release/" +userDebugFile,

});
Expand All @@ -266,12 +276,11 @@ export function activate(context: vscode.ExtensionContext) {
//vscode.window.showInformationMessage("switched to trap_handle");
vscode.debug.activeDebugSession?.customRequest("addDebugFile", {
debugFilepath:
your_path_to_core +
"/os/target/riscv64gc-unknown-none-elf/release/os",
userConf.executable,
});
vscode.debug.activeDebugSession?.customRequest(
"updateCurrentSpace",
"src/trap/mod.rs"
"kernel"//TODO this value used to be src/trap/mod.rs which means border breakpoints belongs to a unique space. now we changed it to 'kernel'
);
vscode.window.showInformationMessage("go to kernel trap_handle");
}
Expand Down Expand Up @@ -334,6 +343,9 @@ export function activate(context: vscode.ExtensionContext) {
vscode.window.showInformationMessage("new process "+userDebugFile+" updated");
}

}
else if (message.event === "c"){

}
}
},
Expand Down
23 changes: 22 additions & 1 deletion src/gdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
showDevDebugOutput: boolean;
qemuPath: string;
qemuArgs: string[];
userSpaceDebuggeeFiles: string[];
userSpaceDebuggeeFolder: string;
KERNEL_IN_BREAKPOINTS_LINE:number;
KERNEL_OUT_BREAKPOINTS_LINE:number;
GO_TO_KERNEL_LINE:number;
KERNEL_IN_BREAKPOINTS_FILENAME:string;
KERNEL_OUT_BREAKPOINTS_FILENAME:string;
GO_TO_KERNEL_FILENAME:string;

//更新新参数,然后更新文档
}

let NEXT_TERM_ID = 1;
Expand Down Expand Up @@ -75,6 +80,22 @@ export class GDBDebugSession extends MI2DebugSession {
this.KERNEL_IN_BREAKPOINTS_LINE=args.KERNEL_IN_BREAKPOINTS_LINE;
this.KERNEL_OUT_BREAKPOINTS_LINE=args.KERNEL_OUT_BREAKPOINTS_LINE;
this.GO_TO_KERNEL_LINE=args.GO_TO_KERNEL_LINE;
this.KERNEL_IN_BREAKPOINTS_FILENAME=args.KERNEL_IN_BREAKPOINTS_FILENAME;
this.KERNEL_OUT_BREAKPOINTS_FILENAME=args.KERNEL_OUT_BREAKPOINTS_FILENAME;
this.GO_TO_KERNEL_FILENAME=args.GO_TO_KERNEL_FILENAME;

this.sendEvent({ event: "userConfInfo", body:{
KERNEL_IN_BREAKPOINTS_LINE:args.KERNEL_IN_BREAKPOINTS_LINE, // src/trap/mod.rs中内核入口行号。可能要修改
KERNEL_OUT_BREAKPOINTS_LINE:args.KERNEL_OUT_BREAKPOINTS_LINE, // src/trap/mod.rs中内核出口行号。可能要修改
GO_TO_KERNEL_LINE:args.GO_TO_KERNEL_LINE, // src/trap/mod.rs中,用于从用户态返回内核的断点行号。在rCore-Tutorial-v3中,这是set_user_trap_entry函数中的stvec::write(TRAMPOLINE as usize, TrapMode::Direct);语句。
KERNEL_IN_BREAKPOINTS_FILENAME:args.KERNEL_IN_BREAKPOINTS_FILENAME,
KERNEL_OUT_BREAKPOINTS_FILENAME:args.KERNEL_OUT_BREAKPOINTS_FILENAME,
GO_TO_KERNEL_FILENAME:args.GO_TO_KERNEL_FILENAME,
executable:args.executable,
userSpaceDebuggeeFolder:args.userSpaceDebuggeeFolder


} } as DebugProtocol.Event);

this.runInTerminalRequest(
{
Expand Down
16 changes: 10 additions & 6 deletions src/mibase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ export class MI2DebugSession extends DebugSession {
public KERNEL_IN_BREAKPOINTS_LINE;//TODO THIS SHOULD HAVE BEEN IN gdb.ts. However Codes that uses those stuff are all in mibase.ts. :(
public KERNEL_OUT_BREAKPOINTS_LINE;
public GO_TO_KERNEL_LINE;
public KERNEL_IN_BREAKPOINTS_FILENAME;//those names are really confusing :(
public KERNEL_OUT_BREAKPOINTS_FILENAME;
public GO_TO_KERNEL_FILENAME;



public constructor(debuggerLinesStartAt1: boolean, isServer: boolean = false) {
Expand Down Expand Up @@ -344,7 +348,7 @@ example: {"token":43,"outOfBandRecord":[],"resultRecords":{"resultClass":"done",
this.addressSpaces.updateCurrentSpace("kernel");
this.sendEvent({ event: "inKernel" } as DebugProtocol.Event);
if (
info.outOfBandRecord[0].output[3][1][3][1] === "src/trap/mod.rs" &&
info.outOfBandRecord[0].output[3][1][3][1] === this.KERNEL_OUT_BREAKPOINTS_FILENAME &&
info.outOfBandRecord[0].output[3][1][5][1] === this.KERNEL_OUT_BREAKPOINTS_LINE+""
) {
this.sendEvent({ event: "kernelToUserBorder" } as DebugProtocol.Event);
Expand Down Expand Up @@ -524,7 +528,7 @@ example: {"token":43,"outOfBandRecord":[],"resultRecords":{"resultClass":"done",
const spaceName = this.addressSpaces.pathToSpaceName(path);
//保存断点信息,如果这个断点不是当前空间的(比如还在内核态时就设置用户态的断点),暂时不通知GDB设置断点
//如果这个断点是当前地址空间,或者是内核入口断点,那么就通知GDB立即设置断点
if ((spaceName === this.addressSpaces.getCurrentSpaceName()) || (path === "src/trap/mod.rs" && args.breakpoints[0].line === this.GO_TO_KERNEL_LINE)
if ((spaceName === this.addressSpaces.getCurrentSpaceName()) || (path === this.GO_TO_KERNEL_FILENAME && args.breakpoints[0].line === this.GO_TO_KERNEL_LINE)
) {
// TODO rules can be set by user
this.addressSpaces.saveBreakpointsToSpace(args, spaceName); }
Expand Down Expand Up @@ -1280,16 +1284,16 @@ example: {"token":43,"outOfBandRecord":[],"resultRecords":{"resultClass":"done",
this.setBreakPointsRequest(
response as DebugProtocol.SetBreakpointsResponse,
{
source: { path: "src/trap/mod.rs" } as DebugProtocol.Source,
breakpoints: [{ line: this.KERNEL_IN_BREAKPOINTS_LINE }] as DebugProtocol.SourceBreakpoint[],
source: { path: this.KERNEL_IN_BREAKPOINTS_FILENAME } as DebugProtocol.Source,
breakpoints: [{ line: this.KERNEL_IN_BREAKPOINTS_LINE }] as DebugProtocol.SourceBreakpoint[], //TODO change this to GO_TO_KERNEL?
} as DebugProtocol.SetBreakpointsArguments
);
break;
case "setKernelOutBreakpoints": //remove previous breakpoints in this source
this.setBreakPointsRequest(
response as DebugProtocol.SetBreakpointsResponse,
{
source: { path: "src/trap/mod.rs" } as DebugProtocol.Source,
source: { path: this.KERNEL_OUT_BREAKPOINTS_FILENAME } as DebugProtocol.Source,
breakpoints: [{ line: this.KERNEL_OUT_BREAKPOINTS_LINE }] as DebugProtocol.SourceBreakpoint[],
} as DebugProtocol.SetBreakpointsArguments
);
Expand All @@ -1306,7 +1310,7 @@ example: {"token":43,"outOfBandRecord":[],"resultRecords":{"resultClass":"done",
this.setBreakPointsRequest(
response as DebugProtocol.SetBreakpointsResponse,
{
source: { path: "src/trap/mod.rs" } as DebugProtocol.Source,
source: { path: this.GO_TO_KERNEL_FILENAME } as DebugProtocol.Source,
breakpoints: [{ line: this.GO_TO_KERNEL_LINE }] as DebugProtocol.SourceBreakpoint[],
} as DebugProtocol.SetBreakpointsArguments
);
Expand Down

0 comments on commit 2d39d51

Please sign in to comment.