debuggingbyprinting.docx
《debuggingbyprinting.docx》由会员分享,可在线阅读,更多相关《debuggingbyprinting.docx(5页珍藏版)》请在冰豆网上搜索。
![debuggingbyprinting.docx](https://file1.bdocx.com/fileroot1/2023-5/23/777c095d-da1f-4279-8db8-cf683f25526f/777c095d-da1f-4279-8db8-cf683f25526f1.gif)
debuggingbyprinting
debuggingbyprinting
Probablythesimplestwaytogetsomedebuginformationfromyourkernelcodeisbyprintingoutvariousinformationwiththekernel'sequivalentofprintf-theprintkfunctionanditsderivatives.
Thekinprintkisusedtospecificallyremindkerneldevelopersthattheenvironmentisdifferent.
Contents[hide]
1Usage
2LogLevels
3Ratelimitingandonetimemessages
4Printkfromuserspace
5Internals/Changingthesizeoftheprintkbuffer
6ProsandCons
7Debuggingearlybootproblems
7.1Accessingtheprintkbufferafterasilenthangonboot
7.1.1RedbootexampleonaFreescaleADSboard
7.1.2U-bootexampleonanOMAPOSKboard
7.1.3Grub
7.2UsingCONFIG_DEBUG_LLandprintascii()(ARMonly)
8NetConsole
8.1Netconsoleresources
9Misc
9.1Dmesg/Clearingthebuffer
9.2PrintkTimestamps
9.3Printingbuffersashex
9.4DynamicDebugging
10Disablingprintkmessagesatcompiletime
11Referencesandexternallinks
Usage
printkworksmoreorlessthesamewayasprintfinuserspace,soifyoueverdebuggedyouruserspaceprogramusingprintf,youarereadytodothesamewithyourkernelcode,e.g.byadding:
printk("MyDebuggerisPrintk\n");
Thiswasn'tthatdifficult,wasit?
Usuallyyouwouldprintoutsomemoreinterestinginformationlike
printk("Var1%dvar2%d\n",var1,var2);
justlikeinuserspace.
Inordertoseethekernelmessages,justusethe
$dmesg
commandinoneofyourshells-thisonewillprintoutthewholekernellog
buffertoyou.Mostoftheconversionspecifierssupportedbytheuser-spacelibraryroutineprintf()arealsoavailableinthekernel;therearesomenotableadditions,including"%pf",whichwillprintthesymbolnameinplaceofthenumericpointervalue,ifavailable.
Thesupportedformatstringsarequiteextensivelydocumentedin
Documentation/printk-formats.txt
Howeverpleasenote:
alwaysuse%zu,%zdor%zxforprinting
size_tandssize_tvalues.ssize_tandsize_tarequitecommonvaluesinthekernel,so
pleaseusethe%ztoavoidannoyingcompilewarnings.Author'spracticaladvice:
Ifyouwanttodebuganoops(e.gcausedbyreleasingaresourcetwice)inyourdriverandyoudon'thaveacluewheretheoopshappens,simplyaddthisline
printk(KERN_ALERT"DEBUG:
Passed%s%d\n",__FUNCTION__,__LINE__);
aftereachpossiblyoffendingstatement.
Recompileand(re-)loadthemoduleandtriggertheerrorcondition-yourlognowshowsyouthelastlinethatwassuccessfullyexecutedbeforetheoopshappened.
Ofcourseyoushouldremovethese'rude'statementsbeforeshippingyourmodule;)LogLevels
Ifyoulookintorealkernelcodeyouwillalwaysseesomethinglike:
printk(KERN_ERR"somethingwentwrong,returncode:
%d\n",ret);
where"KERN_ERR"isoneoftheeightdifferentloglevelsdefinedin
include/linux/kern_levels.handspecifiestheseverityoftheerrormessage.
NotethatthereisNOcommabetweentheKERN_ERRandtheformatstring(asthepreprocessorconcatenatesbothstrings)
Theloglevelsare:
Name
String
Meaning
aliasfunction
KERN_EMERG
"0"
Emergencymessages,systemisabouttocrashorisunstable
pr_emerg
KERN_ALERT
"1"
Somethingbadhappenedandactionmustbetakenimmediately
pr_alert
KERN_CRIT
"2"
Acriticalconditionoccurredlikeaserioushardware/softwarefailure
pr_crit
KERN_ERR
"3"
Anerrorcondition,oftenusedbydriverstoindicatedifficultieswiththehardware
pr_err
KERN_WARNING
"4"
Awarning,meaningnothingseriousbyitselfbutmightindicateproblems
pr_warning
KERN_NOTICE
"5"
Nothingserious,butnotablynevertheless.Oftenusedtoreportsecurityevents.
pr_notice
KERN_INFO
"6"
Informationalmessagee.g.startupinformationatdriverinitialization
pr_info
KERN_DEBUG
"7"
Debugmessages
pr_debug,pr_develifDEBUGisdefined
KERN_DEFAULT
"d"
ThedefaultkernelloglevelKERN_CONT
""
"continued"lineoflogprintout(onlydoneafteralinethathadnoenclosing\n)[1]
pr_cont