URL
https://opencores.org/ocsvn/test_project/test_project/trunk
Subversion Repositories test_project
[/] [test_project/] [trunk/] [linux_sd_driver/] [Documentation/] [s390/] [Debugging390.txt] - Rev 78
Go to most recent revision | Compare with Previous | Blame | View Log
Debugging on Linux for s/390 & z/ArchitecturebyDenis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)Copyright (C) 2000-2001 IBM Deutschland Entwicklung GmbH, IBM CorporationBest viewed with fixed width fontsOverview of Document:=====================This document is intended to give a good overview of how to debugLinux for s/390 & z/Architecture. It isn't intended as a complete reference & not atutorial on the fundamentals of C & assembly. It doesn't go into390 IO in any detail. It is intended to complement the documents in thereference section below & any other worthwhile references you get.It is intended like the Enterprise Systems Architecture/390 Reference Summaryto be printed out & used as a quick cheat sheet self help style reference whenproblems occur.Contents========Register SetAddress Spaces on Intel LinuxAddress Spaces on Linux for s/390 & z/ArchitectureThe Linux for s/390 & z/Architecture Kernel Task StructureRegister Usage & Stackframes on Linux for s/390 & z/ArchitectureA sample program with commentsCompiling programs for debugging on Linux for s/390 & z/ArchitectureFiguring out gcc compile errorsDebugging ToolsobjdumpstracePerformance DebuggingDebugging under VMs/390 & z/Architecture IO OverviewDebugging IO on s/390 & z/Architecture under VMGDB on s/390 & z/ArchitectureStack chaining in gdb by handExamining core dumpslddDebugging modulesThe proc file systemStarting points for debugging scripting languages etc.Dumptool & LcrashSysRqReferencesSpecial ThanksRegister Set============The current architectures have the following registers.16 General propose registers, 32 bit on s/390 64 bit on z/Architecture, r0-r15 or gpr0-gpr15 used for arithmetic & addressing.16 Control registers, 32 bit on s/390 64 bit on z/Architecture, ( cr0-cr15 kernel usage only ) used for memory management,interrupt control,debugging control etc.16 Access registers ( ar0-ar15 ) 32 bit on s/390 & z/Architecturenot used by normal programs but potentially couldbe used as temporary storage. Their main purpose is their 1 to 1association with general purpose registers and are used inthe kernel for copying data between kernel & user address spaces.Access register 0 ( & access register 1 on z/Architecture ( needs 64 bitpointer ) ) is currently used by the pthread library as a pointer tothe current running threads private area.16 64 bit floating point registers (fp0-fp15 ) IEEE & HFP floatingpoint format compliant on G5 upwards & a Floating point control reg (FPC)4 64 bit registers (fp0,fp2,fp4 & fp6) HFP only on older machines.Note:Linux (currently) always uses IEEE & emulates G5 IEEE format on older machines,( provided the kernel is configured for this ).The PSW is the most important register on the machine itis 64 bit on s/390 & 128 bit on z/Architecture & serves the roles ofa program counter (pc), condition code register,memory space designator.In IBM standard notation I am counting bit 0 as the MSB.It has several advantages over a normal program counterin that you can change address translation & program counterin a single instruction. To change address translation,e.g. switching address translation off requires that youhave a logical=physical mapping for the address you arecurrently running at.Bit Values/390 z/Architecture0 0 Reserved ( must be 0 ) otherwise specification exception occurs.1 1 Program Event Recording 1 PER enabled,PER is used to facilitate debugging e.g. single stepping.2-4 2-4 Reserved ( must be 0 ).5 5 Dynamic address translation 1=DAT on.6 6 Input/Output interrupt Mask7 7 External interrupt Mask used primarily for interprocessor signalling &clock interrupts.8-11 8-11 PSW Key used for complex memory protection mechanism not used under linux12 12 1 on s/390 0 on z/Architecture13 13 Machine Check Mask 1=enable machine check interrupts14 14 Wait State set this to 1 to stop the processor except for interrupts & givetime to other LPARS used in CPU idle in the kernel to increase overallusage of processor resources.15 15 Problem state ( if set to 1 certain instructions are disabled )all linux user programs run with this bit 1( useful info for debugging under VM ).16-17 16-17 Address Space Control00 Primary Space Mode when DAT onThe linux kernel currently runs in this mode, CR1 is affiliated withthis mode & points to the primary segment table origin etc.01 Access register mode this mode is used in functions tocopy data between kernel & user space.10 Secondary space mode not used in linux however CR7 theregister affiliated with this mode is & this & normallyCR13=CR7 to allow us to copy data between kernel & user space.We do this as follows:We set ar2 to 0 to designate itsaffiliated gpr ( gpr2 )to point to primary=kernel space.We set ar4 to 1 to designate itsaffiliated gpr ( gpr4 ) to point to secondary=home=user space& then essentially do a memcopy(gpr2,gpr4,size) tocopy data between the address spaces, the reason we use home space for thekernel & don't keep secondary space free is that code will not run insecondary space.11 Home Space Mode all user programs run in this mode.it is affiliated with CR13.18-19 18-19 Condition codes (CC)20 20 Fixed point overflow mask if 1=FPU exceptions for this eventoccur ( normally 0 )21 21 Decimal overflow mask if 1=FPU exceptions for this event occur( normally 0 )22 22 Exponent underflow mask if 1=FPU exceptions for this event occur( normally 0 )23 23 Significance Mask if 1=FPU exceptions for this event occur( normally 0 )24-31 24-30 Reserved Must be 0.31 Extended Addressing Mode32 Basic Addressing ModeUsed to set addressing modePSW 31 PSW 320 0 24 bit0 1 31 bit1 1 64 bit32 1=31 bit addressing mode 0=24 bit addressing mode (for backwardcompatibility), linux always runs with this bit set to 133-64 Instruction address.33-63 Reserved must be 064-127 AddressIn 24 bits mode bits 64-103=0 bits 104-127 AddressIn 31 bits mode bits 64-96=0 bits 97-127 AddressNote: unlike 31 bit mode on s/390 bit 96 must be zerowhen loading the address with LPSWE otherwise aspecification exception occurs, LPSW is fully backwardcompatible.Prefix Page(s)--------------This per cpu memory area is too intimately tied to the processor not to mention.It exists between the real addresses 0-4096 on s/390 & 0-8192 z/Architecture & is exchangedwith a 1 page on s/390 or 2 pages on z/Architecture in absolute storage by the setprefix instruction in linux'es startup.This page is mapped to a different prefix for each processor in an SMP configuration( assuming the os designer is sane of course :-) ).Bytes 0-512 ( 200 hex ) on s/390 & 0-512,4096-4544,4604-5119 currently on z/Architectureare used by the processor itself for holding such information as exception indications &entry points for exceptions.Bytes after 0xc00 hex are used by linux for per processor globals on s/390 & z/Architecture( there is a gap on z/Architecture too currently between 0xc00 & 1000 which linux uses ).The closest thing to this on traditional architectures is the interruptvector table. This is a good thing & does simplify some of the kernel codinghowever it means that we now cannot catch stray NULL pointers in thekernel without hard coded checks.Address Spaces on Intel Linux=============================The traditional Intel Linux is approximately mapped as follows forgivethe ascii art.0xFFFFFFFF 4GB Himem ****************** ** Kernel Space ** ****************** ****************User Space Himem (typically 0xC0000000 3GB )* User Stack * * ****************** * ** Shared Libs * * Next Process ****************** * to ** * <== * Run * <==* User Program * * ** Data BSS * * ** Text * * ** Sections * * *0x00000000 ***************** ****************Now it is easy to see that on Intel it is quite easy to recognise a kernel addressas being one greater than user space himem ( in this case 0xC0000000).& addresses of less than this are the ones in the current running program on thisprocessor ( if an smp box ).If using the virtual machine ( VM ) as a debugger it is quite difficult toknow which user process is running as the address space you are looking atcould be from any process in the run queue.The limitation of Intels addressing technique is that the linuxkernel uses a very simple real address to virtual addressing techniqueof Real Address=Virtual Address-User Space Himem.This means that on Intel the kernel linux can typically only addressHimem=0xFFFFFFFF-0xC0000000=1GB & this is all the RAM these machinescan typically use.They can lower User Himem to 2GB or lower & thus beable to use 2GB of RAM however this shrinks the maximum sizeof User Space from 3GB to 2GB they have a no win limit of 4GB unlessthey go to 64 Bit.On 390 our limitations & strengths make us slightly different.For backward compatibility we are only allowed use 31 bits (2GB)of our 32 bit addresses, however, we use entirely separate addressspaces for the user & kernel.This means we can support 2GB of non Extended RAM on s/390, & morewith the Extended memory management swap device ¤tly 4TB of physical memory currently on z/Architecture.Address Spaces on Linux for s/390 & z/Architecture==================================================Our addressing scheme is as followsHimem 0x7fffffff 2GB on s/390 ***************** ****************currently 0x3ffffffffff (2^42)-1 * User Stack * * *on z/Architecture. ***************** * ** Shared Libs * * ****************** * ** * * Kernel ** User Program * * ** Data BSS * * ** Text * * ** Sections * * *0x00000000 ***************** ****************This also means that we need to look at the PSW problem state bitor the addressing mode to decide whether we are looking atuser or kernel space.Virtual Addresses on s/390 & z/Architecture===========================================A virtual address on s/390 is made up of 3 partsThe SX ( segment index, roughly corresponding to the PGD & PMD in linux terminology )being bits 1-11.The PX ( page index, corresponding to the page table entry (pte) in linux terminology )being bits 12-19.The remaining bits BX (the byte index are the offset in the page )i.e. bits 20 to 31.On z/Architecture in linux we currently make up an address from 4 parts.The region index bits (RX) 0-32 we currently use bits 22-32The segment index (SX) being bits 33-43The page index (PX) being bits 44-51The byte index (BX) being bits 52-63Notes:1) s/390 has no PMD so the PMD is really the PGD also.A lot of this stuff is defined in pgtable.h.2) Also seeing as s/390's page indexes are only 1k in size(bits 12-19 x 4 bytes per pte ) we use 1 ( page 4k )to make the best use of memory by updating 4 segment indicesentries each time we mess with a PMD & use offsets0,1024,2048 & 3072 in this page as for our segment indexes.On z/Architecture our page indexes are now 2k in size( bits 12-19 x 8 bytes per pte ) we do a similar trickbut only mess with 2 segment indices each time we mess witha PMD.3) As z/Architecture supports up to a massive 5-level page table lookup wecan only use 3 currently on Linux ( as this is all the generic kernelcurrently supports ) however this may change in futurethis allows us to access ( according to my sums )4TB of virtual storage per process i.e.4096*512(PTES)*1024(PMDS)*2048(PGD) = 4398046511104 bytes,enough for another 2 or 3 of years I think :-).to do this we use a region-third-table designation type inour address space control registers.The Linux for s/390 & z/Architecture Kernel Task Structure==========================================================Each process/thread under Linux for S390 has its own kernel task_structdefined in linux/include/linux/sched.hThe S390 on initialisation & resuming of a process on a cpu setsthe __LC_KERNEL_STACK variable in the spare prefix area for this cpu(which we use for per-processor globals).The kernel stack pointer is intimately tied with the task structure foreach processor as follows.s/390************************* 1 page kernel stack ** ( 4K ) ************************** 1 page task_struct ** ( 4K ) *8K aligned ************************z/Architecture************************* 2 page kernel stack ** ( 8K ) ************************** 2 page task_struct ** ( 8K ) *16K aligned ************************What this means is that we don't need to dedicate any register or global variableto point to the current running process & can retrieve it with the followingvery simple construct for s/390 & one very similar for z/Architecture.static inline struct task_struct * get_current(void){struct task_struct *current;__asm__("lhi %0,-8192\n\t""nr %0,15": "=r" (current) );return current;}i.e. just anding the current kernel stack pointer with the mask -8192.Thankfully because Linux doesn't have support for nested IO interrupts& our devices have large buffers can survive interrupts being shut forshort amounts of time we don't need a separate stack for interrupts.Register Usage & Stackframes on Linux for s/390 & z/Architecture=================================================================Overview:---------This is the code that gcc produces at the top & the bottom ofeach function. It usually is fairly consistent & similar fromfunction to function & if you know its layout you can probablymake some headway in finding the ultimate cause of a problemafter a crash without a source level debugger.Note: To follow stackframes requires a knowledge of C or Pascal &limited knowledge of one assembly language.It should be noted that there are some differences between thes/390 & z/Architecture stack layouts as the z/Architecture stack layout didn't haveto maintain compatibility with older linkage formats.Glossary:---------alloca:This is a built in compiler function for runtime allocationof extra space on the callers stack which is obviously freedup on function exit ( e.g. the caller may choose to allocate nothingof a buffer of 4k if required for temporary purposes ), it generatesvery efficient code ( a few cycles ) when compared to alternativeslike malloc.automatics: These are local variables on the stack,i.e they aren't in registers & they aren't static.back-chain:This is a pointer to the stack pointer before entering aframed functions ( see frameless function ) prologue got bydereferencing the address of the current stack pointer,i.e. got by accessing the 32 bit value at the stack pointerscurrent location.base-pointer:This is a pointer to the back of the literal pool whichis an area just behind each procedure used to store constantsin each function.call-clobbered: The caller probably needs to save these registers if thereis something of value in them, on the stack or elsewhere before making acall to another procedure so that it can restore it later.epilogue:The code generated by the compiler to return to the caller.frameless-functionA frameless function in Linux for s390 & z/Architecture is one which doesn'tneed more than the register save area ( 96 bytes on s/390, 160 on z/Architecture )given to it by the caller.A frameless function never:1) Sets up a back chain.2) Calls alloca.3) Calls other normal functions4) Has automatics.GOT-pointer:This is a pointer to the global-offset-table in ELF( Executable Linkable Format, Linux'es most common executable format ),all globals & shared library objects are found using this pointer.lazy-bindingELF shared libraries are typically only loaded when routines in the sharedlibrary are actually first called at runtime. This is lazy binding.procedure-linkage-tableThis is a table found from the GOT which contains pointers to routinesin other shared libraries which can't be called to by easier means.prologue:The code generated by the compiler to set up the stack frame.outgoing-args:This is extra area allocated on the stack of the calling function if theparameters for the callee's cannot all be put in registers, the samearea can be reused by each function the caller calls.routine-descriptor:A COFF executable format based concept of a procedure referenceactually being 8 bytes or more as opposed to a simple pointer to the routine.This is typically defined as followsRoutine Descriptor offset 0=Pointer to FunctionRoutine Descriptor offset 4=Pointer to Table of ContentsThe table of contents/TOC is roughly equivalent to a GOT pointer.& it means that shared libraries etc. can be shared between severalenvironments each with their own TOC.static-chain: This is used in nested functions a concept adopted from pascalby gcc not used in ansi C or C++ ( although quite useful ), basically itis a pointer used to reference local variables of enclosing functions.You might come across this stuff once or twice in your lifetime.e.g.The function below should return 11 though gcc may get upset & toss warningsabout unused variables.int FunctionA(int a){int b;FunctionC(int c){b=c+1;}FunctionC(10);return(b);}s/390 & z/Architecture Register usage=====================================r0 used by syscalls/assembly call-clobberedr1 used by syscalls/assembly call-clobberedr2 argument 0 / return value 0 call-clobberedr3 argument 1 / return value 1 (if long long) call-clobberedr4 argument 2 call-clobberedr5 argument 3 call-clobberedr6 argument 4 savedr7 pointer-to arguments 5 to ... savedr8 this & that savedr9 this & that savedr10 static-chain ( if nested function ) savedr11 frame-pointer ( if function used alloca ) savedr12 got-pointer savedr13 base-pointer savedr14 return-address savedr15 stack-pointer savedf0 argument 0 / return value ( float/double ) call-clobberedf2 argument 1 call-clobberedf4 z/Architecture argument 2 savedf6 z/Architecture argument 3 savedThe remaining floating pointsf1,f3,f5 f7-f15 are call-clobbered.Notes:------1) The only requirement is that registers which are usedby the callee are saved, e.g. the compiler is perfectlycapable of using r11 for purposes other than a frame aframe pointer if a frame pointer is not needed.2) In functions with variable arguments e.g. printf the calling procedureis identical to one without variable arguments & the same number ofparameters. However, the prologue of this function is somewhat morehairy owing to it having to move these parameters to the stack toget va_start, va_arg & va_end to work.3) Access registers are currently unused by gcc but are used inthe kernel. Possibilities exist to use them at the moment fortemporary storage but it isn't recommended.4) Only 4 of the floating point registers are used forparameter passing as older machines such as G3 only have only 4& it keeps the stack frame compatible with other compilers.However with IEEE floating point emulation under linux on theolder machines you are free to use the other 12.5) A long long or double parameter cannot be have thefirst 4 bytes in a register & the second four bytes in theoutgoing args area. It must be purely in the outgoing argsarea if crossing this boundary.6) Floating point parameters are mixed with outgoing argson the outgoing args area in the order the are passed in as parameters.7) Floating point arguments 2 & 3 are saved in the outgoing args area forz/ArchitectureStack Frame Layout------------------s/390 z/Architecture0 0 back chain ( a 0 here signifies end of back chain )4 8 eos ( end of stack, not used on Linux for S390 used in other linkage formats )8 16 glue used in other s/390 linkage formats for saved routine descriptors etc.12 24 glue used in other s/390 linkage formats for saved routine descriptors etc.16 32 scratch area20 40 scratch area24 48 saved r6 of caller function28 56 saved r7 of caller function32 64 saved r8 of caller function36 72 saved r9 of caller function40 80 saved r10 of caller function44 88 saved r11 of caller function48 96 saved r12 of caller function52 104 saved r13 of caller function56 112 saved r14 of caller function60 120 saved r15 of caller function64 128 saved f4 of caller function72 132 saved f6 of caller function80 undefined96 160 outgoing args passed from caller to callee96+x 160+x possible stack alignment ( 8 bytes desirable )96+x+y 160+x+y alloca space of caller ( if used )96+x+y+z 160+x+y+z automatics of caller ( if used )0 back-chainA sample program with comments.===============================Comments on the function test-----------------------------1) It didn't need to set up a pointer to the constant pool gpr13 as it isn't used( :-( ).2) This is a frameless function & no stack is bought.3) The compiler was clever enough to recognise that it could return thevalue in r2 as well as use it for the passed in parameter ( :-) ).4) The basr ( branch relative & save ) trick works as follows the instructionhas a special case with r0,r0 with some instruction operands is understood asthe literal value 0, some risc architectures also do this ). So nowwe are branching to the next address & the address new program counter isin r13,so now we subtract the size of the function prologue we have executed+ the size of the literal pool to get to the top of the literal pool0040037c int test(int b){ # Function prologue below40037c: 90 de f0 34 stm %r13,%r14,52(%r15) # Save registers r13 & r14400380: 0d d0 basr %r13,%r0 # Set up pointer to constant pool using400382: a7 da ff fa ahi %r13,-6 # basr trickreturn(5+b);# Huge main program400386: a7 2a 00 05 ahi %r2,5 # add 5 to r2# Function epilogue below40038a: 98 de f0 34 lm %r13,%r14,52(%r15) # restore registers r13 & 1440038e: 07 fe br %r14 # return}Comments on the function main-----------------------------1) The compiler did this function optimally ( 8-) )Literal pool for main.400390: ff ff ff ec .long 0xffffffecmain(int argc,char *argv[]){ # Function prologue below400394: 90 bf f0 2c stm %r11,%r15,44(%r15) # Save necessary registers400398: 18 0f lr %r0,%r15 # copy stack pointer to r040039a: a7 fa ff a0 ahi %r15,-96 # Make area for callee saving40039e: 0d d0 basr %r13,%r0 # Set up r13 to point to4003a0: a7 da ff f0 ahi %r13,-16 # literal pool4003a4: 50 00 f0 00 st %r0,0(%r15) # Save backchainreturn(test(5)); # Main Program Below4003a8: 58 e0 d0 00 l %r14,0(%r13) # load relative address of test from# literal pool4003ac: a7 28 00 05 lhi %r2,5 # Set first parameter to 54003b0: 4d ee d0 00 bas %r14,0(%r14,%r13) # jump to test setting r14 as return# address using branch & save instruction.# Function Epilogue below4003b4: 98 bf f0 8c lm %r11,%r15,140(%r15)# Restore necessary registers.4003b8: 07 fe br %r14 # return to do program exit}Compiler updates----------------main(int argc,char *argv[]){4004fc: 90 7f f0 1c stm %r7,%r15,28(%r15)400500: a7 d5 00 04 bras %r13,400508 <main+0xc>400504: 00 40 04 f4 .long 0x004004f4# compiler now puts constant pool in code to so it saves an instruction400508: 18 0f lr %r0,%r1540050a: a7 fa ff a0 ahi %r15,-9640050e: 50 00 f0 00 st %r0,0(%r15)return(test(5));400512: 58 10 d0 00 l %r1,0(%r13)400516: a7 28 00 05 lhi %r2,540051a: 0d e1 basr %r14,%r1# compiler adds 1 extra instruction to epilogue this is done to# avoid processor pipeline stalls owing to data dependencies on g5 &# above as register 14 in the old code was needed directly after being loaded# by the lm %r11,%r15,140(%r15) for the br %14.40051c: 58 40 f0 98 l %r4,152(%r15)400520: 98 7f f0 7c lm %r7,%r15,124(%r15)400524: 07 f4 br %r4}Hartmut ( our compiler developer ) also has been threatening to take out thestack backchain in optimised code as this also causes pipeline stalls, youhave been warned.64 bit z/Architecture code disassembly--------------------------------------If you understand the stuff above you'll understand the stuffbelow too so I'll avoid repeating myself & just say thatsome of the instructions have g's on the end of them to indicatethey are 64 bit & the stack offsets are a bigger,the only other difference you'll find between 32 & 64 bit is thatwe now use f4 & f6 for floating point arguments on 64 bit.00000000800005b0 <test>:int test(int b){return(5+b);800005b0: a7 2a 00 05 ahi %r2,5800005b4: b9 14 00 22 lgfr %r2,%r2 # downcast to integer800005b8: 07 fe br %r14800005ba: 07 07 bcr 0,%r7}00000000800005bc <main>:main(int argc,char *argv[]){800005bc: eb bf f0 58 00 24 stmg %r11,%r15,88(%r15)800005c2: b9 04 00 1f lgr %r1,%r15800005c6: a7 fb ff 60 aghi %r15,-160800005ca: e3 10 f0 00 00 24 stg %r1,0(%r15)return(test(5));800005d0: a7 29 00 05 lghi %r2,5# brasl allows jumps > 64k & is overkill here bras would do fune800005d4: c0 e5 ff ff ff ee brasl %r14,800005b0 <test>800005da: e3 40 f1 10 00 04 lg %r4,272(%r15)800005e0: eb bf f0 f8 00 04 lmg %r11,%r15,248(%r15)800005e6: 07 f4 br %r4}Compiling programs for debugging on Linux for s/390 & z/Architecture====================================================================-gdwarf-2 now works it should be considered the default debuggingformat for s/390 & z/Architecture as it is more reliable for debuggingshared libraries, normal -g debugging works much better nowThanks to the IBM java compiler developers bug reports.This is typically done adding/appending the flags -g or -gdwarf-2 to theCFLAGS & LDFLAGS variables Makefile of the program concerned.If using gdb & you would like accurate displays of registers &stack traces compile without optimisation i.e make surethat there is no -O2 or similar on the CFLAGS line of the Makefile &the emitted gcc commands, obviously this will produce worse code( not advisable for shipment ) but it is an aid to the debugging process.This aids debugging because the compiler will copy parameters passed inin registers onto the stack so backtracing & looking at passed inparameters will work, however some larger programs which use inline functionswill not compile without optimisation.Debugging with optimisation has since much improved after fixingsome bugs, please make sure you are using gdb-5.0 or later developedafter Nov'2000.Figuring out gcc compile errors===============================If you are getting a lot of syntax errors compiling a program & the problemisn't blatantly obvious from the source.It often helps to just preprocess the file, this is done with the -Eoption in gcc.What this does is that it runs through the very first phase of compilation( compilation in gcc is done in several stages & gcc calls many programs toachieve its end result ) with the -E option gcc just calls the gcc preprocessor (cpp).The c preprocessor does the following, it joins all the files #included togetherrecursively ( #include files can #include other files ) & also the c file you wish to compile.It puts a fully qualified path of the #included files in a comment & itdoes macro expansion.This is useful for debugging because1) You can double check whether the files you expect to be included are the onesthat are being included ( e.g. double check that you aren't going to the i386 asm directory ).2) Check that macro definitions aren't clashing with typedefs,3) Check that definitions aren't being used before they are being included.4) Helps put the line emitting the error under the microscope if it contains macros.For convenience the Linux kernel's makefile will do preprocessing automatically for youby suffixing the file you want built with .i ( instead of .o )e.g.from the linux directory typemake arch/s390/kernel/signal.ithis will builds390-gcc -D__KERNEL__ -I/home1/barrow/linux/include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer-fno-strict-aliasing -D__SMP__ -pipe -fno-strength-reduce -E arch/s390/kernel/signal.c> arch/s390/kernel/signal.iNow look at signal.i you should see something like.# 1 "/home1/barrow/linux/include/asm/types.h" 1typedef unsigned short umode_t;typedef __signed__ char __s8;typedef unsigned char __u8;typedef __signed__ short __s16;typedef unsigned short __u16;If instead you are getting errors further down e.g.unknown instruction:2515 "move.l" or better still unknown instruction:2515"Fixme not implemented yet, call Martin" you are probably are attempting to compile some codemeant for another architecture or code that is simply not implemented, with a fixme statementstuck into the inline assembly code so that the author of the file now knows he has work to do.To look at the assembly emitted by gcc just before it is about to call gas ( the gnu assembler )use the -S option.Again for your convenience the Linux kernel's Makefile will hold your hand &do all this donkey work for you also by building the file with the .s suffix.e.g.from the Linux directory typemake arch/s390/kernel/signal.ss390-gcc -D__KERNEL__ -I/home1/barrow/linux/include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer-fno-strict-aliasing -D__SMP__ -pipe -fno-strength-reduce -S arch/s390/kernel/signal.c-o arch/s390/kernel/signal.sThis will output something like, ( please note the constant pool & the useful commentsin the prologue to give you a hand at interpreting it )..LC54:.string "misaligned (__u16 *) in __xchg\n".LC57:.string "misaligned (__u32 *) in __xchg\n".L$PG1: # Pool sys_sigsuspend.LC192:.long -262401.LC193:.long -1.LC194:.long schedule-.L$PG1.LC195:.long do_signal-.L$PG1.align 4.globl sys_sigsuspend.type sys_sigsuspend,@functionsys_sigsuspend:# leaf function 0# automatics 16# outgoing args 0# need frame pointer 0# call alloca 0# has varargs 0# incoming args (stack) 0# function length 168STM 8,15,32(15)LR 0,15AHI 15,-112BASR 13,0.L$CO1: AHI 13,.L$PG1-.L$CO1ST 0,0(15)LR 8,2N 5,.LC192-.L$PG1(13)Adding -g to the above output makes the output even more usefule.g. typingmake CC:="s390-gcc -g" kernel/sched.swhich compiles.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.salso outputs stabs ( debugger ) info, from this info you can find out theoffsets & sizes of various elements in structures.e.g. the stab for the structurestruct rlimit {unsigned long rlim_cur;unsigned long rlim_max;};is.stabs "rlimit:T(151,2)=s8rlim_cur:(0,5),0,32;rlim_max:(0,5),32,32;;",128,0,0,0from this stab you can see thatrlimit_cur starts at bit offset 0 & is 32 bits in sizerlimit_max starts at bit offset 32 & is 32 bits in size.Debugging Tools:================objdump=======This is a tool with many options the most useful being ( if compiled with -g).objdump --source <victim program or object file> > <victims debug listing >The whole kernel can be compiled like this ( Doing this will make a 17MB kernel& a 200 MB listing ) however you have to strip it before building the imageusing the strip command to make it a more reasonable size to boot it.A source/assembly mixed dump of the kernel can be done with the lineobjdump --source vmlinux > vmlinux.lstAlso, if the file isn't compiled -g, this will output as much debugging informationas it can (e.g. function names). This is very slow as it spends lotsof time searching for debugging info. The following self explanatory line should be usedinstead if the code isn't compiled -g, as it is much faster:objdump --disassemble-all --syms vmlinux > vmlinux.lstAs hard drive space is valuable most of us use the following approach.1) Look at the emitted psw on the console to find the crash address in the kernel.2) Look at the file System.map ( in the linux directory ) produced when buildingthe kernel to find the closest address less than the current PSW to find theoffending function.3) use grep or similar to search the source tree looking for the source filewith this function if you don't know where it is.4) rebuild this object file with -g on, as an example suppose the file was( /arch/s390/kernel/signal.o )5) Assuming the file with the erroneous function is signal.c Move to the base of theLinux source tree.6) rm /arch/s390/kernel/signal.o7) make /arch/s390/kernel/signal.o8) watch the gcc command line emitted9) type it in again or alternatively cut & paste it on the console adding the -g option.10) objdump --source arch/s390/kernel/signal.o > signal.lstThis will output the source & the assembly intermixed, as the snippet below showsThis will unfortunately output addresses which aren't the sameas the kernel ones you should be able to get around the mental arithmeticby playing with the --adjust-vma parameter to objdump.static inline void spin_lock(spinlock_t *lp){a0: 18 34 lr %r3,%r4a2: a7 3a 03 bc ahi %r3,956__asm__ __volatile(" lhi 1,-1\n"a6: a7 18 ff ff lhi %r1,-1aa: 1f 00 slr %r0,%r0ac: ba 01 30 00 cs %r0,%r1,0(%r3)b0: a7 44 ff fd jm aa <sys_sigsuspend+0x2e>saveset = current->blocked;b4: d2 07 f0 68 mvc 104(8,%r15),972(%r4)b8: 43 ccreturn (set->sig[0] & mask) != 0;}6) If debugging under VM go down to that section in the document for more info.I now have a tool which takes the pain out of --adjust-vma& you are able to do something likemake /arch/s390/kernel/traps.lst& it automatically generates the correctly relocated entries forthe text segment in traps.lst.This tool is now standard in linux distro's in scripts/makelststrace:-------Q. What is it ?A. It is a tool for intercepting calls to the kernel & logging themto a file & on the screen.Q. What use is it ?A. You can use it to find out what files a particular program opens.Example 1---------If you wanted to know does ping work but didn't have the sourcestrace ping -c 1 127.0.0.1& then look at the man pages for each of the syscalls below,( In fact this is sometimes easier than looking at some spaghettisource which conditionally compiles for several architectures ).Not everything that it throws out needs to make sense immediately.Just looking quickly you can see that it is making up a RAW socketfor the ICMP protocol.Doing an alarm(10) for a 10 second timeout& doing a gettimeofday call before & after each read to seehow long the replies took, & writing some text to stdout so the userhas an idea what is going on.socket(PF_INET, SOCK_RAW, IPPROTO_ICMP) = 3getuid() = 0setuid(0) = 0stat("/usr/share/locale/C/libc.cat", 0xbffff134) = -1 ENOENT (No such file or directory)stat("/usr/share/locale/libc/C", 0xbffff134) = -1 ENOENT (No such file or directory)stat("/usr/local/share/locale/C/libc.cat", 0xbffff134) = -1 ENOENT (No such file or directory)getpid() = 353setsockopt(3, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0setsockopt(3, SOL_SOCKET, SO_RCVBUF, [49152], 4) = 0fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(3, 1), ...}) = 0mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40008000ioctl(1, TCGETS, {B9600 opost isig icanon echo ...}) = 0write(1, "PING 127.0.0.1 (127.0.0.1): 56 d"..., 42PING 127.0.0.1 (127.0.0.1): 56 data bytes) = 42sigaction(SIGINT, {0x8049ba0, [], SA_RESTART}, {SIG_DFL}) = 0sigaction(SIGALRM, {0x8049600, [], SA_RESTART}, {SIG_DFL}) = 0gettimeofday({948904719, 138951}, NULL) = 0sendto(3, "\10\0D\201a\1\0\0\17#\2178\307\36"..., 64, 0, {sin_family=AF_INET,sin_port=htons(0), sin_addr=inet_addr("127.0.0.1")}, 16) = 64sigaction(SIGALRM, {0x8049600, [], SA_RESTART}, {0x8049600, [], SA_RESTART}) = 0sigaction(SIGALRM, {0x8049ba0, [], SA_RESTART}, {0x8049600, [], SA_RESTART}) = 0alarm(10) = 0recvfrom(3, "E\0\0T\0005\0\0@\1|r\177\0\0\1\177"..., 192, 0,{sin_family=AF_INET, sin_port=htons(50882), sin_addr=inet_addr("127.0.0.1")}, [16]) = 84gettimeofday({948904719, 160224}, NULL) = 0recvfrom(3, "E\0\0T\0006\0\0\377\1\275p\177\0"..., 192, 0,{sin_family=AF_INET, sin_port=htons(50882), sin_addr=inet_addr("127.0.0.1")}, [16]) = 84gettimeofday({948904719, 166952}, NULL) = 0write(1, "64 bytes from 127.0.0.1: icmp_se"...,5764 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=28.0 msExample 2---------strace passwd 2>&1 | grep openproduces the following outputopen("/etc/ld.so.cache", O_RDONLY) = 3open("/opt/kde/lib/libc.so.5", O_RDONLY) = -1 ENOENT (No such file or directory)open("/lib/libc.so.5", O_RDONLY) = 3open("/dev", O_RDONLY) = 3open("/var/run/utmp", O_RDONLY) = 3open("/etc/passwd", O_RDONLY) = 3open("/etc/shadow", O_RDONLY) = 3open("/etc/login.defs", O_RDONLY) = 4open("/dev/tty", O_RDONLY) = 4The 2>&1 is done to redirect stderr to stdout & grep is then filtering this inputthrough the pipe for each line containing the string open.Example 3---------Getting sophisticatedtelnetd crashes & I don't know whySteps-----1) Replace the following line in /etc/inetd.conftelnet stream tcp nowait root /usr/sbin/in.telnetd -hwithtelnet stream tcp nowait root /blah2) Create the file /blah with the following contents to start tracing telnetd#!/bin/bash/usr/bin/strace -o/t1 -f /usr/sbin/in.telnetd -h3) chmod 700 /blah to make it executable only to root4)killall -HUP inetdor ps aux | grep inetdget inetd's process id& kill -HUP inetd to restart it.Important options------------------o is used to tell strace to output to a file in our case t1 in the root directory-f is to follow children i.e.e.g in our case above telnetd will start the login process & subsequently a shell like bash.You will be able to tell which is which from the process ID's listed on the left hand sideof the strace output.-p<pid> will tell strace to attach to a running process, yup this can be done providedit isn't being traced or debugged already & you have enough privileges,the reason 2 processes cannot trace or debug the same program is that stracebecomes the parent process of the one being debugged & processes ( unlike people )can have only one parent.However the file /t1 will get big quite quicklyto test it telnet 127.0.0.1now look at what files in.telnetd execve'd413 execve("/usr/sbin/in.telnetd", ["/usr/sbin/in.telnetd", "-h"], [/* 17 vars */]) = 0414 execve("/bin/login", ["/bin/login", "-h", "localhost", "-p"], [/* 2 vars */]) = 0Whey it worked!.Other hints:------------If the program is not very interactive ( i.e. not much keyboard input )& is crashing in one architecture but not in another you can doan strace of both programs under as identical a scenario as you canon both architectures outputting to a file then.do a diff of the two traces using the diff programi.e.diff output1 output2& maybe you'll be able to see where the call paths differed, thisis possibly near the cause of the crash.More info---------Look at man pages for strace & the various syscallse.g. man strace, man alarm, man socket.Performance Debugging=====================gcc is capable of compiling in profiling code just add the -p optionto the CFLAGS, this obviously affects program size & performance.This can be used by the gprof gnu profiling tool or thegcov the gnu code coverage tool ( code coverage is a means of testingcode quality by checking if all the code in an executable in exercised bya tester ).Using top to find out where processes are sleeping in the kernel----------------------------------------------------------------To do this copy the System.map from the root directory wherethe linux kernel was built to the /boot directory on yourlinux machine.Start topNow type fU<return>You should see a new field called WCHAN whichtells you where each process is sleeping here is a typical output.6:59pm up 41 min, 1 user, load average: 0.00, 0.00, 0.0028 processes: 27 sleeping, 1 running, 0 zombie, 0 stoppedCPU states: 0.0% user, 0.1% system, 0.0% nice, 99.8% idleMem: 254900K av, 45976K used, 208924K free, 0K shrd, 28636K buffSwap: 0K av, 0K used, 0K free 8620K cachedPID USER PRI NI SIZE RSS SHARE WCHAN STAT LIB %CPU %MEM TIME COMMAND750 root 12 0 848 848 700 do_select S 0 0.1 0.3 0:00 in.telnetd767 root 16 0 1140 1140 964 R 0 0.1 0.4 0:00 top1 root 8 0 212 212 180 do_select S 0 0.0 0.0 0:00 init2 root 9 0 0 0 0 down_inte SW 0 0.0 0.0 0:00 kmcheckThe time command----------------Another related command is the time command which gives you an indicationof where a process is spending the majority of its time.e.g.time ping -c 5 ncoutputsreal 0m4.054suser 0m0.010ssys 0m0.010sDebugging under VM==================Notes-----Addresses & values in the VM debugger are always hex never decimalAddress ranges are of the format <HexValue1>-<HexValue2> or <HexValue1>.<HexValue2>e.g. The address range 0x2000 to 0x3000 can be described as 2000-3000 or 2000.1000The VM Debugger is case insensitive.VM's strengths are usually other debuggers weaknesses you can get at any resourceno matter how sensitive e.g. memory management resources,change address translationin the PSW. For kernel hacking you will reap dividends if you get good at it.The VM Debugger displays operators but not operands, probably because someof it was written when memory was expensive & the programmer was probably proud thatit fitted into 2k of memory & the programmers & didn't want to shock hardcore VM'ers bychanging the interface :-), also the debugger displays useful information on the same line &the author of the code probably felt that it was a good idea not to go overthe 80 columns on the screen.As some of you are probably in a panic now this isn't as unintuitive as it may seemas the 390 instructions are easy to decode mentally & you can make a good guess at a lotof them as all the operands are nibble ( half byte aligned ) & if you have an objdump listingalso it is quite easy to follow, if you don't have an objdump listing keep a copy ofthe s/390 Reference Summary & look at between pages 2 & 7 or alternatively thes/390 principles of operation.e.g. even I can guess that0001AFF8' LR 180F CC 0is a ( load register ) lr r0,r15Also it is very easy to tell the length of a 390 instruction from the 2 most significantbits in the instruction ( not that this info is really useful except if you are trying tomake sense of a hexdump of code ).Here is a tableBits Instruction Length------------------------------------------00 2 Bytes01 4 Bytes10 4 Bytes11 6 BytesThe debugger also displays other useful info on the same line such as theaddresses being operated on destination addresses of branches & condition codes.e.g.00019736' AHI A7DAFF0E CC 1000198BA' BRC A7840004 -> 000198C2' CC 0000198CE' STM 900EF068 >> 0FA95E78 CC 2Useful VM debugger commands---------------------------I suppose I'd better mention this before I startto list the current active traces doQ TRthere can be a maximum of 255 of these per set( more about trace sets later ).To stop traces issue aTR END.To delete a particular breakpoint issueTR DEL <breakpoint number>The PA1 key drops to CP mode so you can issue debugger commands,Doing alt c (on my 3270 console at least ) clears the screen.hitting b <enter> comes back to the running operating systemfrom cp mode ( in our case linux ).It is typically useful to add shortcuts to your profile.exec fileif you have one ( this is roughly equivalent to autoexec.bat in DOS ).file here are a few from mine./* this gives me command history on issuing f12 */set pf12 retrieve/* this continues */set pf8 imm b/* goes to trace set a */set pf1 imm tr goto a/* goes to trace set b */set pf2 imm tr goto b/* goes to trace set c */set pf3 imm tr goto cInstruction Tracing-------------------Setting a simple breakpointTR I PSWA <address>To debug a particular function tryTR I R <function address range>TR I on its own will single step.TR I DATA <MNEMONIC> <OPTIONAL RANGE> will trace for particular mnemonicse.g.TR I DATA 4D R 0197BC.4000will trace for BAS'es ( opcode 4D ) in the range 0197BC.4000if you were inclined you could add traces for all branch instructions &suffix them with the run prefix so you would have a backtrace on screenwhen a program crashes.TR BR <INTO OR FROM> will trace branches into or out of an address.e.g.TR BR INTO 0 is often quite useful if a program is getting awkward & decidingto branch to 0 & crashing as this will stop at the address before in jumps to 0.TR I R <address range> RUN cmd d gsingle steps a range of addresses but stays running &displays the gprs on each step.Displaying & modifying Registers--------------------------------D G will display all the gprsAdding a extra G to all the commands is necessary to access the full 64 bitcontent in VM on z/Architecture obviously this isn't required for access registersas these are still 32 bit.e.g. DGG instead of DGD X will display all the control registersD AR will display all the access registersD AR4-7 will display access registers 4 to 7CPU ALL D G will display the GRPS of all CPUS in the configurationD PSW will display the current PSWst PSW 2000 will put the value 2000 into the PSW &cause crash your machine.D PREFIX displays the prefix offsetDisplaying Memory-----------------To display memory mapped using the current PSW's mapping tryD <range>To make VM display a message each time it hits a particular address & continue tryD I<range> will disassemble/display a range of instructions.ST addr 32 bit word will store a 32 bit aligned addressD T<range> will display the EBCDIC in an address ( if you are that way inclined )D R<range> will display real addresses ( without DAT ) but with prefixing.There are other complex options to display if you need to get at say home spacebut are in primary space the easiest thing to do is to temporarilymodify the PSW to the other addressing mode, display the stuff & thenrestore it.Hints-----If you want to issue a debugger command without halting your virtual machine with thePA1 key try prefixing the command with #CP e.g.#cp tr i pswa 2000also suffixing most debugger commands with RUN will cause them notto stop just display the mnemonic at the current instruction on the console.If you have several breakpoints you want to put into your program &you get fed up of cross referencing with System.mapyou can do the following trick for several symbols.grep do_signal System.mapwhich emits the following among other things0001f4e0 T do_signalnow you can doTR I PSWA 0001f4e0 cmd msg * do_signalThis sends a message to your own console each time do_signal is entered.( As an aside I wrote a perl script once which automatically generated a REXXscript with breakpoints on every kernel procedure, this isn't a good ideabecause there are thousands of these routines & VM can only set 255 breakpointsat a time so you nearly had to spend as long pruning the file down as you wouldentering the msg's by hand ),however, the trick might be useful for a single object file.On linux'es 3270 emulator x3270 there is a very useful option under the file mentSave Screens In File this is very good of keeping a copy of traces.From CMS help <command name> will give you online help on a particular command.e.g.HELP DISPLAYAlso CP has a file called profile.exec which automatically gets calledon startup of CMS ( like autoexec.bat ), keeping on a DOS analogy sessionCP has a feature similar to doskey, it may be useful for you touse profile.exec to define some keystrokes.e.g.SET PF9 IMM BThis does a single step in VM on pressing F8.SET PF10 ^This sets up the ^ key.which can be used for ^c (ctrl-c),^z (ctrl-z) which can't be typed directly into some 3270 consoles.SET PF11 ^-This types the starting keystrokes for a sysrq see SysRq below.SET PF12 RETRIEVEThis retrieves command history on pressing F12.Sometimes in VM the display is set up to scroll automatically thiscan be very annoying if there are messages you wish to look atto stop this doTERM MORE 255 255This will nearly stop automatic screen updates, however it willcause a denial of service if lots of messages go to the 3270 console,so it would be foolish to use this as the default on a production machine.Tracing particular processes----------------------------The kernel's text segment is intentionally at an address in memory that it willvery seldom collide with text segments of user programs ( thanks Martin ),this simplifies debugging the kernel.However it is quite common for user processes to have addresses which collidethis can make debugging a particular process under VM painful under normalcircumstances as the process may change when doing aTR I R <address range>.Thankfully after reading VM's online help I figured out how to debugI particular process.Your first problem is to find the STD ( segment table designation )of the program you wish to debug.There are several ways you can do this here are a few1) objdump --syms <program to be debugged> | grep mainTo get the address of main in the program.tr i pswa <address of main>Start the program, if VM drops to CP on what looks like the entrypoint of the main function this is most likely the process you wish to debug.Now do a D X13 or D XG13 on z/Architecture.On 31 bit the STD is bits 1-19 ( the STO segment table origin )& 25-31 ( the STL segment table length ) of CR13.now typeTR I R STD <CR13's value> 0.7fffffffe.g.TR I R STD 8F32E1FF 0.7fffffffAnother very useful variation isTR STORE INTO STD <CR13's value> <address range>for finding out when a particular variable changes.An alternative way of finding the STD of a currently running processis to do the following, ( this method is more complex butcould be quite convenient if you aren't updating the kernel much &so your kernel structures will stay constant for a reasonable period oftime ).grep task /proc/<pid>/statusfrom this you should see something liketask: 0f160000 ksp: 0f161de8 pt_regs: 0f161f68This now gives you a pointer to the task structure.Now make CC:="s390-gcc -g" kernel/sched.sTo get the task_struct stabinfo.( task_struct is defined in include/linux/sched.h ).Now we want to look attask->active_mm->pgdon my machine the active_mm in the task structure stab isactive_mm:(4,12),672,32its offset is 672/8=84=0x54the pgd member in the mm_struct stab ispgd:(4,6)=*(29,5),96,32so its offset is 96/8=12=0xcso we'llhexdump -s 0xf160054 /dev/mem | morei.e. task_struct+active_mm offsetto look at the active_mm memberf160054 0fee cc60 0019 e334 0000 0000 0000 0011hexdump -s 0x0feecc6c /dev/mem | morei.e. active_mm+pgd offsetfeecc6c 0f2c 0000 0000 0001 0000 0001 0000 0010we get something likenow doTR I R STD <pgd|0x7f> 0.7fffffffi.e. the 0x7f is added because the pgd onlygives the page table origin & we need to set the low bitsto the maximum possible segment table length.TR I R STD 0f2c007f 0.7fffffffon z/Architecture you'll probably need to doTR I R STD <pgd|0x7> 0.ffffffffffffffffto set the TableType to 0x1 & the Table length to 3.Tracing Program Exceptions--------------------------If you get a crash which says something likeillegal operation or specification exception followed by a register dumpYou can restart linux & trace these using the tr prog <range or value> trace option.The most common ones you will normally be tracing for is1=operation exception2=privileged operation exception4=protection exception5=addressing exception6=specification exception10=segment translation exception11=page translation exceptionThe full list of these is on page 22 of the current s/390 Reference Summary.e.g.tr prog 10 will trace segment translation exceptions.tr prog on its own will trace all program interruption codes.Trace Sets----------On starting VM you are initially in the INITIAL trace set.You can do a Q TR to verify this.If you have a complex tracing situation where you wish to wait for instancetill a driver is open before you start tracing IO, but know in yourheart that you are going to have to make several runs through the code till youhave a clue whats going on.What you can do isTR I PSWA <Driver open address>hit b to continue till breakpointreach the breakpointnow do yourTR GOTO BTR IO 7c08-7c09 inst int runor whatever the IO channels you wish to trace are & hit bTo got back to the initial trace set doTR GOTO INITIAL& the TR I PSWA <Driver open address> will be the only active breakpoint again.Tracing linux syscalls under VM-------------------------------Syscalls are implemented on Linux for S390 by the Supervisor call instruction (SVC) there 256possibilities of these as the instruction is made up of a 0xA opcode & the second byte beingthe syscall number. They are traced using the simple command.TR SVC <Optional value or range>the syscalls are defined in linux/include/asm-s390/unistd.he.g. to trace all file opens just doTR SVC 5 ( as this is the syscall number of open )SMP Specific commands---------------------To find out how many cpus you haveQ CPUS displays all the CPU's available to your virtual machineTo find the cpu that the current cpu VM debugger commands are being directed at doQ CPU to change the current cpu VM debugger commands are being directed at doCPU <desired cpu no>On a SMP guest issue a command to all CPUs try prefixing the command with cpu all.To issue a command to a particular cpu try cpu <cpu number> e.g.CPU 01 TR I R 2000.3000If you are running on a guest with several cpus & you have a IO related problem& cannot follow the flow of code but you know it isn't smp related.from the bash prompt issueshutdown -h now or halt.do a Q CPUS to find out how many cpus you havedetach each one of them from cp except cpu 0by issuing aDETACH CPU 01-(number of cpus in configuration)& boot linux again.TR SIGP will trace inter processor signal processor instructions.DEFINE CPU 01-(number in configuration)will get your guests cpus back.Help for displaying ascii textstrings-------------------------------------On the very latest VM Nucleus'es VM can now display ascii( thanks Neale for the hint ) by doingD TX<lowaddr>.<len>e.g.D TX0.100Alternatively=============Under older VM debuggers ( I love EBDIC too ) you can use this little program I wrote whichwill convert a command line of hex digits to ascii text which can be compiled under linux &you can copy the hex digits from your x3270 terminal to your xterm if you are debuggingfrom a linuxbox.This is quite useful when looking at a parameter passed in as a text stringunder VM ( unless you are good at decoding ASCII in your head ).e.g. consider tracing an open syscallTR SVC 5We have stopped at a breakpoint000151B0' SVC 0A05 -> 0001909A' CC 0D 20.8 to check the SVC old psw in the prefix area & see was it from userspace( for the layout of the prefix area consult P18 of the s/390 390 Reference Summaryif you have it available ).V00000020 070C2000 800151B2The problem state bit wasn't set & it's also too early in the boot sequencefor it to be a userspace SVC if it was we would have to temporarily switch thepsw to user space addressing so we could get at the first parameter of the open ingpr2.Next do aD G2GPR 2 = 00014CB4Now display what gpr2 is pointing toD 00014CB4.20V00014CB4 2F646576 2F636F6E 736F6C65 00001BF5V00014CC4 FC00014C B4001001 E0001000 B8070707Now copy the text till the first 00 hex ( which is the end of the stringto an xterm & do hex2ascii on it.hex2ascii 2F646576 2F636F6E 736F6C65 00outputsDecoded Hex:=/ d e v / c o n s o l e 0x00We were opening the console device,You can compile the code below yourself for practice :-),/** hex2ascii.c* a useful little tool for converting a hexadecimal command line to ascii** Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)* (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation.*/#include <stdio.h>int main(int argc,char *argv[]){int cnt1,cnt2,len,toggle=0;int startcnt=1;unsigned char c,hex;if(argc>1&&(strcmp(argv[1],"-a")==0))startcnt=2;printf("Decoded Hex:=");for(cnt1=startcnt;cnt1<argc;cnt1++){len=strlen(argv[cnt1]);for(cnt2=0;cnt2<len;cnt2++){c=argv[cnt1][cnt2];if(c>='0'&&c<='9')c=c-'0';if(c>='A'&&c<='F')c=c-'A'+10;if(c>='a'&&c<='f')c=c-'a'+10;switch(toggle){case 0:hex=c<<4;toggle=1;break;case 1:hex+=c;if(hex<32||hex>127){if(startcnt==1)printf("0x%02X ",(int)hex);elseprintf(".");}else{printf("%c",hex);if(startcnt==1)printf(" ");}toggle=0;break;}}}printf("\n");}Stack tracing under VM----------------------A basic backtrace-----------------Here are the tricks I use 9 out of 10 times it works pretty well,When your backchain reaches a dead end--------------------------------------This can happen when an exception happens in the kernel & the kernel is entered twiceif you reach the NULL pointer at the end of the back chain you should beable to sniff further back if you follow the following tricks.1) A kernel address should be easy to recognise since it is inprimary space & the problem state bit isn't set & alsoThe Hi bit of the address is set.2) Another backchain should also be easy to recognise since it is anaddress pointing to another address approximately 100 bytes or 0x70 hexbehind the current stackpointer.Here is some practice.boot the kernel & hit PA1 at some random timed g to display the gprs, this should display something likeGPR 0 = 00000001 00156018 0014359C 00000000GPR 4 = 00000001 001B8888 000003E0 00000000GPR 8 = 00100080 00100084 00000000 000FE000GPR 12 = 00010400 8001B2DC 8001B36A 000FFED8Note that GPR14 is a return address but as we are real men we are going totrace the stack.display 0x40 bytes after the stack pointer.V000FFED8 000FFF38 8001B838 80014C8E 000FFF38V000FFEE8 00000000 00000000 000003E0 00000000V000FFEF8 00100080 00100084 00000000 000FE000V000FFF08 00010400 8001B2DC 8001B36A 000FFED8Ah now look at whats in sp+56 (sp+0x38) this is 8001B36A our saved r14 ifyou look above at our stackframe & also agrees with GPR14.now backchaind 000FFF38.40we now are taking the contents of SP to get our first backchain.V000FFF38 000FFFA0 00000000 00014995 00147094V000FFF48 00147090 001470A0 000003E0 00000000V000FFF58 00100080 00100084 00000000 001BF1D0V000FFF68 00010400 800149BA 80014CA6 000FFF38This displays a 2nd return address of 80014CA6now do d 000FFFA0.40 for our 3rd backchainV000FFFA0 04B52002 0001107F 00000000 00000000V000FFFB0 00000000 00000000 FF000000 0001107FV000FFFC0 00000000 00000000 00000000 00000000V000FFFD0 00010400 80010802 8001085A 000FFFA0our 3rd return address is 8001085Aas the 04B52002 looks suspiciously like rubbish it is fair to assume that the kernel entry routinesfor the sake of optimisation don't set up a backchain.now look at System.map to see if the addresses make any sense.grep -i 0001b3 System.mapoutputs among other things0001b304 T cpu_idleso 8001B36Ais cpu_idle+0x66 ( quiet the cpu is asleep, don't wake it )grep -i 00014 System.mapproduces among other things00014a78 T start_kernelso 0014CA6 is start_kernel+some hex number I can't add in my head.grep -i 00108 System.mapthis produces00010800 T _stextso 8001085A is _stext+0x5aCongrats you've done your first backchain.s/390 & z/Architecture IO Overview==================================I am not going to give a course in 390 IO architecture as this would take me quite awhile & I'm no expert. Instead I'll give a 390 IO architecture summary for Dummies if you havethe s/390 principles of operation available read this instead. If nothing else you may find a fewuseful keywords in here & be able to use them on a web search engine like altavista to findmore useful information.Unlike other bus architectures modern 390 systems do their IO using mostlyfibre optics & devices such as tapes & disks can be shared between several mainframes,also S390 can support up to 65536 devices while a high end PC based system might be chokingwith around 64. Here is some of the common IO terminologySubchannel:This is the logical number most IO commands use to talk to an IO device there can be up to0x10000 (65536) of these in a configuration typically there is a few hundred. Under VMfor simplicity they are allocated contiguously, however on the native hardware they are notthey typically stay consistent between boots provided no new hardware is inserted or removed.Under Linux for 390 we use these as IRQ's & also when issuing an IO command (CLEAR SUBCHANNEL,HALT SUBCHANNEL,MODIFY SUBCHANNEL,RESUME SUBCHANNEL,START SUBCHANNEL,STORE SUBCHANNEL &TEST SUBCHANNEL ) we use this as the ID of the device we wish to talk to, the mostimportant of these instructions are START SUBCHANNEL ( to start IO ), TEST SUBCHANNEL ( to checkwhether the IO completed successfully ), & HALT SUBCHANNEL ( to kill IO ), a subchannelcan have up to 8 channel paths to a device this offers redundancy if one is not available.Device Number:This number remains static & Is closely tied to the hardware, there are 65536 of thesealso they are made up of a CHPID ( Channel Path ID, the most significant 8 bits )& another lsb 8 bits. These remain static even if more devices are inserted or removedfrom the hardware, there is a 1 to 1 mapping between Subchannels & Device Numbers provideddevices aren't inserted or removed.Channel Control Words:CCWS are linked lists of instructions initially pointed to by an operation request block (ORB),which is initially given to Start Subchannel (SSCH) command along with the subchannel numberfor the IO subsystem to process while the CPU continues executing normal code.These come in two flavours, Format 0 ( 24 bit for backward )compatibility & Format 1 ( 31 bit ). These are typically used to issue read & write( & many other instructions ) they consist of a length field & an absolute address field.For each IO typically get 1 or 2 interrupts one for channel end ( primary status ) when thechannel is idle & the second for device end ( secondary status ) sometimes you get bothconcurrently, you check how the IO went on by issuing a TEST SUBCHANNEL at each interrupt,from which you receive an Interruption response block (IRB). If you get channel & device endstatus in the IRB without channel checks etc. your IO probably went okay. If you didn't youprobably need a doctor to examine the IRB & extended status word etc.If an error occurs, more sophisticated control units have a facility known asconcurrent sense this means that if an error occurs Extended sense information willbe presented in the Extended status word in the IRB if not you have to issue asubsequent SENSE CCW command after the test subchannel.TPI( Test pending interrupt) can also be used for polled IO but in multitasking multiprocessorsystems it isn't recommended except for checking special cases ( i.e. non looping checks forpending IO etc. ).Store Subchannel & Modify Subchannel can be used to examine & modify operating characteristicsof a subchannel ( e.g. channel paths ).Other IO related Terms:Sysplex: S390's Clustering TechnologyQDIO: S390's new high speed IO architecture to support devices such as gigabit ethernet,this architecture is also designed to be forward compatible with up & coming 64 bit machines.General ConceptsInput Output Processors (IOP's) are responsible for communicating betweenthe mainframe CPU's & the channel & relieve the mainframe CPU's from theburden of communicating with IO devices directly, this allows the CPU's toconcentrate on data processing.IOP's can use one or more links ( known as channel paths ) to talk to eachIO device. It first checks for path availability & chooses an available one,then starts ( & sometimes terminates IO ).There are two types of channel path: ESCON & the Parallel IO interface.IO devices are attached to control units, control units provide thelogic to interface the channel paths & channel path IO protocols tothe IO devices, they can be integrated with the devices or housed separately& often talk to several similar devices ( typical examples would be raidcontrollers or a control unit which connects to 1000 3270 terminals ).+---------------------------------------------------------------+| +-----+ +-----+ +-----+ +-----+ +----------+ +----------+ || | CPU | | CPU | | CPU | | CPU | | Main | | Expanded | || | | | | | | | | | Memory | | Storage | || +-----+ +-----+ +-----+ +-----+ +----------+ +----------+ ||---------------------------------------------------------------+| IOP | IOP | IOP ||---------------------------------------------------------------| C | C | C | C | C | C | C | C | C | C | C | C | C | C | C | C |----------------------------------------------------------------|| |||| Bus & Tag Channel Path || ESCON|| ====================== || Channel|| || || || Path+----------+ +----------+ +----------+| | | | | || CU | | CU | | CU || | | | | |+----------+ +----------+ +----------+| | | | |+----------+ +----------+ +----------+ +----------+ +----------+|I/O Device| |I/O Device| |I/O Device| |I/O Device| |I/O Device|+----------+ +----------+ +----------+ +----------+ +----------+CPU = Central Processing UnitC = ChannelIOP = IP ProcessorCU = Control UnitThe 390 IO systems come in 2 flavours the current 390 machines support bothThe Older 360 & 370 Interface,sometimes called the Parallel I/O interface,sometimes called Bus-and Tag & sometimes Original Equipment ManufacturersInterface (OEMI).This byte wide Parallel channel path/bus has parity & data on the "Bus" cable& control lines on the "Tag" cable. These can operate in byte multiplex mode forsharing between several slow devices or burst mode & monopolize the channel for thewhole burst. Up to 256 devices can be addressed on one of these cables. These cables areabout one inch in diameter. The maximum unextended length supported by these cables is125 Meters but this can be extended up to 2km with a fibre optic channel extendedsuch as a 3044. The maximum burst speed supported is 4.5 megabytes per second howeversome really old processors support only transfer rates of 3.0, 2.0 & 1.0 MB/sec.One of these paths can be daisy chained to up to 8 control units.ESCON if fibre optic it is also called FICONWas introduced by IBM in 1990. Has 2 fibre optic cables & uses either leds or lasersfor communication at a signaling rate of up to 200 megabits/sec. As 10bits are transferredfor every 8 bits info this drops to 160 megabits/sec & to 18.6 Megabytes/sec oncecontrol info & CRC are added. ESCON only operates in burst mode.ESCONs typical max cable length is 3km for the led version & 20km for the laser versionknown as XDF ( extended distance facility ). This can be further extended by using anESCON director which triples the above mentioned ranges. Unlike Bus & Tag as ESCON isserial it uses a packet switching architecture the standard Bus & Tag control protocolis however present within the packets. Up to 256 devices can be attached to each controlunit that uses one of these interfaces.Common 390 Devices include:Network adapters typically OSA2,3172's,2116's & OSA-E gigabit ethernet adapters,Consoles 3270 & 3215 ( a teletype emulated under linux for a line mode console ).DASD's direct access storage devices ( otherwise known as hard disks ).Tape Drives.CTC ( Channel to Channel Adapters ),ESCON or Parallel Cables used as a very high speed serial linkbetween 2 machines. We use 2 cables under linux to do a bi-directional serial link.Debugging IO on s/390 & z/Architecture under VM===============================================Now we are ready to go on with IO tracing commands under VMA few self explanatory queries:Q OSAQ CTCQ DISK ( This command is CMS specific )Q DASDQ OSA on my machine returnsOSA 7C08 ON OSA 7C08 SUBCHANNEL = 0000OSA 7C09 ON OSA 7C09 SUBCHANNEL = 0001OSA 7C14 ON OSA 7C14 SUBCHANNEL = 0002OSA 7C15 ON OSA 7C15 SUBCHANNEL = 0003If you have a guest with certain privileges you may be able to see deviceswhich don't belong to you. To avoid this, add the option V.e.g.Q V OSANow using the device numbers returned by this command we willTrace the io starting up on the first device 7c08 & 7c09In our simplest case we can trace thestart subchannelslike TR SSCH 7C08-7C09or the halt subchannelsor TR HSCH 7C08-7C09MSCH's ,STSCH's I think you can guess the restIngo's favourite trick is tracing all the IO's & CCWS & spooling them into the reader of anotherVM guest so he can ftp the logfile back to his own machine.I'll do a small bit of this & give youa look at the output.1) Spool stdout to VM readerSP PRT TO (another vm guest ) or * for the local vm guest2) Fill the reader with the traceTR IO 7c08-7c09 INST INT CCW PRT RUN3) Start up linuxi 00c4) Finish the traceTR END5) close the readerC PRT6) list reader contentsRDRLIST7) copy it to linux4's minidiskRECEIVE / LOG TXT A1 ( replace8)filel & press F11 to look at itYou should see something like:00020942' SSCH B2334000 0048813C CC 0 SCH 0000 DEV 7C08CPA 000FFDF0 PARM 00E2C9C4 KEY 0 FPI C0 LPM 80CCW 000FFDF0 E4200100 00487FE8 0000 E4240100 ........IDAL 43D8AFE8IDAL 0FB7600000020B0A' I/O DEV 7C08 -> 000197BC' SCH 0000 PARM 00E2C9C400021628' TSCH B2354000 >> 00488164 CC 0 SCH 0000 DEV 7C08CCWA 000FFDF8 DEV STS 0C SCH STS 00 CNT 00ECKEY 0 FPI C0 CC 0 CTLS 400700022238' STSCH B2344000 >> 00488108 CC 0 SCH 0000 DEV 7C08If you don't like messing up your readed ( because you possibly booted from it )you can alternatively spool it to another readers guest.Other common VM device related commands---------------------------------------------These commands are listed only because they havebeen of use to me in the past & may be of use toyou too. For more complete info on each of the commandsuse type HELP <command> from CMS.detaching devicesDET <devno range>ATT <devno range> <guest>attach a device to guest * for your own guestREADY <devno> cause VM to issue a fake interrupt.The VARY command is normally only available to VM administrators.VARY ON PATH <path> TO <devno range>VARY OFF PATH <PATH> FROM <devno range>This is used to switch on or off channel paths to devices.Q CHPID <channel path ID>This displays state of devices using this channel pathD SCHIB <subchannel>This displays the subchannel information SCHIB block for the device.this I believe is also only available to administrators.DEFINE CTC <devno>defines a virtual CTC channel to channel connection2 need to be defined on each guest for the CTC driver to use.COUPLE devno userid remote devnoJoins a local virtual device to a remote virtual device( commonly used for the CTC driver ).Building a VM ramdisk under CMS which linux can usedef vfb-<blocksize> <subchannel> <number blocks>blocksize is commonly 4096 for linux.Formatting itformat <subchannel> <driver letter e.g. x> (blksize <blocksize>Sharing a disk between multiple guestsLINK userid devno1 devno2 mode passwordGDB on S390===========N.B. if compiling for debugging gdb works better without optimisation( see Compiling programs for debugging )invocation----------gdb <victim program> <optional corefile>Online help-----------help: gives help on commandse.g.helphelp displayNote gdb's online help is very good use it.Assembly--------info registers: displays registers other than floating point.info all-registers: displays floating points as well.disassemble: disassemblese.g.disassemble without parameters will disassemble the current functiondisassemble $pc $pc+10Viewing & modifying variables-----------------------------print or p: displays variable or registere.g. p/x $sp will display the stack pointerdisplay: prints variable or register each time program stopse.g.display/x $pc will display the program counterdisplay argcundisplay : undo's display'sinfo breakpoints: shows all current breakpointsinfo stack: shows stack back trace ( if this doesn't work too well, I'll show you thestacktrace by hand below ).info locals: displays local variables.info args: display current procedure arguments.set args: will set argc & argv each time the victim program is invoked.set <variable>=valueset argc=100set $pc=0Modifying execution-------------------step: steps n lines of sourcecodestep steps 1 line.step 100 steps 100 lines of code.next: like step except this will not step into subroutinesstepi: steps a single machine code instruction.e.g. stepi 100nexti: steps a single machine code instruction but will not step into subroutines.finish: will run until exit of the current routinerun: (re)starts a programcont: continues a programquit: exits gdb.breakpoints------------breaksets a breakpointe.g.break mainbreak *$pcbreak *0x400618heres a really useful one for large programsrbrSet a breakpoint for all functions matching REGEXPe.g.rbr 390will set a breakpoint with all functions with 390 in their name.info breakpointslists all breakpointsdelete: delete breakpoint by number or delete them alle.g.delete 1 will delete the first breakpointdelete will delete them allwatch: This will set a watchpoint ( usually hardware assisted ),This will watch a variable till it changese.g.watch cnt, will watch the variable cnt till it changes.As an aside unfortunately gdb's, architecture independent watchpoint codeis inconsistent & not very good, watchpoints usually work but not always.info watchpoints: Display currently active watchpointscondition: ( another useful one )Specify breakpoint number N to break only if COND is true.Usage is `condition N COND', where N is an integer and COND is anexpression to be evaluated whenever breakpoint N is reached.User defined functions/macros-----------------------------define: ( Note this is very very useful,simple & powerful )usage define <name> <list of commands> endexamples which you should consider putting into .gdbinit in your home directorydefine dstepidisassemble $pc $pc+10enddefine enextidisassemble $pc $pc+10endOther hard to classify stuff----------------------------signal n:sends the victim program a signal.e.g. signal 3 will send a SIGQUIT.info signals:what gdb does when the victim receives certain signals.list:e.g.list lists current function sourcelist 1,10 list first 10 lines of current file.list test.c:1,10directory:Adds directories to be searched for source if gdb cannot find the source.(note it is a bit sensitive about slashes)e.g. To add the root of the filesystem to the searchpath dodirectory //call <function>This calls a function in the victim program, this is pretty powerfule.g.(gdb) call printf("hello world")outputs:$1 = 11You might now be thinking that the line above didn't work, something extra had to be done.(gdb) call fflush(stdout)hello world$2 = 0As an aside the debugger also calls malloc & free under the hoodto make space for the "hello world" string.hints-----1) command completion works just like bash( if you are a bad typist like me this really helps )e.g. hit br <TAB> & cursor up & down :-).2) if you have a debugging problem that takes a few steps to recreateput the steps into a file called .gdbinit in your current working directoryif you have defined a few extra useful user defined commands put these inyour home directory & they will be read each time gdb is launched.A typical .gdbinit file might be.break mainrunbreak runtime_exceptioncontstack chaining in gdb by hand-----------------------------This is done using a the same trick described for VMp/x (*($sp+56))&0x7fffffff get the first backchain.For z/ArchitectureReplace 56 with 112 & ignore the &0x7fffffffin the macros below & do nasty casts to longs like the followingas gdb unfortunately deals with printed arguments as ints whichmesses up everything.i.e. here is a 3rd backchain dereferencep/x *(long *)(***(long ***)$sp+112)this outputs$5 = 0x528f18on my machine.Now you can useinfo symbol (*($sp+56))&0x7fffffffyou might see something like.rl_getc + 36 in section .text telling you what is located at address 0x528f18Now do.p/x (*(*$sp+56))&0x7fffffffThis outputs$6 = 0x528ed0Now do.info symbol (*(*$sp+56))&0x7fffffffrl_read_key + 180 in section .textnow dop/x (*(**$sp+56))&0x7fffffff& so on.Disassembling instructions without debug info---------------------------------------------gdb typically complains if there is a lack of debuggingsymbols in the disassemble command with"No function contains specified address." To get aroundthis dox/<number lines to disassemble>xi <address>e.g.x/20xi 0x400730Note: Remember gdb has history just like bash you don't need to retype thewhole line just use the up & down arrows.For more info-------------From your linuxbox doman gdb or info gdb.core dumps----------What a core dump ?,A core dump is a file generated by the kernel ( if allowed ) which contains the registers,& all active pages of the program which has crashed.From this file gdb will allow you to look at the registers & stack trace & memory of theprogram as if it just crashed on your system, it is usually called core & created in thecurrent working directory.This is very useful in that a customer can mail a core dump to a technical support department& the technical support department can reconstruct what happened.Provided they have an identical copy of this program with debugging symbols compiled in &the source base of this build is available.In short it is far more useful than something like a crash log could ever hope to be.In theory all that is missing to restart a core dumped program is a kernel patch whichwill do the following.1) Make a new kernel task structure2) Reload all the dumped pages back into the kernel's memory management structures.3) Do the required clock fixups4) Get all files & network connections for the process back into an identical state ( really difficult ).5) A few more difficult things I haven't thought of.Why have I never seen one ?.Probably because you haven't used the commandulimit -c unlimited in bashto allow core dumps, now doulimit -ato verify that the limit was accepted.A sample core dumpTo create this I'm going to doulimit -c unlimitedgdbto launch gdb (my victim app. ) now be bad & do the following from anothertelnet/xterm session to the same machineps -aux | grep gdbkill -SIGSEGV <gdb's pid>or alternatively use killall -SIGSEGV gdb if you have the killall command.Now look at the core dump../gdb coreDisplays the followingGNU gdb 4.18Copyright 1998 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type "show copying" to see the conditions.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was configured as "s390-ibm-linux"...Core was generated by `./gdb'.Program terminated with signal 11, Segmentation fault.Reading symbols from /usr/lib/libncurses.so.4...done.Reading symbols from /lib/libm.so.6...done.Reading symbols from /lib/libc.so.6...done.Reading symbols from /lib/ld-linux.so.2...done.#0 0x40126d1a in read () from /lib/libc.so.6Setting up the environment for debugging gdb.Breakpoint 1 at 0x4dc6f8: file utils.c, line 471.Breakpoint 2 at 0x4d87a4: file top.c, line 2609.(top-gdb) info stack#0 0x40126d1a in read () from /lib/libc.so.6#1 0x528f26 in rl_getc (stream=0x7ffffde8) at input.c:402#2 0x528ed0 in rl_read_key () at input.c:381#3 0x5167e6 in readline_internal_char () at readline.c:454#4 0x5168ee in readline_internal_charloop () at readline.c:507#5 0x51692c in readline_internal () at readline.c:521#6 0x5164fe in readline (prompt=0x7ffff810 "\177ÿøx\177ÿ÷Ø\177ÿøxÀ")at readline.c:349#7 0x4d7a8a in command_line_input (prrompt=0x564420 "(gdb) ", repeat=1,annotation_suffix=0x4d6b44 "prompt") at top.c:2091#8 0x4d6cf0 in command_loop () at top.c:1345#9 0x4e25bc in main (argc=1, argv=0x7ffffdf4) at main.c:635LDD===This is a program which lists the shared libraries which a library needs,Note you also get the relocations of the shared library text segments whichhelp when using objdump --source.e.g.ldd ./gdboutputslibncurses.so.4 => /usr/lib/libncurses.so.4 (0x40018000)libm.so.6 => /lib/libm.so.6 (0x4005e000)libc.so.6 => /lib/libc.so.6 (0x40084000)/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)Debugging shared libraries==========================Most programs use shared libraries, however it can be very painfulwhen you single step instruction into a function like printf for thefirst time & you end up in functions like _dl_runtime_resolve this isthe ld.so doing lazy binding, lazy binding is a concept in ELF whereshared library functions are not loaded into memory unless they areactually used, great for saving memory but a pain to debug.To get around this either relink the program -static or exit gdb typeexport LD_BIND_NOW=true this will stop lazy binding & restart the gdb'ingthe program in question.Debugging modules=================As modules are dynamically loaded into the kernel their address can beanywhere to get around this use the -m option with insmod to emit a loadmap which can be piped into a file if required.The proc file system====================What is it ?.It is a filesystem created by the kernel with files which are created on demandby the kernel if read, or can be used to modify kernel parameters,it is a powerful concept.e.g.cat /proc/sys/net/ipv4/ip_forwardOn my machine outputs0telling me ip_forwarding is not on to switch it on I can doecho 1 > /proc/sys/net/ipv4/ip_forwardcat it againcat /proc/sys/net/ipv4/ip_forwardOn my machine now outputs1IP forwarding is on.There is a lot of useful info in here best found by going in & having a look around,so I'll take you through some entries I consider important.All the processes running on the machine have there own entry defined by/proc/<pid>So lets have a look at the init processcd /proc/1cat cmdlineemitsinit [2]cd /proc/1/fdThis contains numerical entries of all the open files,some of these you can cat e.g. stdout (2)cat /proc/29/mapson my machine emits00400000-00478000 r-xp 00000000 5f:00 4103 /bin/bash00478000-0047e000 rw-p 00077000 5f:00 4103 /bin/bash0047e000-00492000 rwxp 00000000 00:00 040000000-40015000 r-xp 00000000 5f:00 14382 /lib/ld-2.1.2.so40015000-40016000 rw-p 00014000 5f:00 14382 /lib/ld-2.1.2.so40016000-40017000 rwxp 00000000 00:00 040017000-40018000 rw-p 00000000 00:00 040018000-4001b000 r-xp 00000000 5f:00 14435 /lib/libtermcap.so.2.0.84001b000-4001c000 rw-p 00002000 5f:00 14435 /lib/libtermcap.so.2.0.84001c000-4010d000 r-xp 00000000 5f:00 14387 /lib/libc-2.1.2.so4010d000-40111000 rw-p 000f0000 5f:00 14387 /lib/libc-2.1.2.so40111000-40114000 rw-p 00000000 00:00 040114000-4011e000 r-xp 00000000 5f:00 14408 /lib/libnss_files-2.1.2.so4011e000-4011f000 rw-p 00009000 5f:00 14408 /lib/libnss_files-2.1.2.so7fffd000-80000000 rwxp ffffe000 00:00 0Showing us the shared libraries init uses where they are in memory& memory access permissions for each virtual memory area./proc/1/cwd is a softlink to the current working directory./proc/1/root is the root of the filesystem for this process./proc/1/mem is the current running processes memory which youcan read & write to like a file.strace uses this sometimes as it is a bit faster than therather inefficient ptrace interface for peeking at DATA.cat statusName: initState: S (sleeping)Pid: 1PPid: 0Uid: 0 0 0 0Gid: 0 0 0 0Groups:VmSize: 408 kBVmLck: 0 kBVmRSS: 208 kBVmData: 24 kBVmStk: 8 kBVmExe: 368 kBVmLib: 0 kBSigPnd: 0000000000000000SigBlk: 0000000000000000SigIgn: 7fffffffd7f0d8fcSigCgt: 00000000280b2603CapInh: 00000000fffffeffCapPrm: 00000000ffffffffCapEff: 00000000fffffeffUser PSW: 070de000 80414146task: 004b6000 tss: 004b62d8 ksp: 004b7ca8 pt_regs: 004b7f68User GPRS:00000400 00000000 0000000b 7ffffa9000000000 00000000 00000000 0045d9f40045cafc 7ffffa90 7fffff18 0045cb0800010400 804039e8 80403af8 7ffff8b0User ACRS:00000000 00000000 00000000 0000000000000001 00000000 00000000 0000000000000000 00000000 00000000 0000000000000000 00000000 00000000 00000000Kernel BackChain CallChain BackChain CallChain004b7ca8 8002bd0c 004b7d18 8002b92c004b7db8 8005cd50 004b7e38 8005d12a004b7f08 80019114Showing among other things memory usage & status of some signals &the processes'es registers from the kernel task_structureas well as a backchain which may be useful if a process crashesin the kernel for some unknown reason.Some driver debugging techniques================================debug feature-------------Some of our drivers now support a "debug feature" in/proc/s390dbf see s390dbf.txt in the linux/Documentation directoryfor more info.e.g.to switch on the lcs "debug feature"echo 5 > /proc/s390dbf/lcs/level& then after the error occurred.cat /proc/s390dbf/lcs/sprintf >/logfilethe logfile now contains some information which may helptech support resolve a problem in the field.high level debugging network drivers------------------------------------ifconfig is a quite useful commandit gives the current state of network drivers.If you suspect your network device driver is deadone way to check is typeifconfig <network device>e.g. tr0You should see something liketr0 Link encap:16/4 Mbps Token Ring (New) HWaddr 00:04:AC:20:8E:48inet addr:9.164.185.132 Bcast:9.164.191.255 Mask:255.255.224.0UP BROADCAST RUNNING MULTICAST MTU:2000 Metric:1RX packets:246134 errors:0 dropped:0 overruns:0 frame:0TX packets:5 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:100if the device doesn't say uptry/etc/rc.d/init.d/network start( this starts the network stack & hopefully calls ifconfig tr0 up ).ifconfig looks at the output of /proc/net/dev & presents it in a more presentable formNow ping the device from a machine in the same subnet.if the RX packets count & TX packets counts don't increment you probablyhave problems.nextcat /proc/net/arpDo you see any hardware addresses in the cache if not you may have problems.Next tryping -c 5 <broadcast_addr> i.e. the Bcast field above in the output ofifconfig. Do you see any replies from machines other than the local machineif not you may have problems. also if the TX packets count in ifconfighasn't incremented either you have serious problems in your driver(e.g. the txbusy field of the network device being stuck on )or you may have multiple network devices connected.chandev-------There is a new device layer for channel devices, somedrivers e.g. lcs are registered with this layer.If the device uses the channel device layer you'll beable to find what interrupts it uses & the current stateof the device.See the manpage chandev.8 &type cat /proc/chandev for more info.Starting points for debugging scripting languages etc.======================================================bash/shbash -x <scriptname>e.g. bash -x /usr/bin/bashbugdisplays the following lines as it executes them.+ MACHINE=i586+ OS=linux-gnu+ CC=gcc+ CFLAGS= -DPROGRAM='bash' -DHOSTTYPE='i586' -DOSTYPE='linux-gnu' -DMACHTYPE='i586-pc-linux-gnu' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./lib -O2 -pipe+ RELEASE=2.01+ PATCHLEVEL=1+ RELSTATUS=release+ MACHTYPE=i586-pc-linux-gnuperl -d <scriptname> runs the perlscript in a fully interactive debugger<like gdb>.Type 'h' in the debugger for help.for debugging java typejdb <filename> another fully interactive gdb style debugger.& type ? in the debugger for help.Dumptool & Lcrash ( lkcd )==========================Michael Holzheu & others here at IBM have a fairly mature port ofSGI's lcrash tool which allows one to look at kernel structures in arunning kernel.It also complements a tool called dumptool which dumps all the kernel'smemory pages & registers to either a tape or a disk.This can be used by tech support or an ambitious end user dopost mortem debugging of a machine like gdb core dumps.Going into how to use this tool in detail will be explainedin other documentation supplied by IBM with the patches & thelcrash homepage http://oss.sgi.com/projects/lkcd/ & the lcrash manpage.How they work-------------Lcrash is a perfectly normal program,however, it requires 2additional files, Kerntypes which is built using a patch to thelinux kernel sources in the linux root directory & the System.map.Kerntypes is an objectfile whose sole purpose in lifeis to provide stabs debug info to lcrash, to do thisKerntypes is built from kerntypes.c which just includes the most commonlyreferenced header files used when debugging, lcrash can then read the.stabs section of this file.Debugging a live system it uses /dev/memalternatively for post mortem debugging it uses the datacollected by dumptool.SysRq=====This is now supported by linux for s/390 & z/Architecture.To enable it do compile the kernel withKernel Hacking -> Magic SysRq Key Enabledecho "1" > /proc/sys/kernel/sysrqalso typeecho "8" >/proc/sys/kernel/printkTo make printk output go to console.On 390 all commands are prefixed with^-e.g.^-t will show tasks.^-? or some unknown command will display help.The sysrq key reading is very picky ( I have to type the keys in anxterm session & paste them into the x3270 console )& it may be wise to predefine the keys as described in the VM hints aboveThis is particularly useful for syncing disks unmounting & rebootingif the machine gets partially hung.Read Documentation/sysrq.txt for more infoReferences:===========Enterprise Systems Architecture Reference SummaryEnterprise Systems Architecture Principles of OperationHartmut Penners s390 stack frame sheet.IBM Mainframe Channel Attachment a technology brief from a CISCO webpageVarious bits of man & info pages of Linux.Linux & GDB source.Various info & man pages.CMS Help on tracing commands.Linux for s/390 Elf Application Binary InterfaceLinux for z/Series Elf Application Binary Interface ( Both Highly Recommended )z/Architecture Principles of Operation SA22-7832-00Enterprise Systems Architecture/390 Reference Summary SA22-7209-01 & theEnterprise Systems Architecture/390 Principles of Operation SA22-7201-05Special Thanks==============Special thanks to Neale Ferguson who maintains a muchprettier HTML version of this page athttp://penguinvm.princeton.edu/notes.html#Debug390Bob Grainger Stefan Bader & others for reporting bugs
Go to most recent revision | Compare with Previous | Blame | View Log
