CH5DebuggingLinuxKernelPPT课件

上传人:hs****ma 文档编号:568780208 上传时间:2024-07-26 格式:PPT 页数:52 大小:687.50KB
返回 下载 相关 举报
CH5DebuggingLinuxKernelPPT课件_第1页
第1页 / 共52页
CH5DebuggingLinuxKernelPPT课件_第2页
第2页 / 共52页
CH5DebuggingLinuxKernelPPT课件_第3页
第3页 / 共52页
CH5DebuggingLinuxKernelPPT课件_第4页
第4页 / 共52页
CH5DebuggingLinuxKernelPPT课件_第5页
第5页 / 共52页
点击查看更多>>
资源描述

《CH5DebuggingLinuxKernelPPT课件》由会员分享,可在线阅读,更多相关《CH5DebuggingLinuxKernelPPT课件(52页珍藏版)》请在金锄头文库上搜索。

1、The successs roadDebugging Linux Kernel1Kernel debugging overviewFirst stage kernel debug facilitiesPrintk/PrintasciiKgdbWatch Oops and PanicLight LEDUse hardware debugger: BDI2000Use proc filesystem to debug driversFS2410 drivers debugging Examples2Kernel debuggingThe Linux kernel comes with many d

2、ifferent mechanisms to monitor and determine the state of the kernel and your driversYou primary tools will be printk, the /proc file system and the kgdb source debuggerWhen use printk, make sure printascii work well,Need to add uart macro in arch/arm/kernel/debug-armv.SWhen use kgdb, need code anot

3、her uart for kgdbIf any LED available, you can use for debug purpose. If you have plenty funds, BDI200 is a powerful tool.3Simplest Form of DebuggingIn many ways, kernel debugging harkens back to simpler timesLinus believes that if you require more than printk() to debug the problem, then you should

4、nt be messing with the kernel in the first place So, your primary debugging tool will typically be printk()Fortunately, its not your only tool4Analyzing boot messagesUnderstanding when the message is printed during kernel startup.Locate where the error occur via analyzing error messages.Get system i

5、nformation, such as memory size, bogomipsGet device driver informationAll of these depend on the understanding of Linux kernel startup sequence.5Print functionsPrint messages to serial port before call kernel.puts() Uncompressing Linux.donePrint critical error message before start_kernel()_error:Err

6、or: a Print to log_buf after the beginning of start_kernel()printk()Linux version 2.4.18-rmk7-pxa1Flush the log_buf to console after console_init()printk()6Debug Options in kernelKernel hacking Verbose user fault messages CONFIG_DEBUG_USERInclude debugging information in kernel binary CONFIG_DEBUG_I

7、NFOKernel debugging CONFIG_DEBUG_KERNEL Debug memory allocations CONFIG_DEBUG_SLAB Magic SysRq key CONFIG_MAGIC_SYSRQ Spinlock debugging CONFIG_DEBUG_SPINLOCK Wait queue debugging CONFIG_DEBUG_WAITQ Verbose BUG() reporting (adds 70K) CONFIG_DEBUG_BUGVERBOSE Verbose kernel error messages CONFIG_DEBUG

8、_ERRORS Kernel low-level debugging functions CONFIG_DEBUG_LL7Printkprintk(fmt,)printk() is simply the name of the kernels formatted print function. But floating point is not supported.loglevelThe kernel uses the loglevel to decide whether to print the message to the console. The kernel displays all

9、messages with a loglevel below a specified value on the consoleLogbufferKernel messages are stored in a circular buffer of size LOG_BUF_LEN. This size is configurable at compile time via the CONFIG_LOG_BUF_SHIFT option. The default for a uniprocessor machine is 16KB.Read source code: kernel/printk.c

10、8Printk loglevelsLoglevel Description KERN_EMERGAn emergency condition; the system is probably deadKERN_ALERTA problem that requires immediate attentionKERN_CRITA critical conditionKERN_ERRAn errorKERN_WARNINGA warningKERN_NOTICEA normal, but perhaps noteworthy, conditionKERN_INFOAn informational me

11、ssageKERN_DEBUGA debug messagetypically superfluous9kernel/printk.c#define LOG_BUF_LEN(16384) /* This must be a power of two */* printks without a loglevel use this. */#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */* We show everything that is MORE important than this. */#define MINIMUM_CONSOL

12、E_LOGLEVEL 1 /* Minimum loglevel we let people use */#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */static char log_bufLOG_BUF_LEN;asmlinkage int printk(const char *fmt, .)static char printk_buf1024;/* * Copy the output into log_buf. If the caller didnt provide * appro

13、priate log level tags, we insert them here */asmlinkage long sys_syslog(int type, char * buf, int len) if (type != 3) & !capable(CAP_SYS_ADMIN) return -EPERM;return do_syslog(type, buf, len);10Printascii hackkernel debug:in arch/arm/kernel/debug-armv.S(enabled by CONFIG_DEBUG_LL) and drop a printasc

14、ii() into printk() (kernel/printk.c) just after the vsprintf() call, and monitor the relevant serial port.check:linux/arch/arm/kernel/debug.S include:linux/include/asm-arm/arch-ep93xx/debug-macro.Sand former file implement the uart print ascii support. 11Printascii hackKernel patch:Kconfig patch:./k

15、ernel/arch/arm/Kconfig.debug config DEBUG_LL_PRINTK_HACKbool printk hackdepends on DEBUG_LLhelpIf you say Y here, a copy of every printk message will be sent to the serial port. This can be extremely helpful for debugging but it also introduces some overhead and gets in the way with serial driver. M

16、ost people should say N here.Printk hack:kernel/printk.c 12Printascii hackpatch: Second printk hack:kernel/printk.c +/* send printk output to a serial port, too. */+#ifdef CONFIG_DEBUG_LL_PRINTK_HACK+extern void printascii(const char *);+#define do_printascii_hack(s) printascii(s)+#else+#define do_p

17、rintascii_hack(s) do while(0)+#endifprinted_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args); +/* Send a copy to the serial port (if enabled) */+do_printascii_hack(printk_buf);+13Printascii hackUsed in debug example:./kernel/arch/arm/mach-s3c2410/pm.cextern void printascii(const char *);s

18、tatic void pm_dbg(const char *fmt, .)va_list va;char buff256;va_start(va, fmt);vsprintf(buff, fmt, va);va_end(va);printascii(buff);#define DBG(fmt.) pm_dbg(fmt)Using example:DBG(saved %08lx value %08lxn, ptr-reg, ptr-val);14LED Light UpBy turning on/off the LED (if have any), you can know the execut

19、ion flow. SMDK2410 reference board have 4 leds connected to GPIOsWrite to GPIO data registers15LED Light UpMacro in arch/arm/boot/compressed/head.Sled1on: args = 0, pretend = 0, frame = 0 frame_needed = 0, uses_anonymous_args = 0 link register save eliminated.ldr r0, .L2mov r1, #224 lr needed for pr

20、ologuestr r1, r0, #0mov pc, lr.L3:.align 2.L2:.word 1442840660 data register for port F(GPFDAT). 0x56000054blled1on16OopsAn oops is the usual way a kernel communicates to the user that something bad happenedThis involves printing an error message to the console, dumping the contents of the registers

21、, and providing a back traceAn oops might occur for multiple reasons, including a memory access violation or an illegal instructionDecode the oops messages with ksymoopsarm-linux-ksymoops m System.map saved_oops.txt 17KGDBKernel hacking optionsUsually communicate with gdb debugger via serial port.A

22、kgdb stub should implemented in kernelUsually kgdb have been implemented on X86 platform.For other platform such as ARM, A kgdb patch, even porting by your self, is required.18Source Debugger - kgdbSometimes, you just need a source code debuggerkgdb a kernel-aware version of gdbkgdb requires:A devel

23、opment machineA target machineA serial connection between the two systems19Compiling for use with kgdbTo take advantage of kgdb for debugging, the kernel and you modules must add ggdb to the compile line:Make sure you do *not* include fomit-frame-pointerOmitting the frame pointers with prevent you f

24、rom debugging your code21kgdb SetupCopy the new image to the targetBoot the targetHostTargetRS-23222Running kgdbOn the development machine, cd to where the test kernel is located and start gdb/ddd$ cd /linux$ gdb vmlinuxSample .gdbinitshell echo -e 003 /dev/ttyS0set remotebaud 115200target remote /d

25、ev/ttyS023Hardware ToolsBDI2000A good tools for gdb debugger.Can debug Linux kernelCan debug hardwareCan program flashCan not debug Linux application26Using Hardware Debuggers27Using Hardware DebuggersIf your CPU architecture supports it, JTAG hardware assisted debugging can be a saviorAllows you to

26、 set a breakpointat any instruction Includes ISRs, drivers, exceptionhandlers etc.GNU-aware versions allow you to see “C” source codeUses gdb as the UI28JTAG ContdUseful for debugging the boot firmware, tooGet a JTAG thats Linux-awareUnderstands the MMU and process virtual memory mapsCurrently, the

27、Abatron BDI-2000 and the EPI Majic are the front runners29Use proc for drivers debugA simple addition to our driver will allow us to display information on demandAs the driver developer, we determine what information to display in /procPLZ refer to the experiment manual for details.30/proc Example C

28、ode (1 of 3)/* module.c */#include #if defined(CONFIG_MODVERSIONS)#define MODVERSIONS#include #endif#ifdef MODULE#include #endif#include #include #include #define MYNAME procplaystruct proc_dir_entry *my_proc = NULL;31/proc Example (2 of 3)static int get_my_info(char *, char *, off_t, int, int *, vo

29、id *);static int _init my_init_module(void) int rc = -1;printk( Starting procplayn);my_proc = create_proc_entry(MYNAME, S_IFREG | S_IRUGO, NULL);if (my_proc) my_proc-nlink = 1;my_proc-read_proc = get_my_info;rc = 0; else rc = -1;return rc;static void _exit my_cleanup_module(void) printk( Ending proc

30、playn);remove_proc_entry(MYNAME, NULL);32/proc Example (3 of 3)static int get_my_info(char *sysbuf, char *p_mybuf, off_t offset,int l_sysbuf, int *eof, void *data)staticint count = 0;staticchar mybuf80;int len;/* All information is supplied on a single call.Attempting to read more information should

31、 always return 0 which means no more.“ */if (offset 0)return 0;/* Build the output string and remember its length */len = sprintf(mybuf, Greetings, hacker %dn, count+);*p_mybuf = mybuf;return len;module_init(my_init_module);module_exit(my_cleanup_module);33Example of Debug with /procIn this example,

32、 assume the module is in file module-test2.c# arm-linux-gcc -D_KERNEL_ -DMODULE -c -I/usr/src/linux-2.4/include module-test2.cInstalled with# insmod module-test2.o34Porting Linux kernelPerhaps the basic kernel doesnt works at all.So we need some kernel debugging techniquesMore drivers should be port

33、ed after building a basic Linux kernel.Basic drivers should be supported. For example:serial port, ethernet interface, MTD drivers35Porting basic driversNow we have got a zImage for the board. A basic Linux kernel is ported for the board.But you can do nothing without a console or ethernet device dr

34、iver.So we need add basic device drivers to the kernel.The serial console driver: S3C2410 onchip serial portThe ethernet device driverCS8900A onboard ethernet driverThe FLASH MTD driver To access the 4MB NOR flash on the board36Add Serial DriverAdd config options in drivers/serial/Config.indep_bool

35、S3C2410 serial port support CONFIG_SERIAL_S3C2410 $CONFIG_ARCH_S3C2410dep_bool Console on S3C2410 serial port CONFIG_SERIAL_S3C2410_CONSOLE $CONFIG_SERIAL_S3C2410if ; thendefine_bool CONFIG_SERIAL_CORE ydefine_bool CONFIG_SERIAL_CORE_CONSOLE yAdd compile options in drivers/serial/Makefileobj-$(CONFI

36、G_SERIAL_S3C2410) += s3c2410.oAdd compile options in drivers/char/Makefileifeq ($(CONFIG_ARCH_S3C2410),y) KEYMAP = defkeymap.oendifAdd drivers/serial/s3c2410.c37Porting serial driver (1)drivers/serial/s3c2410.cReplace some head files:#include #include #include Definitions#define S3C2410_VA_UART (UAR

37、T_CTL_BASE | 0xa0000000)#define UART_NR3#define MINOR_START5#define SERIAL_S3C2410_NAMEttyS#define SERIAL_S3C2410_MAJOR204#define CALLOUT_S3C2410_NAMEcua#define CALLOUT_S3C2410_MAJOR20538Porting serial driver (2)drivers/serial/s3c2410.cDefine s3c2410_ports static struct uart_port s3c2410_portsUART_N

38、R = membase:(void *) S3C2410_VA_UART, mapbase:S3C2410_VA_UART, iotype: SERIAL_IO_MEM, irq:IRQ_S3CUART_RX0, uartclk: 130252800, /* 24000000, */ fifosize: 15, /* well loose chars if 16 */ unused: 4, 5, ops:&s3c2410_pops, type:PORT_S3C2410, flags:ASYNC_BOOT_AUTOCONF, line:0, , 39Porting serial driver (

39、3)drivers/serial/s3c2410.cInitializing functionstatic int _init s3c2410uart_init(void)int i, ret;/* reset the uarts into an known state */for (i = 0; i UART_NR; i+) s3c2410uart_reset(&s3c2410_portsi);if(machine_is_smdk2410() s3c2410_portsi.uartclk=130252800;ret = uart_register_driver(&s3c2410_reg);i

40、f (ret) return ret;for (i = 0; i irq_claimed |= PORT_RXIRQ_ENABLED; retval = request_irq(RX_IRQ(port), s3c2410uart_int, 0, s3c2410-rx, port); priv-irq_claimed |= PORT_TXIRQ_ENABLED; retval = request_irq(ER_IRQ(port), s3c2410uart_err_int, 0, s3c2410-error, port); if(machine_is_smdk2410() UART_PUT_UCO

41、N(port, S3C2410_UCON_DEFAULT);41Enable serial consoleConfig the kernel to support serial consoleEnable the S3C2410 serial supportEnable the serial console supportSpecify the CMDLINE: console=ttyS0,115200n8 Build the kernel to get a zImageBoot on the target. Linux boot messages should printed into se

42、rial console when it works.View log_buf to get the boot messages before serial console works.42Add ethernet driver (1)There are 1 ethernet interface on FS2410 board.CS8900 10Mbit ethernet interfaceThe 10M ethernet interface has been brought up in Linux. drivers/net/cs8900a.c is the ethernet driver.I

43、t is easy to move the driver into our kernel tree.Hope the driver is compatible with our kernel.43Add ethernet driver (2)Add config options in drivers/net/Config.inif $CONFIG_ARCH_S3C2410 = y ; then tristate SMDK2410GX CS8900A Ethernet support CONFIG_CS8900AfiAdd compile options in drivers/net/Makef

44、ileobj-$(CONFIG_CS8900A) += cs8900a.oAdd driver C code, copy it into drivers/net/drivers/net/cs8900a.c44Enable ethernet driverNow Configure the kernel and select the option:10M/100M network device support-*SMDK2410GX CS8900A Ethernet supportFortunately, the driver can be built through directly.But t

45、his is not the last result. Lets test it on the board.Maybe the driver doesnt work on the board at all!Usually we can add some printk into the initializing function. To determine where error occurs. To see if the chip have been found during probe.45Add MTD driver (1)At first, we answer some question

46、s about the flash on your board.Chip type number: Is it INTEL compatible or AMD compatible?physical base address: Is it at 0x00000000?Size: How many bytes of the total flash?Buswidth: 16bitThe number of chips: parallel 2 chips or only 1 chip?One bank or multiple banks?Then we can determine the MTD g

47、eometry and map info.Geography is configured in flash chips driversMap info is configured in mapping driversMTD driver implement char and block devices of FLASH 46Add MTD driver (2)Select chip driver configure47Add MTD driver (3)Configure the mapping informationModify drivers/mtd/maps/physmap.c or a

48、dd a new file for your boardDefine the parameters if physmap.c is usedDefine map info and partition info in the file48Add MTD driver (4)Define FLASH mapping in physmap.cstruct map_info physmap_map = name: Physically mapped flash,size: WINDOW_SIZE,buswidth: BUSWIDTH,;static struct mtd_partition physm

49、ap_partitions = name:bootROM,size:0x40000,offset:0,mask_flags: MTD_WRITEABLE, /* force read-only */, name:“Kernel Image,size:0x100000,offset:MTDPART_OFS_APPEND, name:File Sysytem,size:MTDPART_SIZ_FULL,offset:MTDPART_OFS_APPEND,49Enable MTD driverConfigure the MTD supportBuild kernel to get a zImageB

50、oot the image on targetWe can find the MTD device information in boot messagesWe can check the mtd device in shell:$ cat /proc/mtdThe mtd partition devices will be used during deployment.50More porting issuesBesides the 3 basic drivers, more drivers may be required.Dig into the driver source code if you want to use it.Study device driver programming.Read Linux device driver 2nd/3rd 51个人观点供参考,欢迎讨论

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 工作计划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号