Let's look at another function, kdb_init (). Obviously, this function is the entrance of the entire KDB, or the initialization function.
12393 + 12394 +/* 12395 + * kdb_init 12396 + * 12397 + * initialize the kernel debugger environment. 12398 + * 12399 + * parameters: 12400 + * none. 12401 + * returns: 12402 + * none. 12403 + * locking: 12404 + * none. 12405 + * remarks: 12406 + * none. 12407 + */12408 + 12409 + void _ init 12410 + kdb_init (void) 12411 + {12412 + kdb_initial_cpu = smp_processor_id (); 12413 +/* 12414 + * This must B E called before any callto kdb_printf. 12415 + */12416 + kdb_io_init (); 12417 + 12418 + kdb_inittab ();/* initialize command table */12419 + kdb_initbptab (); /* initialize breakpoint table */12420 + kdb_id_init ();/* initialize disassembler */12421 + kdba_init (); /* architecture dependent initialization */12422 + 12423 +/* 12424 + * use printk () to get message in log_buf []; 12425 + */1242 6 + printk ("KDB version % d. % d % s by Keith Owens, Scott lurndal. "/12427 +" Copyright SGI, All Rights Reserved/N ", 12428 + kdb_major_version, kdb_minor_version, kdb_test_version); 12429 + 12430 + kdb_cmd_init (); /* preset commands from kdb_cmds */12431 + kdb_initial_cpu =-1;/* avoid recursion problems */12432 + KDB (kdb_reason_cpu_up, 0, null ); /* Do KDB setup on boot CPU */12433 + kdb_initial _ CPU = smp_processor_id (); 12434 + atomic_notifier_chain_register (& panic_notifier_list, & kdb_block); 12435 + register_cpu_notifier (& kdb_cpu_nfb ); 12436 + 12437 + # ifdef kdba_setjmp 12438 + kdbjmpbuf = vmalloc (nr_cpus * sizeof (* kdbjmpbuf); 12439 + If (! Kdbjmpbuf) 12440 + printk (kern_err "Cannot allocate kdbjmpbuf, no KDB recovery will be possible/N "); 12441 + # endif/* kdba_setjmp */12442 + 12443 + kdb_initial_cpu =-1; 12444 + kdb_wait_for_cpus_secs = max (10, 2 * num_online_cpus ()); 12445 +} How complicated KDB is, as can be seen from this initialization function. to be honest, there are not many complicated modules such as light initialization in Linux kernel. there are five functions with "init" in their names. let's take a look: 1. kdb_io_init () 8377 +/* 8378 + * kdb_io_init 8379 + * 8380 + * initializ E kernel debugger output environment. 8381 + * 8382 + * parameters: 8383 + * none. 8384 + * returns: 8385 + * none. 8386 + * locking: 8387 + * none. 8388 + * remarks: 8389 + * select a console device. only use a VT console if the user specified 8390 + * or defaulted console =/^ tty [0-9] * $/8391 + * 8392 + * fixme: 2.6.22-RC1 initializes the serial console long after KDB starts, 8393 + * So booting With 'console = tty console = ttys0' does not create the console 8394 + * entry for ttys0 in time. for now simply assume that we have a working 8395 + * console, until a better solution can be found. 8396 + */8397 + 8398 + void _ init 8399 + kdb_io_init (void) 8400 + {8401 +/* 8402 + * select a console. 8403 + */8404 + struct console * c = console_drivers; 8405 + int vt_console = 0; 8406 + 8407 + while (C) {8408 + # If 0/* fixme: We don't register serial records les in time */8409 + If (c-> flags & con_consdev )&&! Kdbcons) 8410 + kdbcons = C; 8411 + # else 8412 + If (! Kdbcons) 8413 + kdbcons = C; 8414 + # endif 8415 + If (c-> flags & con_enabled) & 8416 + strncmp (c-> name, "tty ", 3) = 0) {8417 + char * P = C-> name + 3; 8418 + while (isdigit (* p) 8419 ++ P; 8420 + If (* P = '/0') 8421 + vt_console = 1; 8422 +} 8423 + C = C-> next; 8424 +} 8425 + 8426 + If (kdbcons = NULL) {8427 + printk (kern_err "KDB: initialization failed-no console. KDB is disabled. /n "); 8 428 + kdb_flag_set (no_console); 8429 + kdb_on = 0; 8430 +} 8431 + If (! Vt_console) 8432 + kdb_flag_set (no_vt_console); 8433 + kdb_input_flush (); 8434 + return; 8435 +} the so-called vt_console is between tty0 and tty9. console_drivers is a global variable. Various console drivers register with this variable by calling register_console. for example, drivers of various serial ports under the drivers/serial directory call this function. For example, if the 8250 serial port driver registers a serial8250_lele.kdbcons, it is also a global variable. it will be assigned to the first console of lele_drivers, such as tty0. For example, ttys0.kdb _ input_flush () is the latency of 12212. 2. kdb_inittab () 12213 +/* + * kdb_init Tab 12214 + * 12215 + * This function is called by the kdb_init function to initialize 12216 + * The KDB command table. it must be called prior to any other 12217 + * Call to kdb_register_repeat. 12218 + * 12219 + * inputs: 12220 + * none. 12221 + * Outputs: 12222 + * none. 12223 + * returns: 12224 + * none. 12225 + * locking: 12226 + * none. 12227 + * remarks: 12228 + * 12229 + */12230 + 12231 + Static void _ init 12232 + kdb_inittab (void) 12233 + {12234 + int I; 12235 + kdbtab_t * KP; 12236 + 12237 + for (I = 0, Kp = kdb_commands; I <kdb_max_commands; I ++, KP ++) {12238 + KP-> partition _name = NULL; 12239 +} 12240 + 12241 + kdb_register_repeat ("MD", kdb_md, "<vaddr>", "Display memory contents, also mdwcn, e.g. md8c1 ", 1, kdb_repeat_no_args); 12242 + kdb_register_repeat (" mcm ", kdb_md," <vaddr> <bytes> "," Display raw memory ", 0, kdb_repeat_no_args); 12243 + kdb_register_repeat (" MDP ", kdb_md," <paddr> <bytes> "," display physical memory ", 0, latency); 12244 + kdb_register_repeat ("MDS", kdb_md, "<vaddr>", "Display memory symbolically", 0, percentage); 12245 + kdb_register_repeat ("mm", kdb_mm, "<vaddr> <contents>", "modify memory contents", 0, kdb_repeat_no_args); 12246 + kdb_reg Ister_repeat ("ID", kdb_id, "<vaddr>", "display instructions", 1, kdb_repeat_no_args); 12247 + kdb_register_repeat ("go", kdb_go, "[<vaddr>]", "Continue execution", 1, kdb_repeat_none); 12248 + kdb_register_repeat ("RD", kdb_rd, "", "display registers", 1, kdb_repeat_none); 12249 + records ("RM", kdb_rm, "<Reg> <contents>", "Modify registers", 0, kdb_repeat_none); 12250 + kdb_register_repe At ("Ef", kdb_ef, "<vaddr>", "display exception frame", 0, kdb_repeat_none); 12251 + kdb_register_repeat ("BT", kdb_bt, "[<vaddr>]", "Stack traceback", 1, kdb_repeat_none); 12252 + kdb_register_repeat ("BTP", kdb_bt, "<pid> ", "display stack for Process <pid>", 0, kdb_repeat_none); 12253 + kdb_register_repeat ("BTA", kdb_bt, "[drstczeuima]", "display stack all processes", 0, kdb_repeat_none); 12254 + k Db_register_repeat ("BTC", kdb_bt, "", "backtrace current process on each CPU", 0, kdb_repeat_none); 12255 + kdb_register_repeat ("BTT", kdb_bt, "<vaddr>", "backtrace process given its struct task address", 0, kdb_repeat_none); 12256 + kdb_register_repeat ("ll", kdb_ll, "<first-element> <linkoffset> <cmd>", "execute cmd for each element in linked list", 0, kdb_repeat_none); 12257 + kdb_register_repea T ("env", kdb_env, "", "Show environment variables", 0, kdb_repeat_none); 12258 + kdb_register_repeat ("set", kdb_set ,"", "Set environment variables", 0, kdb_repeat_none); 12259 + kdb_register_repeat ("help", kdb_help, "", "display help message", 1, kdb_repeat_none ); 12260 + kdb_register_repeat ("? ", Kdb_help," "," display help message ", 0, kdb_repeat_none); 12261 + kdb_register_repeat (" CPU ", kdb_cpu," <cpunum> ", "switch to new CPU", 0, kdb_repeat_none); 12262 + kdb_register_repeat ("Ps", kdb_ps, "", "display active task list", 0, kdb_repeat_none ); 12263 + keys ("PID", kdb_pid, "<pidnum>", "switch to another task", 0, kdb_repeat_none); 12264 + kdb_register_repeat ("reboot", kdb_re Boot, "", "reboot the machine immediately", 0, kdb_repeat_none); 12265 + # If defined (config_modules) 12266 + kdb_register_repeat ("lsmod", kdb_lsmod ,"", "list loaded kernel modules", 0, kdb_repeat_none); 12267 + # endif 12268 + # If defined (config_magic_sysrq) 12269 + notify ("Sr", kdb_sr, "<key> ", "Magic sysrq key", 0, kdb_repeat_none); 12270 + # endif 12271 + kdb_register_repeat ("dmesg", KD B _dmesg, "[Lines]", "display syslog buffer", 0, kdb_repeat_none); 12272 + kdb_register_repeat ("defcmd", kdb_defcmd, "Name/" usage/"/" Help/"", "define a set of commands, down to endefcmd", 0, kdb_repeat_none); 12273 + kdb_register_repeat ("kill ", kdb_kill, "<-signal> <pid>", "send a signal to a process", 0, kdb_repeat_none); 12274 + kdb_register_repeat ("summary", kdb_summary ,"", "summarize the sys TEM ", 4, kdb_repeat_none); 12275 + kdb_register_repeat (" per_cpu ", kdb_per_cpu," "," display per_cpu variables ", 3, kdb_repeat_none ); 12276 +} What is this function doing? We can see that it is constantly calling the kdb_register_repeat () function. if you have used KDB and are familiar with its commands, you will find that every command has appeared in this function. Obviously, kdb_register_repeat () is nothing else, to register each command, the second parameter of kdb_register_repeat () Is the function related to the command, such as dmesg, corresponding to kdb_dmesg, which means that once you execute the dmesg command, the actually called function is kdb_dmesg (). all commands are added to a global variable kdb_commands, which is equivalent to a table. the default value is 50. In other words, you can define 50 commands. Of course, you can also define more. One variable records the maximum number of commands, which is kdb_max_commands. 8540 +/* 8541 + * kdb_commands describes the available commands. 8542 + */8543 + static kdbtab_t * kdb_commands; 8544 + static int kdb_max_commands; 3. kdb_id_init () 7724 +/* 7725 + * kdb_disinit 7726 + * 7727 + * initialize the disassembly information structure 7728 + * For the GNU disassembler. 7729 + * 7730 + * parameters: 7731 + * none. 7732 + * Outputs: 7733 + * none. 7734 + * returns: 7735 + * zero for success, a KDB diagnostic if failure. 7736 + * locking: