对于希望深入理解 wine 内部实现的开发者来说,从一个简单的 windows 代码示例着手,逐步追踪其在 linux 系统中的执行流程,无疑是最佳的学习路径。本文将指导你如何通过编译一个经典的 “hello, world!” windows 程序,并利用 wine 的调试工具,来揭开 wine 将 windows api 调用转换为 linux 系统调用的神秘面纱。
Wine 工作原理简介
首先,我们需要明确 Wine 的核心工作机制。Wine(Wine Is Not an Emulator,即 Wine 不是模拟器)是一个能够在多种 POSIX 兼容操作系统(如Linux、macOS 和 BSD)上运行 Windows 应用程序的兼容层。 它并非像虚拟机或模拟器那样模拟整个 Windows 操作系统,而是将 Windows API 调用动态地转换为相应的 POSIX 调用,从而消除了性能和内存上的额外开销,并允许 Windows 应用程序无缝集成到桌面环境中。
Wine主要由以下几个部分构成:
一组实现了Windows API的Linux库: 这是 Wine 的核心,包含了大量 Windows DLL 的开源实现。wineserver: 一个实现 Windows 核心功能(如进程和线程管理、窗口管理等)的服务器进程。加载器(loader): 负责加载 Windows 可执行文件(.exe),并将其链接到实现了 Windows API 的 Linux 库上。
实践步骤:从“Hello, Wine!”开始
现在,让我们通过一个实际操作来深入理解这一过程。
第一步:准备 Windows 示例代码
我们将从一个最简单的 Windows “Hello, Wine!” 程序开始。你可以使用 C 语言创建一个显示消息框的简单程序。
这是一个经典的 Win32 API “Hello, Wine!” 示例 (
hello.c
):
#include int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){ MessageBox(NULL, TEXT("Hello, Wine!"), TEXT("HelloMsg"), 0); return 0;}
第二步:在Linux上编译 Windows 程序
为了避免切换到 Windows 操作系统,我们在 Linux 上编译生成一个 Windows 程序。要在 Linux 上编译此代码以生成 Windows 可执行文件,需要安装 MinGW-w64 交叉编译工具链。 大多数 Linux 发行版的软件包管理器中都提供了 MinGW-w64 。例如,在统信 UOS 或 deepin Linux 上,你可以通过以下命令安装:
是 Wine 内置的调试器。 它可以让你像使用 GDB 一样,在 Wine 环境中单步执行 Windows 程序、设置断点、查看内存和寄存器等。
你可以像这样启动
winedbg
来调试您的程序:
winedbg hello.exewinedbg> b MessageBoxa
winedbg> cont
程序将在调用
MessageBoxA
时中断,此时你可以检查调用堆栈、变量等信息,从而更深入地了解 Wine 是如何处理这个 API 调用的。
Wine-dbg>b MessageBoxA013c:fixme:dbghelp_dwarf:dwarf2_parse_compilation_unit Should have a compilation unit here 0Breakpoint 1 at 0x007f8495fcef60 MessageBoxA [/home/builder/wine-src/wine.win64/../dlls/user32/msgbox.c:390] in user32Wine-dbg>00c0:err:clipboard:convert_selection Timed out waiting for SelectionNotify eventcon00c0:err:clipboard:convert_selection Timed out waiting for SelectionNotify eventcont013c:fixme:dbghelp:elf_search_auxv can't find symbol in module013c:fixme:dbghelp_dwarf:dwarf2_get_cie wrong CIE pointer at 0 from FDE 279c013c:fixme:dbghelp_dwarf:dwarf2_get_cie wrong CIE pointer at 0 from FDE 279c013c:fixme:dbghelp_dwarf:dwarf2_get_cie wrong CIE pointer at 0 from FDE 1974Stopped on breakpoint 1 at 0x007f8495fcef60 MessageBoxA [/home/builder/wine-src/wine.win64/../dlls/user32/msgbox.c:390] in user32MessageBoxA () at /home/builder/wine-src/wine.win64/../dlls/user32/msgbox.c:390Unable to access file '/home/builder/wine-src/wine.win64/../dlls/user32/msgbox.c'Wine-dbg> btBacktrace:013c:fixme:dbghelp_dwarf:dwarf2_get_cie wrong CIE pointer at 0 from FDE 279c013c:fixme:dbghelp_dwarf:dwarf2_get_cie wrong CIE pointer at 0 from FDE 279c013c:fixme:dbghelp_dwarf:dwarf2_get_cie wrong CIE pointer at 0 from FDE 279c013c:fixme:dbghelp_dwarf:dwarf2_get_cie wrong CIE pointer at 0 from FDE 279c=>0 0x007f8495fcef60 MessageBoxA(hWnd=, text=, title=, type=) [/home/builder/wine-src/wine.win64/../dlls/user32/msgbox.c:390] in user32 (0x007ffffe2ffdb0) 1 0x0000014000148d in hello (+0x148d) (0x007ffffe2ffdb0) 2 0x000001400012ee in hello (+0x12ee) (0x007ffffe8e4210) 3 0x00000140001406 in hello (+0x1406) (0x007ffffe2ffe60)013c:fixme:dbghelp_dwarf:dwarf2_get_cie wrong CIE pointer at 0 from FDE 1974013c:fixme:dbghelp_dwarf:dwarf2_get_cie wrong CIE pointer at 0 from FDE 1974013c:fixme:dbghelp_dwarf:dwarf2_get_cie wrong CIE pointer at 0 from FDE 1974 4 0x007f849656a711 BaseThreadInitThunk+0x11(unknown=, entry=, arg=) [/home/builder/wine-src/wine.win64/../dlls/kernel32/thread.c:61] in kernel32.dll.so (0x007ffffe2ffe60)
小结
通过从一个简单的 Windows 示例代码入手,结合 Wine 提供的强大调试和追踪工具,你可以清晰地观察到 Wine 将 Windows API 调用转换为 Linux 等价实现的全过程。这个过程不仅能帮助你深入理解 Wine 的工作原理,更能为你将来为 Wine 贡献代码或解决应用程序兼容性问题打下坚实的基础。
给滚动元素平滑过渡 如何在滚动条属性(scrollleft)发生改变时为元素添加平滑的过渡效果? 解决方案:scroll-behavior 属性 为滚动容器设置 scroll-behavior 属性可以实现平滑滚动。 html 代码: click the button to slide right!…
旋转后长方形与画布轴距计算 在给定的画布中,有一个长方形,在随机旋转一定角度后,如何计算其在画布上的轴距,即距离左上角的距离? 以下提供一种计算长方形相对于画布左上角的新轴距的方法: const x = 200; // 初始 x 坐标const y = 90; // 初始 y 坐标const w =…
如何在前端实现 Windows 10 设置界面中的鼠标移动探照灯效果 想要在前端开发中实现 Windows 10 设置界面中类似的鼠标移动探照灯效果,可以通过以下途径: CSS 解决方案 DEMO 1: Windows 10 网格悬停效果:https://codepen.io/tr4553r7/pe…