在Goldfish模拟器中测试嵌入式Linux内核驱动的ioctl调用
Got it, let's walk through how to test your Goldfish accelerometer driver's ioctl call step by step. I've messed around with Goldfish emulators for driver testing before, so here's what you need to do:
1. Write the User-Space Test Program
First, you need a simple C program that talks to your /dev/accelrometer device and triggers the TEST ioctl command. Critical note: you must define the TEST command exactly the same way as you did in the kernel driver—ioctl commands use a standardized format, and mismatches will cause failures.
Here's a working example:
#include <stdio.h> #include <fcntl.h> #include <sys/ioctl.h> #include <errno.h> #include <unistd.h> // Match this EXACTLY to the definition in your kernel driver! // For example, if your driver uses #define TEST _IO('a', 1), use the same here #define TEST _IO('a', 1) int main() { int device_fd; int result; // Open the accelerometer device file device_fd = open("/dev/accelrometer", O_RDWR); if (device_fd < 0) { perror("Failed to open /dev/accelrometer"); return errno; } // Send the TEST ioctl command to the driver result = ioctl(device_fd, TEST); if (result < 0) { perror("ioctl TEST command failed"); close(device_fd); return errno; } printf("ioctl TEST command sent successfully!\n"); close(device_fd); return 0; }
2. Cross-Compile for Goldfish (Android ARM)
Goldfish emulators run Android's ARM (or x86) binaries, so you can't compile this with your local desktop GCC. Use the Android NDK to cross-compile:
Option 1: Standalone Toolchain (Simpler for single files)
- Download a stable Android NDK (I recommend r21e, as newer versions may have compatibility issues with older Goldfish kernels)
- Generate an ARM standalone toolchain:
$NDK_ROOT/build/tools/make_standalone_toolchain.py --arch arm --api 21 --install-dir ~/android-arm-toolchain - Compile your test program:
~/android-arm-toolchain/bin/arm-linux-androideabi-gcc test_ioctl.c -o test_ioctl
Option 2: ndk-build (For larger projects)
Create an Android.mk file in the same directory as your test code:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := test_ioctl LOCAL_SRC_FILES := test_ioctl.c include $(BUILD_EXECUTABLE)
Then run:
ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk
Your compiled binary will be in libs/armeabi-v7a/test_ioctl.
3. Run the Program on the Goldfish Emulator
- Start your Goldfish emulator, and make sure your driver is loaded (either compiled into the kernel, or loaded via
insmodif it's a module). - Push the compiled binary to the emulator:
adb push test_ioctl /data/local/tmp/ - Grant execute permissions:
adb shell chmod +x /data/local/tmp/test_ioctl - Run the program (you'll need root access to interact with
/devdevices):adb shell su -c "/data/local/tmp/test_ioctl"
4. Verify the Ioctl Worked
Check the kernel log to confirm your driver's print statement showed up:
adb shell dmesg | grep "Hello world ioctl!"
If you see the message, your ioctl call was successful!
Quick Troubleshooting
- Failed to open device: Check if
/dev/accelrometerexists withadb shell ls -l /dev/accelrometer. If it's there but permission denied, runadb shell chmod 666 /dev/accelrometerto temporarily open permissions. - ioctl failed: Double-check that your
TESTcommand definition matches the driver exactly. Also, update your kernel driver's ioctl function to return proper values (right now it has no return statement—addreturn 0;after the printk, and adefault: return -EINVAL;case to handle unknown commands). - Cross-compile errors: Make sure your NDK API level matches the emulator's Android version (e.g., Android 5.0 uses API 21).
内容的提问来源于stack exchange,提问作者Wenlong.Wang




