Add a debug header to the Function

Source: Internet
Author: User

When debugging a program, you often need to view the flow direction of function calls of the program. On the PC, it is very simple. You can perform single-step debugging or GDB command lines on F10 and F11. However, the actual debugging found that in dealing with the call of multiple inheritance and virtual functions, the single step of the vc-express-2008 debugger does not play a good role. [There is no separate example for this conclusion, make up later ]. However, it is difficult to implement single-step debugging on embedded devices. Printing logs has become a useful debugging method. However, in a relatively large program, it is not realistic to manually add debugging statements to all function headers [except for a single field of code, for everyone's understanding ]. So I thought of writing a small tool to add debugging statements to all function headers.

To put it bluntly, go to the Code:

# Include <iostream> # include <vector> using namespace STD; /***************************************: for the input. H ,. print conditions are added to all function headers of the CPP file: the code file can be compiled normally. This program does not perform grammar or semantic check steps: 1. Read the file [Simplified to applying for a suitable space for one-time reading] 2. Find the function header [simplified to a function header without considering an additional macro definition] ① find all functions not enclosed in single quotes or double quotes '{' ② exclude '{' leading [non-null and wrap character] Is if (), For (), while (), switch () ③ exclude '{'leading not ') '[do not consider static/const modifiers] ③ the rest are function headers 3. Add a print after' {': [Press enter to wrap] debug_func ("tagxxx --- FUNC: % s, line: % d, f Ile: % s ",__ function __,__ line __,__ file __); [Carriage Return and line feed] ********************************* * *****************/# define switch_length 6 # define for_length 3 # define while_length 5 # define if_length 2 # define buf_size 100000 # define path_size 300 # define dbg_length 20 const char * debugsentence = "\ ndebug_func (\" tagxxx --- FUNC: % s, line: % d, file: % s \ n \ ",__ function __,__ line __,__ file _);"; vector <int> debugplace;/ /Record the valid {location void readfile (const char * filename, char * BUF) {file * fp = fopen (filename, "rb "); // change it to RB... strange, it is a TXT file, fread content is incorrect, and there is more. This is a typical place. Maybe C ++ has a better way of reading and writing files, and is used to the C method. If (! FP) {printf ("error: no such file"); exit (-1);} fflush (FP); int n = fread (BUF, sizeof (char), buf_size, FP); fclose (FP);} void writefile (const char * filename, char * BUF) {file * fp = fopen (filename, "WB "); // change it to WB... otherwise, if (! FP) {printf ("error: no such file"); exit (-1);} fwrite (BUF, sizeof (char), strlen (BUF), FP ); fclose (FP);} bool printable (char ch) {If (CH> = 'A' & Ch <= 'Z') return true; if (CH> = 'A' & Ch <= 'Z') return true; If (CH> = '0' & Ch <= '9') return true; if (CH = ')' | CH = ';') return true; return false;} void analy (char * BUF) {bool sinquoteflag = false, douquoteflag = false; bool valid = false; int Line = 0; char chtmp = 0x00; int rightside = 0; int placemark [3] = {-1,-1,-1}; // mark position: ')', '(', '('position of the first printable character on the left Char dbgstring [dbg_length] = ""; // outer loop, traverse all characters for (INT I = 2; I <buf_size; I ++) {chtmp = Buf [I]; If (BUF [I] = '\ n ') line ++; // exclude '{' in single quotes and double quotes, regardless of \ 'escape and comment. However, to be able to be used in the real world, this cannot be avoided, and the source code of the compiler must be used. If (BUF [I] = '\ '') sinquoteflag =! Sinquoteflag; If (BUF [I] = '\ "') douquoteflag =! Douquoteflag; If (sinquoteflag | douquoteflag) continue; If (BUF [I] = '{') {// debug statement for (int K = 0; k <DBG_LENGTH-1; k ++) {dbgstring [k] = Buf [I + k];} printf ("% d ----- % s ----------- ^", I, dbgstring ); // debug statement // debugplace. push_back (I + 1); valid = true; placemark [0] =-1; placemark [1] =-1; placemark [2] =-1; // backtrack exclude leading non ')' For (Int J = I-1; j> = 0; j --) {If (! Printable (BUF [J]) continue; // ')' position placemark [0] = J; break;} // not found, if (placemark [0] <1 | Buf [placemark [0]! = ') Continue; else {// record the number of extra parentheses to facilitate the discovery of the left parentheses on the outermost layer, and trace back to evaluate the string, if you encounter a left brace, subtract one right side = 1; // the exclusion is for/while/do/switch/keyword for (int K = placemark [0]-1; k> = 1; k --) {// ')', a single double quotation mark is directly considered as not a function header, if (BUF [k] = '\ ''| Buf [k] =' \" ') {valid = false; break ;} if (BUF [k] = ') rightside ++; If (BUF [k] =' (') rightside --; If (rightside) continue; // '(' position placemark [1] = K; break;} If (placemark [1] <= 1) Conti NUE; // backtrace to exclude leading non-printable for (int l = placemark [1]-1; L> = 0; l --) {If (! Printable (BUF [l]) continue; // '('position of the first printable character on the left placemark [2] = L; break ;} if (placemark [2] <0) continue; If (placemark [2]> = if_length) {If (BUF [placemark [2] = 'F' & Buf [placemark [2]-1] = 'I') {If (! Printable (BUF [placemark [2]-2]) {continue ;}}if (placemark [2]> = for_length) {If (BUF [placemark [2] = 'R' & Buf [placemark [2]-1] = 'O' & Buf [placemark [2]- 2] = 'F ') {If (! Printable (BUF [placemark [2]-3]) {continue ;}}if (placemark [2]> = while_length) {If (BUF [placemark [2] = 'E' & Buf [placemark [2]-1] = 'l' & Buf [placemark [2]- 2] = 'I' & Buf [placemark [2]-3] = 'H' & Buf [placemark [2]-4] = 'W ') {If (! Printable (BUF [placemark [2]-5]) {continue ;}}if (placemark [2]> = switch_length) {If (BUF [placemark [2] = 'H' & Buf [placemark [2]-1] = 'C' & Buf [placemark [2]- 2] = 'T' & Buf [placemark [2]-3] = 'I' & Buf [placemark [2]-4] = 'W '& & Buf [placemark [2]-5] ='s ') {If (! Printable (BUF [placemark [2]-6]) {continue ;}}if (valid) {debugplace. push_back (I + 1); // converts it to the index value starting from 1, that is, the length of the string, including '{'} // If (BUF [I] = '{')} // For (INT I = 0; I <sizeof (BUF ); I ++)} void edit (char * BUF) {If (! Debugplace. size () return; char TMP [buf_size]; memset (TMP, 0, sizeof (TMP); int seglength = 0; memcpy (TMP, Buf, debugplace [0]); memcpy (TMP + debugplace [0], debugsentence, strlen (debugsentence); For (unsigned int I = 1; I <debugplace. size (); I ++) {seglength = debugplace [I]-debugplace [I-1]; // copy the fragment memcpy (TMP + I * strlen (debugsentence) + debugplace [I-1], BUF + debugplace [I-1], seglength); // copy the debug statement memcpy (TMP + I * strlen (debugsentence) + debugplace [I-1] + seglength, debugsentence, strlen (debugsentence); // if (I = debugplace. size ()-1) {memcpy (TMP + (I + 1) * strlen (debugsentence) + debugplace [I], BUF + debugplace [I], strlen (BUF) -debugplace [I]);} memcpy (BUF, TMP, strlen (TMP);} void main (INT argc, char * argv []) {If (argc <2) {printf ("the CMD format is: \ n \ dbghead filename1 filename2 filename3 .... "); return;} Char Buf [buf_size]; for (INT I = 1; I <argc; I ++) {memset (BUF, 0, sizeof (BUF )); readfile (argv [I], Buf); analy (BUF); Edit (BUF); writefile (argv [I], Buf );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.