Binder驅動程序的初始化是在方法binder_init中,和羅老師列出的代碼有些區別,不過內容基本相同。 函數
static int __init binder_init(void) { int ret; //建立一個工做隊列 binder_deferred_workqueue = create_singlethread_workqueue("binder"); if (!binder_deferred_workqueue) return -ENOMEM; //建立 /proc/binder 文件夾 binder_debugfs_dir_entry_root = debugfs_create_dir("binder", NULL); if (binder_debugfs_dir_entry_root)//建立 /proc/binder/proc文件夾 binder_debugfs_dir_entry_proc = debugfs_create_dir("proc", binder_debugfs_dir_entry_root); ret = misc_register(&binder_miscdev); if (binder_debugfs_dir_entry_root) { debugfs_create_file("state", S_IRUGO, binder_debugfs_dir_entry_root, NULL, &binder_state_fops); debugfs_create_file("stats", S_IRUGO, binder_debugfs_dir_entry_root, NULL, &binder_stats_fops); debugfs_create_file("transactions", S_IRUGO, binder_debugfs_dir_entry_root, NULL, &binder_transactions_fops); debugfs_create_file("transaction_log", S_IRUGO, binder_debugfs_dir_entry_root, &binder_transaction_log, &binder_transaction_log_fops); debugfs_create_file("failed_transaction_log", S_IRUGO, binder_debugfs_dir_entry_root, &binder_transaction_log_failed, &binder_transaction_log_fops); } return ret; } device_initcall(binder_init);
首先在目標設備上建立了一個/proc/binder/proc目錄,每個使用了Binder進程間通訊機制的進程在該目錄下都對應有一個文件,這些文件是以進程ID來命名的,經過它們就能夠讀取到各個進程的Binder線程池、Binder實體對象、Binder引用對象以及內核緩衝區等信息。線程
而後if語句塊又在/proc/binder目錄下建立了五個文件state、stats、transactions、transaction_log和failed_transaction_log,經過這五個文件就能夠讀取到Binder驅動程序的運行情況。例如,各個命令協議(BinderDriverCommandProtocol)和返回協議(BinderDriverReturnProtocol)的請求次數、日誌記錄信息,以及正在執行進程間通訊過程的進程信息等。debug
剩下就一個misc_register方法,它是用來建立一個misc類型的字符設備的,這個設備的屬性是根據以下參數來決定的日誌
static const struct file_operations binder_fops = { .owner = THIS_MODULE, .poll = binder_poll, .unlocked_ioctl = binder_ioctl, .compat_ioctl = binder_ioctl, .mmap = binder_mmap, .open = binder_open, .flush = binder_flush, .release = binder_release, }; static struct miscdevice binder_miscdev = { .minor = MISC_DYNAMIC_MINOR, .name = "binder", .fops = &binder_fops };
Binder驅動程序在目標設備上建立了一個Binder設備文件/dev/binder,這個設備文件的操做方法列表是由全局變量binder_fops指定的。全局變量binder_fops爲Binder設備文件/dev/binder指定文件打開、內存映射和IO控制函數分別爲binder_open、binder_mmap和binder_ioctl。code