Debugging390.txt 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  1. Debugging on Linux for s/390 & z/Architecture
  2. by
  3. Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  4. Copyright (C) 2000-2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
  5. Best viewed with fixed width fonts
  6. Overview of Document:
  7. =====================
  8. This document is intended to give a good overview of how to debug
  9. Linux for s/390 & z/Architecture. It isn't intended as a complete reference & not a
  10. tutorial on the fundamentals of C & assembly. It doesn't go into
  11. 390 IO in any detail. It is intended to complement the documents in the
  12. reference section below & any other worthwhile references you get.
  13. It is intended like the Enterprise Systems Architecture/390 Reference Summary
  14. to be printed out & used as a quick cheat sheet self help style reference when
  15. problems occur.
  16. Contents
  17. ========
  18. Register Set
  19. Address Spaces on Intel Linux
  20. Address Spaces on Linux for s/390 & z/Architecture
  21. The Linux for s/390 & z/Architecture Kernel Task Structure
  22. Register Usage & Stackframes on Linux for s/390 & z/Architecture
  23. A sample program with comments
  24. Compiling programs for debugging on Linux for s/390 & z/Architecture
  25. Figuring out gcc compile errors
  26. Debugging Tools
  27. objdump
  28. strace
  29. Performance Debugging
  30. Debugging under VM
  31. s/390 & z/Architecture IO Overview
  32. Debugging IO on s/390 & z/Architecture under VM
  33. GDB on s/390 & z/Architecture
  34. Stack chaining in gdb by hand
  35. Examining core dumps
  36. ldd
  37. Debugging modules
  38. The proc file system
  39. Starting points for debugging scripting languages etc.
  40. Dumptool & Lcrash
  41. SysRq
  42. References
  43. Special Thanks
  44. Register Set
  45. ============
  46. The current architectures have the following registers.
  47. 16 General propose registers, 32 bit on s/390 64 bit on z/Architecture, r0-r15 or gpr0-gpr15 used for arithmetic & addressing.
  48. 16 Control registers, 32 bit on s/390 64 bit on z/Architecture, ( cr0-cr15 kernel usage only ) used for memory management,
  49. interrupt control,debugging control etc.
  50. 16 Access registers ( ar0-ar15 ) 32 bit on s/390 & z/Architecture
  51. not used by normal programs but potentially could
  52. be used as temporary storage. Their main purpose is their 1 to 1
  53. association with general purpose registers and are used in
  54. the kernel for copying data between kernel & user address spaces.
  55. Access register 0 ( & access register 1 on z/Architecture ( needs 64 bit
  56. pointer ) ) is currently used by the pthread library as a pointer to
  57. the current running threads private area.
  58. 16 64 bit floating point registers (fp0-fp15 ) IEEE & HFP floating
  59. point format compliant on G5 upwards & a Floating point control reg (FPC)
  60. 4 64 bit registers (fp0,fp2,fp4 & fp6) HFP only on older machines.
  61. Note:
  62. Linux (currently) always uses IEEE & emulates G5 IEEE format on older machines,
  63. ( provided the kernel is configured for this ).
  64. The PSW is the most important register on the machine it
  65. is 64 bit on s/390 & 128 bit on z/Architecture & serves the roles of
  66. a program counter (pc), condition code register,memory space designator.
  67. In IBM standard notation I am counting bit 0 as the MSB.
  68. It has several advantages over a normal program counter
  69. in that you can change address translation & program counter
  70. in a single instruction. To change address translation,
  71. e.g. switching address translation off requires that you
  72. have a logical=physical mapping for the address you are
  73. currently running at.
  74. Bit Value
  75. s/390 z/Architecture
  76. 0 0 Reserved ( must be 0 ) otherwise specification exception occurs.
  77. 1 1 Program Event Recording 1 PER enabled,
  78. PER is used to facilitate debugging e.g. single stepping.
  79. 2-4 2-4 Reserved ( must be 0 ).
  80. 5 5 Dynamic address translation 1=DAT on.
  81. 6 6 Input/Output interrupt Mask
  82. 7 7 External interrupt Mask used primarily for interprocessor signalling &
  83. clock interrupts.
  84. 8-11 8-11 PSW Key used for complex memory protection mechanism not used under linux
  85. 12 12 1 on s/390 0 on z/Architecture
  86. 13 13 Machine Check Mask 1=enable machine check interrupts
  87. 14 14 Wait State set this to 1 to stop the processor except for interrupts & give
  88. time to other LPARS used in CPU idle in the kernel to increase overall
  89. usage of processor resources.
  90. 15 15 Problem state ( if set to 1 certain instructions are disabled )
  91. all linux user programs run with this bit 1
  92. ( useful info for debugging under VM ).
  93. 16-17 16-17 Address Space Control
  94. 00 Primary Space Mode when DAT on
  95. The linux kernel currently runs in this mode, CR1 is affiliated with
  96. this mode & points to the primary segment table origin etc.
  97. 01 Access register mode this mode is used in functions to
  98. copy data between kernel & user space.
  99. 10 Secondary space mode not used in linux however CR7 the
  100. register affiliated with this mode is & this & normally
  101. CR13=CR7 to allow us to copy data between kernel & user space.
  102. We do this as follows:
  103. We set ar2 to 0 to designate its
  104. affiliated gpr ( gpr2 )to point to primary=kernel space.
  105. We set ar4 to 1 to designate its
  106. affiliated gpr ( gpr4 ) to point to secondary=home=user space
  107. & then essentially do a memcopy(gpr2,gpr4,size) to
  108. copy data between the address spaces, the reason we use home space for the
  109. kernel & don't keep secondary space free is that code will not run in
  110. secondary space.
  111. 11 Home Space Mode all user programs run in this mode.
  112. it is affiliated with CR13.
  113. 18-19 18-19 Condition codes (CC)
  114. 20 20 Fixed point overflow mask if 1=FPU exceptions for this event
  115. occur ( normally 0 )
  116. 21 21 Decimal overflow mask if 1=FPU exceptions for this event occur
  117. ( normally 0 )
  118. 22 22 Exponent underflow mask if 1=FPU exceptions for this event occur
  119. ( normally 0 )
  120. 23 23 Significance Mask if 1=FPU exceptions for this event occur
  121. ( normally 0 )
  122. 24-31 24-30 Reserved Must be 0.
  123. 31 Extended Addressing Mode
  124. 32 Basic Addressing Mode
  125. Used to set addressing mode
  126. PSW 31 PSW 32
  127. 0 0 24 bit
  128. 0 1 31 bit
  129. 1 1 64 bit
  130. 32 1=31 bit addressing mode 0=24 bit addressing mode (for backward
  131. compatibility), linux always runs with this bit set to 1
  132. 33-64 Instruction address.
  133. 33-63 Reserved must be 0
  134. 64-127 Address
  135. In 24 bits mode bits 64-103=0 bits 104-127 Address
  136. In 31 bits mode bits 64-96=0 bits 97-127 Address
  137. Note: unlike 31 bit mode on s/390 bit 96 must be zero
  138. when loading the address with LPSWE otherwise a
  139. specification exception occurs, LPSW is fully backward
  140. compatible.
  141. Prefix Page(s)
  142. --------------
  143. This per cpu memory area is too intimately tied to the processor not to mention.
  144. It exists between the real addresses 0-4096 on s/390 & 0-8192 z/Architecture & is exchanged
  145. with a 1 page on s/390 or 2 pages on z/Architecture in absolute storage by the set
  146. prefix instruction in linux'es startup.
  147. This page is mapped to a different prefix for each processor in an SMP configuration
  148. ( assuming the os designer is sane of course :-) ).
  149. Bytes 0-512 ( 200 hex ) on s/390 & 0-512,4096-4544,4604-5119 currently on z/Architecture
  150. are used by the processor itself for holding such information as exception indications &
  151. entry points for exceptions.
  152. Bytes after 0xc00 hex are used by linux for per processor globals on s/390 & z/Architecture
  153. ( there is a gap on z/Architecture too currently between 0xc00 & 1000 which linux uses ).
  154. The closest thing to this on traditional architectures is the interrupt
  155. vector table. This is a good thing & does simplify some of the kernel coding
  156. however it means that we now cannot catch stray NULL pointers in the
  157. kernel without hard coded checks.
  158. Address Spaces on Intel Linux
  159. =============================
  160. The traditional Intel Linux is approximately mapped as follows forgive
  161. the ascii art.
  162. 0xFFFFFFFF 4GB Himem *****************
  163. * *
  164. * Kernel Space *
  165. * *
  166. ***************** ****************
  167. User Space Himem (typically 0xC0000000 3GB )* User Stack * * *
  168. ***************** * *
  169. * Shared Libs * * Next Process *
  170. ***************** * to *
  171. * * <== * Run * <==
  172. * User Program * * *
  173. * Data BSS * * *
  174. * Text * * *
  175. * Sections * * *
  176. 0x00000000 ***************** ****************
  177. Now it is easy to see that on Intel it is quite easy to recognise a kernel address
  178. as being one greater than user space himem ( in this case 0xC0000000).
  179. & addresses of less than this are the ones in the current running program on this
  180. processor ( if an smp box ).
  181. If using the virtual machine ( VM ) as a debugger it is quite difficult to
  182. know which user process is running as the address space you are looking at
  183. could be from any process in the run queue.
  184. The limitation of Intels addressing technique is that the linux
  185. kernel uses a very simple real address to virtual addressing technique
  186. of Real Address=Virtual Address-User Space Himem.
  187. This means that on Intel the kernel linux can typically only address
  188. Himem=0xFFFFFFFF-0xC0000000=1GB & this is all the RAM these machines
  189. can typically use.
  190. They can lower User Himem to 2GB or lower & thus be
  191. able to use 2GB of RAM however this shrinks the maximum size
  192. of User Space from 3GB to 2GB they have a no win limit of 4GB unless
  193. they go to 64 Bit.
  194. On 390 our limitations & strengths make us slightly different.
  195. For backward compatibility we are only allowed use 31 bits (2GB)
  196. of our 32 bit addresses, however, we use entirely separate address
  197. spaces for the user & kernel.
  198. This means we can support 2GB of non Extended RAM on s/390, & more
  199. with the Extended memory management swap device &
  200. currently 4TB of physical memory currently on z/Architecture.
  201. Address Spaces on Linux for s/390 & z/Architecture
  202. ==================================================
  203. Our addressing scheme is as follows
  204. Himem 0x7fffffff 2GB on s/390 ***************** ****************
  205. currently 0x3ffffffffff (2^42)-1 * User Stack * * *
  206. on z/Architecture. ***************** * *
  207. * Shared Libs * * *
  208. ***************** * *
  209. * * * Kernel *
  210. * User Program * * *
  211. * Data BSS * * *
  212. * Text * * *
  213. * Sections * * *
  214. 0x00000000 ***************** ****************
  215. This also means that we need to look at the PSW problem state bit
  216. or the addressing mode to decide whether we are looking at
  217. user or kernel space.
  218. Virtual Addresses on s/390 & z/Architecture
  219. ===========================================
  220. A virtual address on s/390 is made up of 3 parts
  221. The SX ( segment index, roughly corresponding to the PGD & PMD in linux terminology )
  222. being bits 1-11.
  223. The PX ( page index, corresponding to the page table entry (pte) in linux terminology )
  224. being bits 12-19.
  225. The remaining bits BX (the byte index are the offset in the page )
  226. i.e. bits 20 to 31.
  227. On z/Architecture in linux we currently make up an address from 4 parts.
  228. The region index bits (RX) 0-32 we currently use bits 22-32
  229. The segment index (SX) being bits 33-43
  230. The page index (PX) being bits 44-51
  231. The byte index (BX) being bits 52-63
  232. Notes:
  233. 1) s/390 has no PMD so the PMD is really the PGD also.
  234. A lot of this stuff is defined in pgtable.h.
  235. 2) Also seeing as s/390's page indexes are only 1k in size
  236. (bits 12-19 x 4 bytes per pte ) we use 1 ( page 4k )
  237. to make the best use of memory by updating 4 segment indices
  238. entries each time we mess with a PMD & use offsets
  239. 0,1024,2048 & 3072 in this page as for our segment indexes.
  240. On z/Architecture our page indexes are now 2k in size
  241. ( bits 12-19 x 8 bytes per pte ) we do a similar trick
  242. but only mess with 2 segment indices each time we mess with
  243. a PMD.
  244. 3) As z/Architecture supports up to a massive 5-level page table lookup we
  245. can only use 3 currently on Linux ( as this is all the generic kernel
  246. currently supports ) however this may change in future
  247. this allows us to access ( according to my sums )
  248. 4TB of virtual storage per process i.e.
  249. 4096*512(PTES)*1024(PMDS)*2048(PGD) = 4398046511104 bytes,
  250. enough for another 2 or 3 of years I think :-).
  251. to do this we use a region-third-table designation type in
  252. our address space control registers.
  253. The Linux for s/390 & z/Architecture Kernel Task Structure
  254. ==========================================================
  255. Each process/thread under Linux for S390 has its own kernel task_struct
  256. defined in linux/include/linux/sched.h
  257. The S390 on initialisation & resuming of a process on a cpu sets
  258. the __LC_KERNEL_STACK variable in the spare prefix area for this cpu
  259. (which we use for per-processor globals).
  260. The kernel stack pointer is intimately tied with the task structure for
  261. each processor as follows.
  262. s/390
  263. ************************
  264. * 1 page kernel stack *
  265. * ( 4K ) *
  266. ************************
  267. * 1 page task_struct *
  268. * ( 4K ) *
  269. 8K aligned ************************
  270. z/Architecture
  271. ************************
  272. * 2 page kernel stack *
  273. * ( 8K ) *
  274. ************************
  275. * 2 page task_struct *
  276. * ( 8K ) *
  277. 16K aligned ************************
  278. What this means is that we don't need to dedicate any register or global variable
  279. to point to the current running process & can retrieve it with the following
  280. very simple construct for s/390 & one very similar for z/Architecture.
  281. static inline struct task_struct * get_current(void)
  282. {
  283. struct task_struct *current;
  284. __asm__("lhi %0,-8192\n\t"
  285. "nr %0,15"
  286. : "=r" (current) );
  287. return current;
  288. }
  289. i.e. just anding the current kernel stack pointer with the mask -8192.
  290. Thankfully because Linux doesn't have support for nested IO interrupts
  291. & our devices have large buffers can survive interrupts being shut for
  292. short amounts of time we don't need a separate stack for interrupts.
  293. Register Usage & Stackframes on Linux for s/390 & z/Architecture
  294. =================================================================
  295. Overview:
  296. ---------
  297. This is the code that gcc produces at the top & the bottom of
  298. each function. It usually is fairly consistent & similar from
  299. function to function & if you know its layout you can probably
  300. make some headway in finding the ultimate cause of a problem
  301. after a crash without a source level debugger.
  302. Note: To follow stackframes requires a knowledge of C or Pascal &
  303. limited knowledge of one assembly language.
  304. It should be noted that there are some differences between the
  305. s/390 & z/Architecture stack layouts as the z/Architecture stack layout didn't have
  306. to maintain compatibility with older linkage formats.
  307. Glossary:
  308. ---------
  309. alloca:
  310. This is a built in compiler function for runtime allocation
  311. of extra space on the callers stack which is obviously freed
  312. up on function exit ( e.g. the caller may choose to allocate nothing
  313. of a buffer of 4k if required for temporary purposes ), it generates
  314. very efficient code ( a few cycles ) when compared to alternatives
  315. like malloc.
  316. automatics: These are local variables on the stack,
  317. i.e they aren't in registers & they aren't static.
  318. back-chain:
  319. This is a pointer to the stack pointer before entering a
  320. framed functions ( see frameless function ) prologue got by
  321. dereferencing the address of the current stack pointer,
  322. i.e. got by accessing the 32 bit value at the stack pointers
  323. current location.
  324. base-pointer:
  325. This is a pointer to the back of the literal pool which
  326. is an area just behind each procedure used to store constants
  327. in each function.
  328. call-clobbered: The caller probably needs to save these registers if there
  329. is something of value in them, on the stack or elsewhere before making a
  330. call to another procedure so that it can restore it later.
  331. epilogue:
  332. The code generated by the compiler to return to the caller.
  333. frameless-function
  334. A frameless function in Linux for s390 & z/Architecture is one which doesn't
  335. need more than the register save area ( 96 bytes on s/390, 160 on z/Architecture )
  336. given to it by the caller.
  337. A frameless function never:
  338. 1) Sets up a back chain.
  339. 2) Calls alloca.
  340. 3) Calls other normal functions
  341. 4) Has automatics.
  342. GOT-pointer:
  343. This is a pointer to the global-offset-table in ELF
  344. ( Executable Linkable Format, Linux'es most common executable format ),
  345. all globals & shared library objects are found using this pointer.
  346. lazy-binding
  347. ELF shared libraries are typically only loaded when routines in the shared
  348. library are actually first called at runtime. This is lazy binding.
  349. procedure-linkage-table
  350. This is a table found from the GOT which contains pointers to routines
  351. in other shared libraries which can't be called to by easier means.
  352. prologue:
  353. The code generated by the compiler to set up the stack frame.
  354. outgoing-args:
  355. This is extra area allocated on the stack of the calling function if the
  356. parameters for the callee's cannot all be put in registers, the same
  357. area can be reused by each function the caller calls.
  358. routine-descriptor:
  359. A COFF executable format based concept of a procedure reference
  360. actually being 8 bytes or more as opposed to a simple pointer to the routine.
  361. This is typically defined as follows
  362. Routine Descriptor offset 0=Pointer to Function
  363. Routine Descriptor offset 4=Pointer to Table of Contents
  364. The table of contents/TOC is roughly equivalent to a GOT pointer.
  365. & it means that shared libraries etc. can be shared between several
  366. environments each with their own TOC.
  367. static-chain: This is used in nested functions a concept adopted from pascal
  368. by gcc not used in ansi C or C++ ( although quite useful ), basically it
  369. is a pointer used to reference local variables of enclosing functions.
  370. You might come across this stuff once or twice in your lifetime.
  371. e.g.
  372. The function below should return 11 though gcc may get upset & toss warnings
  373. about unused variables.
  374. int FunctionA(int a)
  375. {
  376. int b;
  377. FunctionC(int c)
  378. {
  379. b=c+1;
  380. }
  381. FunctionC(10);
  382. return(b);
  383. }
  384. s/390 & z/Architecture Register usage
  385. =====================================
  386. r0 used by syscalls/assembly call-clobbered
  387. r1 used by syscalls/assembly call-clobbered
  388. r2 argument 0 / return value 0 call-clobbered
  389. r3 argument 1 / return value 1 (if long long) call-clobbered
  390. r4 argument 2 call-clobbered
  391. r5 argument 3 call-clobbered
  392. r6 argument 4 saved
  393. r7 pointer-to arguments 5 to ... saved
  394. r8 this & that saved
  395. r9 this & that saved
  396. r10 static-chain ( if nested function ) saved
  397. r11 frame-pointer ( if function used alloca ) saved
  398. r12 got-pointer saved
  399. r13 base-pointer saved
  400. r14 return-address saved
  401. r15 stack-pointer saved
  402. f0 argument 0 / return value ( float/double ) call-clobbered
  403. f2 argument 1 call-clobbered
  404. f4 z/Architecture argument 2 saved
  405. f6 z/Architecture argument 3 saved
  406. The remaining floating points
  407. f1,f3,f5 f7-f15 are call-clobbered.
  408. Notes:
  409. ------
  410. 1) The only requirement is that registers which are used
  411. by the callee are saved, e.g. the compiler is perfectly
  412. capable of using r11 for purposes other than a frame a
  413. frame pointer if a frame pointer is not needed.
  414. 2) In functions with variable arguments e.g. printf the calling procedure
  415. is identical to one without variable arguments & the same number of
  416. parameters. However, the prologue of this function is somewhat more
  417. hairy owing to it having to move these parameters to the stack to
  418. get va_start, va_arg & va_end to work.
  419. 3) Access registers are currently unused by gcc but are used in
  420. the kernel. Possibilities exist to use them at the moment for
  421. temporary storage but it isn't recommended.
  422. 4) Only 4 of the floating point registers are used for
  423. parameter passing as older machines such as G3 only have only 4
  424. & it keeps the stack frame compatible with other compilers.
  425. However with IEEE floating point emulation under linux on the
  426. older machines you are free to use the other 12.
  427. 5) A long long or double parameter cannot be have the
  428. first 4 bytes in a register & the second four bytes in the
  429. outgoing args area. It must be purely in the outgoing args
  430. area if crossing this boundary.
  431. 6) Floating point parameters are mixed with outgoing args
  432. on the outgoing args area in the order the are passed in as parameters.
  433. 7) Floating point arguments 2 & 3 are saved in the outgoing args area for
  434. z/Architecture
  435. Stack Frame Layout
  436. ------------------
  437. s/390 z/Architecture
  438. 0 0 back chain ( a 0 here signifies end of back chain )
  439. 4 8 eos ( end of stack, not used on Linux for S390 used in other linkage formats )
  440. 8 16 glue used in other s/390 linkage formats for saved routine descriptors etc.
  441. 12 24 glue used in other s/390 linkage formats for saved routine descriptors etc.
  442. 16 32 scratch area
  443. 20 40 scratch area
  444. 24 48 saved r6 of caller function
  445. 28 56 saved r7 of caller function
  446. 32 64 saved r8 of caller function
  447. 36 72 saved r9 of caller function
  448. 40 80 saved r10 of caller function
  449. 44 88 saved r11 of caller function
  450. 48 96 saved r12 of caller function
  451. 52 104 saved r13 of caller function
  452. 56 112 saved r14 of caller function
  453. 60 120 saved r15 of caller function
  454. 64 128 saved f4 of caller function
  455. 72 132 saved f6 of caller function
  456. 80 undefined
  457. 96 160 outgoing args passed from caller to callee
  458. 96+x 160+x possible stack alignment ( 8 bytes desirable )
  459. 96+x+y 160+x+y alloca space of caller ( if used )
  460. 96+x+y+z 160+x+y+z automatics of caller ( if used )
  461. 0 back-chain
  462. A sample program with comments.
  463. ===============================
  464. Comments on the function test
  465. -----------------------------
  466. 1) It didn't need to set up a pointer to the constant pool gpr13 as it isn't used
  467. ( :-( ).
  468. 2) This is a frameless function & no stack is bought.
  469. 3) The compiler was clever enough to recognise that it could return the
  470. value in r2 as well as use it for the passed in parameter ( :-) ).
  471. 4) The basr ( branch relative & save ) trick works as follows the instruction
  472. has a special case with r0,r0 with some instruction operands is understood as
  473. the literal value 0, some risc architectures also do this ). So now
  474. we are branching to the next address & the address new program counter is
  475. in r13,so now we subtract the size of the function prologue we have executed
  476. + the size of the literal pool to get to the top of the literal pool
  477. 0040037c int test(int b)
  478. { # Function prologue below
  479. 40037c: 90 de f0 34 stm %r13,%r14,52(%r15) # Save registers r13 & r14
  480. 400380: 0d d0 basr %r13,%r0 # Set up pointer to constant pool using
  481. 400382: a7 da ff fa ahi %r13,-6 # basr trick
  482. return(5+b);
  483. # Huge main program
  484. 400386: a7 2a 00 05 ahi %r2,5 # add 5 to r2
  485. # Function epilogue below
  486. 40038a: 98 de f0 34 lm %r13,%r14,52(%r15) # restore registers r13 & 14
  487. 40038e: 07 fe br %r14 # return
  488. }
  489. Comments on the function main
  490. -----------------------------
  491. 1) The compiler did this function optimally ( 8-) )
  492. Literal pool for main.
  493. 400390: ff ff ff ec .long 0xffffffec
  494. main(int argc,char *argv[])
  495. { # Function prologue below
  496. 400394: 90 bf f0 2c stm %r11,%r15,44(%r15) # Save necessary registers
  497. 400398: 18 0f lr %r0,%r15 # copy stack pointer to r0
  498. 40039a: a7 fa ff a0 ahi %r15,-96 # Make area for callee saving
  499. 40039e: 0d d0 basr %r13,%r0 # Set up r13 to point to
  500. 4003a0: a7 da ff f0 ahi %r13,-16 # literal pool
  501. 4003a4: 50 00 f0 00 st %r0,0(%r15) # Save backchain
  502. return(test(5)); # Main Program Below
  503. 4003a8: 58 e0 d0 00 l %r14,0(%r13) # load relative address of test from
  504. # literal pool
  505. 4003ac: a7 28 00 05 lhi %r2,5 # Set first parameter to 5
  506. 4003b0: 4d ee d0 00 bas %r14,0(%r14,%r13) # jump to test setting r14 as return
  507. # address using branch & save instruction.
  508. # Function Epilogue below
  509. 4003b4: 98 bf f0 8c lm %r11,%r15,140(%r15)# Restore necessary registers.
  510. 4003b8: 07 fe br %r14 # return to do program exit
  511. }
  512. Compiler updates
  513. ----------------
  514. main(int argc,char *argv[])
  515. {
  516. 4004fc: 90 7f f0 1c stm %r7,%r15,28(%r15)
  517. 400500: a7 d5 00 04 bras %r13,400508 <main+0xc>
  518. 400504: 00 40 04 f4 .long 0x004004f4
  519. # compiler now puts constant pool in code to so it saves an instruction
  520. 400508: 18 0f lr %r0,%r15
  521. 40050a: a7 fa ff a0 ahi %r15,-96
  522. 40050e: 50 00 f0 00 st %r0,0(%r15)
  523. return(test(5));
  524. 400512: 58 10 d0 00 l %r1,0(%r13)
  525. 400516: a7 28 00 05 lhi %r2,5
  526. 40051a: 0d e1 basr %r14,%r1
  527. # compiler adds 1 extra instruction to epilogue this is done to
  528. # avoid processor pipeline stalls owing to data dependencies on g5 &
  529. # above as register 14 in the old code was needed directly after being loaded
  530. # by the lm %r11,%r15,140(%r15) for the br %14.
  531. 40051c: 58 40 f0 98 l %r4,152(%r15)
  532. 400520: 98 7f f0 7c lm %r7,%r15,124(%r15)
  533. 400524: 07 f4 br %r4
  534. }
  535. Hartmut ( our compiler developer ) also has been threatening to take out the
  536. stack backchain in optimised code as this also causes pipeline stalls, you
  537. have been warned.
  538. 64 bit z/Architecture code disassembly
  539. --------------------------------------
  540. If you understand the stuff above you'll understand the stuff
  541. below too so I'll avoid repeating myself & just say that
  542. some of the instructions have g's on the end of them to indicate
  543. they are 64 bit & the stack offsets are a bigger,
  544. the only other difference you'll find between 32 & 64 bit is that
  545. we now use f4 & f6 for floating point arguments on 64 bit.
  546. 00000000800005b0 <test>:
  547. int test(int b)
  548. {
  549. return(5+b);
  550. 800005b0: a7 2a 00 05 ahi %r2,5
  551. 800005b4: b9 14 00 22 lgfr %r2,%r2 # downcast to integer
  552. 800005b8: 07 fe br %r14
  553. 800005ba: 07 07 bcr 0,%r7
  554. }
  555. 00000000800005bc <main>:
  556. main(int argc,char *argv[])
  557. {
  558. 800005bc: eb bf f0 58 00 24 stmg %r11,%r15,88(%r15)
  559. 800005c2: b9 04 00 1f lgr %r1,%r15
  560. 800005c6: a7 fb ff 60 aghi %r15,-160
  561. 800005ca: e3 10 f0 00 00 24 stg %r1,0(%r15)
  562. return(test(5));
  563. 800005d0: a7 29 00 05 lghi %r2,5
  564. # brasl allows jumps > 64k & is overkill here bras would do fune
  565. 800005d4: c0 e5 ff ff ff ee brasl %r14,800005b0 <test>
  566. 800005da: e3 40 f1 10 00 04 lg %r4,272(%r15)
  567. 800005e0: eb bf f0 f8 00 04 lmg %r11,%r15,248(%r15)
  568. 800005e6: 07 f4 br %r4
  569. }
  570. Compiling programs for debugging on Linux for s/390 & z/Architecture
  571. ====================================================================
  572. -gdwarf-2 now works it should be considered the default debugging
  573. format for s/390 & z/Architecture as it is more reliable for debugging
  574. shared libraries, normal -g debugging works much better now
  575. Thanks to the IBM java compiler developers bug reports.
  576. This is typically done adding/appending the flags -g or -gdwarf-2 to the
  577. CFLAGS & LDFLAGS variables Makefile of the program concerned.
  578. If using gdb & you would like accurate displays of registers &
  579. stack traces compile without optimisation i.e make sure
  580. that there is no -O2 or similar on the CFLAGS line of the Makefile &
  581. the emitted gcc commands, obviously this will produce worse code
  582. ( not advisable for shipment ) but it is an aid to the debugging process.
  583. This aids debugging because the compiler will copy parameters passed in
  584. in registers onto the stack so backtracing & looking at passed in
  585. parameters will work, however some larger programs which use inline functions
  586. will not compile without optimisation.
  587. Debugging with optimisation has since much improved after fixing
  588. some bugs, please make sure you are using gdb-5.0 or later developed
  589. after Nov'2000.
  590. Figuring out gcc compile errors
  591. ===============================
  592. If you are getting a lot of syntax errors compiling a program & the problem
  593. isn't blatantly obvious from the source.
  594. It often helps to just preprocess the file, this is done with the -E
  595. option in gcc.
  596. What this does is that it runs through the very first phase of compilation
  597. ( compilation in gcc is done in several stages & gcc calls many programs to
  598. achieve its end result ) with the -E option gcc just calls the gcc preprocessor (cpp).
  599. The c preprocessor does the following, it joins all the files #included together
  600. recursively ( #include files can #include other files ) & also the c file you wish to compile.
  601. It puts a fully qualified path of the #included files in a comment & it
  602. does macro expansion.
  603. This is useful for debugging because
  604. 1) You can double check whether the files you expect to be included are the ones
  605. that are being included ( e.g. double check that you aren't going to the i386 asm directory ).
  606. 2) Check that macro definitions aren't clashing with typedefs,
  607. 3) Check that definitions aren't being used before they are being included.
  608. 4) Helps put the line emitting the error under the microscope if it contains macros.
  609. For convenience the Linux kernel's makefile will do preprocessing automatically for you
  610. by suffixing the file you want built with .i ( instead of .o )
  611. e.g.
  612. from the linux directory type
  613. make arch/s390/kernel/signal.i
  614. this will build
  615. s390-gcc -D__KERNEL__ -I/home1/barrow/linux/include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer
  616. -fno-strict-aliasing -D__SMP__ -pipe -fno-strength-reduce -E arch/s390/kernel/signal.c
  617. > arch/s390/kernel/signal.i
  618. Now look at signal.i you should see something like.
  619. # 1 "/home1/barrow/linux/include/asm/types.h" 1
  620. typedef unsigned short umode_t;
  621. typedef __signed__ char __s8;
  622. typedef unsigned char __u8;
  623. typedef __signed__ short __s16;
  624. typedef unsigned short __u16;
  625. If instead you are getting errors further down e.g.
  626. unknown instruction:2515 "move.l" or better still unknown instruction:2515
  627. "Fixme not implemented yet, call Martin" you are probably are attempting to compile some code
  628. meant for another architecture or code that is simply not implemented, with a fixme statement
  629. stuck into the inline assembly code so that the author of the file now knows he has work to do.
  630. To look at the assembly emitted by gcc just before it is about to call gas ( the gnu assembler )
  631. use the -S option.
  632. Again for your convenience the Linux kernel's Makefile will hold your hand &
  633. do all this donkey work for you also by building the file with the .s suffix.
  634. e.g.
  635. from the Linux directory type
  636. make arch/s390/kernel/signal.s
  637. s390-gcc -D__KERNEL__ -I/home1/barrow/linux/include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer
  638. -fno-strict-aliasing -D__SMP__ -pipe -fno-strength-reduce -S arch/s390/kernel/signal.c
  639. -o arch/s390/kernel/signal.s
  640. This will output something like, ( please note the constant pool & the useful comments
  641. in the prologue to give you a hand at interpreting it ).
  642. .LC54:
  643. .string "misaligned (__u16 *) in __xchg\n"
  644. .LC57:
  645. .string "misaligned (__u32 *) in __xchg\n"
  646. .L$PG1: # Pool sys_sigsuspend
  647. .LC192:
  648. .long -262401
  649. .LC193:
  650. .long -1
  651. .LC194:
  652. .long schedule-.L$PG1
  653. .LC195:
  654. .long do_signal-.L$PG1
  655. .align 4
  656. .globl sys_sigsuspend
  657. .type sys_sigsuspend,@function
  658. sys_sigsuspend:
  659. # leaf function 0
  660. # automatics 16
  661. # outgoing args 0
  662. # need frame pointer 0
  663. # call alloca 0
  664. # has varargs 0
  665. # incoming args (stack) 0
  666. # function length 168
  667. STM 8,15,32(15)
  668. LR 0,15
  669. AHI 15,-112
  670. BASR 13,0
  671. .L$CO1: AHI 13,.L$PG1-.L$CO1
  672. ST 0,0(15)
  673. LR 8,2
  674. N 5,.LC192-.L$PG1(13)
  675. Adding -g to the above output makes the output even more useful
  676. e.g. typing
  677. make CC:="s390-gcc -g" kernel/sched.s
  678. which compiles.
  679. s390-gcc -g -D__KERNEL__ -I/home/barrow/linux-2.3/include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strict-aliasing -pipe -fno-strength-reduce -S kernel/sched.c -o kernel/sched.s
  680. also outputs stabs ( debugger ) info, from this info you can find out the
  681. offsets & sizes of various elements in structures.
  682. e.g. the stab for the structure
  683. struct rlimit {
  684. unsigned long rlim_cur;
  685. unsigned long rlim_max;
  686. };
  687. is
  688. .stabs "rlimit:T(151,2)=s8rlim_cur:(0,5),0,32;rlim_max:(0,5),32,32;;",128,0,0,0
  689. from this stab you can see that
  690. rlimit_cur starts at bit offset 0 & is 32 bits in size
  691. rlimit_max starts at bit offset 32 & is 32 bits in size.
  692. Debugging Tools:
  693. ================
  694. objdump
  695. =======
  696. This is a tool with many options the most useful being ( if compiled with -g).
  697. objdump --source <victim program or object file> > <victims debug listing >
  698. The whole kernel can be compiled like this ( Doing this will make a 17MB kernel
  699. & a 200 MB listing ) however you have to strip it before building the image
  700. using the strip command to make it a more reasonable size to boot it.
  701. A source/assembly mixed dump of the kernel can be done with the line
  702. objdump --source vmlinux > vmlinux.lst
  703. Also, if the file isn't compiled -g, this will output as much debugging information
  704. as it can (e.g. function names). This is very slow as it spends lots
  705. of time searching for debugging info. The following self explanatory line should be used
  706. instead if the code isn't compiled -g, as it is much faster:
  707. objdump --disassemble-all --syms vmlinux > vmlinux.lst
  708. As hard drive space is valuable most of us use the following approach.
  709. 1) Look at the emitted psw on the console to find the crash address in the kernel.
  710. 2) Look at the file System.map ( in the linux directory ) produced when building
  711. the kernel to find the closest address less than the current PSW to find the
  712. offending function.
  713. 3) use grep or similar to search the source tree looking for the source file
  714. with this function if you don't know where it is.
  715. 4) rebuild this object file with -g on, as an example suppose the file was
  716. ( /arch/s390/kernel/signal.o )
  717. 5) Assuming the file with the erroneous function is signal.c Move to the base of the
  718. Linux source tree.
  719. 6) rm /arch/s390/kernel/signal.o
  720. 7) make /arch/s390/kernel/signal.o
  721. 8) watch the gcc command line emitted
  722. 9) type it in again or alternatively cut & paste it on the console adding the -g option.
  723. 10) objdump --source arch/s390/kernel/signal.o > signal.lst
  724. This will output the source & the assembly intermixed, as the snippet below shows
  725. This will unfortunately output addresses which aren't the same
  726. as the kernel ones you should be able to get around the mental arithmetic
  727. by playing with the --adjust-vma parameter to objdump.
  728. static inline void spin_lock(spinlock_t *lp)
  729. {
  730. a0: 18 34 lr %r3,%r4
  731. a2: a7 3a 03 bc ahi %r3,956
  732. __asm__ __volatile(" lhi 1,-1\n"
  733. a6: a7 18 ff ff lhi %r1,-1
  734. aa: 1f 00 slr %r0,%r0
  735. ac: ba 01 30 00 cs %r0,%r1,0(%r3)
  736. b0: a7 44 ff fd jm aa <sys_sigsuspend+0x2e>
  737. saveset = current->blocked;
  738. b4: d2 07 f0 68 mvc 104(8,%r15),972(%r4)
  739. b8: 43 cc
  740. return (set->sig[0] & mask) != 0;
  741. }
  742. 6) If debugging under VM go down to that section in the document for more info.
  743. I now have a tool which takes the pain out of --adjust-vma
  744. & you are able to do something like
  745. make /arch/s390/kernel/traps.lst
  746. & it automatically generates the correctly relocated entries for
  747. the text segment in traps.lst.
  748. This tool is now standard in linux distro's in scripts/makelst
  749. strace:
  750. -------
  751. Q. What is it ?
  752. A. It is a tool for intercepting calls to the kernel & logging them
  753. to a file & on the screen.
  754. Q. What use is it ?
  755. A. You can use it to find out what files a particular program opens.
  756. Example 1
  757. ---------
  758. If you wanted to know does ping work but didn't have the source
  759. strace ping -c 1 127.0.0.1
  760. & then look at the man pages for each of the syscalls below,
  761. ( In fact this is sometimes easier than looking at some spaghetti
  762. source which conditionally compiles for several architectures ).
  763. Not everything that it throws out needs to make sense immediately.
  764. Just looking quickly you can see that it is making up a RAW socket
  765. for the ICMP protocol.
  766. Doing an alarm(10) for a 10 second timeout
  767. & doing a gettimeofday call before & after each read to see
  768. how long the replies took, & writing some text to stdout so the user
  769. has an idea what is going on.
  770. socket(PF_INET, SOCK_RAW, IPPROTO_ICMP) = 3
  771. getuid() = 0
  772. setuid(0) = 0
  773. stat("/usr/share/locale/C/libc.cat", 0xbffff134) = -1 ENOENT (No such file or directory)
  774. stat("/usr/share/locale/libc/C", 0xbffff134) = -1 ENOENT (No such file or directory)
  775. stat("/usr/local/share/locale/C/libc.cat", 0xbffff134) = -1 ENOENT (No such file or directory)
  776. getpid() = 353
  777. setsockopt(3, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0
  778. setsockopt(3, SOL_SOCKET, SO_RCVBUF, [49152], 4) = 0
  779. fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(3, 1), ...}) = 0
  780. mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40008000
  781. ioctl(1, TCGETS, {B9600 opost isig icanon echo ...}) = 0
  782. write(1, "PING 127.0.0.1 (127.0.0.1): 56 d"..., 42PING 127.0.0.1 (127.0.0.1): 56 data bytes
  783. ) = 42
  784. sigaction(SIGINT, {0x8049ba0, [], SA_RESTART}, {SIG_DFL}) = 0
  785. sigaction(SIGALRM, {0x8049600, [], SA_RESTART}, {SIG_DFL}) = 0
  786. gettimeofday({948904719, 138951}, NULL) = 0
  787. sendto(3, "\10\0D\201a\1\0\0\17#\2178\307\36"..., 64, 0, {sin_family=AF_INET,
  788. sin_port=htons(0), sin_addr=inet_addr("127.0.0.1")}, 16) = 64
  789. sigaction(SIGALRM, {0x8049600, [], SA_RESTART}, {0x8049600, [], SA_RESTART}) = 0
  790. sigaction(SIGALRM, {0x8049ba0, [], SA_RESTART}, {0x8049600, [], SA_RESTART}) = 0
  791. alarm(10) = 0
  792. recvfrom(3, "E\0\0T\0005\0\0@\1|r\177\0\0\1\177"..., 192, 0,
  793. {sin_family=AF_INET, sin_port=htons(50882), sin_addr=inet_addr("127.0.0.1")}, [16]) = 84
  794. gettimeofday({948904719, 160224}, NULL) = 0
  795. recvfrom(3, "E\0\0T\0006\0\0\377\1\275p\177\0"..., 192, 0,
  796. {sin_family=AF_INET, sin_port=htons(50882), sin_addr=inet_addr("127.0.0.1")}, [16]) = 84
  797. gettimeofday({948904719, 166952}, NULL) = 0
  798. write(1, "64 bytes from 127.0.0.1: icmp_se"...,
  799. 5764 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=28.0 ms
  800. Example 2
  801. ---------
  802. strace passwd 2>&1 | grep open
  803. produces the following output
  804. open("/etc/ld.so.cache", O_RDONLY) = 3
  805. open("/opt/kde/lib/libc.so.5", O_RDONLY) = -1 ENOENT (No such file or directory)
  806. open("/lib/libc.so.5", O_RDONLY) = 3
  807. open("/dev", O_RDONLY) = 3
  808. open("/var/run/utmp", O_RDONLY) = 3
  809. open("/etc/passwd", O_RDONLY) = 3
  810. open("/etc/shadow", O_RDONLY) = 3
  811. open("/etc/login.defs", O_RDONLY) = 4
  812. open("/dev/tty", O_RDONLY) = 4
  813. The 2>&1 is done to redirect stderr to stdout & grep is then filtering this input
  814. through the pipe for each line containing the string open.
  815. Example 3
  816. ---------
  817. Getting sophisticated
  818. telnetd crashes & I don't know why
  819. Steps
  820. -----
  821. 1) Replace the following line in /etc/inetd.conf
  822. telnet stream tcp nowait root /usr/sbin/in.telnetd -h
  823. with
  824. telnet stream tcp nowait root /blah
  825. 2) Create the file /blah with the following contents to start tracing telnetd
  826. #!/bin/bash
  827. /usr/bin/strace -o/t1 -f /usr/sbin/in.telnetd -h
  828. 3) chmod 700 /blah to make it executable only to root
  829. 4)
  830. killall -HUP inetd
  831. or ps aux | grep inetd
  832. get inetd's process id
  833. & kill -HUP inetd to restart it.
  834. Important options
  835. -----------------
  836. -o is used to tell strace to output to a file in our case t1 in the root directory
  837. -f is to follow children i.e.
  838. e.g in our case above telnetd will start the login process & subsequently a shell like bash.
  839. You will be able to tell which is which from the process ID's listed on the left hand side
  840. of the strace output.
  841. -p<pid> will tell strace to attach to a running process, yup this can be done provided
  842. it isn't being traced or debugged already & you have enough privileges,
  843. the reason 2 processes cannot trace or debug the same program is that strace
  844. becomes the parent process of the one being debugged & processes ( unlike people )
  845. can have only one parent.
  846. However the file /t1 will get big quite quickly
  847. to test it telnet 127.0.0.1
  848. now look at what files in.telnetd execve'd
  849. 413 execve("/usr/sbin/in.telnetd", ["/usr/sbin/in.telnetd", "-h"], [/* 17 vars */]) = 0
  850. 414 execve("/bin/login", ["/bin/login", "-h", "localhost", "-p"], [/* 2 vars */]) = 0
  851. Whey it worked!.
  852. Other hints:
  853. ------------
  854. If the program is not very interactive ( i.e. not much keyboard input )
  855. & is crashing in one architecture but not in another you can do
  856. an strace of both programs under as identical a scenario as you can
  857. on both architectures outputting to a file then.
  858. do a diff of the two traces using the diff program
  859. i.e.
  860. diff output1 output2
  861. & maybe you'll be able to see where the call paths differed, this
  862. is possibly near the cause of the crash.
  863. More info
  864. ---------
  865. Look at man pages for strace & the various syscalls
  866. e.g. man strace, man alarm, man socket.
  867. Performance Debugging
  868. =====================
  869. gcc is capable of compiling in profiling code just add the -p option
  870. to the CFLAGS, this obviously affects program size & performance.
  871. This can be used by the gprof gnu profiling tool or the
  872. gcov the gnu code coverage tool ( code coverage is a means of testing
  873. code quality by checking if all the code in an executable in exercised by
  874. a tester ).
  875. Using top to find out where processes are sleeping in the kernel
  876. ----------------------------------------------------------------
  877. To do this copy the System.map from the root directory where
  878. the linux kernel was built to the /boot directory on your
  879. linux machine.
  880. Start top
  881. Now type fU<return>
  882. You should see a new field called WCHAN which
  883. tells you where each process is sleeping here is a typical output.
  884. 6:59pm up 41 min, 1 user, load average: 0.00, 0.00, 0.00
  885. 28 processes: 27 sleeping, 1 running, 0 zombie, 0 stopped
  886. CPU states: 0.0% user, 0.1% system, 0.0% nice, 99.8% idle
  887. Mem: 254900K av, 45976K used, 208924K free, 0K shrd, 28636K buff
  888. Swap: 0K av, 0K used, 0K free 8620K cached
  889. PID USER PRI NI SIZE RSS SHARE WCHAN STAT LIB %CPU %MEM TIME COMMAND
  890. 750 root 12 0 848 848 700 do_select S 0 0.1 0.3 0:00 in.telnetd
  891. 767 root 16 0 1140 1140 964 R 0 0.1 0.4 0:00 top
  892. 1 root 8 0 212 212 180 do_select S 0 0.0 0.0 0:00 init
  893. 2 root 9 0 0 0 0 down_inte SW 0 0.0 0.0 0:00 kmcheck
  894. The time command
  895. ----------------
  896. Another related command is the time command which gives you an indication
  897. of where a process is spending the majority of its time.
  898. e.g.
  899. time ping -c 5 nc
  900. outputs
  901. real 0m4.054s
  902. user 0m0.010s
  903. sys 0m0.010s
  904. Debugging under VM
  905. ==================
  906. Notes
  907. -----
  908. Addresses & values in the VM debugger are always hex never decimal
  909. Address ranges are of the format <HexValue1>-<HexValue2> or <HexValue1>.<HexValue2>
  910. e.g. The address range 0x2000 to 0x3000 can be described as 2000-3000 or 2000.1000
  911. The VM Debugger is case insensitive.
  912. VM's strengths are usually other debuggers weaknesses you can get at any resource
  913. no matter how sensitive e.g. memory management resources,change address translation
  914. in the PSW. For kernel hacking you will reap dividends if you get good at it.
  915. The VM Debugger displays operators but not operands, probably because some
  916. of it was written when memory was expensive & the programmer was probably proud that
  917. it fitted into 2k of memory & the programmers & didn't want to shock hardcore VM'ers by
  918. changing the interface :-), also the debugger displays useful information on the same line &
  919. the author of the code probably felt that it was a good idea not to go over
  920. the 80 columns on the screen.
  921. As some of you are probably in a panic now this isn't as unintuitive as it may seem
  922. as the 390 instructions are easy to decode mentally & you can make a good guess at a lot
  923. of them as all the operands are nibble ( half byte aligned ) & if you have an objdump listing
  924. also it is quite easy to follow, if you don't have an objdump listing keep a copy of
  925. the s/390 Reference Summary & look at between pages 2 & 7 or alternatively the
  926. s/390 principles of operation.
  927. e.g. even I can guess that
  928. 0001AFF8' LR 180F CC 0
  929. is a ( load register ) lr r0,r15
  930. Also it is very easy to tell the length of a 390 instruction from the 2 most significant
  931. bits in the instruction ( not that this info is really useful except if you are trying to
  932. make sense of a hexdump of code ).
  933. Here is a table
  934. Bits Instruction Length
  935. ------------------------------------------
  936. 00 2 Bytes
  937. 01 4 Bytes
  938. 10 4 Bytes
  939. 11 6 Bytes
  940. The debugger also displays other useful info on the same line such as the
  941. addresses being operated on destination addresses of branches & condition codes.
  942. e.g.
  943. 00019736' AHI A7DAFF0E CC 1
  944. 000198BA' BRC A7840004 -> 000198C2' CC 0
  945. 000198CE' STM 900EF068 >> 0FA95E78 CC 2
  946. Useful VM debugger commands
  947. ---------------------------
  948. I suppose I'd better mention this before I start
  949. to list the current active traces do
  950. Q TR
  951. there can be a maximum of 255 of these per set
  952. ( more about trace sets later ).
  953. To stop traces issue a
  954. TR END.
  955. To delete a particular breakpoint issue
  956. TR DEL <breakpoint number>
  957. The PA1 key drops to CP mode so you can issue debugger commands,
  958. Doing alt c (on my 3270 console at least ) clears the screen.
  959. hitting b <enter> comes back to the running operating system
  960. from cp mode ( in our case linux ).
  961. It is typically useful to add shortcuts to your profile.exec file
  962. if you have one ( this is roughly equivalent to autoexec.bat in DOS ).
  963. file here are a few from mine.
  964. /* this gives me command history on issuing f12 */
  965. set pf12 retrieve
  966. /* this continues */
  967. set pf8 imm b
  968. /* goes to trace set a */
  969. set pf1 imm tr goto a
  970. /* goes to trace set b */
  971. set pf2 imm tr goto b
  972. /* goes to trace set c */
  973. set pf3 imm tr goto c
  974. Instruction Tracing
  975. -------------------
  976. Setting a simple breakpoint
  977. TR I PSWA <address>
  978. To debug a particular function try
  979. TR I R <function address range>
  980. TR I on its own will single step.
  981. TR I DATA <MNEMONIC> <OPTIONAL RANGE> will trace for particular mnemonics
  982. e.g.
  983. TR I DATA 4D R 0197BC.4000
  984. will trace for BAS'es ( opcode 4D ) in the range 0197BC.4000
  985. if you were inclined you could add traces for all branch instructions &
  986. suffix them with the run prefix so you would have a backtrace on screen
  987. when a program crashes.
  988. TR BR <INTO OR FROM> will trace branches into or out of an address.
  989. e.g.
  990. TR BR INTO 0 is often quite useful if a program is getting awkward & deciding
  991. to branch to 0 & crashing as this will stop at the address before in jumps to 0.
  992. TR I R <address range> RUN cmd d g
  993. single steps a range of addresses but stays running &
  994. displays the gprs on each step.
  995. Displaying & modifying Registers
  996. --------------------------------
  997. D G will display all the gprs
  998. Adding a extra G to all the commands is necessary to access the full 64 bit
  999. content in VM on z/Architecture obviously this isn't required for access registers
  1000. as these are still 32 bit.
  1001. e.g. DGG instead of DG
  1002. D X will display all the control registers
  1003. D AR will display all the access registers
  1004. D AR4-7 will display access registers 4 to 7
  1005. CPU ALL D G will display the GRPS of all CPUS in the configuration
  1006. D PSW will display the current PSW
  1007. st PSW 2000 will put the value 2000 into the PSW &
  1008. cause crash your machine.
  1009. D PREFIX displays the prefix offset
  1010. Displaying Memory
  1011. -----------------
  1012. To display memory mapped using the current PSW's mapping try
  1013. D <range>
  1014. To make VM display a message each time it hits a particular address & continue try
  1015. D I<range> will disassemble/display a range of instructions.
  1016. ST addr 32 bit word will store a 32 bit aligned address
  1017. D T<range> will display the EBCDIC in an address ( if you are that way inclined )
  1018. D R<range> will display real addresses ( without DAT ) but with prefixing.
  1019. There are other complex options to display if you need to get at say home space
  1020. but are in primary space the easiest thing to do is to temporarily
  1021. modify the PSW to the other addressing mode, display the stuff & then
  1022. restore it.
  1023. Hints
  1024. -----
  1025. If you want to issue a debugger command without halting your virtual machine with the
  1026. PA1 key try prefixing the command with #CP e.g.
  1027. #cp tr i pswa 2000
  1028. also suffixing most debugger commands with RUN will cause them not
  1029. to stop just display the mnemonic at the current instruction on the console.
  1030. If you have several breakpoints you want to put into your program &
  1031. you get fed up of cross referencing with System.map
  1032. you can do the following trick for several symbols.
  1033. grep do_signal System.map
  1034. which emits the following among other things
  1035. 0001f4e0 T do_signal
  1036. now you can do
  1037. TR I PSWA 0001f4e0 cmd msg * do_signal
  1038. This sends a message to your own console each time do_signal is entered.
  1039. ( As an aside I wrote a perl script once which automatically generated a REXX
  1040. script with breakpoints on every kernel procedure, this isn't a good idea
  1041. because there are thousands of these routines & VM can only set 255 breakpoints
  1042. at a time so you nearly had to spend as long pruning the file down as you would
  1043. entering the msg's by hand ),however, the trick might be useful for a single object file.
  1044. On linux'es 3270 emulator x3270 there is a very useful option under the file ment
  1045. Save Screens In File this is very good of keeping a copy of traces.
  1046. From CMS help <command name> will give you online help on a particular command.
  1047. e.g.
  1048. HELP DISPLAY
  1049. Also CP has a file called profile.exec which automatically gets called
  1050. on startup of CMS ( like autoexec.bat ), keeping on a DOS analogy session
  1051. CP has a feature similar to doskey, it may be useful for you to
  1052. use profile.exec to define some keystrokes.
  1053. e.g.
  1054. SET PF9 IMM B
  1055. This does a single step in VM on pressing F8.
  1056. SET PF10 ^
  1057. This sets up the ^ key.
  1058. which can be used for ^c (ctrl-c),^z (ctrl-z) which can't be typed directly into some 3270 consoles.
  1059. SET PF11 ^-
  1060. This types the starting keystrokes for a sysrq see SysRq below.
  1061. SET PF12 RETRIEVE
  1062. This retrieves command history on pressing F12.
  1063. Sometimes in VM the display is set up to scroll automatically this
  1064. can be very annoying if there are messages you wish to look at
  1065. to stop this do
  1066. TERM MORE 255 255
  1067. This will nearly stop automatic screen updates, however it will
  1068. cause a denial of service if lots of messages go to the 3270 console,
  1069. so it would be foolish to use this as the default on a production machine.
  1070. Tracing particular processes
  1071. ----------------------------
  1072. The kernel's text segment is intentionally at an address in memory that it will
  1073. very seldom collide with text segments of user programs ( thanks Martin ),
  1074. this simplifies debugging the kernel.
  1075. However it is quite common for user processes to have addresses which collide
  1076. this can make debugging a particular process under VM painful under normal
  1077. circumstances as the process may change when doing a
  1078. TR I R <address range>.
  1079. Thankfully after reading VM's online help I figured out how to debug
  1080. I particular process.
  1081. Your first problem is to find the STD ( segment table designation )
  1082. of the program you wish to debug.
  1083. There are several ways you can do this here are a few
  1084. 1) objdump --syms <program to be debugged> | grep main
  1085. To get the address of main in the program.
  1086. tr i pswa <address of main>
  1087. Start the program, if VM drops to CP on what looks like the entry
  1088. point of the main function this is most likely the process you wish to debug.
  1089. Now do a D X13 or D XG13 on z/Architecture.
  1090. On 31 bit the STD is bits 1-19 ( the STO segment table origin )
  1091. & 25-31 ( the STL segment table length ) of CR13.
  1092. now type
  1093. TR I R STD <CR13's value> 0.7fffffff
  1094. e.g.
  1095. TR I R STD 8F32E1FF 0.7fffffff
  1096. Another very useful variation is
  1097. TR STORE INTO STD <CR13's value> <address range>
  1098. for finding out when a particular variable changes.
  1099. An alternative way of finding the STD of a currently running process
  1100. is to do the following, ( this method is more complex but
  1101. could be quite convenient if you aren't updating the kernel much &
  1102. so your kernel structures will stay constant for a reasonable period of
  1103. time ).
  1104. grep task /proc/<pid>/status
  1105. from this you should see something like
  1106. task: 0f160000 ksp: 0f161de8 pt_regs: 0f161f68
  1107. This now gives you a pointer to the task structure.
  1108. Now make CC:="s390-gcc -g" kernel/sched.s
  1109. To get the task_struct stabinfo.
  1110. ( task_struct is defined in include/linux/sched.h ).
  1111. Now we want to look at
  1112. task->active_mm->pgd
  1113. on my machine the active_mm in the task structure stab is
  1114. active_mm:(4,12),672,32
  1115. its offset is 672/8=84=0x54
  1116. the pgd member in the mm_struct stab is
  1117. pgd:(4,6)=*(29,5),96,32
  1118. so its offset is 96/8=12=0xc
  1119. so we'll
  1120. hexdump -s 0xf160054 /dev/mem | more
  1121. i.e. task_struct+active_mm offset
  1122. to look at the active_mm member
  1123. f160054 0fee cc60 0019 e334 0000 0000 0000 0011
  1124. hexdump -s 0x0feecc6c /dev/mem | more
  1125. i.e. active_mm+pgd offset
  1126. feecc6c 0f2c 0000 0000 0001 0000 0001 0000 0010
  1127. we get something like
  1128. now do
  1129. TR I R STD <pgd|0x7f> 0.7fffffff
  1130. i.e. the 0x7f is added because the pgd only
  1131. gives the page table origin & we need to set the low bits
  1132. to the maximum possible segment table length.
  1133. TR I R STD 0f2c007f 0.7fffffff
  1134. on z/Architecture you'll probably need to do
  1135. TR I R STD <pgd|0x7> 0.ffffffffffffffff
  1136. to set the TableType to 0x1 & the Table length to 3.
  1137. Tracing Program Exceptions
  1138. --------------------------
  1139. If you get a crash which says something like
  1140. illegal operation or specification exception followed by a register dump
  1141. You can restart linux & trace these using the tr prog <range or value> trace option.
  1142. The most common ones you will normally be tracing for is
  1143. 1=operation exception
  1144. 2=privileged operation exception
  1145. 4=protection exception
  1146. 5=addressing exception
  1147. 6=specification exception
  1148. 10=segment translation exception
  1149. 11=page translation exception
  1150. The full list of these is on page 22 of the current s/390 Reference Summary.
  1151. e.g.
  1152. tr prog 10 will trace segment translation exceptions.
  1153. tr prog on its own will trace all program interruption codes.
  1154. Trace Sets
  1155. ----------
  1156. On starting VM you are initially in the INITIAL trace set.
  1157. You can do a Q TR to verify this.
  1158. If you have a complex tracing situation where you wish to wait for instance
  1159. till a driver is open before you start tracing IO, but know in your
  1160. heart that you are going to have to make several runs through the code till you
  1161. have a clue whats going on.
  1162. What you can do is
  1163. TR I PSWA <Driver open address>
  1164. hit b to continue till breakpoint
  1165. reach the breakpoint
  1166. now do your
  1167. TR GOTO B
  1168. TR IO 7c08-7c09 inst int run
  1169. or whatever the IO channels you wish to trace are & hit b
  1170. To got back to the initial trace set do
  1171. TR GOTO INITIAL
  1172. & the TR I PSWA <Driver open address> will be the only active breakpoint again.
  1173. Tracing linux syscalls under VM
  1174. -------------------------------
  1175. Syscalls are implemented on Linux for S390 by the Supervisor call instruction (SVC) there 256
  1176. possibilities of these as the instruction is made up of a 0xA opcode & the second byte being
  1177. the syscall number. They are traced using the simple command.
  1178. TR SVC <Optional value or range>
  1179. the syscalls are defined in linux/include/asm-s390/unistd.h
  1180. e.g. to trace all file opens just do
  1181. TR SVC 5 ( as this is the syscall number of open )
  1182. SMP Specific commands
  1183. ---------------------
  1184. To find out how many cpus you have
  1185. Q CPUS displays all the CPU's available to your virtual machine
  1186. To find the cpu that the current cpu VM debugger commands are being directed at do
  1187. Q CPU to change the current cpu VM debugger commands are being directed at do
  1188. CPU <desired cpu no>
  1189. On a SMP guest issue a command to all CPUs try prefixing the command with cpu all.
  1190. To issue a command to a particular cpu try cpu <cpu number> e.g.
  1191. CPU 01 TR I R 2000.3000
  1192. If you are running on a guest with several cpus & you have a IO related problem
  1193. & cannot follow the flow of code but you know it isn't smp related.
  1194. from the bash prompt issue
  1195. shutdown -h now or halt.
  1196. do a Q CPUS to find out how many cpus you have
  1197. detach each one of them from cp except cpu 0
  1198. by issuing a
  1199. DETACH CPU 01-(number of cpus in configuration)
  1200. & boot linux again.
  1201. TR SIGP will trace inter processor signal processor instructions.
  1202. DEFINE CPU 01-(number in configuration)
  1203. will get your guests cpus back.
  1204. Help for displaying ascii textstrings
  1205. -------------------------------------
  1206. On the very latest VM Nucleus'es VM can now display ascii
  1207. ( thanks Neale for the hint ) by doing
  1208. D TX<lowaddr>.<len>
  1209. e.g.
  1210. D TX0.100
  1211. Alternatively
  1212. =============
  1213. Under older VM debuggers ( I love EBDIC too ) you can use this little program I wrote which
  1214. will convert a command line of hex digits to ascii text which can be compiled under linux &
  1215. you can copy the hex digits from your x3270 terminal to your xterm if you are debugging
  1216. from a linuxbox.
  1217. This is quite useful when looking at a parameter passed in as a text string
  1218. under VM ( unless you are good at decoding ASCII in your head ).
  1219. e.g. consider tracing an open syscall
  1220. TR SVC 5
  1221. We have stopped at a breakpoint
  1222. 000151B0' SVC 0A05 -> 0001909A' CC 0
  1223. D 20.8 to check the SVC old psw in the prefix area & see was it from userspace
  1224. ( for the layout of the prefix area consult P18 of the s/390 390 Reference Summary
  1225. if you have it available ).
  1226. V00000020 070C2000 800151B2
  1227. The problem state bit wasn't set & it's also too early in the boot sequence
  1228. for it to be a userspace SVC if it was we would have to temporarily switch the
  1229. psw to user space addressing so we could get at the first parameter of the open in
  1230. gpr2.
  1231. Next do a
  1232. D G2
  1233. GPR 2 = 00014CB4
  1234. Now display what gpr2 is pointing to
  1235. D 00014CB4.20
  1236. V00014CB4 2F646576 2F636F6E 736F6C65 00001BF5
  1237. V00014CC4 FC00014C B4001001 E0001000 B8070707
  1238. Now copy the text till the first 00 hex ( which is the end of the string
  1239. to an xterm & do hex2ascii on it.
  1240. hex2ascii 2F646576 2F636F6E 736F6C65 00
  1241. outputs
  1242. Decoded Hex:=/ d e v / c o n s o l e 0x00
  1243. We were opening the console device,
  1244. You can compile the code below yourself for practice :-),
  1245. /*
  1246. * hex2ascii.c
  1247. * a useful little tool for converting a hexadecimal command line to ascii
  1248. *
  1249. * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  1250. * (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation.
  1251. */
  1252. #include <stdio.h>
  1253. int main(int argc,char *argv[])
  1254. {
  1255. int cnt1,cnt2,len,toggle=0;
  1256. int startcnt=1;
  1257. unsigned char c,hex;
  1258. if(argc>1&&(strcmp(argv[1],"-a")==0))
  1259. startcnt=2;
  1260. printf("Decoded Hex:=");
  1261. for(cnt1=startcnt;cnt1<argc;cnt1++)
  1262. {
  1263. len=strlen(argv[cnt1]);
  1264. for(cnt2=0;cnt2<len;cnt2++)
  1265. {
  1266. c=argv[cnt1][cnt2];
  1267. if(c>='0'&&c<='9')
  1268. c=c-'0';
  1269. if(c>='A'&&c<='F')
  1270. c=c-'A'+10;
  1271. if(c>='a'&&c<='f')
  1272. c=c-'a'+10;
  1273. switch(toggle)
  1274. {
  1275. case 0:
  1276. hex=c<<4;
  1277. toggle=1;
  1278. break;
  1279. case 1:
  1280. hex+=c;
  1281. if(hex<32||hex>127)
  1282. {
  1283. if(startcnt==1)
  1284. printf("0x%02X ",(int)hex);
  1285. else
  1286. printf(".");
  1287. }
  1288. else
  1289. {
  1290. printf("%c",hex);
  1291. if(startcnt==1)
  1292. printf(" ");
  1293. }
  1294. toggle=0;
  1295. break;
  1296. }
  1297. }
  1298. }
  1299. printf("\n");
  1300. }
  1301. Stack tracing under VM
  1302. ----------------------
  1303. A basic backtrace
  1304. -----------------
  1305. Here are the tricks I use 9 out of 10 times it works pretty well,
  1306. When your backchain reaches a dead end
  1307. --------------------------------------
  1308. This can happen when an exception happens in the kernel & the kernel is entered twice
  1309. if you reach the NULL pointer at the end of the back chain you should be
  1310. able to sniff further back if you follow the following tricks.
  1311. 1) A kernel address should be easy to recognise since it is in
  1312. primary space & the problem state bit isn't set & also
  1313. The Hi bit of the address is set.
  1314. 2) Another backchain should also be easy to recognise since it is an
  1315. address pointing to another address approximately 100 bytes or 0x70 hex
  1316. behind the current stackpointer.
  1317. Here is some practice.
  1318. boot the kernel & hit PA1 at some random time
  1319. d g to display the gprs, this should display something like
  1320. GPR 0 = 00000001 00156018 0014359C 00000000
  1321. GPR 4 = 00000001 001B8888 000003E0 00000000
  1322. GPR 8 = 00100080 00100084 00000000 000FE000
  1323. GPR 12 = 00010400 8001B2DC 8001B36A 000FFED8
  1324. Note that GPR14 is a return address but as we are real men we are going to
  1325. trace the stack.
  1326. display 0x40 bytes after the stack pointer.
  1327. V000FFED8 000FFF38 8001B838 80014C8E 000FFF38
  1328. V000FFEE8 00000000 00000000 000003E0 00000000
  1329. V000FFEF8 00100080 00100084 00000000 000FE000
  1330. V000FFF08 00010400 8001B2DC 8001B36A 000FFED8
  1331. Ah now look at whats in sp+56 (sp+0x38) this is 8001B36A our saved r14 if
  1332. you look above at our stackframe & also agrees with GPR14.
  1333. now backchain
  1334. d 000FFF38.40
  1335. we now are taking the contents of SP to get our first backchain.
  1336. V000FFF38 000FFFA0 00000000 00014995 00147094
  1337. V000FFF48 00147090 001470A0 000003E0 00000000
  1338. V000FFF58 00100080 00100084 00000000 001BF1D0
  1339. V000FFF68 00010400 800149BA 80014CA6 000FFF38
  1340. This displays a 2nd return address of 80014CA6
  1341. now do d 000FFFA0.40 for our 3rd backchain
  1342. V000FFFA0 04B52002 0001107F 00000000 00000000
  1343. V000FFFB0 00000000 00000000 FF000000 0001107F
  1344. V000FFFC0 00000000 00000000 00000000 00000000
  1345. V000FFFD0 00010400 80010802 8001085A 000FFFA0
  1346. our 3rd return address is 8001085A
  1347. as the 04B52002 looks suspiciously like rubbish it is fair to assume that the kernel entry routines
  1348. for the sake of optimisation don't set up a backchain.
  1349. now look at System.map to see if the addresses make any sense.
  1350. grep -i 0001b3 System.map
  1351. outputs among other things
  1352. 0001b304 T cpu_idle
  1353. so 8001B36A
  1354. is cpu_idle+0x66 ( quiet the cpu is asleep, don't wake it )
  1355. grep -i 00014 System.map
  1356. produces among other things
  1357. 00014a78 T start_kernel
  1358. so 0014CA6 is start_kernel+some hex number I can't add in my head.
  1359. grep -i 00108 System.map
  1360. this produces
  1361. 00010800 T _stext
  1362. so 8001085A is _stext+0x5a
  1363. Congrats you've done your first backchain.
  1364. s/390 & z/Architecture IO Overview
  1365. ==================================
  1366. I am not going to give a course in 390 IO architecture as this would take me quite a
  1367. while & I'm no expert. Instead I'll give a 390 IO architecture summary for Dummies if you have
  1368. the s/390 principles of operation available read this instead. If nothing else you may find a few
  1369. useful keywords in here & be able to use them on a web search engine like altavista to find
  1370. more useful information.
  1371. Unlike other bus architectures modern 390 systems do their IO using mostly
  1372. fibre optics & devices such as tapes & disks can be shared between several mainframes,
  1373. also S390 can support up to 65536 devices while a high end PC based system might be choking
  1374. with around 64. Here is some of the common IO terminology
  1375. Subchannel:
  1376. This is the logical number most IO commands use to talk to an IO device there can be up to
  1377. 0x10000 (65536) of these in a configuration typically there is a few hundred. Under VM
  1378. for simplicity they are allocated contiguously, however on the native hardware they are not
  1379. they typically stay consistent between boots provided no new hardware is inserted or removed.
  1380. Under Linux for 390 we use these as IRQ's & also when issuing an IO command (CLEAR SUBCHANNEL,
  1381. HALT SUBCHANNEL,MODIFY SUBCHANNEL,RESUME SUBCHANNEL,START SUBCHANNEL,STORE SUBCHANNEL &
  1382. TEST SUBCHANNEL ) we use this as the ID of the device we wish to talk to, the most
  1383. important of these instructions are START SUBCHANNEL ( to start IO ), TEST SUBCHANNEL ( to check
  1384. whether the IO completed successfully ), & HALT SUBCHANNEL ( to kill IO ), a subchannel
  1385. can have up to 8 channel paths to a device this offers redundancy if one is not available.
  1386. Device Number:
  1387. This number remains static & Is closely tied to the hardware, there are 65536 of these
  1388. also they are made up of a CHPID ( Channel Path ID, the most significant 8 bits )
  1389. & another lsb 8 bits. These remain static even if more devices are inserted or removed
  1390. from the hardware, there is a 1 to 1 mapping between Subchannels & Device Numbers provided
  1391. devices aren't inserted or removed.
  1392. Channel Control Words:
  1393. CCWS are linked lists of instructions initially pointed to by an operation request block (ORB),
  1394. which is initially given to Start Subchannel (SSCH) command along with the subchannel number
  1395. for the IO subsystem to process while the CPU continues executing normal code.
  1396. These come in two flavours, Format 0 ( 24 bit for backward )
  1397. compatibility & Format 1 ( 31 bit ). These are typically used to issue read & write
  1398. ( & many other instructions ) they consist of a length field & an absolute address field.
  1399. For each IO typically get 1 or 2 interrupts one for channel end ( primary status ) when the
  1400. channel is idle & the second for device end ( secondary status ) sometimes you get both
  1401. concurrently, you check how the IO went on by issuing a TEST SUBCHANNEL at each interrupt,
  1402. from which you receive an Interruption response block (IRB). If you get channel & device end
  1403. status in the IRB without channel checks etc. your IO probably went okay. If you didn't you
  1404. probably need a doctor to examine the IRB & extended status word etc.
  1405. If an error occurs, more sophisticated control units have a facility known as
  1406. concurrent sense this means that if an error occurs Extended sense information will
  1407. be presented in the Extended status word in the IRB if not you have to issue a
  1408. subsequent SENSE CCW command after the test subchannel.
  1409. TPI( Test pending interrupt) can also be used for polled IO but in multitasking multiprocessor
  1410. systems it isn't recommended except for checking special cases ( i.e. non looping checks for
  1411. pending IO etc. ).
  1412. Store Subchannel & Modify Subchannel can be used to examine & modify operating characteristics
  1413. of a subchannel ( e.g. channel paths ).
  1414. Other IO related Terms:
  1415. Sysplex: S390's Clustering Technology
  1416. QDIO: S390's new high speed IO architecture to support devices such as gigabit ethernet,
  1417. this architecture is also designed to be forward compatible with up & coming 64 bit machines.
  1418. General Concepts
  1419. Input Output Processors (IOP's) are responsible for communicating between
  1420. the mainframe CPU's & the channel & relieve the mainframe CPU's from the
  1421. burden of communicating with IO devices directly, this allows the CPU's to
  1422. concentrate on data processing.
  1423. IOP's can use one or more links ( known as channel paths ) to talk to each
  1424. IO device. It first checks for path availability & chooses an available one,
  1425. then starts ( & sometimes terminates IO ).
  1426. There are two types of channel path: ESCON & the Parallel IO interface.
  1427. IO devices are attached to control units, control units provide the
  1428. logic to interface the channel paths & channel path IO protocols to
  1429. the IO devices, they can be integrated with the devices or housed separately
  1430. & often talk to several similar devices ( typical examples would be raid
  1431. controllers or a control unit which connects to 1000 3270 terminals ).
  1432. +---------------------------------------------------------------+
  1433. | +-----+ +-----+ +-----+ +-----+ +----------+ +----------+ |
  1434. | | CPU | | CPU | | CPU | | CPU | | Main | | Expanded | |
  1435. | | | | | | | | | | Memory | | Storage | |
  1436. | +-----+ +-----+ +-----+ +-----+ +----------+ +----------+ |
  1437. |---------------------------------------------------------------+
  1438. | IOP | IOP | IOP |
  1439. |---------------------------------------------------------------
  1440. | C | C | C | C | C | C | C | C | C | C | C | C | C | C | C | C |
  1441. ----------------------------------------------------------------
  1442. || ||
  1443. || Bus & Tag Channel Path || ESCON
  1444. || ====================== || Channel
  1445. || || || || Path
  1446. +----------+ +----------+ +----------+
  1447. | | | | | |
  1448. | CU | | CU | | CU |
  1449. | | | | | |
  1450. +----------+ +----------+ +----------+
  1451. | | | | |
  1452. +----------+ +----------+ +----------+ +----------+ +----------+
  1453. |I/O Device| |I/O Device| |I/O Device| |I/O Device| |I/O Device|
  1454. +----------+ +----------+ +----------+ +----------+ +----------+
  1455. CPU = Central Processing Unit
  1456. C = Channel
  1457. IOP = IP Processor
  1458. CU = Control Unit
  1459. The 390 IO systems come in 2 flavours the current 390 machines support both
  1460. The Older 360 & 370 Interface,sometimes called the Parallel I/O interface,
  1461. sometimes called Bus-and Tag & sometimes Original Equipment Manufacturers
  1462. Interface (OEMI).
  1463. This byte wide Parallel channel path/bus has parity & data on the "Bus" cable
  1464. & control lines on the "Tag" cable. These can operate in byte multiplex mode for
  1465. sharing between several slow devices or burst mode & monopolize the channel for the
  1466. whole burst. Up to 256 devices can be addressed on one of these cables. These cables are
  1467. about one inch in diameter. The maximum unextended length supported by these cables is
  1468. 125 Meters but this can be extended up to 2km with a fibre optic channel extended
  1469. such as a 3044. The maximum burst speed supported is 4.5 megabytes per second however
  1470. some really old processors support only transfer rates of 3.0, 2.0 & 1.0 MB/sec.
  1471. One of these paths can be daisy chained to up to 8 control units.
  1472. ESCON if fibre optic it is also called FICON
  1473. Was introduced by IBM in 1990. Has 2 fibre optic cables & uses either leds or lasers
  1474. for communication at a signaling rate of up to 200 megabits/sec. As 10bits are transferred
  1475. for every 8 bits info this drops to 160 megabits/sec & to 18.6 Megabytes/sec once
  1476. control info & CRC are added. ESCON only operates in burst mode.
  1477. ESCONs typical max cable length is 3km for the led version & 20km for the laser version
  1478. known as XDF ( extended distance facility ). This can be further extended by using an
  1479. ESCON director which triples the above mentioned ranges. Unlike Bus & Tag as ESCON is
  1480. serial it uses a packet switching architecture the standard Bus & Tag control protocol
  1481. is however present within the packets. Up to 256 devices can be attached to each control
  1482. unit that uses one of these interfaces.
  1483. Common 390 Devices include:
  1484. Network adapters typically OSA2,3172's,2116's & OSA-E gigabit ethernet adapters,
  1485. Consoles 3270 & 3215 ( a teletype emulated under linux for a line mode console ).
  1486. DASD's direct access storage devices ( otherwise known as hard disks ).
  1487. Tape Drives.
  1488. CTC ( Channel to Channel Adapters ),
  1489. ESCON or Parallel Cables used as a very high speed serial link
  1490. between 2 machines. We use 2 cables under linux to do a bi-directional serial link.
  1491. Debugging IO on s/390 & z/Architecture under VM
  1492. ===============================================
  1493. Now we are ready to go on with IO tracing commands under VM
  1494. A few self explanatory queries:
  1495. Q OSA
  1496. Q CTC
  1497. Q DISK ( This command is CMS specific )
  1498. Q DASD
  1499. Q OSA on my machine returns
  1500. OSA 7C08 ON OSA 7C08 SUBCHANNEL = 0000
  1501. OSA 7C09 ON OSA 7C09 SUBCHANNEL = 0001
  1502. OSA 7C14 ON OSA 7C14 SUBCHANNEL = 0002
  1503. OSA 7C15 ON OSA 7C15 SUBCHANNEL = 0003
  1504. If you have a guest with certain privileges you may be able to see devices
  1505. which don't belong to you. To avoid this, add the option V.
  1506. e.g.
  1507. Q V OSA
  1508. Now using the device numbers returned by this command we will
  1509. Trace the io starting up on the first device 7c08 & 7c09
  1510. In our simplest case we can trace the
  1511. start subchannels
  1512. like TR SSCH 7C08-7C09
  1513. or the halt subchannels
  1514. or TR HSCH 7C08-7C09
  1515. MSCH's ,STSCH's I think you can guess the rest
  1516. Ingo's favourite trick is tracing all the IO's & CCWS & spooling them into the reader of another
  1517. VM guest so he can ftp the logfile back to his own machine.I'll do a small bit of this & give you
  1518. a look at the output.
  1519. 1) Spool stdout to VM reader
  1520. SP PRT TO (another vm guest ) or * for the local vm guest
  1521. 2) Fill the reader with the trace
  1522. TR IO 7c08-7c09 INST INT CCW PRT RUN
  1523. 3) Start up linux
  1524. i 00c
  1525. 4) Finish the trace
  1526. TR END
  1527. 5) close the reader
  1528. C PRT
  1529. 6) list reader contents
  1530. RDRLIST
  1531. 7) copy it to linux4's minidisk
  1532. RECEIVE / LOG TXT A1 ( replace
  1533. 8)
  1534. filel & press F11 to look at it
  1535. You should see something like:
  1536. 00020942' SSCH B2334000 0048813C CC 0 SCH 0000 DEV 7C08
  1537. CPA 000FFDF0 PARM 00E2C9C4 KEY 0 FPI C0 LPM 80
  1538. CCW 000FFDF0 E4200100 00487FE8 0000 E4240100 ........
  1539. IDAL 43D8AFE8
  1540. IDAL 0FB76000
  1541. 00020B0A' I/O DEV 7C08 -> 000197BC' SCH 0000 PARM 00E2C9C4
  1542. 00021628' TSCH B2354000 >> 00488164 CC 0 SCH 0000 DEV 7C08
  1543. CCWA 000FFDF8 DEV STS 0C SCH STS 00 CNT 00EC
  1544. KEY 0 FPI C0 CC 0 CTLS 4007
  1545. 00022238' STSCH B2344000 >> 00488108 CC 0 SCH 0000 DEV 7C08
  1546. If you don't like messing up your readed ( because you possibly booted from it )
  1547. you can alternatively spool it to another readers guest.
  1548. Other common VM device related commands
  1549. ---------------------------------------------
  1550. These commands are listed only because they have
  1551. been of use to me in the past & may be of use to
  1552. you too. For more complete info on each of the commands
  1553. use type HELP <command> from CMS.
  1554. detaching devices
  1555. DET <devno range>
  1556. ATT <devno range> <guest>
  1557. attach a device to guest * for your own guest
  1558. READY <devno> cause VM to issue a fake interrupt.
  1559. The VARY command is normally only available to VM administrators.
  1560. VARY ON PATH <path> TO <devno range>
  1561. VARY OFF PATH <PATH> FROM <devno range>
  1562. This is used to switch on or off channel paths to devices.
  1563. Q CHPID <channel path ID>
  1564. This displays state of devices using this channel path
  1565. D SCHIB <subchannel>
  1566. This displays the subchannel information SCHIB block for the device.
  1567. this I believe is also only available to administrators.
  1568. DEFINE CTC <devno>
  1569. defines a virtual CTC channel to channel connection
  1570. 2 need to be defined on each guest for the CTC driver to use.
  1571. COUPLE devno userid remote devno
  1572. Joins a local virtual device to a remote virtual device
  1573. ( commonly used for the CTC driver ).
  1574. Building a VM ramdisk under CMS which linux can use
  1575. def vfb-<blocksize> <subchannel> <number blocks>
  1576. blocksize is commonly 4096 for linux.
  1577. Formatting it
  1578. format <subchannel> <driver letter e.g. x> (blksize <blocksize>
  1579. Sharing a disk between multiple guests
  1580. LINK userid devno1 devno2 mode password
  1581. GDB on S390
  1582. ===========
  1583. N.B. if compiling for debugging gdb works better without optimisation
  1584. ( see Compiling programs for debugging )
  1585. invocation
  1586. ----------
  1587. gdb <victim program> <optional corefile>
  1588. Online help
  1589. -----------
  1590. help: gives help on commands
  1591. e.g.
  1592. help
  1593. help display
  1594. Note gdb's online help is very good use it.
  1595. Assembly
  1596. --------
  1597. info registers: displays registers other than floating point.
  1598. info all-registers: displays floating points as well.
  1599. disassemble: disassembles
  1600. e.g.
  1601. disassemble without parameters will disassemble the current function
  1602. disassemble $pc $pc+10
  1603. Viewing & modifying variables
  1604. -----------------------------
  1605. print or p: displays variable or register
  1606. e.g. p/x $sp will display the stack pointer
  1607. display: prints variable or register each time program stops
  1608. e.g.
  1609. display/x $pc will display the program counter
  1610. display argc
  1611. undisplay : undo's display's
  1612. info breakpoints: shows all current breakpoints
  1613. info stack: shows stack back trace ( if this doesn't work too well, I'll show you the
  1614. stacktrace by hand below ).
  1615. info locals: displays local variables.
  1616. info args: display current procedure arguments.
  1617. set args: will set argc & argv each time the victim program is invoked.
  1618. set <variable>=value
  1619. set argc=100
  1620. set $pc=0
  1621. Modifying execution
  1622. -------------------
  1623. step: steps n lines of sourcecode
  1624. step steps 1 line.
  1625. step 100 steps 100 lines of code.
  1626. next: like step except this will not step into subroutines
  1627. stepi: steps a single machine code instruction.
  1628. e.g. stepi 100
  1629. nexti: steps a single machine code instruction but will not step into subroutines.
  1630. finish: will run until exit of the current routine
  1631. run: (re)starts a program
  1632. cont: continues a program
  1633. quit: exits gdb.
  1634. breakpoints
  1635. ------------
  1636. break
  1637. sets a breakpoint
  1638. e.g.
  1639. break main
  1640. break *$pc
  1641. break *0x400618
  1642. heres a really useful one for large programs
  1643. rbr
  1644. Set a breakpoint for all functions matching REGEXP
  1645. e.g.
  1646. rbr 390
  1647. will set a breakpoint with all functions with 390 in their name.
  1648. info breakpoints
  1649. lists all breakpoints
  1650. delete: delete breakpoint by number or delete them all
  1651. e.g.
  1652. delete 1 will delete the first breakpoint
  1653. delete will delete them all
  1654. watch: This will set a watchpoint ( usually hardware assisted ),
  1655. This will watch a variable till it changes
  1656. e.g.
  1657. watch cnt, will watch the variable cnt till it changes.
  1658. As an aside unfortunately gdb's, architecture independent watchpoint code
  1659. is inconsistent & not very good, watchpoints usually work but not always.
  1660. info watchpoints: Display currently active watchpoints
  1661. condition: ( another useful one )
  1662. Specify breakpoint number N to break only if COND is true.
  1663. Usage is `condition N COND', where N is an integer and COND is an
  1664. expression to be evaluated whenever breakpoint N is reached.
  1665. User defined functions/macros
  1666. -----------------------------
  1667. define: ( Note this is very very useful,simple & powerful )
  1668. usage define <name> <list of commands> end
  1669. examples which you should consider putting into .gdbinit in your home directory
  1670. define d
  1671. stepi
  1672. disassemble $pc $pc+10
  1673. end
  1674. define e
  1675. nexti
  1676. disassemble $pc $pc+10
  1677. end
  1678. Other hard to classify stuff
  1679. ----------------------------
  1680. signal n:
  1681. sends the victim program a signal.
  1682. e.g. signal 3 will send a SIGQUIT.
  1683. info signals:
  1684. what gdb does when the victim receives certain signals.
  1685. list:
  1686. e.g.
  1687. list lists current function source
  1688. list 1,10 list first 10 lines of current file.
  1689. list test.c:1,10
  1690. directory:
  1691. Adds directories to be searched for source if gdb cannot find the source.
  1692. (note it is a bit sensitive about slashes)
  1693. e.g. To add the root of the filesystem to the searchpath do
  1694. directory //
  1695. call <function>
  1696. This calls a function in the victim program, this is pretty powerful
  1697. e.g.
  1698. (gdb) call printf("hello world")
  1699. outputs:
  1700. $1 = 11
  1701. You might now be thinking that the line above didn't work, something extra had to be done.
  1702. (gdb) call fflush(stdout)
  1703. hello world$2 = 0
  1704. As an aside the debugger also calls malloc & free under the hood
  1705. to make space for the "hello world" string.
  1706. hints
  1707. -----
  1708. 1) command completion works just like bash
  1709. ( if you are a bad typist like me this really helps )
  1710. e.g. hit br <TAB> & cursor up & down :-).
  1711. 2) if you have a debugging problem that takes a few steps to recreate
  1712. put the steps into a file called .gdbinit in your current working directory
  1713. if you have defined a few extra useful user defined commands put these in
  1714. your home directory & they will be read each time gdb is launched.
  1715. A typical .gdbinit file might be.
  1716. break main
  1717. run
  1718. break runtime_exception
  1719. cont
  1720. stack chaining in gdb by hand
  1721. -----------------------------
  1722. This is done using a the same trick described for VM
  1723. p/x (*($sp+56))&0x7fffffff get the first backchain.
  1724. For z/Architecture
  1725. Replace 56 with 112 & ignore the &0x7fffffff
  1726. in the macros below & do nasty casts to longs like the following
  1727. as gdb unfortunately deals with printed arguments as ints which
  1728. messes up everything.
  1729. i.e. here is a 3rd backchain dereference
  1730. p/x *(long *)(***(long ***)$sp+112)
  1731. this outputs
  1732. $5 = 0x528f18
  1733. on my machine.
  1734. Now you can use
  1735. info symbol (*($sp+56))&0x7fffffff
  1736. you might see something like.
  1737. rl_getc + 36 in section .text telling you what is located at address 0x528f18
  1738. Now do.
  1739. p/x (*(*$sp+56))&0x7fffffff
  1740. This outputs
  1741. $6 = 0x528ed0
  1742. Now do.
  1743. info symbol (*(*$sp+56))&0x7fffffff
  1744. rl_read_key + 180 in section .text
  1745. now do
  1746. p/x (*(**$sp+56))&0x7fffffff
  1747. & so on.
  1748. Disassembling instructions without debug info
  1749. ---------------------------------------------
  1750. gdb typically complains if there is a lack of debugging
  1751. symbols in the disassemble command with
  1752. "No function contains specified address." To get around
  1753. this do
  1754. x/<number lines to disassemble>xi <address>
  1755. e.g.
  1756. x/20xi 0x400730
  1757. Note: Remember gdb has history just like bash you don't need to retype the
  1758. whole line just use the up & down arrows.
  1759. For more info
  1760. -------------
  1761. From your linuxbox do
  1762. man gdb or info gdb.
  1763. core dumps
  1764. ----------
  1765. What a core dump ?,
  1766. A core dump is a file generated by the kernel ( if allowed ) which contains the registers,
  1767. & all active pages of the program which has crashed.
  1768. From this file gdb will allow you to look at the registers & stack trace & memory of the
  1769. program as if it just crashed on your system, it is usually called core & created in the
  1770. current working directory.
  1771. This is very useful in that a customer can mail a core dump to a technical support department
  1772. & the technical support department can reconstruct what happened.
  1773. Provided they have an identical copy of this program with debugging symbols compiled in &
  1774. the source base of this build is available.
  1775. In short it is far more useful than something like a crash log could ever hope to be.
  1776. In theory all that is missing to restart a core dumped program is a kernel patch which
  1777. will do the following.
  1778. 1) Make a new kernel task structure
  1779. 2) Reload all the dumped pages back into the kernel's memory management structures.
  1780. 3) Do the required clock fixups
  1781. 4) Get all files & network connections for the process back into an identical state ( really difficult ).
  1782. 5) A few more difficult things I haven't thought of.
  1783. Why have I never seen one ?.
  1784. Probably because you haven't used the command
  1785. ulimit -c unlimited in bash
  1786. to allow core dumps, now do
  1787. ulimit -a
  1788. to verify that the limit was accepted.
  1789. A sample core dump
  1790. To create this I'm going to do
  1791. ulimit -c unlimited
  1792. gdb
  1793. to launch gdb (my victim app. ) now be bad & do the following from another
  1794. telnet/xterm session to the same machine
  1795. ps -aux | grep gdb
  1796. kill -SIGSEGV <gdb's pid>
  1797. or alternatively use killall -SIGSEGV gdb if you have the killall command.
  1798. Now look at the core dump.
  1799. ./gdb core
  1800. Displays the following
  1801. GNU gdb 4.18
  1802. Copyright 1998 Free Software Foundation, Inc.
  1803. GDB is free software, covered by the GNU General Public License, and you are
  1804. welcome to change it and/or distribute copies of it under certain conditions.
  1805. Type "show copying" to see the conditions.
  1806. There is absolutely no warranty for GDB. Type "show warranty" for details.
  1807. This GDB was configured as "s390-ibm-linux"...
  1808. Core was generated by `./gdb'.
  1809. Program terminated with signal 11, Segmentation fault.
  1810. Reading symbols from /usr/lib/libncurses.so.4...done.
  1811. Reading symbols from /lib/libm.so.6...done.
  1812. Reading symbols from /lib/libc.so.6...done.
  1813. Reading symbols from /lib/ld-linux.so.2...done.
  1814. #0 0x40126d1a in read () from /lib/libc.so.6
  1815. Setting up the environment for debugging gdb.
  1816. Breakpoint 1 at 0x4dc6f8: file utils.c, line 471.
  1817. Breakpoint 2 at 0x4d87a4: file top.c, line 2609.
  1818. (top-gdb) info stack
  1819. #0 0x40126d1a in read () from /lib/libc.so.6
  1820. #1 0x528f26 in rl_getc (stream=0x7ffffde8) at input.c:402
  1821. #2 0x528ed0 in rl_read_key () at input.c:381
  1822. #3 0x5167e6 in readline_internal_char () at readline.c:454
  1823. #4 0x5168ee in readline_internal_charloop () at readline.c:507
  1824. #5 0x51692c in readline_internal () at readline.c:521
  1825. #6 0x5164fe in readline (prompt=0x7ffff810 "\177�ÿ�øx\177�ÿ�÷�Ø\177�ÿ�øx�À")
  1826. at readline.c:349
  1827. #7 0x4d7a8a in command_line_input (prrompt=0x564420 "(gdb) ", repeat=1,
  1828. annotation_suffix=0x4d6b44 "prompt") at top.c:2091
  1829. #8 0x4d6cf0 in command_loop () at top.c:1345
  1830. #9 0x4e25bc in main (argc=1, argv=0x7ffffdf4) at main.c:635
  1831. LDD
  1832. ===
  1833. This is a program which lists the shared libraries which a library needs,
  1834. Note you also get the relocations of the shared library text segments which
  1835. help when using objdump --source.
  1836. e.g.
  1837. ldd ./gdb
  1838. outputs
  1839. libncurses.so.4 => /usr/lib/libncurses.so.4 (0x40018000)
  1840. libm.so.6 => /lib/libm.so.6 (0x4005e000)
  1841. libc.so.6 => /lib/libc.so.6 (0x40084000)
  1842. /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
  1843. Debugging shared libraries
  1844. ==========================
  1845. Most programs use shared libraries, however it can be very painful
  1846. when you single step instruction into a function like printf for the
  1847. first time & you end up in functions like _dl_runtime_resolve this is
  1848. the ld.so doing lazy binding, lazy binding is a concept in ELF where
  1849. shared library functions are not loaded into memory unless they are
  1850. actually used, great for saving memory but a pain to debug.
  1851. To get around this either relink the program -static or exit gdb type
  1852. export LD_BIND_NOW=true this will stop lazy binding & restart the gdb'ing
  1853. the program in question.
  1854. Debugging modules
  1855. =================
  1856. As modules are dynamically loaded into the kernel their address can be
  1857. anywhere to get around this use the -m option with insmod to emit a load
  1858. map which can be piped into a file if required.
  1859. The proc file system
  1860. ====================
  1861. What is it ?.
  1862. It is a filesystem created by the kernel with files which are created on demand
  1863. by the kernel if read, or can be used to modify kernel parameters,
  1864. it is a powerful concept.
  1865. e.g.
  1866. cat /proc/sys/net/ipv4/ip_forward
  1867. On my machine outputs
  1868. 0
  1869. telling me ip_forwarding is not on to switch it on I can do
  1870. echo 1 > /proc/sys/net/ipv4/ip_forward
  1871. cat it again
  1872. cat /proc/sys/net/ipv4/ip_forward
  1873. On my machine now outputs
  1874. 1
  1875. IP forwarding is on.
  1876. There is a lot of useful info in here best found by going in & having a look around,
  1877. so I'll take you through some entries I consider important.
  1878. All the processes running on the machine have there own entry defined by
  1879. /proc/<pid>
  1880. So lets have a look at the init process
  1881. cd /proc/1
  1882. cat cmdline
  1883. emits
  1884. init [2]
  1885. cd /proc/1/fd
  1886. This contains numerical entries of all the open files,
  1887. some of these you can cat e.g. stdout (2)
  1888. cat /proc/29/maps
  1889. on my machine emits
  1890. 00400000-00478000 r-xp 00000000 5f:00 4103 /bin/bash
  1891. 00478000-0047e000 rw-p 00077000 5f:00 4103 /bin/bash
  1892. 0047e000-00492000 rwxp 00000000 00:00 0
  1893. 40000000-40015000 r-xp 00000000 5f:00 14382 /lib/ld-2.1.2.so
  1894. 40015000-40016000 rw-p 00014000 5f:00 14382 /lib/ld-2.1.2.so
  1895. 40016000-40017000 rwxp 00000000 00:00 0
  1896. 40017000-40018000 rw-p 00000000 00:00 0
  1897. 40018000-4001b000 r-xp 00000000 5f:00 14435 /lib/libtermcap.so.2.0.8
  1898. 4001b000-4001c000 rw-p 00002000 5f:00 14435 /lib/libtermcap.so.2.0.8
  1899. 4001c000-4010d000 r-xp 00000000 5f:00 14387 /lib/libc-2.1.2.so
  1900. 4010d000-40111000 rw-p 000f0000 5f:00 14387 /lib/libc-2.1.2.so
  1901. 40111000-40114000 rw-p 00000000 00:00 0
  1902. 40114000-4011e000 r-xp 00000000 5f:00 14408 /lib/libnss_files-2.1.2.so
  1903. 4011e000-4011f000 rw-p 00009000 5f:00 14408 /lib/libnss_files-2.1.2.so
  1904. 7fffd000-80000000 rwxp ffffe000 00:00 0
  1905. Showing us the shared libraries init uses where they are in memory
  1906. & memory access permissions for each virtual memory area.
  1907. /proc/1/cwd is a softlink to the current working directory.
  1908. /proc/1/root is the root of the filesystem for this process.
  1909. /proc/1/mem is the current running processes memory which you
  1910. can read & write to like a file.
  1911. strace uses this sometimes as it is a bit faster than the
  1912. rather inefficient ptrace interface for peeking at DATA.
  1913. cat status
  1914. Name: init
  1915. State: S (sleeping)
  1916. Pid: 1
  1917. PPid: 0
  1918. Uid: 0 0 0 0
  1919. Gid: 0 0 0 0
  1920. Groups:
  1921. VmSize: 408 kB
  1922. VmLck: 0 kB
  1923. VmRSS: 208 kB
  1924. VmData: 24 kB
  1925. VmStk: 8 kB
  1926. VmExe: 368 kB
  1927. VmLib: 0 kB
  1928. SigPnd: 0000000000000000
  1929. SigBlk: 0000000000000000
  1930. SigIgn: 7fffffffd7f0d8fc
  1931. SigCgt: 00000000280b2603
  1932. CapInh: 00000000fffffeff
  1933. CapPrm: 00000000ffffffff
  1934. CapEff: 00000000fffffeff
  1935. User PSW: 070de000 80414146
  1936. task: 004b6000 tss: 004b62d8 ksp: 004b7ca8 pt_regs: 004b7f68
  1937. User GPRS:
  1938. 00000400 00000000 0000000b 7ffffa90
  1939. 00000000 00000000 00000000 0045d9f4
  1940. 0045cafc 7ffffa90 7fffff18 0045cb08
  1941. 00010400 804039e8 80403af8 7ffff8b0
  1942. User ACRS:
  1943. 00000000 00000000 00000000 00000000
  1944. 00000001 00000000 00000000 00000000
  1945. 00000000 00000000 00000000 00000000
  1946. 00000000 00000000 00000000 00000000
  1947. Kernel BackChain CallChain BackChain CallChain
  1948. 004b7ca8 8002bd0c 004b7d18 8002b92c
  1949. 004b7db8 8005cd50 004b7e38 8005d12a
  1950. 004b7f08 80019114
  1951. Showing among other things memory usage & status of some signals &
  1952. the processes'es registers from the kernel task_structure
  1953. as well as a backchain which may be useful if a process crashes
  1954. in the kernel for some unknown reason.
  1955. Some driver debugging techniques
  1956. ================================
  1957. debug feature
  1958. -------------
  1959. Some of our drivers now support a "debug feature" in
  1960. /proc/s390dbf see s390dbf.txt in the linux/Documentation directory
  1961. for more info.
  1962. e.g.
  1963. to switch on the lcs "debug feature"
  1964. echo 5 > /proc/s390dbf/lcs/level
  1965. & then after the error occurred.
  1966. cat /proc/s390dbf/lcs/sprintf >/logfile
  1967. the logfile now contains some information which may help
  1968. tech support resolve a problem in the field.
  1969. high level debugging network drivers
  1970. ------------------------------------
  1971. ifconfig is a quite useful command
  1972. it gives the current state of network drivers.
  1973. If you suspect your network device driver is dead
  1974. one way to check is type
  1975. ifconfig <network device>
  1976. e.g. tr0
  1977. You should see something like
  1978. tr0 Link encap:16/4 Mbps Token Ring (New) HWaddr 00:04:AC:20:8E:48
  1979. inet addr:9.164.185.132 Bcast:9.164.191.255 Mask:255.255.224.0
  1980. UP BROADCAST RUNNING MULTICAST MTU:2000 Metric:1
  1981. RX packets:246134 errors:0 dropped:0 overruns:0 frame:0
  1982. TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
  1983. collisions:0 txqueuelen:100
  1984. if the device doesn't say up
  1985. try
  1986. /etc/rc.d/init.d/network start
  1987. ( this starts the network stack & hopefully calls ifconfig tr0 up ).
  1988. ifconfig looks at the output of /proc/net/dev & presents it in a more presentable form
  1989. Now ping the device from a machine in the same subnet.
  1990. if the RX packets count & TX packets counts don't increment you probably
  1991. have problems.
  1992. next
  1993. cat /proc/net/arp
  1994. Do you see any hardware addresses in the cache if not you may have problems.
  1995. Next try
  1996. ping -c 5 <broadcast_addr> i.e. the Bcast field above in the output of
  1997. ifconfig. Do you see any replies from machines other than the local machine
  1998. if not you may have problems. also if the TX packets count in ifconfig
  1999. hasn't incremented either you have serious problems in your driver
  2000. (e.g. the txbusy field of the network device being stuck on )
  2001. or you may have multiple network devices connected.
  2002. chandev
  2003. -------
  2004. There is a new device layer for channel devices, some
  2005. drivers e.g. lcs are registered with this layer.
  2006. If the device uses the channel device layer you'll be
  2007. able to find what interrupts it uses & the current state
  2008. of the device.
  2009. See the manpage chandev.8 &type cat /proc/chandev for more info.
  2010. Starting points for debugging scripting languages etc.
  2011. ======================================================
  2012. bash/sh
  2013. bash -x <scriptname>
  2014. e.g. bash -x /usr/bin/bashbug
  2015. displays the following lines as it executes them.
  2016. + MACHINE=i586
  2017. + OS=linux-gnu
  2018. + CC=gcc
  2019. + CFLAGS= -DPROGRAM='bash' -DHOSTTYPE='i586' -DOSTYPE='linux-gnu' -DMACHTYPE='i586-pc-linux-gnu' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./lib -O2 -pipe
  2020. + RELEASE=2.01
  2021. + PATCHLEVEL=1
  2022. + RELSTATUS=release
  2023. + MACHTYPE=i586-pc-linux-gnu
  2024. perl -d <scriptname> runs the perlscript in a fully interactive debugger
  2025. <like gdb>.
  2026. Type 'h' in the debugger for help.
  2027. for debugging java type
  2028. jdb <filename> another fully interactive gdb style debugger.
  2029. & type ? in the debugger for help.
  2030. Dumptool & Lcrash ( lkcd )
  2031. ==========================
  2032. Michael Holzheu & others here at IBM have a fairly mature port of
  2033. SGI's lcrash tool which allows one to look at kernel structures in a
  2034. running kernel.
  2035. It also complements a tool called dumptool which dumps all the kernel's
  2036. memory pages & registers to either a tape or a disk.
  2037. This can be used by tech support or an ambitious end user do
  2038. post mortem debugging of a machine like gdb core dumps.
  2039. Going into how to use this tool in detail will be explained
  2040. in other documentation supplied by IBM with the patches & the
  2041. lcrash homepage http://oss.sgi.com/projects/lkcd/ & the lcrash manpage.
  2042. How they work
  2043. -------------
  2044. Lcrash is a perfectly normal program,however, it requires 2
  2045. additional files, Kerntypes which is built using a patch to the
  2046. linux kernel sources in the linux root directory & the System.map.
  2047. Kerntypes is an objectfile whose sole purpose in life
  2048. is to provide stabs debug info to lcrash, to do this
  2049. Kerntypes is built from kerntypes.c which just includes the most commonly
  2050. referenced header files used when debugging, lcrash can then read the
  2051. .stabs section of this file.
  2052. Debugging a live system it uses /dev/mem
  2053. alternatively for post mortem debugging it uses the data
  2054. collected by dumptool.
  2055. SysRq
  2056. =====
  2057. This is now supported by linux for s/390 & z/Architecture.
  2058. To enable it do compile the kernel with
  2059. Kernel Hacking -> Magic SysRq Key Enabled
  2060. echo "1" > /proc/sys/kernel/sysrq
  2061. also type
  2062. echo "8" >/proc/sys/kernel/printk
  2063. To make printk output go to console.
  2064. On 390 all commands are prefixed with
  2065. ^-
  2066. e.g.
  2067. ^-t will show tasks.
  2068. ^-? or some unknown command will display help.
  2069. The sysrq key reading is very picky ( I have to type the keys in an
  2070. xterm session & paste them into the x3270 console )
  2071. & it may be wise to predefine the keys as described in the VM hints above
  2072. This is particularly useful for syncing disks unmounting & rebooting
  2073. if the machine gets partially hung.
  2074. Read Documentation/sysrq.txt for more info
  2075. References:
  2076. ===========
  2077. Enterprise Systems Architecture Reference Summary
  2078. Enterprise Systems Architecture Principles of Operation
  2079. Hartmut Penners s390 stack frame sheet.
  2080. IBM Mainframe Channel Attachment a technology brief from a CISCO webpage
  2081. Various bits of man & info pages of Linux.
  2082. Linux & GDB source.
  2083. Various info & man pages.
  2084. CMS Help on tracing commands.
  2085. Linux for s/390 Elf Application Binary Interface
  2086. Linux for z/Series Elf Application Binary Interface ( Both Highly Recommended )
  2087. z/Architecture Principles of Operation SA22-7832-00
  2088. Enterprise Systems Architecture/390 Reference Summary SA22-7209-01 & the
  2089. Enterprise Systems Architecture/390 Principles of Operation SA22-7201-05
  2090. Special Thanks
  2091. ==============
  2092. Special thanks to Neale Ferguson who maintains a much
  2093. prettier HTML version of this page at
  2094. http://penguinvm.princeton.edu/notes.html#Debug390
  2095. Bob Grainger Stefan Bader & others for reporting bugs