11d0f30a8SSimon GlassLogging in U-Boot 21d0f30a8SSimon Glass================= 31d0f30a8SSimon Glass 41d0f30a8SSimon GlassIntroduction 51d0f30a8SSimon Glass------------ 61d0f30a8SSimon Glass 71d0f30a8SSimon GlassU-Boot's internal operation involves many different steps and actions. From 81d0f30a8SSimon Glasssetting up the board to displaying a start-up screen to loading an Operating 91d0f30a8SSimon GlassSystem, there are many component parts each with many actions. 101d0f30a8SSimon Glass 111d0f30a8SSimon GlassMost of the time this internal detail is not useful. Displaying it on the 121d0f30a8SSimon Glassconsole would delay booting (U-Boot's primary purpose) and confuse users. 131d0f30a8SSimon Glass 141d0f30a8SSimon GlassBut for digging into what is happening in a particular area, or for debugging 151d0f30a8SSimon Glassa problem it is often useful to see what U-Boot is doing in more detail than 161d0f30a8SSimon Glassis visible from the basic console output. 171d0f30a8SSimon Glass 181d0f30a8SSimon GlassU-Boot's logging feature aims to satisfy this goal for both users and 191d0f30a8SSimon Glassdevelopers. 201d0f30a8SSimon Glass 211d0f30a8SSimon Glass 221d0f30a8SSimon GlassLogging levels 231d0f30a8SSimon Glass-------------- 241d0f30a8SSimon Glass 251d0f30a8SSimon GlassThere are a number logging levels available, in increasing order of verbosity: 261d0f30a8SSimon Glass 271d0f30a8SSimon Glass LOGL_EMERG - Printed before U-Boot halts 281d0f30a8SSimon Glass LOGL_ALERT - Indicates action must be taken immediate or U-Boot will crash 291d0f30a8SSimon Glass LOGL_CRIT - Indicates a critical error that will cause boot failure 301d0f30a8SSimon Glass LOGL_ERR - Indicates an error that may cause boot failure 311d0f30a8SSimon Glass LOGL_WARNING - Warning about an unexpected condition 321d0f30a8SSimon Glass LOGL_NOTE - Important information about progress 331d0f30a8SSimon Glass LOGL_INFO - Information about normal boot progress 341d0f30a8SSimon Glass LOGL_DEBUG - Debug information (useful for debugging a driver or subsystem) 351d0f30a8SSimon Glass LOGL_DEBUG_CONTENT - Debug message showing full message content 361d0f30a8SSimon Glass LOGL_DEBUG_IO - Debug message showing hardware I/O access 371d0f30a8SSimon Glass 381d0f30a8SSimon Glass 391d0f30a8SSimon GlassLogging category 401d0f30a8SSimon Glass---------------- 411d0f30a8SSimon Glass 421d0f30a8SSimon GlassLogging can come from a wide variety of places within U-Boot. Each log message 431d0f30a8SSimon Glasshas a category which is intended to allow messages to be filtered according to 441d0f30a8SSimon Glasstheir source. 451d0f30a8SSimon Glass 461d0f30a8SSimon GlassThe following main categories are defined: 471d0f30a8SSimon Glass 481d0f30a8SSimon Glass LOGC_NONE - Unknown category (e.g. a debug() statement) 491d0f30a8SSimon Glass UCLASS_... - Related to a particular uclass (e.g. UCLASS_USB) 501d0f30a8SSimon Glass LOGC_ARCH - Related to architecture-specific code 511d0f30a8SSimon Glass LOGC_BOARD - Related to board-specific code 521d0f30a8SSimon Glass LOGC_CORE - Related to core driver-model support 531d0f30a8SSimon Glass LOGC_DT - Related to device tree control 541973b381SHeinrich Schuchardt LOGC_EFI - Related to EFI implementation 551d0f30a8SSimon Glass 561d0f30a8SSimon Glass 571d0f30a8SSimon GlassEnabling logging 581d0f30a8SSimon Glass---------------- 591d0f30a8SSimon Glass 601d0f30a8SSimon GlassThe following options are used to enable logging at compile time: 611d0f30a8SSimon Glass 621d0f30a8SSimon Glass CONFIG_LOG - Enables the logging system 631d0f30a8SSimon Glass CONFIG_MAX_LOG_LEVEL - Max log level to build (anything higher is compiled 641d0f30a8SSimon Glass out) 651d0f30a8SSimon Glass CONFIG_LOG_CONSOLE - Enable writing log records to the console 661d0f30a8SSimon Glass 671d0f30a8SSimon GlassIf CONFIG_LOG is not set, then no logging will be available. 681d0f30a8SSimon Glass 691d0f30a8SSimon GlassThe above have SPL versions also, e.g. CONFIG_SPL_MAX_LOG_LEVEL. 701d0f30a8SSimon Glass 711d0f30a8SSimon Glass 72*f9811e85SSimon GlassTemporary logging within a single file 73*f9811e85SSimon Glass-------------------------------------- 74*f9811e85SSimon Glass 75*f9811e85SSimon GlassSometimes it is useful to turn on logging just in one file. You can use this: 76*f9811e85SSimon Glass 77*f9811e85SSimon Glass #define LOG_DEBUG 78*f9811e85SSimon Glass 79*f9811e85SSimon Glassto enable building in of all logging statements in a single file. Put it at 80*f9811e85SSimon Glassthe top of the file, before any #includes. 81*f9811e85SSimon Glass 82*f9811e85SSimon GlassTo actually get U-Boot to output this you need to also set the default logging 83*f9811e85SSimon Glasslevel - e.g. set CONFIG_LOG_DEFAULT_LEVEL to 7 (LOGL_DEBUG) or more. Otherwise 84*f9811e85SSimon Glassdebug output is suppressed and will not be generated. 85*f9811e85SSimon Glass 86*f9811e85SSimon Glass 878dee7b96SSimon GlassConvenience functions 888dee7b96SSimon Glass--------------------- 898dee7b96SSimon Glass 908dee7b96SSimon GlassA number of convenience functions are available to shorten the code needed 918dee7b96SSimon Glassfor logging: 928dee7b96SSimon Glass 938dee7b96SSimon Glass log_err(_fmt...) 948dee7b96SSimon Glass log_warning(_fmt...) 958dee7b96SSimon Glass log_notice(_fmt...) 968dee7b96SSimon Glass log_info(_fmt...) 978dee7b96SSimon Glass log_debug(_fmt...) 988dee7b96SSimon Glass log_content(_fmt...) 998dee7b96SSimon Glass log_io(_fmt...) 1008dee7b96SSimon Glass 1018dee7b96SSimon GlassWith these the log level is implicit in the name. The category is set by 1028dee7b96SSimon GlassLOG_CATEGORY, which you can only define once per file, above all #includes: 1038dee7b96SSimon Glass 1048dee7b96SSimon Glass #define LOG_CATEGORY LOGC_ALLOC 1058dee7b96SSimon Glass 1068dee7b96SSimon Glassor 1078dee7b96SSimon Glass 1088dee7b96SSimon Glass #define LOG_CATEGORY UCLASS_SPI 1098dee7b96SSimon Glass 1108dee7b96SSimon GlassRemember that all uclasses IDs are log categories too. 1118dee7b96SSimon Glass 1128dee7b96SSimon Glass 1138cb7c042SSimon GlassLog commands 1148cb7c042SSimon Glass------------ 1158cb7c042SSimon Glass 1168cb7c042SSimon GlassThe 'log' command provides access to several features: 1178cb7c042SSimon Glass 1188cb7c042SSimon Glass level - access the default log level 1198cb7c042SSimon Glass format - access the console log format 1208cb7c042SSimon Glass rec - output a log record 1218cb7c042SSimon Glass test - run tests 1228cb7c042SSimon Glass 1238cb7c042SSimon GlassType 'help log' for details. 1248cb7c042SSimon Glass 1258cb7c042SSimon Glass 1261d0f30a8SSimon GlassUsing DEBUG 1271d0f30a8SSimon Glass----------- 1281d0f30a8SSimon Glass 1291d0f30a8SSimon GlassU-Boot has traditionally used a #define called DEBUG to enable debugging on a 1301d0f30a8SSimon Glassfile-by-file basis. The debug() macro compiles to a printf() statement if 1311d0f30a8SSimon GlassDEBUG is enabled, and an empty statement if not. 1321d0f30a8SSimon Glass 1331d0f30a8SSimon GlassWith logging enabled, debug() statements are interpreted as logging output 1341d0f30a8SSimon Glasswith a level of LOGL_DEBUG and a category of LOGC_NONE. 1351d0f30a8SSimon Glass 1361d0f30a8SSimon GlassThe logging facilities are intended to replace DEBUG, but if DEBUG is defined 1371d0f30a8SSimon Glassat the top of a file, then it takes precedence. This means that debug() 1381d0f30a8SSimon Glassstatements will result in output to the console and this output will not be 1391d0f30a8SSimon Glasslogged. 1401d0f30a8SSimon Glass 1411d0f30a8SSimon Glass 1421d0f30a8SSimon GlassLogging destinations 1431d0f30a8SSimon Glass-------------------- 1441d0f30a8SSimon Glass 1451d0f30a8SSimon GlassIf logging information goes nowhere then it serves no purpose. U-Boot provides 1461d0f30a8SSimon Glassseveral possible determinations for logging information, all of which can be 1471d0f30a8SSimon Glassenabled or disabled independently: 1481d0f30a8SSimon Glass 1491d0f30a8SSimon Glass console - goes to stdout 1501d0f30a8SSimon Glass 1511d0f30a8SSimon Glass 1528cb7c042SSimon GlassLog format 1538cb7c042SSimon Glass---------- 1548cb7c042SSimon Glass 1558cb7c042SSimon GlassYou can control the log format using the 'log format' command. The basic 1568cb7c042SSimon Glassformat is: 1578cb7c042SSimon Glass 1588cb7c042SSimon Glass LEVEL.category,file.c:123-func() message 1598cb7c042SSimon Glass 1608cb7c042SSimon GlassIn the above, file.c:123 is the filename where the log record was generated and 1618cb7c042SSimon Glassfunc() is the function name. By default ('log format default') only the 1628cb7c042SSimon Glassfunction name and message are displayed on the console. You can control which 1638cb7c042SSimon Glassfields are present, but not the field order. 1648cb7c042SSimon Glass 1658cb7c042SSimon Glass 1661d0f30a8SSimon GlassFilters 1671d0f30a8SSimon Glass------- 1681d0f30a8SSimon Glass 1691d0f30a8SSimon GlassFilters are attached to log drivers to control what those drivers emit. Only 1701d0f30a8SSimon Glassrecords that pass through the filter make it to the driver. 1711d0f30a8SSimon Glass 1721d0f30a8SSimon GlassFilters can be based on several criteria: 1731d0f30a8SSimon Glass 1741d0f30a8SSimon Glass - maximum log level 1751d0f30a8SSimon Glass - in a set of categories 1761d0f30a8SSimon Glass - in a set of files 1771d0f30a8SSimon Glass 1781d0f30a8SSimon GlassIf no filters are attached to a driver then a default filter is used, which 1791d0f30a8SSimon Glasslimits output to records with a level less than CONFIG_LOG_MAX_LEVEL. 1801d0f30a8SSimon Glass 1811d0f30a8SSimon Glass 1821d0f30a8SSimon GlassLogging statements 1831d0f30a8SSimon Glass------------------ 1841d0f30a8SSimon Glass 1851d0f30a8SSimon GlassThe main logging function is: 1861d0f30a8SSimon Glass 1871d0f30a8SSimon Glass log(category, level, format_string, ...) 1881d0f30a8SSimon Glass 1891d0f30a8SSimon GlassAlso debug() and error() will generate log records - these use LOG_CATEGORY 1901d0f30a8SSimon Glassas the category, so you should #define this right at the top of the source 1911d0f30a8SSimon Glassfile to ensure the category is correct. 1921d0f30a8SSimon Glass 1933707c6eeSSimon GlassYou can also define CONFIG_LOG_ERROR_RETURN to enable the log_ret() macro. This 1943707c6eeSSimon Glasscan be used whenever your function returns an error value: 1953707c6eeSSimon Glass 1963707c6eeSSimon Glass return log_ret(uclass_first_device(UCLASS_MMC, &dev)); 1973707c6eeSSimon Glass 1983707c6eeSSimon GlassThis will write a log record when an error code is detected (a value < 0). This 1993707c6eeSSimon Glasscan make it easier to trace errors that are generated deep in the call stack. 2003707c6eeSSimon Glass 2011d0f30a8SSimon Glass 2021d0f30a8SSimon GlassCode size 2031d0f30a8SSimon Glass--------- 2041d0f30a8SSimon Glass 2051d0f30a8SSimon GlassCode size impact depends largely on what is enabled. The following numbers are 206b71ac160SSimon Glassgenerated by 'buildman -S' for snow, which is a Thumb-2 board (all units in 207b71ac160SSimon Glassbytes): 2081d0f30a8SSimon Glass 2091d0f30a8SSimon GlassThis series: adds bss +20.0 data +4.0 rodata +4.0 text +44.0 2101d0f30a8SSimon GlassCONFIG_LOG: bss -52.0 data +92.0 rodata -635.0 text +1048.0 2111d0f30a8SSimon GlassCONFIG_LOG_MAX_LEVEL=7: bss +188.0 data +4.0 rodata +49183.0 text +98124.0 2121d0f30a8SSimon Glass 2131d0f30a8SSimon GlassThe last option turns every debug() statement into a logging call, which 2141d0f30a8SSimon Glassbloats the code hugely. The advantage is that it is then possible to enable 2151d0f30a8SSimon Glassall logging within U-Boot. 2161d0f30a8SSimon Glass 2171d0f30a8SSimon Glass 2181d0f30a8SSimon GlassTo Do 2191d0f30a8SSimon Glass----- 2201d0f30a8SSimon Glass 2211d0f30a8SSimon GlassThere are lots of useful additions that could be made. None of the below is 2221d0f30a8SSimon Glassimplemented! If you do one, please add a test in test/py/tests/test_log.py 2231d0f30a8SSimon Glass 2241d0f30a8SSimon GlassConvenience functions to support setting the category: 2251d0f30a8SSimon Glass 2261d0f30a8SSimon Glass log_arch(level, format_string, ...) - category LOGC_ARCH 2271d0f30a8SSimon Glass log_board(level, format_string, ...) - category LOGC_BOARD 2281d0f30a8SSimon Glass log_core(level, format_string, ...) - category LOGC_CORE 2291d0f30a8SSimon Glass log_dt(level, format_string, ...) - category LOGC_DT 2301d0f30a8SSimon Glass 2311d0f30a8SSimon GlassMore logging destinations: 2321d0f30a8SSimon Glass 2331d0f30a8SSimon Glass device - goes to a device (e.g. serial) 2341d0f30a8SSimon Glass buffer - recorded in a memory buffer 2351d0f30a8SSimon Glass 2361d0f30a8SSimon GlassConvert debug() statements in the code to log() statements 2371d0f30a8SSimon Glass 2381d0f30a8SSimon GlassSupport making printf() emit log statements a L_INFO level 2391d0f30a8SSimon Glass 2401d0f30a8SSimon GlassConvert error() statements in the code to log() statements 2411d0f30a8SSimon Glass 2421d0f30a8SSimon GlassFigure out what to do with BUG(), BUG_ON() and warn_non_spl() 2431d0f30a8SSimon Glass 2441d0f30a8SSimon GlassFigure out what to do with assert() 2451d0f30a8SSimon Glass 2461d0f30a8SSimon GlassAdd a way to browse log records 2471d0f30a8SSimon Glass 2481d0f30a8SSimon GlassAdd a way to record log records for browsing using an external tool 2491d0f30a8SSimon Glass 2501d0f30a8SSimon GlassAdd commands to add and remove filters 2511d0f30a8SSimon Glass 2521d0f30a8SSimon GlassAdd commands to add and remove log devices 2531d0f30a8SSimon Glass 2541d0f30a8SSimon GlassAllow sharing of printf format strings in log records to reduce storage size 2551d0f30a8SSimon Glassfor large numbers of log records 2561d0f30a8SSimon Glass 2571d0f30a8SSimon GlassAdd a command-line option to sandbox to set the default logging level 2581d0f30a8SSimon Glass 2591d0f30a8SSimon GlassConvert core driver model code to use logging 2601d0f30a8SSimon Glass 2611d0f30a8SSimon GlassConvert uclasses to use logging with the correct category 2621d0f30a8SSimon Glass 2631d0f30a8SSimon GlassConsider making log() calls emit an automatic newline, perhaps with a logn() 2641d0f30a8SSimon Glass function to avoid that 2651d0f30a8SSimon Glass 2661d0f30a8SSimon GlassPassing log records through to linux (e.g. via device tree /chosen) 2671d0f30a8SSimon Glass 2681d0f30a8SSimon GlassProvide a command to access the number of log records generated, and the 2691d0f30a8SSimon Glassnumber dropped due to them being generated before the log system was ready. 2701d0f30a8SSimon Glass 2711d0f30a8SSimon GlassAdd a printf() format string pragma so that log statements are checked properly 2721d0f30a8SSimon Glass 2731d0f30a8SSimon GlassEnhance the log console driver to show level / category / file / line 2741d0f30a8SSimon Glassinformation 2751d0f30a8SSimon Glass 2761d0f30a8SSimon GlassAdd a command to add new log records and delete existing records. 2771d0f30a8SSimon Glass 2781d0f30a8SSimon GlassProvide additional log() functions - e.g. logc() to specify the category 2791d0f30a8SSimon Glass 2801d0f30a8SSimon Glass-- 2811d0f30a8SSimon GlassSimon Glass <sjg@chromium.org> 2821d0f30a8SSimon Glass15-Sep-17 283