Collect error reports

Source: Internet
Author: User

The program is always difficult to avoid the problems that can arise when serious crashes or even exits. Many applications now collect related error messages in the event of a crash, sending error reports that allow developers to locate and fix problems.

Recently in the actual work also encountered a similar problem, the client through the Plsql and other tools to connect to the remote database server for operational operations, for security reasons, here the client is not directly connected to the server, but first connected to a similar to a springboard machine, and then by the springboard machine connected to the real server, the data flow as shown.

Now you need to extract the command that the client entered at the Springboard Machine, the SQL statement to be executed. To the communication process will find that the communication data is not clear, but there are certain rules to follow, because the Oracle company does not expose the data format, different versions of the data format may differ, and so on, it is not easy to extract the SQL statements from the communication packet completely and accurately, the current practice is based on empirical guessing analysis.

As it turns out, this approach works well in most cases, and only in rare cases, because of certain features of the input data and the lack of protection of the parsing process, memory-out-of-bounds read-write problems occur, causing the entire program to crash. It can be seen that the current experience is not enough to cover all cases by 100%.

There is one way to prevent the program from exiting abnormally, and to collect the data that will cause the program to crash, by generating the child process after receiving the input data, the sub process is responsible for parsing, and the parent process waits for the result. If the child process exits normally, the parsing is successful and no additional processing is required, whereas if the child process exits unexpectedly, the parsing process is out of order, and the parent process takes the input data to the disk file for the developer to analyze and perfect the SQL statement parsing experience.

Here is an example of the above ideas to do a simple explanation. The program function is to enter a string, and if the string length is odd, the exception exits.

1#include <stdio.h>2#include <string.h>3 intMain ()4 {5     Charbuf[ -];6      for (;;) {7scanf"%[^\n]%*c", buf);8         if(Strlen (BUF) &1)9Memset0,0,1); Ten     }    One     return 0; A}
View Code

The following are the improvement practices.

1#include <stdio.h>2#include <sys/types.h>3#include <unistd.h>4#include <stdlib.h>5#include <string.h>6#include <wait.h>7#include <sys/stat.h>8#include <fcntl.h>9 voidSaveerr (Const Char*buf)Ten { OneFILE *FP = fopen ("Err.log","A +"); Afprintf (FP,"%s\n", buf); - fclose (FP); - } the intMain () - { -     intfd[2], status; - pid_t pid; +     Charbuf[ -], tmp[ -]; -  +      for (;;) { Ascanf"%[^\n]%*c", buf); at pipe (FD); -PID =fork (); -         if(PID >0) { -Close (fd[0]); -Write (fd[1], buf, strlen (BUF)); -Waitpid (PID, &status,0); in             if(!wifexited (status)) - Saveerr (BUF); to}Else if(0==pid) { +Close (fd[1]); -Read (fd[0], TMP,sizeof(TMP)); the             if(Strlen (TMP) &1) *Memset0,0,1); $             return 0;Panax Notoginseng         } -     } the     return 0; +}
View Code

Collect error reports

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.