執行建構時使用的 lunch 指令。請注意,lunch 項目應與您要偵錯的裝置完全相符。如果午餐項目與已連結的裝置不符,您會收到以下格式的錯誤訊息:
You used the wrong lunch: TARGET_PRODUCT (aosp_arm64) does not match attached device (xyzabc)
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Use debuggers\n\nThis page details using [LLDB](https://lldb.llvm.org/)\nfor OS development.\nFor app development, see\n[Debug your app](https://developer.android.com/studio/debug/index)\ninstead, which explains how to use the Android Studio GUI (based on LLDB).\n\nGDB is no longer supported or provided. If you're switching from GDB to LLDB, you should\nprobably start by reading the\n[LLDB Tutorial](https://lldb.llvm.org/use/tutorial.html).\nIf you're an expert GDB user, the\n[GDB to LLDB command map](https://lldb.llvm.org/use/map.html)\nis very helpful while transitioning.\n\nPrerequisites\n-------------\n\nTo use a debugger:\n\n- Set up the build environment with the usual `envsetup.sh` command.\n- Run the same `lunch` command you used when building. Note that the lunch item should exactly match the device you are debugging. If the lunch item doesn't match with the attached device, you'll get an error of the form: `You used the wrong lunch: TARGET_PRODUCT (aosp_arm64) does not match attached device (xyzabc)`\n- Connect your device to machine.\n\nFor more help with setting up your environment, see\n[Set up environment](/docs/setup/build/building#initialize).\n\nDebug a binary\n--------------\n\n\nTo debug a binary that you built on your machine, first you'll have to copy the binary to the device\nand then launch the debugger. For example: \n\n adb push test.exe /data/local/tmp/test.exe\n lldbclient.py --port 5038 -r /data/local/tmp/test.exe\n\nDebug running apps or processes\n-------------------------------\n\nTo connect to a running app or native daemon, use\n`lldbclient.py` with a PID. For example, to debug the process with PID\n1234, run this on the host:\n**Note:** Do not confuse `-p` with `--port`. `-p` is for PID and `--port` is used to supply an unused port number. See `lldbclient.py --help` for more info. \n\n```\nlldbclient.py -p 1234\n```\n\nThe script sets up port forwarding, starts the appropriate\nremote debugging stub on the device, starts the debugger on\nthe host, configures it to find symbols, and connects\nit to the remote debugging stub.\n| **Note:** In Android 6 and lower the script was a shell script called `lldbclient` instead of a Python script called `lldbclient.py`.\n\nDebug native process startup\n----------------------------\n\nTo debug a process as it starts, use `lldbclient.py` with the `-r`\noption. For example, to debug `ls /bin`, run this on the host: \n\n```\nlldbclient.py -r /system/bin/ls /bin\n```\n\nThen, enter `continue` at the debugger's prompt.\n\nDebug app startup\n-----------------\n\nSometimes you want to debug an app as it starts, such as when there's a crash\nand you want to step through code to see what happened *before* the crash.\n[Attaching](#running) works in some cases, but in other cases is\nimpossible because the app crashes before you can attach. The\n`logwrapper` approach (used for `strace`)\ndoesn't always work because the app might not have\npermissions to open a port, and `lldbserver` inherits that\nrestriction.\n\nTo debug app startup, use the developer options in Settings to instruct\nthe app to wait for a Java debugger to attach:\n\n1. Go to **Settings \\\u003e Developer options \\\u003e Select debug app** and choose your app from the list, then click **Wait for debugger**.\n2. Start the app, either from the launcher or by using the command line to run: \n\n ```\n adb shell am start -a android.intent.action.MAIN -n APP_NAME/.APP_ACTIVITY\n ```\n3. Wait for the app to load and a dialog to appear telling you the app is waiting for a debugger.\n4. Attach `lldbserver`/`lldbclient` normally, set breakpoints, then continue the process.\n\nTo let the app run, attach a Java Debug Wire Protocol (JDWP)\ndebugger such as Java Debugger (jdb): \n\n adb forward tcp:12345 jdwp:\u003cvar translate=\"no\"\u003eXXX\u003c/var\u003e # (Where XXX is the PID\n of the debugged process.)\n jdb -attach localhost:12345\n\nDebug apps or processes that crash\n----------------------------------\n\nIf you want `debuggerd` to suspend crashed processes so that you can\nattach a debugger, set the appropriate property:\n\n- After Android 11 \n\n adb shell setprop debug.debuggerd.wait_for_debugger true\n\n- Android 11 and lower \n\n adb shell setprop debug.debuggerd.wait_for_gdb true\n\n- Android 6.0 Marshmallow and lower \n\n adb shell setprop debug.db.uid 999999\n\nAt the end of the usual crash output, `debuggerd` provides copy and paste\ninstructions in logcat showing how to connect the debugger to the crashed process.\n\nDebug with VS Code\n------------------\n\nLLDB supports debugging platform code on\n[Visual Studio Code](https://code.visualstudio.com/).\nYou can use the VS Code debugger frontend instead of the LLDB CLI interface to control and\ndebug native code running on devices.\n\nBefore using VS Code for debugging, install the\n[CodeLLDB extension](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb).\n\nTo debug code using VS Code:\n\n1. Ensure that all build artifacts (such as symbols) required to run `lldbclient.py` or `lldbclient.py` are present.\n2. In VS Code, press **Ctrl+Shift+P** to run a command, search for **Debug:\n Add Configuration...** , then select **LLDB** . This opens a `launch.json` file and adds a new JSON object to a list.\n3. Replace the newly added debugger configuration with two comment lines - `// #lldbclient-generated-begin` and `// #lldbclient-generated-end`, so that your configuration list looks like this: \n\n ```json\n \"configurations\": [\n // #lldbclient-generated-begin\n // #lldbclient-generated-end\n ]\n ```\n\n `lldbclient.py` uses these comments to detect where to write the config. If there\n are other items in the list, add the comment lines to the end after the other configurations.\n4. Run the following command in the terminal where you've run `envsetup.sh` and `lunch`: \n\n lldbclient.py --setup-forwarding vscode-lldb \\\n --vscode-launch-file \u003cvar translate=\"no\"\u003eLAUNCH_JSON_PATH\u003c/var\u003e \\\n \u003cvar translate=\"no\"\u003eANY_OTHER_FLAGS\u003c/var\u003e \u003cvar translate=\"no\"\u003e-p pid | -n proc-name | -r ...\u003c/var\u003e\n\n `lldbclient.py` writes the generated config into `launch.json` and\n continues running. This is expected; don't kill the `lldbclient.py` program. If you\n omit the `--vscode-launch-file` the script will print the JSON snippet that you will\n need to copy and paste into `launch.json` manually.\n\n The `-r` flag must be the last flag if it is present due to how flags are parsed\n by the tool.\n5. Open the **Run and Debug** side bar - the new configuration should appear in the debugger list. Press **Start Debugging (F5)** . The debugger should connect after 10 to 30 seconds.\n\n If the new configuration did not appear in the Run and Debug view, reload the window to\n refresh the debugger list - press **Ctrl+Shift+P** and type\n `reload window`.\n6. When you're done debugging, go to the terminal running `lldbclient.py` and press **Enter** to end the `lldbclient.py` program. Subsequent runs of the script would generate the config between the `#lldbclient-generated` comments and replace the old contents, you do not need to remove them manually.\n\nTo add custom properties to the generated launch config, you can use the\n`--vscode-launch-props` flag. For example: \n\n lldbclient.py --setup-forwarding vscode-lldb \\\n --vscode-launch-props \\\n '{\"initCommands\" : [\"script print(\\\"Hello\\\")\"], \"preLaunchTask\" : \"Build\"}' \\\n \u003cvar translate=\"no\"\u003e...\u003c/var\u003e\n\nThe example properties would make VS Code run a task named `Build` before debugging and appends a new debug initialization step to the steps generated by the script. You can find an overview of available properties in the [VS Code documentation](https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes) and in the User Manual of the [CodeLLDB extension](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb).\n\n\u003cbr /\u003e"]]