Embedded Debugging Concepts GDB

Hello friends, welcome back. So today I am going to discuss this embedded debugging concepts myself Sanjay and I have discussed, you know Linux Dynamic library and gdb in the last blog. I strongly recommend you to read earlier blogs for better understanding If you find something missing, please provide feedback in comment box so that I can take corrective action So gdb utilizes system call ptrace to fetch the required information This particular discussion is limited to native debugging using x86 64 architecture We will be debugging That embedded platform later, but the all the concepts are similar and Whether you are using gdb or any other debugger, so basic things Will be similar. So what are the basic things step by step debugging? so one way you know that we you have traditional way of debugging using print Ichabod and But it is not always practical to use print command You know when problem is somewhere deep inside that code so step-by-step debugging is required So step by step debugging, what can be the step by step debugging. Let me show you that source file here let me check Hello.C that source file there so source level suppose instruction statement by Statement at source level it will go. Let’s say past this particular point and it will Step to this one, then it will step to this one That is called step Ok so one by one, so in this case But in between it will go inside this also that entry called is step in So step, this particular step command is basically step in so it will execute these Then come here go inside this function But if we don’t want that then you we can use next command so instead of step we use next command, then it will say first execute this one then this Particular second second statement and simply jump to the next one Okay So not go inside the square root function Suppose if you go inside square root function by mistake order to analyze something there So then you can also step out using the finish Command from there And assembly level you have to use this step. I or you can use si and top you can simply use s and next I for Simple similar way like step over at assembly struction. So in assembly structure and what is a step over? means that if you want to step some call function that say You know ,some function call is there In the assembly then if you want to step over that you don’t want to go to inside that call function then and program counter you can print Isn’t this command? So I am going to show you this once we? pass understand that but all features are available and where command will provide you that Where exactly your code is right now and you can always use list or disassembly command to view that C code or the assembly So this is related to that step-by-step debugging in that . Second thing is once you are debugging definitely you need to analyze different variables so you can use info locals for local variables and all those kind of things You can change the variable here. For example, we want to check it at the boundary so you can make number, you know 90 or whatever you want to change that so to see the flow at the last and You can use simple print number in the gdb it will print the number and you can also use the print of normally see kind of format here within the gdb and for the registers, You can use info register info all registers This would be like some main registers and this info all complete register including floating-point everything For x86 platform and any other platform also similar approach is there and print and $reg_name? For example a stern and PC so PC you can put $PC and you can change that You know register value using this particular command suppose you have to you know skip some particular instruction so you can change PC or another any other instruction memory if you are to See some memory is accessible to you at the user level so you can use this particular Command stack frame So this is the interesting one so Let me first go up to this particular point Okay, we have to see the break point also after that I will go stack frame What is the stack frame ? once you go inside any function now? Let us say if you are going inside this particular? Square root function so you are passing It a parameter , what parameter you are passing ? you are passing the number parameter Okay, so this particular parameter and also the local variable inside that function? all those things and Where it has to return all those things it is stored in the stack and that is called the stack frame of the function Okay, so when we use the back trace command ,it gives you know that stack it gives basically that request with the current one will be the First current one it will show you like wherever it is right now. It will show you that word first okay, and then the older one so This I will show you and let me first go to this quickly gdb Hello and whatever the command I have to this as the executable Inside this, let me put a breakpoint at main and Run it Now or continue to that Oh Program is not run not being run. So let me run it it will stop there and let me simply step inside stab stab stab so it is going inside the square root function Has come out of that. Also, I believe Then it has stopped because of the hold let me do control C to quit out of this and set Hold equal to zero so that we can move further Step. Let it go inside printf command Let it go inside printf command and it is Inside this be print of C Hawaiian so it is right now inside we print of C that C batteries So what it is showing we print of C is the top and it was called by the second one and this was called by the third one if I use this particular Info frame so it will give me information about this current frame info frame So current stack frame it is showing Info stack level this one Zero, what is the state level zero? What is the current one? zero Okay suppose if you are you to check that at this point – so you can do directly if you write frame, it will always be the whatever that currents at zero you can put frame – That will give you second one. So second one You can see the print up related second frame and here if you use that info locals You know It will give the local Values and argument if there would be any argument then it would go give otherwise you could not give okay so that’s why it you can see that and And you can always you know change that if I use the info cream Whatever the current set I have it will give that one if I use again cream equal Zero, then it will set and select display that That one frame zero Black point Brad point is most important thing in this You know debugging because if if this is this kind of a small, you know This code is it hardly PI? List this so few lines are there it is inside pretty tough So let me continue it that it okay. It has come out of this Let me list min Okay, so a small code so you can play with step and a step over step out but it is thousands of lines Of course different files like, you know, you are going to debug Linux So this will be very time-consuming so in that case you need something that you but stop it a particular function and then analyze that particular function so There you need break point. So there are two kinds of break point in every architecture every tip over Wherever you go, whether the gdb or anywhere, you will find to kind of you know Black points one is hardware break one. Another is software back point so software break point basically depends on the debugger meditation why because the theory the software development I can share one example the debugger may agreement for example, like At the particular wherever you put a breakpoint that instruction It will replace by some other restriction which can throw exception for example trap Ok, it depends on the debugger and architecture implementation so whatever exception it is using for that particular purpose then as soon as You know new your program executes sent move to that particular point. It will throw the exception Ok, then within the exception handler Basically debugger will stop this execution and that You can verify the other thing debugger will provide you all the information Related to that and but one thing is here like because you are replacing destruction But does that paint it means that? Address where you are putting the black point Should be read. All right and black point you put always in the Instruction program memory. Ok, so if it is in a flash and executing from flash not in the track and You know on the memory where you can put a breakpoint so what will happen you will be not able to use the software breakpoint Other thing is because this software breakpoint you are simply replacing the instruction. So there is no limit on a limit your RAM memory size So if it is read-only memory The next solution to that is hardware breakpoint. What is the hardware point? Software breakpoint was you know debugger implementation hardware breakpoint is the device core architecture implementation it means it has It used to have some registers For this purpose some power Struction related some for date are related. So instruction specific register or Some architecture for example x86 use the debugger registers So once that you are address where you once you put a breakpoint Hydra back point it will not replace the exception, but it will put that in in that condition in their particular debug register So as soon as the instruction address matches that then activation how you know will be hired by the architecture itself It will halt there and then you can play with that So because this registered architecture dependent number of registers unlimited so that’s why you will we have having some limit on the hardware back point may be to four depending on the Architecture, so but you had the solution, okay You can always use simply Brad come on Brad function name and all that info Breck will give you the list and by default if You set a breakpoint. It may be Hardware white port okay sometime maybe it is It depends like what is the sighting? So this setting always differ setting you can always you know change so in this particular case like Auto hardware is off. So it means that It means that White board, it will not set the hardware breakpoint. It will set the software back point So for setting Hardware Brackman, you have to use the H black hardware Brad Okay, so that we will see Here, let’s say if if you are going to put a breakpoint at this condition Let’s say you are going to put a breakpoint that Mine is already there. Let me see the perp. Okay, I am going to put a 13 brat 13 Okay now if I in for breakfast, it gives like to break point and Address also you can match from here or if you want you can always this is showing the 13 You know here this is this particular thing Add No hardware breakpoint and let me check if I can put harbor of a point H Pratt 11 Oh Support to 2/3 no, it is not possible it has the support or it it has used that I will check that later and Let me run it first continue Why it is showing that oh That program is not being run That’s why it is showing this thing. Let me first run it Okay now list Where it is, and we can always print You know where we are Okay main this is this and Let me now try H pack and 11 That’s it because it was not executing that fight was showing that particular You know error so hardware back bunch of crap right point We know that both are there now if all right, then it will go to that particular thing. Okay If I here continue, then it will stop particular point So black point will hit it will stop there right now hardware breakpoint it Okay, so temporary break but what is the temporary we use a tea break tea break? Like if use that there It will be automatically deleted right now. You can see that although that break point is hit if eyes See that that point is already hit already hit one time, but it has not deleted So if you need some required only for one pro Let’s say you want to go to Marin only one so you can use temporary break point. Why? To waste sort or software break point or hardware hardware account, especially software break point Anyway, there is no limit but it is like always bothering to delete that later if you are going to analyze the complete program That is also possible. Okay, then this is wrong. This should be at break actually Okay, so that bracket condition. Hmm. So this is the typo here as breath It should be so bad if condition suppose right now. Let let me see that number, but the number value right? No number Number is only one. So, let’s see if I can stop it as when it is five if I right now if I continue and if I continue it has stopped because right now the my hold value is One, let me make it zero so that it can continue Okay now, let’s see what is the number now Still One Or Now – okay. So anyway, it is stopping at that or maybe info locals if I use that So – number equal to two so I would like to stop it only at five. So what I can do BRAC if Number Equal to five Fine unless there is a hardware I put braaap and it will not stop and If it is top then we will anyway Remove that first so Let’s see. What is that? Number right now print number – why because we have another breakpoint also, so if we are going to remove another breakpoint So we have to let me clear and see breakpoint IB Let me delete I can tell it also delete one delayed three and Last one if I use now I be This is now five. Okay What is the number value right now? print number Two okay, let’s continue What is the number value right now Five so it is talked about five So that is the conditional breakpoint and it is also possible to put a breakpoint at that any a particular address Let’s say if you are doing disassembling So right now it is at this particular point if you want to stop it Let’s say this particular point so what you need to do prayer and This Let’s say I am going to stop it just before calling this function Or maybe Yeah, this one. Okay, then You have to hash Paste address and continue Okay, there it is top there and that is the This assembly oh this Should be correct, okay Right on it going to inside called Q if I use instruction si Then on the I believe it will go So you can see inside this jump it is doing inside that particular soap It is like it is like right now we’ll go to you know address a solution which I discussed on there for Shared like reading so that is also possible. Okay so we have seen like Instruction we have multiple ways. We can put prac point conditional breakpoint We can put Hardware back point of input software breakpoint. We can put crack one at the Assembly level everything watch fine Watch point. What about the data data one case we have seen for example number what change so number was five Compared so it stopped struction Egyptian holiday, but if I would like to simply set it simply number X X Okay, so I will again try. Let’s see I will remove all other condition delete let’s say Let me try it depends on that stack frame So it is not because I not not there so I can always try food till it come out should always work. No problem Then oh I three is not that four is there so I can check Quite no breakpoint. Now what I want to put a watch point when? That particular variable access data so a watch a watch Number whenever it was access, you know Constant value Cannot watch Where it is? Just a moment Let me check where at it so Stepped it is somewhere here. Let me come out of this. I Should be able to see at least a local bearable Number is there. So now a watch number Fine because earlier it was not in this range. So that’s why it was inside the library. It was not all allowing okay, so when I returned back from that particular library function and within that extract frame by a tech frame then it is allowing and now I can always see using this and Let’s continue Okay, stop Because that particular number is just accessed You can see here. Just accept not in fact change Here it is. Getting changed. So both read write is happening Here it is, you know again one time read read read read again here exception. So if I am going to suppose if I want Simply First of all, I will delete this one also suppose if I want simply to put that Brad point when somebody change it not every time only you know accessing should not So what I can do Brack I Have to go watch one watch point. Then we have to watch This particular register that it is cutting change Okay, then continue It will stock there only because that is the only place it is getting changed not at another place So we have that math hat To see when somebody change it. We have a method to hard technician with somebody access it. We have a method to hard execution if Some specific condition is there so that is the brag point condition bit if prep point What is the catch point? Apart from hardware software brekford watchman catch point is for specific to some event Suppose some system call it there So I don’t know when system call will happen so you can put always this catch Sis call or maybe any module loading and all that, so if you run it continue It will stop as soon as that will happen. And again, I will check my All this list and delete one by one Because so that we can do another experiment Okay, mmm info we will list that catch all so anyway We have seen that already in Kobe and dil it will delete that. That’s not a good thing Attaching to running process. This is very very important concept in the debugging so far, whatever. We have seen that We started the execution of the application through the GTV okay, and then like Then We only put blackboard everything But many times what will happen that you get some crash In the running kernel catch some crash some error in that any code or you have to analyze a particular waveform and That is not happening at the runtime you you you would like to You know check the value of the runtime only you don’t want to control from the beginning itself So that’s why attaching means without disturbing the running program that concept that lets come out of this What will what we will do right now we will run it simply hello, so, this is the reason I put actually if you I Will share you that? that thing normally if I oh Sorry PS, let me check if some okay let me Okay, I can use this one. Also, I think three six seven one. So let me kill simply this one so that I can Use a fresh one PS Okay, then I would like to Run it like this three six five so if I Share the program here. I Put this particular condition and hold equal to one so why I put this condition because if you dot through this particular condition It will simply execute this come out of this you will be not able to attach Suppose some problem with this square root function. I I want to analyze that by the immediately after that Okay, so anything it can be anything like so that’s why I put this yeah I didn’t put wild one because most of the time many times what happens the compiler will optimize this because as per compiler it is while one so It should not Anyway a good beyond this so it will optimize so that would not solve my problem here. I would like to stop here So that’s why I put this particular point. So it has anyway stop there so that we can check using the command Sudo I have to use because I have like to attach that particular Process so for process level I need the sudo permission then gdb attached and three eight six five three eight six five And I need to put my breath point my password And Or Something Typo I have to put three to six by ugly. Ah Exactly so it is talk at the same point if you see that list Here and if you see the disassembly Now if you have to try something you can try that thing or if simply you want to Change the program counter there are two way to do that number one You simply change change that value of this particular. You know, what about the variable? You know this hold equal to 0 and suppose if you are using while when your compiled error is not optimizing that You have set some optimization level that it will not optimize so in that case while one also you can use so in that particular case you have to Change the program counter. Ah be you want to suppose skip certain Destruction. So in that case also proof changing the program counter may help. Let me see. What is the program counter right now? Program counter is this this one and This is the condition. This is that again jump not equal in this so I would like to see this particular Instructions, let me go to this particular, okay So or is there any call or something let’s go first this one? six five eight So let me know down this address And what I need to do added to simply Sat Program counter Equal to zero acts and the whatever the address I have taken Fine, and I can verify a and program counter School, then I can put a breath point at this location six six another location Just for another experiment So I will put a breakpoint and I told you to put a breakpoint I need to use This for assembly level Aha you can always check that then continue So what happened It is at six six seven or That address so I would like to go inside this call kill in the assembly so with which instruction I have to use Just a moment attached I think your encounter did not change or it has come again the while to while The program counter was change, but it came again to that That thing so that is why you know Why one so probably I have to check exactly whether it will come here or not So it is right now six five six six Five six where I need to come Six five six six five eight is this Assembly level Sometime Rbp Sure exactly what is stopping there? Because we need to it still at the hold Okay Black point is there I’ve dragged point. Is there that point there and what is the program counter right now? Let let me see program counter One hundred six five – six five – is over six five – Is this thing? So we have to put six this this third? Let me put the complete address should not matter whether I put that one over but let me put the complete and drag point star and this address Okay Then I have to assembly label step in Six five six it is it is Oh Brad point instead of rapid I have to Change that Program counter, sorry program counter to that value and That is simply this value So print program counter We will see whether This program counter has changed but whether it goes next or it comes back that we need to check So again, I will check the program counter Because there we have put a breakpoint also that is why it is creating problem now so delete, which break point Six five eight prep point delete – we have to remove this. Okay then again Now six five D it has moved further, so if you see that disassembly here It has Now it should go up to this if we have 6 6 C we have recommend at that if we have IB IB 6 6 C. Oh No, we don’t have recommend at that point. So we have to set a breakpoint at 6:16 That’s why probably last time it did not stop 6 6 C May depending on future nor define. Oh We have to huge star okay then Let’s try Continue to stop there. Okay now disassembly 6 6 C It has stopped say 1 it has taught and I would like to go inside this call Let lets a single stop single step single step Now it is going inside there or here if I see Back-trace So you will see like it is inside that particular okay, so that’s You know that way basically this debug attached the run time it works Ok and I provided frequently use gdb quartz because there are lot of Gdb, commands and when we will be working on particular target embedded targets So not sure how many commands will be, you know supported in that particular target but so that’s why I provided the frequently you into what and Thanks for your time. Please don’t forget to subscribe this youtube channel met Curie and if you are interested or looking for open-source tools and technology check out this particular euro website Find best open source com. Okay. Thanks

Tags: , , , , , , , , , , , ,

There are no comments yet

Why not be the first

Leave a Reply

Your email address will not be published. Required fields are marked *