於是我們還是直接來看kdb()吧.這個函數有多長?說出來嚇死你,近500行,光注釋就有一兩百行.寫代碼的估計一邊寫著,一邊心裡嘀咕著:XP不發威,你當我是DOS啊! 1615 /* 1616 * kdb 1617 * 1618 * This function is the entry point for the kernel debugger. It 1619 * provides a command parser and associated support functions to 1620 * allow examination and control of an active kernel. 1621 * 1622 * The breakpoint trap code should invoke this function with 1623 * one of KDB_REASON_BREAK (int 03) or KDB_REASON_DEBUG (debug register) 1624 * 1625 * the die_if_kernel function should invoke this function with 1626 * KDB_REASON_OOPS. 1627 * 1628 * In single step mode, one cpu is released to run without 1629 * breakpoints. Interrupts and NMI are reset to their original values, 1630 * the cpu is allowed to do one instruction which causes a trap 1631 * into kdb with KDB_REASON_DEBUG. 1632 * 1633 * Inputs: 1634 * reason The reason KDB was invoked 1635 * error The hardware-defined error code 1636 * regs The exception frame at time of fault/breakpoint. If reason 1637 * is SILENT or CPU_UP then regs is NULL, otherwise it 1638 * should always be valid. 1639 * Returns: 1640 * 0 KDB was invoked for an event which it wasn't responsible 1641 * 1 KDB handled the event for which it was invoked. 1642 * Locking: 1643 * none 1644 * Remarks: 1645 * No assumptions of system state. This function may be invoked 1646 * with arbitrary locks held. It will stop all other processors 1647 * in an SMP environment, disable all interrupts and does not use 1648 * the operating systems keyboard driver. 1649 * 1650 * This code is reentrant but only for cpu switch. Any other 1651 * reentrancy is an error, although kdb will attempt to recover. 1652 * 1653 * At the start of a kdb session the initial processor is running 1654 * kdb() and the other processors can be doing anything. When the 1655 * initial processor calls smp_kdb_stop() the other processors are 1656 * driven through kdb_ipi which calls kdb() with reason SWITCH. 1657 * That brings all processors into this routine, one with a "real" 1658 * reason code, the other with SWITCH. 1659 * 1660 * Because the other processors are driven via smp_kdb_stop(), 1661 * they enter here from the NMI handler. Until the other 1662 * processors exit from here and exit from kdb_ipi, they will not 1663 * take any more NMI requests. The initial cpu will still take NMI. 1664 * 1665 * Multiple race and reentrancy conditions, each with different 1666 * advoidance mechanisms. 1667 * 1668 * Two cpus hit debug points at the same time. 1669 * 1670 * kdb_lock and kdb_initial_cpu ensure that only one cpu gets 1671 * control of kdb. The others spin on kdb_initial_cpu until 1672 * they are driven through NMI into kdb_ipi. When the initial 1673 * cpu releases the others from NMI, they resume trying to get 1674 * kdb_initial_cpu to start a new event. 1675 * 1676 * A cpu is released from kdb and starts a new event before the 1677 * original event has completely ended. 1678 * 1679 * kdb_previous_event() prevents any cpu from entering 1680 * kdb_initial_cpu state until the previous event has completely 1681 * ended on all cpus. 1682 * 1683 * An exception occurs inside kdb. 1684 * 1685 * kdb_initial_cpu detects recursive entry to kdb and attempts 1686 * to recover. The recovery uses longjmp() which means that 1687 * recursive calls to kdb never return. Beware of assumptions 1688 * like 1689 * 1690 * ++depth; 1691 * kdb(); 1692 * --depth; 1693 * 1694 * If the kdb call is recursive then longjmp takes over and 1695 * --depth is never executed. 1696 * 1697 * NMI handling. 1698 * 1699 * NMI handling is tricky. The initial cpu is invoked by some kdb event, 1700 * this event could be NMI driven but usually is not. The other cpus are 1701 * driven into kdb() via kdb_ipi which uses NMI so at the start the other 1702 * cpus will not accept NMI. Some operations such as SS release one cpu 1703 * but hold all the others. Releasing a cpu means it drops back to 1704 * whatever it was doing before the kdb event, this means it drops out of 1705 * kdb_ipi and hence out of NMI status. But the software watchdog uses 1706 * NMI and we do not want spurious watchdog calls into kdb. kdba_read() 1707 * resets the watchdog counters in its input polling loop, when a kdb 1708 * command is running it is subject to NMI watchdog events. 1709 * 1710 * Another problem with NMI handling is the NMI used to drive the other 1711 * cpus into kdb cannot be distinguished from the watchdog NMI. State 1712 * flag WAIT_IPI indicates that a cpu is waiting for NMI via kdb_ipi, 1713 * if not set then software NMI is ignored by kdb_ipi. 1714 * 1715 * Cpu switching. 1716 * 1717 * All cpus are in kdb (or they should be), all but one are 1718 * spinning on KDB_STATE(HOLD_CPU). Only one cpu is not in 1719 * HOLD_CPU state, only that cpu can handle commands. 1720 * 1721 * Go command entered. 1722 * 1723 * If necessary, go will switch to the initial cpu first. If the event 1724 * was caused by a software breakpoint (assumed to be global) that 1725 * requires single-step to get over the breakpoint then only release the 1726 * initial cpu, after the initial cpu has single-stepped the breakpoint 1727 * then release the rest of the cpus. If SSBPT is not required then 1728 * release all the cpus at once. 1729 */ 1730 1731 fastcall int 1732 kdb(kdb_reason_t reason, int error, struct pt_regs *regs) 1733 { 1734 kdb_intstate_t int_state; /* Interrupt state */ 1735 kdb_reason_t reason2 = reason; 1736 int result = 0; /* Default is kdb did not handle it */ 1737 int ss_event, old_regs_saved = 0; 1738 struct pt_regs *old_regs = NULL; 1739 kdb_dbtrap_t db_result=KDB_DB_NOBPT; 1740 preempt_disable(); 1741 atomic_inc(&kdb_event); 1742 1743 switch(reason) { 1744 case KDB_REASON_OOPS: 1745 case KDB_REASON_NMI: 1746 KDB_FLAG_SET(CATASTROPHIC); /* kernel state is dubious now */ 1747 break; 1748 default: 1749 break; 1750 } 1751 switch(reason) { 1752 case KDB_REASON_ENTER: 1753 case KDB_REASON_ENTER_SLAVE: 1754 case KDB_REASON_BREAK: 1755 case KDB_REASON_DEBUG: 1756 case KDB_REASON_OOPS: 1757 case KDB_REASON_SWITCH: 1758 case KDB_REASON_KEYBOARD: 1759 case KDB_REASON_NMI: 1760 if (regs && regs != get_irq_regs()) { 1761 old_regs = set_irq_regs(regs); 1762 old_regs_saved = 1; 1763 } 1764 break; 1765 default: 1766 break; 1767 } 1768 if (kdb_continue_catastrophic > 2) { 1769 kdb_printf("kdb_continue_catastrophic is out of range, setting to 2/n"); 1770 kdb_continue_catastrophic = 2; 1771 } 1772 if (!kdb_on && KDB_FLAG(CATASTROPHIC) && kdb_continue_catastrophic == 2) { 1773 KDB_FLAG_SET(ONLY_DO_DUMP); 1774 } 1775 if (!kdb_on && !KDB_FLAG(ONLY_DO_DUMP)) 1776 goto out; 1777 1778 KDB_DEBUG_STATE("kdb 1", reason); 1779 KDB_STATE_CLEAR(SUPPRESS); 1780 1781 /* Filter out userspace breakpoints first, no point in doing all 1782 * the kdb smp fiddling when it is really a gdb trap. 1783 * Save the single step status first, kdba_db_trap clears ss status. 1784 * kdba_b[dp]_trap sets SSBPT if required. 1785 */ 1786 ss_event = KDB_STATE(DOING_SS) || KDB_STATE(SSBPT); 1787 #ifdef CONFIG_CPU_XSCALE 1788 if ( KDB_STATE(A_XSC_ICH) ) { 1789 /* restore changed I_BIT */ 1790 KDB_STATE_CLEAR(A_XSC_ICH); 1791 kdba_restore_retirq(regs, KDB_STATE(A_XSC_IRQ)); 1792 if ( !ss_event ) { 1793 kdb_printf("Stranger!!! Why IRQ bit is changed====/n"); 1794 } 1795 } 1796 #endif 1797 if (reason == KDB_REASON_BREAK) { 1798 db_result = kdba_bp_trap(regs, error); /* Only call this once */ 1799 } 1800 if (reason == KDB_REASON_DEBUG) { 1801 db_result = kdba_db_trap(regs, error); /* Only call this once */ 1802 } 1803 1804 if ((reason == KDB_REASON_BREAK || reason == KDB_REASON_DEBUG) 1805 && db_result == KDB_DB_NOBPT) { 1806 KDB_DEBUG_STATE("kdb 2", reason); 1807 goto out; /* Not one of mine */ 1808 } 1809 1810 /* Turn off single step if it was being used */ 1811 if (ss_event) { 1812 kdba_clearsinglestep(regs); 1813 /* Single step after a breakpoint removes the need for a delayed reinstall */ 1814 if (reason == KDB_REASON_BREAK || reason == KDB_REASON_DEBUG) 1815 KDB_STATE_CLEAR(SSBPT); 1816 }