[0083] Embodiment two:
[0084] Reference figure 2 , Shows a step flow chart of the second embodiment of an exception handling method for the Android Android platform of the present invention. The Android Android platform may include a JAVA layer and a C++ layer, and specifically may include the following steps:
[0085] Step 201: During the operation of a program in the JAVA layer, when abnormal data in the C++ layer is detected, convert the abnormal data in the C++ layer into abnormal data in the JAVA layer;
[0086] In a preferred embodiment of the present invention, an exception handling function corresponding to the abnormal data of the C++ layer may be registered in the Android platform; the step 201 may include the following sub-steps:
[0087] Sub-step S31, calling a preset query or setting signal processing function sigaction to obtain the abnormal data of the C++ layer;
[0088] Sub-step S32, using the exception handling function to call the JAVA local interface JNI;
[0089] Sub-step S33, using the exception handling function to call a preset JAVA layer signal capture function through the JAVA local interface JNI; the preset JAVA layer signal capture function can be used to generate JAVA layer exception data.
[0090] In the embodiment of the present invention, for the abnormal data of the C++ layer that may occur during the running of the program, a corresponding exception handling function is registered on the Android platform.
[0091] When an exception occurs in the C++ layer, obtain the exception handling function registered in the Android platform for the exception of the C++ layer, and use the exception handling function to call the JNI (Java Native Interface) of the JAVA layer and call the signal capture function of the JAVA layer , Convert the C++ layer to the abnormal data of the JAVA layer.
[0092] In the embodiment of the present invention, the LINUX signal mechanism is used to acquire and transmit abnormal data. In the LINUX signal mechanism, the signal is used to notify the program that an asynchronous event has occurred. It should be noted that the signal is used to notify a program of what event has occurred, and does not need to pass any data to the program. The program that receives the signal can have different processing methods for various signals. In the embodiment of the present invention, for the signal that needs to be processed, the program can specify a processing function, and the function is used for processing.
[0093] Specifically, first send the abnormal data captured in the C++ layer to the current program by querying or setting the signal processing sigaction() function. The program finds the corresponding exception handling function according to the abnormal data in the C++ layer, and uses the exception handling function Call the preset JAVA layer signal capture function through the JAVA local interface JNI, and run the abnormal data of the C++ layer to generate the abnormal data of the JAVA layer.
[0094] It should be noted that in the embodiment of the present invention, the abnormal data of the C++ layer can be converted into the abnormal data of the JAVA layer in other ways, and the embodiment of the present invention does not limit this.
[0095] Step 202: Collect abnormal data of the converted JAVA layer;
[0096] In a preferred embodiment of the present invention, the step 202 may include the following sub-steps:
[0097] Sub-step S41, calling a preset uncaught exception handling function UncaughtExceptionHandler to obtain the call stack information; the call stack information includes the abnormal function call relationship of the JAVA layer;
[0098] Sub-step S42, extracting abnormal data corresponding to the function call relationship from the log LOG information.
[0099] In a specific implementation, the log LOG information in the Android platform stores the function call relationship when the program is running. The call stack information can record the function call relationship of the program in operation (also called the function execution sequence). Each program thread has its own call stack information. The call stack information records the program running sequence and also records when the program is abnormal. Therefore, in the embodiment of the present invention, the call stack information can be obtained to obtain the corresponding abnormal function call relationship from the log LOG information in the Android platform.
[0100] The call stack information plays an important role in the program. Usually when debugging a program, you need to view the call stack information of the current function. The call stack information is a function call chain that reflects the function call relationship. For example, function f1 calls function f2, function f2 calls function f3, then f1→f2→f3 is one Call stack information. Specifically, there may be multiple call situations between one function and another function. For example, the call stack of function f1 to function f3 can be f1→f2→f3, or f1→f4→f5→f3. Therefore, when When the program is abnormal, detects an error, or is interrupted by a breakpoint, you can view the current call stack information of the current program.
[0101] In the embodiment of the present invention, after the abnormal data of the C++ layer is converted into the abnormal data of the JAVA layer, the uncaught exception handling function UncaughtExceptionHandler of the JAVA layer is called to obtain the call stack information, and the abnormal data of the JAVA layer in the call stack information is called The function call relationship obtains the corresponding abnormal data from the log information.
[0102] It should be noted that, in the embodiment of the present invention, the abnormal data of the JAVA layer can also be obtained in other ways, which is not limited in the embodiment of the present invention.
[0103] Step 203: If abnormal data of the JAVA layer is detected, call a preset uncaught exception handling function UncaughtExceptionHandler to obtain call stack information; the call stack information includes the abnormal function call relationship of the JAVA layer;
[0104] In the JAVA layer, the application program is written. The application program in the conceptual space provided by Android Android to developers is a loosely represented concept, and there is not much substantial representation. In the actual space of Android, you can't see the concept of application in the actual sense. Although there is an application class, it refers to the context state of the application, which is an extremely weak concept. Set the exception handling class as the default exception handling handle in the onCreate() function of the application application.
[0105] Specifically, the exception handling class is written in the JAVA layer to implement the public void uncaughtException(Thread thread, Throwable exception) of the uncaught exception handling function UncaughtExceptionHandler in the onCreate() function, and print the exception data in the uncaughtException handling function uncaughtException The call stack information is stored locally.
[0106] Step 204: Extract abnormal data corresponding to the function call relationship from the log LOG information.
[0107] When the application is running abnormal data in the JAVA layer, it will trigger the default exception handling class uncaughtException function, this function can achieve abnormal data printing and storage functions.
[0108] Step 205: Save the abnormal data of the JAVA layer in the Android platform;
[0109] Step 206: Stop the running of the program.
[0110] By applying the embodiments of the present invention, when abnormal data occurs in the C++ layer of the Android Android platform, the LINUX signal mechanism can be used to convert the abnormal data of the C++ layer into the abnormal data of the JAVA layer by using the LINUX signal mechanism and the Java local interface JNI of the Android platform, so that C++ can be printed. The call stack information of the abnormal data of the layer, and then print out the abnormal information in the log information, unify the abnormal data collection and processing of the C++ layer and the JAVA layer, and improve the system processing performance of the Android platform.
[0111] It should be noted that the embodiments of the present invention can not only capture exceptions for JAVA applications in the terminal, but also can be applied in other occasions where the C++ layer and the JAVA layer are combined.
[0112] In order to enable those skilled in the art to further understand the embodiments of the present invention, specific examples are used for description below.
[0113] Reference image 3 The illustrated schematic diagram of the process of capturing and collecting exceptions on the Android platform of the present invention may specifically include the following steps:
[0114] 1. Detect abnormal data in the JAVA layer;
[0115] 2. Detect abnormal data in the C++ layer;
[0116] 3. Obtain, according to the abnormal data of the C++ layer, the exception handling function registered in the Android exception of the Android, and the abnormal data corresponding to the C++ layer;
[0117] 4. Call the JAVA local interface JNI with the exception handling function;
[0118] 5. The abnormal handling data function uses the JAVA local interface JNI to call the JAVA layer signal capture function;
[0119] 6. The JAVA layer signal capture function runs according to the abnormality of the C++ layer and converts it into the abnormal data of the JAVA layer;
[0120] 7. Call the exception handling function of the JAVA layer;
[0121] 8. Use the exception handling function of the JAVA layer to print the abnormal call stack information of the JAVA layer;
[0122] 9. Print log LOG information according to the abnormal call stack information;
[0123] 10. Read the log LOG information to obtain the abnormal data of the JAVA layer and C++ layer, and store it in the corresponding file for compression processing;
[0124] 11. Ask whether to upload abnormal information to the abnormal information file server; if yes, go to step 12; if not, go to step 13;
[0125] 12. Upload abnormal data to the abnormal information file server;
[0126] 13. Exit the program.
[0127] In the embodiment of the present invention, the principles of abnormal capture and collection of the Android Andorid platform are summarized as follows:
[0128] 1. Abnormal data capture for JAVA layer;
[0129] 1. Write the exception handling class in the JAVA layer to implement the public void uncaughtException(Thread thread, Throwable exception) function of the UncaughtExceptionHandler, print the abnormal call stack information in the uncaughtException function and store it locally, and kill the exception. Application of data. Set the exception handling class as the default exception handling handle in the onCreate() function of the application application.
[0130] 2. When abnormal data occurs in the JAVA layer during the running of the application, the uncaughtException function of the default exception handling class will be triggered, which implements the printing and storage of abnormal data.
[0131] 2. Abnormal data capture for C++ layer;
[0132] 1. Using the LINUX signal mechanism, first use the LINUX mechanism to query or set the signal processing function sigaction() function, and specify the abnormal signal that needs to be captured to set the exception processing function for the abnormal data. Mainly complete the registration process of the corresponding processing of abnormal data.
[0133] 2. In practice, because JAVA syntax can directly obtain call stack information through exception data, that is, the JAVA layer can directly obtain call stack information through exception data, but the C++ layer does not support direct printing of call stack information.
[0134] 3. Use JAVA local interface JNI call to solve the problem that the C++ layer does not support direct printing of the call stack information. When abnormal data occurs in the C++ layer, the LINUX signal mechanism will send a signal to the current process. If the signal is registered in step 1, , It will call the registered exception handling function, the local interface JNI is used in the exception handling function, the JAVA layer signal capture function is called through the exception code of the C++ layer, the JAVA layer signal capture processing function, and the runtime exception is generated, which is provided to the JAVA layer exception capture use.
[0135] 4. The C++ layer abnormal data capture uses the LINUX signal mechanism and the JNI of the Android platform to convert the C++ abnormal data into the JAVA layer abnormal data, thereby unifying the collection and processing of the C++ and JAVA layer abnormal data.
[0136] 3. Abnormal collection;
[0137] 1. First, print the call stack information of the calling JAVA layer. This call stack information includes the abnormal data of the JAVA layer and the abnormal data of the JAVA layer converted from the abnormal data of the C++ layer; secondly, print the abnormal data in the log information of the system. Function call relationship; finally, read the log LOG information of the Android platform, store the log LOG information in a file named according to the program name + time, and compress it.
[0138] 2. Ask the user whether he agrees to upload the file storing the abnormal data to the designated server. When the user agrees to upload to the designated server, upload the file of the abnormal data.
[0139] Applying the embodiment of the present invention, when the Android platform detects abnormal data in the C++ layer, the abnormal data in the C++ layer is converted into the abnormal data in the JAVA layer, and the converted abnormal data in the JAVA layer is collected and uploaded to the server. The embodiment of the present invention converts the abnormal data of the C++ layer into the abnormal data of the JAVA layer and submits it to the JAVA layer for unified collection. Therefore, the JAVA layer can receive and capture the abnormal data regardless of the abnormality of the JAVA layer or the C++ layer. , Realize the unified processing of abnormal data collection, and improve the processing performance of the Android platform.
[0140] It should be noted that, for the sake of simple description, the method embodiments are all expressed as a series of action combinations, but those skilled in the art should know that the embodiments of the present invention are not limited by the described sequence of actions, because According to the embodiments of the present invention, certain steps may be performed in other order or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present invention.