5. Topic Today
How to trace C sources environment
We have cross reference tools.
ctags, cscope, LXR, source insight ... and so on
BUT
12/14/14 5/38
6. Difficulties of Tracing C/C++ Sources
● Preprocessor related
● Too much #if/#elif/#endif
● Too much level of macros
● Configurable included files
12/14/14 6/38
7. Difficulties of Tracing C/C++ Sources (Cont.)
● Runtime related
● Some debugging and error handling stuff
● Multi-task environment
● Hard to realize HW state change
Different task context
12/14/14 7/38
8. Topic Today
How to trace C sources environment
Dynamically
Trace code from runtime execution result
(Mainly in linux kernel source)
12/14/14 8/38
9. Tracing from real execution log
Provides clues to trace code
http://www.clker.com/cliparts/c/c/7/p/7/4/earth-hi.png
http://goo.gl/4PnwFe
12/14/14 9/38
11. menuconfig
● Too much words in menuconfig
● Hard to map definition to code world (CONFIG_XXX)
12/14/14 11/38
12. menuconfig Tips – Search
● Press '/' to search specific configurations
12/14/14 12/38
13. menuconfig Tips – Search (Cont.)
● Search result
Path in menu
Kconfig path
Dependency
12/14/14 13/38
14. menuconfig Tips – Result output
● Linux/.config
#
#
# # Automatically Automatically generated generated file; file; DO DO NOT NOT EDIT.
EDIT.
# # Linux/Linux/arm arm 3.14.25 3.14.25 Kernel Kernel Configuration
Configuration
#
#
CONFIG_CONFIG_ARM=ARM=y
y
CONFIG_CONFIG_SYS_SYS_SUPPORTS_SUPPORTS_APM_APM_EMULATION=EMULATION=y
y
#
# General setup
#
CONFIG_INIT_ENV_ARG_LIMIT=32
CONFIG_CROSS_COMPILE=""
# CONFIG_COMPILE_TEST is not set
CONFIG_LOCALVERSION=""
# CONFIG_LOCALVERSION_AUTO is not set
CONFIG_HAVE_KERNEL_GZIP=y
CONFIG_HAVE_KERNEL_LZ4=y
CONFIG_KERNEL_GZIP=y
# CONFIG_KERNEL_LZMA is not set
# CONFIG_KERNEL_XZ is not set
#
# General setup
#
CONFIG_INIT_ENV_ARG_LIMIT=32
CONFIG_CROSS_COMPILE=""
# CONFIG_COMPILE_TEST is not set
CONFIG_LOCALVERSION=""
# CONFIG_LOCALVERSION_AUTO is not set
CONFIG_HAVE_KERNEL_GZIP=y
CONFIG_HAVE_KERNEL_LZ4=y
CONFIG_KERNEL_GZIP=y
# CONFIG_KERNEL_LZMA is not set
# CONFIG_KERNEL_XZ is not set
12/14/14 14/38
15. Tips of Tracing makefile
● make V=1
● make --dry-run
12/14/14 15/38
16. Tips of Tracing makefile (Cont.)
● make --debug
12/14/14 16/38
17. Tips of Tracing Shell Script
● #!/bin/sh “-x”
#!/bin/sh -x
FOO=something_produced_from_command
if [ x${FOO} != x'' ]; then
#!/bin/sh -x
FOO=something_produced_from_command
if [ x${FOO} != x'' ]; then
echo "get foo"
echo "get foo"
else
else
echo "no foo"
echo "no foo"
fi
fi
● Result
+ FOO=something_produced_from_command
+ '[' xsomething_produced_from_command '!=' x ']'
+ echo 'get foo'
get foo
+ FOO=something_produced_from_command
+ '[' xsomething_produced_from_command '!=' x ']'
+ echo 'get foo'
get foo
12/14/14 17/38
18. Tips of Tracing Macros/Definitions
● gcc options: “-save-temps” or “-E”
$ gcc -save-temps -o main main.c
$ ls
main main.c main.i main.o main.s Makefile
$ gcc -save-temps -o main main.c
$ ls
main main.c main.i main.o main.s Makefile
● Use astyle to beauty result
$$ aassttyylele --AA88 mmaainin..ii
12/14/14 18/38
20. Tips of Structure Definitions
● Point to Real definition in the headers
# 5 "include/linux/linkage.h" 2
# 1 "include/linux/stringify.h" 1
# 6 "include/linux/linkage.h" 2
# 1 "include/linux/export.h" 1
# 26 "include/linux/export.h"
struct kernel_symbol
{
unsigned long value;
const char *name;
};
# 5 "include/linux/linkage.h" 2
# 1 "include/linux/stringify.h" 1
# 6 "include/linux/linkage.h" 2
# 1 "include/linux/export.h" 1
# 26 "include/linux/export.h"
struct kernel_symbol
{
unsigned long value;
const char *name;
};
12/14/14 20/38
21. Kbuild Tips
● Add compile flags for some object
CFLAGS_CFLAGS_ffoooo..oo == --OO00 --gg --ssaavvee--tteemmpp
● Remove compile flags for some object
CCFFLLAAGGSS__RREEMMOOVVEE__ffoooo..oo == --OOss
● All subdirectory
ssuubbddirir--ccccfflalaggss--yy == --ssaavvee--tteemmppss
12/14/14 21/38
22. Qemu Tips
● Qemu log (“-d op -D /path/to/qemu.log”)
Setup the search rule
12/14/14 22/38
23. Qemu Tips (Cont.)
● Qemu log + regular expression
Setup the search rule
Example: get first user space entry point
12/14/14 23/38
24. Qemu Tips (Cont.)
● Other tips
● Qemu log + disassembly (objdump)
● Qemu + gdb ( + qemu log)
– Breakpoint / stack trace
– Qemu options
● -s or -gdb tcp::2345: enable gdb port
● -S: freeze CPU at startup
12/14/14 24/38
25. Ftrace – Overview
● Kernel function tracer ( > 2.6.28 ?)
● Record execution log of kernel function
● Implemented by “-pg”
– Use dynamic ftrace to reduce overhead
– Add “-g -O0” to export more information
● Accessing API – debugfs
– /path/to/debug/tracing/
mount -t debugfs n mount -t debugfs noonnee //ssyyss//kkeerrnneel/l/ddeebbuugg
12/14/14 25/38
37. Reference
[1] Building embedded linux demo case
i. Sherif Mousa (Mar 26, 2014), “Building Embedded Linux”
ii. Linaro wiki, “cross-NG”
[2] Linux-3.14 documentation,
i. “kbuild/makefile.txt”
ii. “trace/ftrace.txt”
[3] 池田宗廣,大岩尚宏,島本裕志,竹步晶雄,平松雅巳(Jan. 2014), “Linux kernel hacks”
[4] man of
i. astyle
ii. qemu
iii. Gnu make
12/14/14 37/38