Debugging Hi-Tech C - Page 1
Forum section: electronics / general

author: icon
author_rank: Regular Contributor
timestamp: April 29, 2012, 06:46:21 pm
content: HiFresh from my battles with this compiler (https://www.eevblog.com/forum/microcontrollers/pichitech-c-conversion-of-char-to-boolean-(bit)/) I'm seeking a bit of advice about the best way of debugging code.In assembler, I'm accustomed to using MPSIM and simulating the hell out of it. I step through the code, watching registers, poking bits when appropriate, and generally making sure it works. In C I've resorted to writing values and bits to spare ports, using LEDs (or an oscilloscope!) to read them, and stepping through the generated ASM code trying to work out what it's doing. The trouble with the latter is it's a) undocumented, b) written by a machine and c) uses hex values instead of symbols.Am I missing some obvious higher level debugging option? Where's the C equivalent of the register watch?CheersJohn. I only just found out that if the Disassembly Listing is focussed, it steps through an ASM line at a time, whereas if the C listing is focussed it steps one whole line of C code - which was damned unhelpful!

author: IanB
author_rank: Super Contributor
timestamp: April 29, 2012, 07:02:11 pm
content: Quote from: icon on April 29, 2012, 06:46:21 pmThis is normally what you do in C as well. I'm not familiar with the particular compiler and tool chain you are using, but normally you would step through the code line by line (either in a simulated environment, or on the actual hardware), examining variables as you go and checking that each line of code and each branch generates the result you expect.Sometimes you may need to break a complex expression down into several lines so that you can see intermediate results.With good tools you can switch to a machine code view that shows your code disassembled in an assembly window with program symbols displayed.The C equivalent of the register watch should be there. Either as a watch window with key variables in it, or a view onto the actual CPU registers to complement the machine code assembly view.

author: icon
author_rank: Regular Contributor
timestamp: April 29, 2012, 07:23:08 pm
content: Quote from: IanB on April 29, 2012, 07:02:11 pmThat's the bit I can't find. I can see the PIC's registers, of course, but then I have to plough through the disassembly, to puzzle out where it's putting my variables. I tried sticking a 'printf()' in, to see where it would direct the output - sadly compilation fails with a 'conflicting declarations' error message, which is... sub-optimal.John

author: IanB
author_rank: Super Contributor
timestamp: April 29, 2012, 07:42:14 pm
content: You may already be familiar with this in general, but this is the kind of thing you would be expecting to see. (In this case with Microsoft Visual Studio.)Maybe someone here who is familiar with Hi-Tech C can help you?

author: Rufus
author_rank: Super Contributor
timestamp: April 29, 2012, 08:02:03 pm
content: Quote from: icon on April 29, 2012, 07:23:08 pmIf you are using MPLAB you have a watch window which will display any global variables you select and a local window automatically displaying all function local variables.You need to build with debug options set so the compiler generates debug information.

author: icon
author_rank: Regular Contributor
timestamp: April 29, 2012, 08:06:51 pm
content: Ah - there is a 'Locals' option in the 'View' Menu, with Address, Symbol Name and Value headings. Sounds ideal! Sadly I can't see any way of populating it. No drop-down, can't type anything, 'Properties' dialog box all greyed out.Anyone?[Edit to add:]I've changed from 'release' to 'debug', but still no 'Locals' available. The Watch window allows you to pick SFRs and 'Symbols' - but these latter aren't my program variables, they're internal compiler gubbins. Still missing some compiler build option?John

author: Rufus
author_rank: Super Contributor
timestamp: April 29, 2012, 08:10:54 pm
content: Quote from: icon on April 29, 2012, 08:06:51 pmLocal variables only exist while executing (or stepping through) the functions that contain them. They appear automatically in the local window.

author: icon
author_rank: Regular Contributor
timestamp: April 29, 2012, 08:26:13 pm
content: Bingo - If I add the mysteriously named symbols in the Watch window ('bitvars2, bitvars3'), my variables are buried underneath those.ThanksJohn

author: Rufus
author_rank: Super Contributor
timestamp: April 30, 2012, 03:15:46 pm
content: Quote from: icon on April 29, 2012, 08:26:13 pmIn the watch window the edit control next to the 'Add Symbol' button is a drop down list which also has search as you type functionality.