WO2022199284A1 - 调用栈回溯方法、装置、设备和介质 - Google Patents
调用栈回溯方法、装置、设备和介质 Download PDFInfo
- Publication number
- WO2022199284A1 WO2022199284A1 PCT/CN2022/076545 CN2022076545W WO2022199284A1 WO 2022199284 A1 WO2022199284 A1 WO 2022199284A1 CN 2022076545 W CN2022076545 W CN 2022076545W WO 2022199284 A1 WO2022199284 A1 WO 2022199284A1
- Authority
- WO
- WIPO (PCT)
- Prior art keywords
- function module
- code segment
- segment corresponding
- backtracking
- stack
- Prior art date
Links
- 238000000034 method Methods 0.000 title claims abstract description 173
- 230000006870 function Effects 0.000 claims abstract description 293
- 230000008569 process Effects 0.000 claims abstract description 57
- 238000010276 construction Methods 0.000 claims abstract description 12
- 238000004590 computer program Methods 0.000 claims description 29
- 230000008859 change Effects 0.000 claims description 9
- 230000002159 abnormal effect Effects 0.000 claims description 4
- 238000010586 diagram Methods 0.000 description 8
- 238000012986 modification Methods 0.000 description 4
- 230000004048 modification Effects 0.000 description 4
- 230000000694 effects Effects 0.000 description 3
- 238000012545 processing Methods 0.000 description 3
- 230000003287 optical effect Effects 0.000 description 2
- 238000012216 screening Methods 0.000 description 2
- 230000009286 beneficial effect Effects 0.000 description 1
- 238000004891 communication Methods 0.000 description 1
- 238000012217 deletion Methods 0.000 description 1
- 230000037430 deletion Effects 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 230000007246 mechanism Effects 0.000 description 1
- 239000013307 optical fiber Substances 0.000 description 1
- 239000004065 semiconductor Substances 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/36—Prevention of errors by analysis, debugging or testing of software
- G06F11/362—Debugging of software
- G06F11/3636—Debugging of software by tracing the execution of the program
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/36—Prevention of errors by analysis, debugging or testing of software
- G06F11/362—Debugging of software
- G06F11/366—Debugging of software using diagnostics
Definitions
- the present disclosure relates to the field of computer technologies, and in particular, to a call stack backtracking method, apparatus, device and medium.
- the system function getStackTrace is usually called directly to achieve this.
- the entire stack backtracking process is uncontrollable, and it needs to perform backtracking of all stack frames (ie, full stack backtracking) before it can end, resulting in a long stack backtracking time.
- the embodiments of the present disclosure provide a call stack traceback method, apparatus, device and medium.
- an embodiment of the present disclosure provides a call stack backtracking method, including:
- the context parameter and the current thread pointer Based on the first address of the first memory area, the context parameter and the current thread pointer, execute the code segment corresponding to the construction function module, and initialize the content stored in the target sub-area;
- the third function module address in the self-defined virtual function module table is the address of the preset target callback function module
- Whether to end the backtracking of the call stack is determined according to the execution times of the code segment corresponding to the preset target callback function module.
- an embodiment of the present disclosure further provides a call stack traceback device, including:
- a memory area application module configured to apply for a first memory area, and determine a target sub-area in the first memory area; wherein, the target sub-area is an area corresponding to the first address of the first memory area;
- the parameter acquisition module is used to obtain the context parameters and the current thread pointer
- a content initialization module configured to execute the code segment corresponding to the construction function module based on the first address of the first memory area, the context parameter and the current thread pointer, and initialize the content stored in the target sub-area;
- a content change module used to change the storage content in the initialized target sub-region to the address of the custom virtual function module table; wherein, the third function module address in the self-defined virtual function module table is a preset target The address of the callback function module;
- the first execution module is used to execute the code segment corresponding to the stack backtracking function module WalkStack based on the first address of the first memory area, to start backtracking of the call stack;
- a second execution module configured to execute the code segment corresponding to the preset target callback function module during the backtracking process of the call stack, and determine the execution times of the code segment corresponding to the preset target callback function module;
- the stack backtracking determination module is configured to determine whether to end the backtracking of the call stack according to the execution times of the code segment corresponding to the preset target callback function module.
- embodiments of the present disclosure further provide an electronic device, including a memory and a processor, wherein a computer program is stored in the memory, and when the computer program is executed by the processor, the electronic device is made to The device implements any of the call stack traceback methods provided in the embodiments of the present disclosure.
- an embodiment of the present disclosure further provides a computer-readable storage medium, where a computer program is stored in the storage medium, and when the computer program is executed by a computing device, the computing device enables the computing device to implement the embodiment of the present disclosure Provides any of the described call stack traceback methods.
- the technical solutions provided by the embodiments of the present disclosure have at least the following advantages: in the embodiments of the present disclosure, first apply for a first memory area, and determine the target sub-area corresponding to the first address of the first memory area, and then pass The content initialization and storage content modification operations realize that the storage content in the target sub-area is determined as the address of the custom virtual function module table, and the third function module address in the table is the address of the preset target callback function module; secondly, Based on the first address of the first memory area, the code segment corresponding to the stack backtracking function module WalkStack is executed to start the backtracking of the call stack.
- the First address find the address of the preset target callback function module, so that in the backtracking process of the call stack, execute the code segment corresponding to the preset target callback function module, and determine its execution times; and then determine whether to end the call according to the execution times
- the backtrace of the stack The embodiment of the present disclosure solves the problem of time-consuming stack backtracking caused by only full stack backtracking in the existing stack backtracking scheme, realizes effective control of stack backtracking during program running, achieves the effect of controlling the number of stack backtracking, and reduces the number of stack backtracking times. Time-consuming stack backtracking.
- FIG. 1 is a flowchart of a method for backtracking a call stack provided by an embodiment of the present disclosure
- FIG. 2 is a flowchart about initialization in a call stack backtracking process provided by an embodiment of the present disclosure
- FIG. 3 is a schematic diagram of a comparison before and after the storage content of the target sub-region is changed according to an embodiment of the present disclosure
- FIG. 5 is a schematic structural diagram of a call stack traceback device provided by an embodiment of the present disclosure.
- FIG. 6 is a schematic structural diagram of an electronic device according to an embodiment of the present disclosure.
- the call stack is usually obtained by directly calling the call stack acquisition function getStackTrace provided by the device system.
- the call stack acquisition function getStackTrace multiple execution steps are usually included, and the number of execution steps is related to the number of stack frames (Stack Frames) included in the call stack. Since the only entry exposed by the device system to the outside world (such as developers) is the function entry corresponding to the call stack acquisition function getStackTrace, in the process of calling this function to acquire the call stack, it is necessary to execute all the steps included in the function implementation process. After the end (that is, the full stack backtracking), the stack backtracking can be ended.
- one of the cores of the technical solutions of the embodiments of the present disclosure is how to reasonably control the stack backtracking, so that the number of steps performed in the stack backtracking process is limited, and other unnecessary execution steps are omitted, in other words, that is, The number of stack frames for backtracking can be freely controlled to reduce the time-consuming of stack backtracking.
- FIG. 1 is a flowchart of a method for backtracking a call stack provided by an embodiment of the present disclosure.
- the method can be executed by a device for backtracking a call stack, which can be implemented in software and/or hardware, and can be integrated into an electronic device with computing capabilities on, such as mobile terminals, notebooks, computers, etc.
- the call stack backtracking device can be integrated on the electronic device in the form of a software development kit (SDK), and when the call stack backtracking needs to be performed during the running process of the application, the software development kit is called by calling the software development kit. Effective control of stack backtracking can be achieved.
- SDK software development kit
- the call stack traceback method may include:
- applying for the first memory area may include: applying for the first memory area for the pointer under the stack frame visitor (StackVisitor) class (this memory area is usually a continuous block).
- the size of the memory area is usually more than 100 bytes, which can be determined according to the program construction), and the first address of the first memory area and the target sub-area corresponding to the first address are determined.
- the string myVisitor may be exemplarily used to represent the first address of the first memory area, as a pointer under the stack frame visitor (StackVisitor) class to be constructed.
- the code segment corresponding to the function module can be obtained by executing the context parameters, and the context parameters can be obtained.
- the context parameter obtaining function ContextCreate
- the returned result is a context parameter
- the context parameter can be exemplarily represented by the string context in the process of building the program code.
- the code segment corresponding to the function module (CurrentFromGdb) can be obtained by executing the thread pointer, and the current thread pointer can be obtained.
- the thread pointer obtaining function (CurrentFromGdb) to obtain its return result.
- the result is the current thread pointer, and the string currentThread may exemplarily be used to represent the current thread pointer during construction of the program code.
- the thread pointer can be used to characterize and distinguish the current thread in the running process of the program.
- the code segment corresponding to the construction function module is the constructor corresponding to the stack frame visitor (StackVisitor) class (for example, it may be represented as StackVisitorC).
- the first address (myVisitor), context parameter (context), and current thread pointer (currentThread) of the first memory area can be used as the parameters of the aforementioned constructor, for example, myVisitor can be used as the first parameter, take currentThread as the second parameter, and take context as the third parameter, execute the constructor to initialize the content stored in the target sub-area, that is, realize the stack frame visitor (StackVisitor) constructed previously.
- the content pointed to by the pointer under the class is initialized, so that the first address (myVisitor) of the first memory area is constructed as a real visitor (Visitor) object. It should be noted that if other parameters are required during the execution of the constructor, the acquisition can be achieved in a manner similar to that of the context parameter or the current thread pointer.
- FIG. 2 is a flowchart of initialization in a call stack backtracking process provided by an embodiment of the present disclosure, which is used to exemplarily describe the initialization preparation stage in the stack backtracking process performed by the embodiment of the present disclosure, but should not be construed as a reference to the present disclosure. Specific limitations of the examples. As shown in Figure 2, from the perspective of the computer program execution flow, the initialization preparation phase may include the following operations:
- S1202 Determine the thread pointer acquisition function (CurrentFromGdb).
- the thread pointer acquisition function (CurrentFromGdb) is used to acquire the current thread pointer during the running of the program.
- S1203 Determine the constructor (StackVisitorC) under the stack frame visitor (StackVisitor) class.
- the constructor (StackVisitorC) is used to initialize the content pointed to by the pointer under the stack frame visitor (StackVisitor) class.
- ContextCreate a context parameter acquisition function under the context (Context) class.
- ContextCreate The context parameter obtaining function (ContextCreate) is used to obtain the context parameters during the running process of the program.
- the functions shown in FIG. 2 are determined according to the functions that need to be used in the backtracking process of the actual execution of the call stack, and can be adjusted according to requirements, such as deletion or addition. Moreover, the determination order of each function shown in Fig. 2 is not strictly limited, that is, the function determination order shown in Fig. 2 is only used as an example. By determining each function through the initialization operation, it can be ensured that each function is successfully called (or the code segment corresponding to each function is successfully executed) during the stack backtracking process.
- S104 Change the storage content in the initialized target sub-area to the address of the user-defined virtual function module table; wherein, the third function module address in the user-defined virtual function module table is the address of the preset target callback function module.
- the storage content in the target sub-area is the address of the default virtual function module table of the system (that is, the address of the default virtual function table, a virtual function module is Corresponding to a virtual function), the stored content at this time is invalid data. If the stored content is not changed, using the controllable stack backtracking logic provided by the embodiments of the present disclosure may easily lead to application crash during the actual implementation of stack backtracking. Therefore, it is necessary to further change the storage content in the initialized target sub-area, so that the address of the custom virtual function module table (ie, the address of the custom virtual function table) is stored in the target sub-area.
- the address of the custom virtual function module table ie, the address of the custom virtual function table
- the custom virtual function module table (or custom virtual function table) can be preconfigured during the process of building the program code.
- the third function module address in the custom virtual function module table is the address of the preset target callback function module callback. That is, after the storage content in the initialized target sub-area is changed, the address of the preset target callback function module callback can be determined through the storage content.
- changing the storage content in the initialized target sub-area to the address of the custom virtual function module table may include: firstly converting the storage content type in the initialized target sub-area to a pointer type, and then converting the The storage content in the initialized target sub-area is changed to the address of the custom virtual function module table.
- the address of the preset target callback function module callback can be determined through the stored content, and the pointer under the constructed stack frame visitor (StackVisitor) class (the first memory area of the first memory area) address) can be used to determine the target sub-area corresponding to the storage content, so it can be understood that the pointer under the stack frame visitor (StackVisitor) class points to the preset target callback function callback, so that during the stack backtracking process, the preset target callback function can be successfully called.
- Set the target callback function callback to effectively control the number of backtracked stack frames.
- the preset target callback function callback is a non-system function custom created by the developer during the construction of the program code, which plays an important role in realizing the controllable stack backtracking.
- FIG. 3 is a schematic diagram of comparison before and after the storage content of a target sub-area (area corresponding to the first address of the first memory area) according to an embodiment of the present disclosure, which is used to illustrate an embodiment of the present disclosure, Specifically, the upper sub-picture in FIG. 3 represents a schematic diagram after the storage content of the target sub-region is initialized, and the lower sub-picture in FIG. 3 represents a schematic diagram after the initialized storage content of the target sub-region is changed.
- the storage content in the target sub-area is the address of the system default virtual function table (that is, The address of the aforementioned default virtual function module table, one virtual function module corresponds to one virtual function), the default virtual function table may include multiple default function addresses, exemplarily may include default address 1 (for example, Destructor1 address), default Address 2 (eg Destructor2 address), default address 3 (eg _cxa_pure_virtual address).
- the default virtual function table may include multiple default function addresses, exemplarily may include default address 1 (for example, Destructor1 address), default Address 2 (eg Destructor2 address), default address 3 (eg _cxa_pure_virtual address).
- the storage content in the target sub-area is the address of the custom virtual function table (that is, the address of the aforementioned custom virtual function module table), and the same is true in the custom virtual function table.
- Multiple function addresses can be included, for example, the first two function addresses in the custom virtual function table and the first two function addresses in the default virtual function table can be the same, both are default address 1 (eg Destructor1 address), default address 2 (eg Destructor2 address) address), and the third function address in the custom virtual function table is the address of the preset target callback function callback.
- the address of the preset target callback function callback is the third function address in the custom virtual function table, which is determined by the device system.
- the reason is that the stack backtracking function WalkStack called when starting the stack backtracking is the system. function, in order to ensure the successful implementation of controllable stack backtracking, the address of the preset target callback function callback cannot be arbitrarily set in the custom virtual function table.
- the code segment corresponding to the stack backtracking function module WalkStack is executed, that is, the execution stack backtracking function WalkStack.
- the first address (myVisitor) of the first memory area can be used as a parameter of the stack backtracking function WalkStack, the stack backtracking function WalkStack can be executed, and the stack backtracking can be started.
- the code segment corresponding to the preset target callback function module is executed, that is, the preset target callback function callback is executed.
- the preset target callback function callback is executed once.
- the execution times of the code segment corresponding to the preset target callback function module that is, the execution times of the preset target callback function, will be recorded synchronously during the execution of the function.
- the historical execution times of the preset target callback function callback is N
- the current After starting the execution of the preset target callback function callback its execution times are updated to N+1, and a timer can be called to count the execution times.
- the execution times can be used as a judgment basis for subsequent determination of whether to end the stack backtracking.
- the execution times threshold of the code segment corresponding to the preset target callback function module may be determined first, and the execution times threshold may be a preset fixed value or a value that can be automatically adjusted during the program running process.
- the disclosed embodiments do not specifically limit this; if the execution times of the code segment corresponding to the preset target callback function module reaches the execution times threshold, the target result obtained based on executing the code segment corresponding to the preset target callback function module, for example, the target result is false, end the backtracking of the call stack; if the execution times of the code segment corresponding to the preset target callback function module does not reach the execution times threshold, other results are obtained based on executing the code segment corresponding to the preset target callback function module, for example, other results are true, continue the backtracking of the call stack, so as to reasonably control the backtracking number of stack frames.
- determining the execution times threshold of the code segment corresponding to the preset target callback function module including:
- the execution times threshold may be a value preset in the construction program code stage
- the type of the thread includes the main thread or the sub-thread;
- the thread type determination method determines that the type of the currently running thread is the main thread, and the execution count threshold can be set to the first value. value, the first value is greater than the second value, but the embodiments of the present disclosure do not limit the specific values of the two.
- the priority of the main thread is higher than that of the sub-thread, and the corresponding threshold of execution times of the main thread is relatively large. Accordingly, more stack frame backtracking can be performed, which is helpful to obtain more stack frame information.
- the execution times threshold corresponding to the sub-thread is relatively high. Small, correspondingly, less stack frame backtracking can be performed, and the time-consuming of stack backtracking can be reasonably controlled.
- the current running status information includes normal operation or abnormal operation. For example, if the current running state information of the application is normal, the execution times threshold can be set to the third value, and if the current running state information of the application is abnormal, the execution times threshold can be set to the fourth value, The fourth value is greater than the third value, but the embodiments of the present disclosure do not limit the specific values of the two.
- the execution count threshold of the code segment corresponding to the preset target callback function module can be flexibly determined in various ways, and accordingly the number of stack frame backtracking can be flexibly adjusted, thereby realizing effective control of the entire stack backtracking.
- first apply for a first memory area and determine the target sub-area corresponding to the first address of the first memory area, and then perform content initialization and storage content modification operations to determine the storage content in the target sub-area as Customize the address of the virtual function module table, the third function module address in the table is the address of the preset target callback function module; secondly, based on the first address of the first memory area, execute the code segment corresponding to the stack traceback function module WalkStack , to start the backtracking of the call stack, in the process of specifically executing the code segment corresponding to the stack backtracking function module WalkStack, the address of the preset target callback function module can be found through the first address of the first memory area, so that the backtracking of the call stack can be performed.
- the code segment corresponding to the preset target callback function module is executed, and the number of executions thereof is determined; and then, according to the number of executions, it is determined whether to end the backtracking of the call stack.
- the embodiment of the present disclosure solves the problem of time-consuming stack backtracking caused by only full stack backtracking in the existing stack backtracking scheme, realizes effective control of stack backtracking during program running, achieves the effect of controlling the number of stack backtracking, and reduces the number of stack backtracking times. Time-consuming stack backtracking.
- the call stack backtracking method may further include: in the process of executing the code segment corresponding to the preset target callback function module, based on the first address of the first memory area , execute the code segment corresponding to the target information acquisition function module to obtain the target information; wherein, the target information acquisition function module includes a method pointer acquisition function module GetMethod and/or an instruction offset value acquisition function module GetDexPc, correspondingly, the target information includes a method The instruction offset value DexPc corresponding to the pointer ArtMethod and/or the method pointer.
- the target information acquisition function belongs to a function that is nested and executed during the execution of the preset target callback function.
- each stack frame is traced back, and in the process of executing the preset target callback function, the target sub-area in the first memory area can be determined based on the first address of the first memory area, and then the target sub-area can be stored according to the target sub-area.
- the user-defined virtual function table determines the address of the preset target callback function, so that the target information acquisition function remains executed in the execution cycle of the preset target callback function.
- the method pointer ArtMethod and/or the method pointer of the stack frame can be correspondingly obtained.
- the corresponding instruction offset value DexPc can be used to obtain at least one of the method name of the stack frame, the class name of the stack frame method, and the source code file name corresponding to the stack frame, and the instruction offset value DexPc can be used to obtain the source code line corresponding to the stack frame.
- the call stack traceback method further includes: based on the obtained method pointer, executing the method description information to obtain the function module PrettyMethod
- the corresponding code segment obtains the description information corresponding to the method pointer.
- the execution method description information obtains the code segment corresponding to the function module PrettyMethod, that is, the execution method description information acquisition function PrettyMethod, which can obtain the description information (or string description information) corresponding to the current method pointer.
- strArtMethod including but not limited to the return value type of the method, the parameter type of the method, the class name of the method, the method name, etc.
- the description information corresponding to the method pointer includes the class name of the method corresponding to the method pointer
- the call stack traceback method provided by the embodiment of the present disclosure further includes:
- the class name list is used to record the class name corresponding to the method pointer that does not need to obtain the instruction offset value;
- the class name list may be preset in the process of constructing the program code, It can also be determined according to a specific class name screening rule during the program running process, and the specific content of the class name screening rule can be flexibly set, which is not specifically limited in the embodiment of the present disclosure, for example, according to the priority of the class name involved in the program running process, Record the class name with lower priority in the class name list;
- the instruction offset value acquisition function GetDexPc is executed later, in the description information obtained by executing the method description information acquisition function PrettyMethod, if the included method's class name matches the class name list successfully, It means that the method pointer under the class name of the method included in the current description information does not need to further obtain the corresponding instruction offset value; if the class name of the method included in the description information does not match the class name list, it means that the current description The method pointer under the class name of the method included in the information needs to further obtain the corresponding instruction offset value.
- the embodiment of the present disclosure can obtain the execution status of the code segment corresponding to the function module GetDexPc for the instruction offset value (or the execution status of the instruction offset value acquisition function GetDexPc) through the obtained class name and the class name list of the method. Re-control is performed to reduce the acquisition of non-concerned information and the occupation of computing resources, which further helps to improve the efficiency of stack backtracking.
- the execution target information obtains the code segment corresponding to the function module, including:
- the level of the currently backtracked stack frame belongs to the preset demand level, then based on the first address of the first memory area, execute the target information to obtain the code segment corresponding to the function module, and the preset demand level is used to define the stack frame for which the stack frame information needs to be obtained. the level to which it belongs.
- the preset target callback function has a corresponding relationship. If the number of executions is N, it means that the level of the current backtracked stack frame is N. If the level of the current backtracked stack frame belongs to the preset requirement level, execute the code segment corresponding to the target information acquisition function module to obtain the target information of the stack frame, otherwise, no execution is required.
- the preset requirement level can be pre-determined in the stage of constructing the program code, or can be dynamically determined according to the setting rules during the running of the program.
- the setting rules can be adaptively determined according to different application programs, which is not specifically limited in the embodiment of the present disclosure. For example, during the running process of the application program, the even or odd stack frame level in the call stack may be determined as the preset demand level and the like.
- the embodiments of the present disclosure can not only achieve effective control of the number of backtracked stack frames, improve stack backtracking efficiency, but also achieve information on several non-consecutive stack frames in the call stack.
- the maximum value of the preset demand level determines the number of stack frame backtracking, that is, the current need to execute
- the number of stack backtracking is 5 times, that is, the number of execution times of the code segment corresponding to the preset target callback function module is 5 times.
- the number of executions of the code segment corresponding to the function module determines the level of the stack frame currently traced back, and only when the third stack frame and the fifth stack frame are traced back will the target information be executed to obtain the code segment corresponding to the function module. , and stop the stack backtracking after backtracking out the 5th stack frame.
- the target information acquisition function module includes the method pointer acquisition function module GetMethod
- the code segment corresponding to the method description information acquisition function module PrettyMethod can also be executed to obtain the method pointer. corresponding description information.
- the first, second, and fourth stack frames are also backtracked, but the code segment corresponding to the target information acquisition function module is not executed to obtain the detailed information of these stack frames.
- the execution time of selectively obtaining the detailed information of two stack frames is shorter, and thus the stack backtracking can be completed faster.
- FIG. 4 is a flowchart of another call stack backtracking method provided by an embodiment of the present disclosure, which is used to exemplarily illustrate the embodiment of the present disclosure from the perspective of a computer program execution flow, but should not be construed as a specific description of the embodiment of the present disclosure. limited.
- the function name of the customized preset target callback function callback in the embodiment of the present disclosure can be represented by myVisitFrame exemplarily.
- the call stack traceback method may include:
- S405 take the first address of the memory area myVisitor as the first parameter, the current thread pointer currentThread as the second parameter, and the context parameter context as the third parameter, call the constructor StackVisitorC, and construct the first address of the memory area myVisitor as The real visitor is the Visitor object (or pointer).
- the backtracking of the call stack is started by calling the stack backtracking function WalkStack.
- the following operations S410-S416 belong to the operations to be performed during the execution of the target callback function myVisitFrame.
- S413 Record the method pointer ArtMethod and the instruction offset value DexPc of the current stack frame as the information of the current stack frame.
- the target number is the execution times threshold of the target callback function myVisitFrame. If it is determined that the number of backtracked stack frames does not reach the target number, the return value of the target callback function myVisitFrame is determined to be true, indicating that the backtracking of the call stack continues to execute; if it is determined that the number of backtracked stack frames reaches the target number, the target callback function myVisitFrame The return value is false, indicating that the backtrace of the call stack can end.
- operations S410, S411 and S412 may be determined according to the acquisition requirements of the stack frame information. limited. In specific applications, the three operations can be selectively performed according to requirements.
- the embodiment of the present disclosure solves the problem of time-consuming stack backtracking caused by only full stack backtracking in the existing stack backtracking scheme by calling a part of the steps of the stack backtracking in the system and changing the virtual function table, and realizes the time-consuming process of the program running process.
- the effective control of stack backtracking in the system achieves the effect of controlling the number of stack backtracking and reduces the time-consuming of stack backtracking.
- FIG. 5 is a schematic structural diagram of a call stack traceback apparatus provided by an embodiment of the present disclosure.
- the apparatus can be implemented by software and/or hardware, and can be integrated on electronic devices with computing capabilities, such as mobile terminals, notebooks, computers, etc. .
- the call stack traceback device 500 may include a memory area application module 501, a parameter acquisition module 502, a content initialization module 503, a content modification module 504, a first execution module 505, and a second execution module. 506 and stack backtracking determination module 507, wherein:
- the memory area application module 501 is used for applying for a first memory area, and determining a target sub-area in the first memory area; wherein, the target sub-area is an area corresponding to the first address of the first memory area;
- a parameter acquisition module 502 used to acquire context parameters and a current thread pointer
- the content initialization module 503 is used for executing the code segment corresponding to the construction function module based on the first address, the context parameter and the current thread pointer of the first memory area, and initializing the content stored in the target sub-area;
- the content change module 504 is used to change the storage content in the initialized target sub-region to the address of the custom virtual function module table; wherein, the third function module address in the custom virtual function module table is the preset target callback The address of the function module;
- the first execution module 505 is used for executing the code segment corresponding to the stack backtracking function module WalkStack based on the first address of the first memory area, to start backtracking of the call stack;
- the second execution module 506 is configured to execute the code segment corresponding to the preset target callback function module during the backtracking process of the call stack, and determine the execution times of the code segment corresponding to the preset target callback function module;
- the stack backtracking determining module 507 is configured to determine whether to end the backtracking of the call stack according to the execution times of the code segment corresponding to the preset target callback function module.
- the call stack traceback apparatus 500 provided by the embodiment of the present disclosure further includes:
- the third execution module is configured to, in the process of executing the code segment corresponding to the preset target callback function module, execute the code segment corresponding to the target information acquisition function module based on the first address of the first memory area to obtain the target information; wherein,
- the target information acquisition function module includes a method pointer acquisition function module GetMethod and/or an instruction offset value acquisition function module GetDexPc.
- the call stack traceback apparatus 500 provided by the embodiment of the present disclosure further includes:
- the execution times threshold determination module is used to determine the execution times threshold of the code segment corresponding to the preset target callback function module
- the stack backtracking determination module 507 is specifically used for:
- the backtracking of the call stack is ended based on the target result obtained by executing the code segment corresponding to the preset target callback function module.
- the execution times threshold determination module includes:
- an obtaining unit configured to obtain a preset threshold of execution times of the code segment corresponding to the preset target callback function module
- the first determining unit is used to obtain the type of the currently running thread, and according to the type of the thread, determine the execution times threshold of the code segment corresponding to the preset target callback function module; wherein, the type of the thread includes a main thread or a sub-thread;
- the second determining unit is configured to obtain the current running state information of the application, and according to the current running state information, determine the execution times threshold of the code segment corresponding to the preset target callback function module; wherein, the current running state information includes running normally or running abnormal.
- the call stack traceback apparatus 500 provided by the embodiment of the present disclosure further includes:
- the fourth execution module is used for, after the third execution module executes the method pointer to obtain the code segment corresponding to the function module GetMethod, based on the obtained method pointer, executes the method description information to obtain the code segment corresponding to the function module PrettyMethod, and obtains the description corresponding to the method pointer information.
- the description information corresponding to the method pointer includes the class name of the method corresponding to the method pointer
- the call stack traceback apparatus 500 provided by the embodiment of the present disclosure further includes:
- the class name list obtaining module is used to obtain a predetermined class name list; wherein, the class name list is used to record the class name corresponding to the method pointer that does not need to obtain the instruction offset value;
- the class name matching module is used to match the class name of the method included in the description information with the class name list, so as to execute the instruction offset value to obtain the code segment corresponding to the function module GetDexPc when the matching is unsuccessful.
- the second execution module 506 includes:
- the stack frame level determination unit is used to determine the level of the current backtracked stack frame based on the execution times of the code segment corresponding to the preset target callback function module;
- the code execution unit is configured to obtain the code segment corresponding to the function module by executing the target information based on the first address of the first memory area if the level of the current backtracked stack frame belongs to the preset demand level.
- the call stack traceback device provided by the embodiment of the present disclosure can execute any call stack traceback method provided by the embodiment of the present disclosure, and has corresponding functional modules and beneficial effects of the execution method.
- any call stack traceback method provided by the embodiment of the present disclosure and has corresponding functional modules and beneficial effects of the execution method.
- FIG. 6 is a schematic structural diagram of an electronic device according to an embodiment of the present disclosure, which is used to exemplarily illustrate an electronic device that implements the call stack backtracking method provided by the embodiment of the present disclosure.
- the electronic devices in the embodiments of the present disclosure may include, but are not limited to, such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablets), PMPs (portable multimedia players), vehicle-mounted terminals (eg, Mobile terminals such as car navigation terminals), etc., and stationary terminals such as digital TVs, desktop computers, smart home devices, wearable electronic devices, servers, and the like.
- the electronic device shown in FIG. 6 is only an example, and should not impose any limitation on the functions and occupancy scope of the embodiments of the present disclosure.
- electronic device 600 includes one or more processors 601 and memory 602 .
- Processor 601 may be a central processing unit (CPU) or other form of processing unit having data processing capabilities and/or instruction execution capabilities, and may control other components in electronic device 600 to perform desired functions.
- CPU central processing unit
- Memory 602 may include one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and/or non-volatile memory.
- Volatile memory may include, for example, random access memory (RAM) and/or cache memory, among others.
- Non-volatile memory may include, for example, read only memory (ROM), hard disk, flash memory, and the like.
- One or more computer program instructions may be stored on the computer-readable storage medium, and the processor 601 may execute the program instructions to implement the call stack backtracking method provided by the embodiments of the present disclosure, and may also implement other desired functions.
- Various contents such as input signals, signal components, noise components, etc. may also be stored in the computer-readable storage medium.
- the call stack backtracking method may include: applying for a first memory area, and determining a target sub-area in the first memory area; wherein, the target sub-area is an area corresponding to a first address of the first memory area; Obtain the context parameter and the current thread pointer; based on the first address, context parameter and current thread pointer of the first memory area, execute the code segment corresponding to the construction function module, and initialize the content stored in the target sub-area; The storage content in the area is changed to the address of the custom virtual function module table; wherein, the third function module address in the custom virtual function module table is the address of the preset target callback function module; based on the first address of the first memory area , execute the code segment corresponding to the stack backtracking function module WalkStack to start the backtracking of the call stack; during the backtracking process of the call stack, execute the code segment corresponding to the preset target callback function module, and determine the code corresponding to the preset target callback function module
- the electronic device 600 may also include an input device 603 and an output device 604 interconnected by a bus system and/or other form of connection mechanism (not shown).
- the input device 603 may also include, for example, a keyboard, a mouse, and the like.
- the output device 604 can output various information to the outside, including the determined distance information, direction information, and the like.
- the output device 604 may include, for example, displays, speakers, printers, and communication networks and their connected remote output devices, among others.
- the electronic device 600 may also include any other suitable components according to the specific application.
- the embodiments of the present disclosure also provide a computer program product, which includes a computer program or computer program instructions, and the computer program or computer program instructions, when executed by a computing device, cause the computing device to implement what is provided by the embodiments of the present disclosure.
- the computer program product may write program code for performing operations of embodiments of the present disclosure in any combination of one or more programming languages, including object-oriented programming languages, such as Java, C++, etc., as well as conventional procedural programming language, such as "C" language or similar programming language.
- the program code may execute entirely on the user electronic device, partly on the user electronic device, as a stand-alone software package, partly on the user electronic device and partly on the remote electronic device, or entirely on the remote electronic device execute on.
- embodiments of the present disclosure may further provide a computer-readable storage medium on which computer program instructions are stored, and when executed by the computing device, the computer program instructions enable the computing device to implement any call stack backtracking method provided by the embodiments of the present disclosure .
- the call stack backtracking method may include: applying for a first memory area, and determining a target sub-area in the first memory area; wherein, the target sub-area is an area corresponding to a first address of the first memory area; Obtain the context parameter and the current thread pointer; based on the first address, context parameter and current thread pointer of the first memory area, execute the code segment corresponding to the construction function module, and initialize the content stored in the target sub-area; The storage content in the area is changed to the address of the custom virtual function module table; wherein, the third function module address in the custom virtual function module table is the address of the preset target callback function module; based on the first address of the first memory area , execute the code segment corresponding to the stack backtracking function module WalkStack to start the backtracking of the call stack; during the backtracking process of the call stack, execute the code segment corresponding to the preset target callback function module, and determine the code corresponding to the preset target callback function module
- a computer-readable storage medium can employ any combination of one or more readable media.
- the readable medium may be a readable signal medium or a readable storage medium.
- the readable storage medium may include, for example, but not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatuses or devices, or any combination of the above. More specific examples (non-exhaustive list) of readable storage media include: electrical connections with one or more wires, portable disks, hard disks, random access memory (RAM), read only memory (ROM), erasable programmable read only memory (EPROM or flash memory), optical fiber, portable compact disk read only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Computer Hardware Design (AREA)
- Quality & Reliability (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Debugging And Monitoring (AREA)
Abstract
提供一种调用栈回溯方法、装置、设备和介质,其中,该方法包括:申请第一内存区域,并确定其目标子区域;执行构造功能模块对应的代码段,对目标子区域中存储的内容进行初始化;将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址;该表中的第三个功能模块地址为预设目标回调功能模块的地址;基于第一内存区域的首地址,执行栈回溯功能模块WalkStack对应的代码段,以启动调用栈的回溯;在调用栈的回溯过程中,执行预设目标回调功能模块对应的代码段,并确定执行次数;根据该执行次数,确定是否结束调用栈的回溯。
Description
相关申请的交叉引用
本申请要求于2021年3月22日提交的,申请名称为“调用栈回溯方法、装置、设备和介质”的、中国专利申请号为“202110303889.0”的优先权,该中国专利申请的全部内容通过引用结合在本申请中。
本公开涉及计算机技术领域,尤其涉及一种调用栈回溯方法、装置、设备和介质。
在应用程序的运行过程中,通常涉及到调用栈(Call stack)的回溯。通过栈回溯,可以获取需求的栈帧信息,以用于定位和分析程序运行过程中的异常。
在现有栈回溯方案中,通常直接调用系统函数getStackTrace来实现。然而,整个栈回溯过程不可控,需要执行所有栈帧的回溯(即全量栈回溯)后,才能结束,导致栈回溯耗时较长。
为了解决上述技术问题或者至少部分地解决上述技术问题,本公开实施例提供了一种调用栈回溯方法、装置、设备和介质。
第一方面,本公开实施例提供了一种调用栈回溯方法,包括:
申请第一内存区域,并确定所述第一内存区域中的目标子区域;其中,所述目标子区域是所述第一内存区域的首地址对应的区域;
获取上下文参数和当前线程指针;
基于所述第一内存区域的首地址、所述上下文参数和所述当前线程指针,执行构造功能模块对应的代码段,对所述目标子区域中存储的内容进行初始化;
将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址;其中,所述自定义虚拟功能模块表中的第三个功能模块地址为预设目标回调功能模块的地址;
基于所述第一内存区域的首地址,执行栈回溯功能模块WalkStack对应的 代码段,以启动调用栈的回溯;
在所述调用栈的回溯过程中,执行所述预设目标回调功能模块对应的代码段,并确定所述预设目标回调功能模块对应的代码段的执行次数;
根据所述预设目标回调功能模块对应的代码段的执行次数,确定是否结束所述调用栈的回溯。
第二方面,本公开实施例还提供了一种调用栈回溯装置,包括:
内存区域申请模块,用于申请第一内存区域,并确定所述第一内存区域中的目标子区域;其中,所述目标子区域是所述第一内存区域的首地址对应的区域;
参数获取模块,用于获取上下文参数和当前线程指针;
内容初始化模块,用于基于所述第一内存区域的首地址、所述上下文参数和所述当前线程指针,执行构造功能模块对应的代码段,对所述目标子区域中存储的内容进行初始化;
内容更改模块,用于将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址;其中,所述自定义虚拟功能模块表中的第三个功能模块地址为预设目标回调功能模块的地址;
第一执行模块,用于基于所述第一内存区域的首地址,执行栈回溯功能模块WalkStack对应的代码段,以启动调用栈的回溯;
第二执行模块,用于在所述调用栈的回溯过程中,执行所述预设目标回调功能模块对应的代码段,并确定所述预设目标回调功能模块对应的代码段的执行次数;
栈回溯确定模块,用于根据所述预设目标回调功能模块对应的代码段的执行次数,确定是否结束所述调用栈的回溯。
第三方面,本公开实施例还提供了一种电子设备,包括存储器和处理器,其中,所述存储器中存储有计算机程序,当所述计算机程序被所述处理器执行时,使得所述电子设备实现本公开实施例提供的任一所述的调用栈回溯方法。
第四方面,本公开实施例还提供了一种计算机可读存储介质,所述存储介质中存储有计算机程序,当所述计算机程序被计算设备执行时,使得所述计算 设备实现本公开实施例提供的任一所述的调用栈回溯方法。
本公开实施例提供的技术方案与现有技术相比至少具有如下优点:在本公开实施例中,首先申请第一内存区域,并确定第一内存区域的首地址对应的目标子区域,然后通过内容初始化和存储内容更改操作,实现将目标子区域中的存储内容确定为自定义虚拟功能模块表的地址,该表中的第三个功能模块地址为预设目标回调功能模块的地址;其次,基于第一内存区域的首地址,执行栈回溯功能模块WalkStack对应的代码段,以启动调用栈的回溯,在具体执行栈回溯功能模块WalkStack对应的代码段的过程中,可以通过第一内存区域的首地址,找到预设目标回调功能模块的地址,从而在调用栈的回溯过程中,执行预设目标回调功能模块对应的代码段,并确定其执行次数;进而根据该执行次数,确定是否结束调用栈的回溯。本公开实施例解决了现有栈回溯方案中只能实现全量栈回溯导致栈回溯耗时的问题,实现了在程序运行过程中对栈回溯的有效控制,达到了控制栈回溯次数的效果,降低了栈回溯的耗时。
此处的附图被并入说明书中并构成本说明书的一部分,示出了符合本公开的实施例,并与说明书一起用于解释本公开的原理。
为了更清楚地说明本公开实施例或现有技术中的技术方案,下面将对实施例或现有技术描述中所需要使用的附图作简单地介绍,显而易见地,对于本领域普通技术人员而言,在不付出创造性劳动性的前提下,还可以根据这些附图获得其他的附图。
图1为本公开实施例提供的一种调用栈回溯方法的流程图;
图2为本公开实施例提供的一种调用栈回溯过程中关于初始化的流程图;
图3为本公开实施例提供的一种关于目标子区域的存储内容更改前和更改后的对比示意图;
图4为本公开实施例提供的另一种调用栈回溯方法的流程图;
图5为本公开实施例提供的一种调用栈回溯装置的结构示意图;
图6为本公开实施例提供的一种电子设备的结构示意图。
为了能够更清楚地理解本公开的上述目的、特征和优点,下面将对本公开 的方案进行进一步描述。需要说明的是,在不冲突的情况下,本公开的实施例及实施例中的特征可以相互组合。
在下面的描述中阐述了很多具体细节以便于充分理解本公开,但本公开还可以采用其他不同于在此描述的方式来实施;显然,说明书中的实施例只是本公开的一部分实施例,而不是全部的实施例。
在现有方案中,如果应用程序运行过程中,需要执行调用栈的回溯(可以简称为栈回溯),通常是直接调用设备系统提供的调用栈获取函数getStackTrace获取调用栈。在调用栈获取函数getStackTrace的实现过程中,通常包括多个执行步骤,执行步骤的次数与调用栈中包括的栈帧(Stack Frame)的数量有关。由于设备系统暴露给外界(例如开发者)的入口只有调用栈获取函数getStackTrace对应的函数入口,所以在调用该函数获取调用栈的过程中,需要把该函数实现过程中包括的多个步骤全部执行结束(即全量栈回溯)后,才可以结束栈回溯。
然而,在实际应用中,并非调用栈中各个栈帧的信息都是需求的信息。示例性地,在调用栈获取函数getStackTrace的实现过程中,包括10个执行步骤,而实际需求的可能只有前面3个步骤的执行结果(即前面3个栈帧的信息),对剩余7个步骤的执行结果并不关注,所以本公开实施例技术方案的核心之一即在于如何对栈回溯进行合理控制,使得栈回溯过程执行的步骤次数有限,省去其他非必须的执行步骤,换言之,即实现自由控制回溯的栈帧的数量,降低栈回溯的耗时。
图1为本公开实施例提供的一种调用栈回溯方法的流程图,该方法可以由调用栈回溯装置执行,该装置可以采用软件和/或硬件实现,并可集成在具有计算能力的电子设备上,例如移动终端、笔记本、电脑等。具体地,该调用栈回溯装置可以采用软件开发工具包(Software Development Kit,SDK)的形式集成在电子设备上,当应用程序运行过程中需要执行调用栈的回溯时,通过调用该软件开发工具包即可实现对栈回溯的有效控制。
如图1所示,本公开实施例提供的调用栈回溯方法可以包括:
S101、申请第一内存区域,并确定第一内存区域中的目标子区域;其中, 目标子区域是第一内存区域的首地址对应的区域。
通过申请第一内存区域,可以为后续执行栈回溯过程中需要的指针参数开辟存储空间,同时也是自定义构建栈回溯过程中需求的指针参数所必不可少的操作。示例性地,申请第一内存区域,并确定第一内存区域中的目标子区域,可以包括:为栈帧访问者(StackVisitor)类下的指针申请第一内存区域(该内存区域通常为一块连续内存区域,大小通常超过100字节,具体可以根据程序构建而定),并确定该第一内存区域的首地址,以及该首地址对应的目标子区域。例如在构建程序代码过程中,可以示例性地采用字符串myVisitor表示第一内存区域的首地址,以作为需要构建的栈帧访问者(StackVisitor)类下的指针。
S102、获取上下文参数和当前线程指针。
在应用程序运行过程中,可以通过执行上下文参数获取功能模块(ContextCreate)对应的代码段,获取上下文参数,从计算机程序执行流程的角度而言,即执行上下文参数获取函数(ContextCreate),获取其返回结果,该返回结果即上下文参数,并且在构建程序代码过程中可以示例性地采用字符串context表示该上下文参数。同时,可以通过执行线程指针获取功能模块(CurrentFromGdb)对应的代码段,获取当前线程指针,从计算机程序执行流程的角度而言,即执行线程指针获取函数(CurrentFromGdb),获取其返回结果,该返回结果即当前线程指针,并且在构建程序代码过程中可以示例性地采用字符串currentThread表示该当前线程指针。线程指针可以用于表征与区分程序运行过程中的当前线程。
S103、基于第一内存区域的首地址、上下文参数和当前线程指针,执行构造功能模块对应的代码段,对目标子区域中存储的内容进行初始化。
在本公开实施例中,构造功能模块对应的代码段即对应栈帧访问者(StackVisitor)类下的构造函数(例如可以表示为StackVisitorC)。从计算机程序执行流程的角度而言,可以将第一内存区域的首地址(myVisitor)、上下文参数(context)和当前线程指针(currentThread),作为前述构造函数的参数,例如可以将myVisitor作为第一个参数、将currentThread作为第二个参数、将context作为第三个参数,执行构造函数,实现对目标子区域中存储的内容进 行初始化,也即实现对在前构建的栈帧访问者(StackVisitor)类下的指针指向的内容初始化,从而使得第一内存区域的首地址(myVisitor)被构建为真正的访问者(Visitor)对象。需要说明的是,如果构造函数执行过程中,还需要其他参数,可以采用与上下文参数或当前线程指针类似的获取方式实现获取。
图2为本公开实施例提供的一种调用栈回溯过程中关于初始化的流程图,用于对本公开实施例执行栈回溯过程中的初始化准备阶段进行示例行性说明,但不应理解为对本公开实施例的具体限定。如图2所示,从计算机程序执行流程的角度而言,初始化准备阶段可以包括以下操作:
S1201、调用栈回溯装置对应的程序包被调用,即初始化开始。
S1202、确定线程指针获取函数(CurrentFromGdb)。线程指针获取函数(CurrentFromGdb)用于获取程序运行过程中的当前线程指针。
S1203、确定栈帧访问者(StackVisitor)类下的构造函数(StackVisitorC)。构造函数(StackVisitorC)用于对栈帧访问者(StackVisitor)类下的指针指向的内容初始化。
S1204、确定场景(Context)类下的上下文参数获取函数(ContextCreate)。上下文参数获取函数(ContextCreate)用于获取程序运行过程中的上下文参数。
S1205、确定栈回溯函数(WalkStack)。栈回溯函数(WalkStack)用于启动调用栈的回溯。
S1206、确定方法指针获取函数(GetMethod)。方法指针获取函数(GetMethod)用于获取调用栈中栈帧的方法指针。
S1207、确定指令偏移值获取函数(GetDexPc)。指令偏移值获取函数(GetDexPc)用于获取调用栈中栈帧的指令偏移值。
S1208、确定方法描述信息获取函数(PrettyMethod)。方法描述信息获取函数(PrettyMethod)用于获取方法指针对应的描述信息。
S1209、初始化完成。
图2中所示出的各个函数是根据真正执行调用栈的回溯过程中,需要用到的函数而定,可以根据需求进行调整,例如删减或者增加等。并且,图2中所示出的各个函数的确定顺序并无严格的顺序限定,即图2中示出的函数确定顺 序仅作为一种示例。通过初始化操作确定各个函数,可以确保在栈回溯过程中成功调用各个函数(或者成功执行各个函数对应的代码段)。
S104、将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址;其中,自定义虚拟功能模块表中的第三个功能模块地址为预设目标回调功能模块的地址。
利用构造函数(StackVisitorC)对目标子区域中的存储内容进行初始化后,目标子区域中的存储内容为系统默认的虚拟功能模块表的地址(即默认的虚拟函数表的地址,一个虚拟功能模块即对应一个虚拟函数),此时的存储内容属于无效数据,如果不更改该存储内容,采用本公开实施例提供的可控栈回溯逻辑,在真正实现栈回溯过程中容易导致应用程序崩溃。因此,需要进一步对初始化后的目标子区域中的存储内容进行更改,使得目标子区域中存储自定义虚拟功能模块表的地址(即自定义的虚拟函数表的地址)。自定义虚拟功能模块表(或自定义虚拟函数表)可以在构建程序代码的过程中进行预先配置。自定义虚拟功能模块表中的第三个功能模块地址为预设目标回调功能模块callback的地址。即对初始化后的目标子区域中的存储内容进行更改后,可以通过存储内容确定预设目标回调功能模块callback的地址。
示例性地,将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址,可以包括:首先将初始化后的目标子区域中的存储内容类型强转化为指针类型,然后将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址。
从计算机程序执行的角度而言,存储内容更改后,通过存储内容可以确定预设目标回调功能模块callback的地址,而构建的栈帧访问者(StackVisitor)类下的指针(第一内存区域的首地址)可以用于确定该存储内容对应的目标子区域,因此可以理解为栈帧访问者(StackVisitor)类下的指针指向预设目标回调函数callback,从而在执行栈回溯过程中,可以成功调用预设目标回调函数callback,对回溯的栈帧数量进行有效控制。其中,预设目标回调函数callback是在构建程序代码阶段,由开发人员自定义创建的非系统函数,对于实现可控栈回溯发挥着重要作用。
图3为本公开实施例提供的一种关于目标子区域(第一内存区域的首地址对应的区域)的存储内容更改前和更改后的对比示意图,用于对本公开实施例进行示例性说明,具体地,图3中上方的子图表示目标子区域的存储内容初始化后的一种示意图,图3中下方的子图表示目标子区域的初始化存储内容更改后的一种示意图。
如图3所示,利用构造函数(StackVisitorC)对申请的第一内存区域中目标子区域中的存储内容进行初始化后,目标子区域中的存储内容为系统默认的虚拟函数表的地址(也即前述默认的虚拟功能模块表的地址,一个虚拟功能模块即对应一个虚拟函数),默认虚拟函数表中可以包括多个默认的函数地址,示例性地可以包括默认地址1(例如Destructor1地址)、默认地址2(例如Destructor2地址)、默认地址3(例如_cxa_pure_virtual地址)。对目标子区域中的初始化存储内容进行更改后,目标子区域中的存储内容为自定义的虚拟函数表的地址(即前述自定义的虚拟功能模块表的地址),自定义虚拟函数表中同样可以包括多个函数地址,例如自定义虚拟函数表中前两个函数地址与默认虚拟函数表中前两个函数地址可以相同,均为默认地址1(例如Destructor1地址)、默认地址2(例如Destructor2地址),而自定义虚拟函数表中第三个函数地址为预设目标回调函数callback的地址。
需要说明的是,预设目标回调函数callback的地址为自定义虚拟函数表中的第三个函数地址,这是由设备系统决定的,原因在于启动栈回溯时所调用的栈回溯函数WalkStack为系统函数,为了保证可控栈回溯的成功实现,预设目标回调函数callback的地址在自定义虚拟函数表中的位置不可随意设置。
S105、基于第一内存区域的首地址,执行栈回溯功能模块WalkStack对应的代码段,以启动调用栈的回溯。
从计算机程序执行流程的角度而言,执行栈回溯功能模块WalkStack对应的代码段,也即执行栈回溯函数WalkStack。具体地,可以将第一内存区域的首地址(myVisitor)作为栈回溯函数WalkStack的参数,执行栈回溯函数WalkStack,启动栈回溯。
S106、在调用栈的回溯过程中,执行预设目标回调功能模块对应的代码段, 并确定预设目标回调功能模块对应的代码段的执行次数。
继续从计算机程序执行流程的角度而言,执行预设目标回调功能模块对应的代码段,也即执行预设目标回调函数callback。在执行栈回溯函数WalkStack的过程中,每回溯出一个栈帧,便执行一次预设目标回调函数callback。预设目标回调功能模块对应的代码段的执行次数,即预设目标回调函数的执行次数,会在该函数执行过程中被同步记录,例如预设目标回调函数callback的历史执行次数为N,当前启动预设目标回调函数callback的执行后,其执行次数更新为N+1,可以调用计时器对该执行次数进行统计。该执行次数可以作为后续确定是否结束栈回溯的一个判断依据。
S107、根据预设目标回调功能模块对应的代码段的执行次数,确定是否结束调用栈的回溯。
示例性地,可以首先确定预设目标回调功能模块对应的代码段的执行次数阈值,该执行次数阈值可以是预先设置的定值,也可以是在程序运行过程中可自动化调整的一个值,本公开实施例对此不作具体限定;如果预设目标回调功能模块对应的代码段的执行次数达到执行次数阈值,则基于执行预设目标回调功能模块对应的代码段得到的目标结果,例如目标结果为false,结束调用栈的回溯;如果预设目标回调功能模块对应的代码段的执行次数未达到执行次数阈值,则基于执行预设目标回调功能模块对应的代码段得到的其他结果,例如其他结果为true,继续调用栈的回溯,从而实现合理控制栈帧的回溯数量。
进一步可选地,确定预设目标回调功能模块对应的代码段的执行次数阈值,包括:
获取预先设置的预设目标回调功能模块对应的代码段的执行次数阈值;即执行次数阈值可以是一个在构造程序代码阶段所预先设置的值;或者
获取当前运行的线程的类型,并根据线程的类型,确定预设目标回调功能模块对应的代码段的执行次数阈值;其中,线程的类型包括主线程或子线程;例如当采用现有方案中可用的线程类型确定方式确定当前运行的线程的类型为主线程,则可以将执行次数阈值设置为第一值,如果确定当前运行的线程的类型为子线程,则可以将执行次数阈值设置为第二值,第一值大于第二值,但本 公开实施例对两者的具体取值不作限定。主线程的优先级高于子线程,主线程对应的执行次数阈值较大,相应地可以执行较多栈帧的回溯,有助于获取较多的栈帧信息,子线程对应的执行次数阈值较小,相应地可以执行较少栈帧的回溯,达到合理控制栈回溯的耗时。
或者
获取应用程序的当前运行状态信息,并根据当前运行状态信息,确定预设目标回调功能模块对应的代码段的执行次数阈值;其中,当前运行状态信息包括运行正常或者运行异常。例如,如果应用程序的当前运行状态信息为运行正常,则可以将执行次数阈值设置为第三值,如果应用程序的当前运行状态信息为运行异常,则可以将执行次数阈值设置为第四值,第四值大于第三值,但本公开实施例对两者的具体取值不作限定。在程序运行正常情况下,由于不需要使用较多的栈帧信息进行应用程序的异常分析与定位,可以进行较少栈帧的回溯,但是在程序运行异常情况下,由于需要使用较多的栈帧信息进行应用程序的异常分析与定位,因此可以进行较多栈帧的回溯。
在本公开实施例中,预设目标回调功能模块对应的代码段的执行次数阈值可以采用多种方式实现灵活确定,相应地栈帧回溯数量可以灵活调整,从而实现对整个栈回溯的有效控制。
在本公开实施例中,首先申请第一内存区域,并确定第一内存区域的首地址对应的目标子区域,然后通过内容初始化和存储内容更改操作,实现将目标子区域中的存储内容确定为自定义虚拟功能模块表的地址,该表中的第三个功能模块地址为预设目标回调功能模块的地址;其次,基于第一内存区域的首地址,执行栈回溯功能模块WalkStack对应的代码段,以启动调用栈的回溯,在具体执行栈回溯功能模块WalkStack对应的代码段的过程中,可以通过第一内存区域的首地址,找到预设目标回调功能模块的地址,从而在调用栈的回溯过程中,执行预设目标回调功能模块对应的代码段,并确定其执行次数;进而根据该执行次数,确定是否结束调用栈的回溯。本公开实施例解决了现有栈回溯方案中只能实现全量栈回溯导致栈回溯耗时的问题,实现了在程序运行过程中对栈回溯的有效控制,达到了控制栈回溯次数的效果,降低了栈回溯的耗时。
在上述技术方案的基础上,可选地,本公开实施例提供的调用栈回溯方法还可以包括:在执行预设目标回调功能模块对应的代码段的过程中,基于第一内存区域的首地址,执行目标信息获取功能模块对应的代码段,以获取目标信息;其中,目标信息获取功能模块包括方法指针获取功能模块GetMethod和/或指令偏移值获取功能模块GetDexPc,相应地,目标信息包括方法指针ArtMethod和/或方法指针对应的指令偏移值DexPc。
从计算机程序执行流程的角度而言,执行预设目标回调功能模块对应的代码段即执行预设目标回调函数,执行目标信息获取功能模块对应的代码段即执行目标信息获取函数。目标信息获取函数属于在预设目标回调函数的执行过程中被嵌套执行的函数。
具体地,每个回溯出一个栈帧,在执行预设目标回调函数的过程中,可以基于第一内存区域的首地址确定第一内存区域中的目标子区域,进而可以根据目标子区域中存储的自定义虚拟函数表,确定预设目标回调函数的地址,使得目标信息获取函数保持在预设目标回调函数的执行周期中执行。针对每个栈帧,在执行预设目标回调函数的过程中,通过执行方法指针获取函数GetMethod和/或指令偏移值获取函数GetDexPc,可以相应得到栈帧的方法指针ArtMethod和/或该方法指针对应的指令偏移值DexPc。并且,方法指针ArtMethod可以用于获取栈帧的方法名、栈帧方法所在类名、栈帧对应的源码文件名中的至少之一,指令偏移值DexPc可以用于获取栈帧对应的源码行号。
本公开实施例通过合理控制栈帧的回溯数量,针对性地获取有限数量的栈帧的信息,极大节省了栈回溯的时间,提高了栈回溯的效率。
在一种可选实施方式中,在执行方法指针获取功能模块GetMethod对应的代码段之后,本公开实施例提供的调用栈回溯方法还包括:基于获取的方法指针,执行方法描述信息获取功能模块PrettyMethod对应的代码段,获取方法指针对应的描述信息。继续从计算机程序执行流程的角度而言,执行方法描述信息获取功能模块PrettyMethod对应的代码段,即执行方法描述信息获取函数PrettyMethod,可以获取当前方法指针对应的描述信息(或称为字符串描述信息strArtMethod),包括但不限于方法的返回值类型、方法的参数类型、方法所在 类名、方法名等。
进一步地,方法指针对应的描述信息包括方法指针对应的方法所在类名,本公开实施例提供的调用栈回溯方法还包括:
获取预先确定的类名列表;其中,类名列表用于记录无需获取指令偏移值的方法指针对应的类名;示例性地,该类名列表可以是在构造程序代码的过程中预先设置,也可以在程序运行过程中按照特定的类名筛选规则进行确定,类名筛选规则的具体内容可以灵活设置,本公开实施例不作具体限定,例如按照程序运行过程中涉及的类名的优先级,将优先级较低的类名记录在该类名列表中;
将描述信息中包括的方法所在类名与类名列表进行匹配,以在匹配不成功的情况下,执行指令偏移值获取功能模块GetDexPc对应的代码段。
针对在先执行方法指针获取函数GetMethod,在后执行指令偏移值获取函数GetDexPc的情况,执行方法描述信息获取函数PrettyMethod得到的描述信息中,如果包括的方法所在类名与类名列表匹配成功,则表示当前描述信息中包括的方法所在类名下的方法指针,不需要进一步获取对应的指令偏移值;如果描述信息中包括的方法所在类名与类名列表匹配不成功,则表示当前描述信息中包括的方法所在类名下的方法指针,需要进一步获取对应的指令偏移值。即本公开实施例可以通过获取的方法所在类名与类名列表,对指令偏移值获取功能模块GetDexPc对应的代码段的执行情况(或称为对指令偏移值获取函数GetDexPc的执行情况)进行再次地控制,减少对非关注信息的获取,减少对计算资源的占用,进一步有助于提高栈回溯的效率。
在一种可选实施方式中,基于第一内存区域的首地址,执行目标信息获取功能模块对应的代码段,包括:
基于预设目标回调功能模块对应的代码段的执行次数,确定当前回溯的栈帧的层级;
如果当前回溯的栈帧的层级属于预设需求层级,则基于第一内存区域的首地址,执行目标信息获取功能模块对应的代码段,预设需求层级用于定义需要获取栈帧信息的栈帧所属的层级。
在本公开实施例中,预设目标回调功能模块对应的代码段的执行次数(或者预设目标回调函数的执行次数)与当前回溯的栈帧的层级存在对应关系,例如预设目标回调函数的执行次数为N,则表示当前回溯的栈帧的层级为N。如果当前回溯的栈帧的层级属于预设需求层级,则执行目标信息获取功能模块对应的代码段,以实现获取栈帧的目标信息,否则不需要执行。预设需求层级可以在构造程序代码阶段进行预先确定,也可以在程序运行过程中按照设定规则进行动态确定,设定规则可以根据不同的应用程序进行适应性确定,本公开实施例不作具体限定,例如可以在应用程序运行过程中,将调用栈中属于偶数或奇数的栈帧层级确定为预设需求层级等。
本公开实施例不仅可以实现有效控制回溯的栈帧的数量,提高栈回溯效率,还可以实现获取调用栈中非连续的若干个栈帧的信息。
示例性地,在当前栈回溯过程中,只关注调用栈中第3个栈帧和第5个栈帧的详细信息,预设需求层级的最大值决定了栈帧回溯的数量,即当前需要执行的栈回溯次数为5次,也即预设目标回调功能模块对应的代码段的执行次数为5次,则在执行预设目标回调功能模块对应的代码段的过程中,可以基于预设目标回调功能模块对应的代码段的执行次数,确定当前回溯的栈帧的层级,并在回溯出第3个栈帧和第5个栈帧的情况下,才会执行目标信息获取功能模块对应的代码段,同时在回溯出第5个栈帧之后停止栈回溯。此外,如果目标信息获取功能模块包括方法指针获取功能模块GetMethod,则在执行方法指针获取功能模块GetMethod对应的代码段后,还可以执行方法描述信息获取功能模块PrettyMethod对应的代码段,以获取方法指针对应的描述信息。
虽然在整个栈回溯过程中,对于第1个、第2个、第4个栈帧,也进行了回溯,但是并没有执行目标信息获取功能模块对应的代码段,以获取这些栈帧的详细信息,相比于获取全部5个栈帧的详细信息的情况,选择性地获取2个栈帧的详细信息的执行时间更短,因而能够更快地完成栈回溯。
图4为本公开实施例提供的另一种调用栈回溯方法的流程图,用于从计算机程序执行流程的角度,对本公开实施例进行示例性说明,但不应理解为对本公开实施例的具体限定。并且,在构造程序代码过程中,本公开实施例中自定 义的预设目标回调函数callback的函数名可以示例性地采用myVisitFrame表示。
如图4所示,本公开实施例提供的调用栈回溯方法可以包括:
S401、开始。
S402、调用线程指针获取函数CurrentFromGdb,并确定其返回值,即当前线程指针currentThread。
S403、调用上下文参数获取函数ContextCreate,并确定其返回值,即上下文参数context。
S404、申请一块连续内存区域,并确定该内存区域的首地址myVisitor。
S405、将内存区域的首地址myVisitor作为第一个参数,将当前线程指针currentThread作为第二个参数,将上下文参数context作为第三个参数,调用构造函数StackVisitorC,将内存区域的首地址myVisitor构造成真正的访问者Visitor对象(或称为指针)。
S406、将指针myVisitor对应的目标子区域中存储内容的类型强转为指针类型,并更改该指针myVisitor指向的内容,使其指向自定义的虚拟函数表,自定义的虚拟函数表中第三个指针指向自定义的目标回调函数myVisitFrame。
S407、将指针myVisitor作为第一参数调用栈回溯函数WalkStack。
通过调用栈回溯函数WalkStack,启动调用栈的回溯。
S408、确定是否回溯出一个栈帧。
如果是,则执行S409,如果否,则结束流程。
S409、调用自定义的目标回调函数myVisitFrame。
以下操作S410-S416属于在目标回调函数myVisitFrame的执行过程中需执行的操作。
S410、将指针myVisitor作为参数调用方法指针获取函数GetMetho,并获取其返回值,即当前栈帧的方法指针ArtMethod。
S411、将当前方法指针ArtMethod作为参数调用方法描述信息获取函数PrettyMethod,并获取其返回值,即当前方法指针ArtMethod的字符串描述信息strArtMethod。
S412、将指针myVisitor作为参数调用指令偏移值获取函数GetDexPc,并获取其返回值,即当前栈帧的指令偏移值DexPc。
S413、记录当前栈帧的方法指针ArtMethod和指令偏移值DexPc,作为当前栈帧的信息。
S414、确定回溯的栈帧数量是否达到目标数量。
其中,目标数量即目标回调函数myVisitFrame的执行次数阈值。如果确定回溯的栈帧数量未达到目标数量,则确定目标回调函数myVisitFrame的返回值为true,表示调用栈的回溯继续执行;如果确定回溯的栈帧数量达到目标数量,则确定目标回调函数myVisitFrame的返回值为false,表示调用栈的回溯可以结束。
S415、返回false。
S416、返回true。
S417、确定自定义的目标回调函数myVisitFrame的返回值是否为true。
如果确定返回值为true,则继续执行栈回溯;如果确定返回值不是true,而是false,则结束栈回溯。
S418、结束。
需要说明的是,操作S410、S411和S412的执行,可以根据栈帧信息的获取需求而定,图4作为示例,示出了三个操作的执行,但不应理解为对本公开实施例的具体限定。在具体应用中,可以根据需求对三个操作进行选择性执行。
本公开实施例通过调用系统中栈回溯的一部分步骤,以及更改虚拟函数表的方式,解决了现有栈回溯方案中只能实现全量栈回溯导致栈回溯耗时的问题,实现了在程序运行过程中对栈回溯的有效控制,达到了控制栈回溯次数的效果,降低了栈回溯的耗时。
图5为本公开实施例提供的一种调用栈回溯装置的结构示意图,该装置可以采用软件和/或硬件实现,并可集成在具有计算能力的电子设备上,例如移动终端、笔记本、电脑等。
如图5所示,本公开实施例提供的调用栈回溯装置500可以包括内存区域申请模块501、参数获取模块502、内容初始化模块503、内容更改模块504、 第一执行模块505、第二执行模块506和栈回溯确定模块507,其中:
内存区域申请模块501,用于申请第一内存区域,并确定第一内存区域中的目标子区域;其中,目标子区域是第一内存区域的首地址对应的区域;
参数获取模块502,用于获取上下文参数和当前线程指针;
内容初始化模块503,用于基于第一内存区域的首地址、上下文参数和当前线程指针,执行构造功能模块对应的代码段,对目标子区域中存储的内容进行初始化;
内容更改模块504,用于将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址;其中,自定义虚拟功能模块表中的第三个功能模块地址为预设目标回调功能模块的地址;
第一执行模块505,用于基于第一内存区域的首地址,执行栈回溯功能模块WalkStack对应的代码段,以启动调用栈的回溯;
第二执行模块506,用于在调用栈的回溯过程中,执行预设目标回调功能模块对应的代码段,并确定预设目标回调功能模块对应的代码段的执行次数;
栈回溯确定模块507,用于根据预设目标回调功能模块对应的代码段的执行次数,确定是否结束调用栈的回溯。
可选地,本公开实施例提供的调用栈回溯装置500还包括:
第三执行模块,用于在执行预设目标回调功能模块对应的代码段的过程中,基于第一内存区域的首地址,执行目标信息获取功能模块对应的代码段,以获取目标信息;其中,目标信息获取功能模块包括方法指针获取功能模块GetMethod和/或指令偏移值获取功能模块GetDexPc。
可选地,本公开实施例提供的调用栈回溯装置500还包括:
执行次数阈值确定模块,用于确定预设目标回调功能模块对应的代码段的执行次数阈值;
相应地,栈回溯确定模块507具体用于:
如果预设目标回调功能模块对应的代码段的执行次数达到执行次数阈值,则基于执行预设目标回调功能模块对应的代码段得到的目标结果,结束调用栈的回溯。
可选地,执行次数阈值确定模块包括:
获取单元,用于获取预先设置的预设目标回调功能模块对应的代码段的执行次数阈值;
或者
第一确定单元,用于获取当前运行的线程的类型,并根据线程的类型,确定预设目标回调功能模块对应的代码段的执行次数阈值;其中,线程的类型包括主线程或子线程;
或者
第二确定单元,用于获取应用程序的当前运行状态信息,并根据当前运行状态信息,确定预设目标回调功能模块对应的代码段的执行次数阈值;其中,当前运行状态信息包括运行正常或者运行异常。
可选地,本公开实施例提供的调用栈回溯装置500还包括:
第四执行模块,用于在第三执行模块执行方法指针获取功能模块GetMethod对应的代码段之后,基于获取的方法指针,执行方法描述信息获取功能模块PrettyMethod对应的代码段,获取方法指针对应的描述信息。
可选地,方法指针对应的描述信息包括方法指针对应的方法所在类名,本公开实施例提供的调用栈回溯装置500还包括:
类名列表获取模块,用于获取预先确定的类名列表;其中,类名列表用于记录无需获取指令偏移值的方法指针对应的类名;
类名匹配模块,用于将描述信息中包括的方法所在类名与类名列表进行匹配,以在匹配不成功的情况下,执行指令偏移值获取功能模块GetDexPc对应的代码段。
可选地,第二执行模块506包括:
栈帧层级确定单元,用于基于预设目标回调功能模块对应的代码段的执行次数,确定当前回溯的栈帧的层级;
代码执行单元,用于如果当前回溯的栈帧的层级属于预设需求层级,则基于第一内存区域的首地址,执行目标信息获取功能模块对应的代码段。
本公开实施例所提供的调用栈回溯装置可执行本公开实施例所提供的任意 调用栈回溯方法,具备执行方法相应的功能模块和有益效果。本公开装置实施例中未详尽描述的内容可以参考本公开任意方法实施例中的描述。
图6为本公开实施例提供的一种电子设备的结构示意图,用于对实现本公开实施例提供的调用栈回溯方法的电子设备进行示例性说明。本公开实施例中的电子设备可以包括但不限于诸如移动电话、笔记本电脑、数字广播接收器、PDA(个人数字助理)、PAD(平板电脑)、PMP(便携式多媒体播放器)、车载终端(例如车载导航终端)等等的移动终端以及诸如数字TV、台式计算机、智能家居设备、可穿戴电子设备、服务器等等的固定终端。图6示出的电子设备仅仅是一个示例,不应对本公开实施例的功能和占用范围带来任何限制。
如图6所示,电子设备600包括一个或多个处理器601和存储器602。处理器601可以是中央处理单元(CPU)或者具有数据处理能力和/或指令执行能力的其他形式的处理单元,并且可以控制电子设备600中的其他组件以执行期望的功能。
存储器602可以包括一个或多个计算机程序产品,计算机程序产品可以包括各种形式的计算机可读存储介质,例如易失性存储器和/或非易失性存储器。易失性存储器例如可以包括随机存取存储器(RAM)和/或高速缓冲存储器(cache)等。非易失性存储器例如可以包括只读存储器(ROM)、硬盘、闪存等。在计算机可读存储介质上可以存储一个或多个计算机程序指令,处理器601可以运行程序指令,以实现本公开实施例提供的调用栈回溯方法,还可以实现其他期望的功能。在计算机可读存储介质中还可以存储诸如输入信号、信号分量、噪声分量等各种内容。
其中,本公开实施例提供的调用栈回溯方法可以包括:申请第一内存区域,并确定第一内存区域中的目标子区域;其中,目标子区域是第一内存区域的首地址对应的区域;获取上下文参数和当前线程指针;基于第一内存区域的首地址、上下文参数和当前线程指针,执行构造功能模块对应的代码段,对目标子区域中存储的内容进行初始化;将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址;其中,自定义虚拟功能模块表中的第三个功能模块地址为预设目标回调功能模块的地址;基于第一内存区域的首地址,执行 栈回溯功能模块WalkStack对应的代码段,以启动调用栈的回溯;在调用栈的回溯过程中,执行预设目标回调功能模块对应的代码段,并确定预设目标回调功能模块对应的代码段的执行次数;根据预设目标回调功能模块对应的代码段的执行次数,确定是否结束调用栈的回溯。应当理解,电子设备600还可以执行本公开方法实施例提供的其他可选实施方案。
在一个示例中,电子设备600还可以包括:输入装置603和输出装置604,这些组件通过总线系统和/或其他形式的连接机构(未示出)互连。
此外,该输入装置603还可以包括例如键盘、鼠标等等。
该输出装置604可以向外部输出各种信息,包括确定出的距离信息、方向信息等。该输出装置604可以包括例如显示器、扬声器、打印机、以及通信网络及其所连接的远程输出设备等等。
当然,为了简化,图6中仅示出了该电子设备600中与本公开有关的组件中的一些,省略了诸如总线、输入/输出接口等等的组件。除此之外,根据具体应用情况,电子设备600还可以包括任何其他适当的组件。
除了上述方法和设备以外,本公开实施例还提供一种计算机程序产品,其包括计算机程序或计算机程序指令,计算机程序或计算机程序指令在被计算设备执行时使得计算设备实现本公开实施例所提供的任意调用栈回溯方法。
计算机程序产品可以以一种或多种程序设计语言的任意组合来编写用于执行本公开实施例操作的程序代码,程序设计语言包括面向对象的程序设计语言,诸如Java、C++等,还包括常规的过程式程序设计语言,诸如“C”语言或类似的程序设计语言。程序代码可以完全地在用户电子设备上执行、部分地在用户电子设备上执行、作为一个独立的软件包执行、部分在用户电子设备上且部分在远程电子设备上执行、或者完全在远程电子设备上执行。
此外,本公开实施例还可以提供一种计算机可读存储介质,其上存储有计算机程序指令,计算机程序指令在被计算设备执行时使得计算设备实现本公开实施例所提供的任意调用栈回溯方法。
其中,本公开实施例提供的调用栈回溯方法可以包括:申请第一内存区域,并确定第一内存区域中的目标子区域;其中,目标子区域是第一内存区域的首 地址对应的区域;获取上下文参数和当前线程指针;基于第一内存区域的首地址、上下文参数和当前线程指针,执行构造功能模块对应的代码段,对目标子区域中存储的内容进行初始化;将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址;其中,自定义虚拟功能模块表中的第三个功能模块地址为预设目标回调功能模块的地址;基于第一内存区域的首地址,执行栈回溯功能模块WalkStack对应的代码段,以启动调用栈的回溯;在调用栈的回溯过程中,执行预设目标回调功能模块对应的代码段,并确定预设目标回调功能模块对应的代码段的执行次数;根据预设目标回调功能模块对应的代码段的执行次数,确定是否结束调用栈的回溯。应当理解,计算机程序指令在被计算设备执行时,还可以使得计算设备实现本公开方法实施例提供的其他可选实施方案。
计算机可读存储介质可以采用一个或多个可读介质的任意组合。可读介质可以是可读信号介质或者可读存储介质。可读存储介质例如可以包括但不限于电、磁、光、电磁、红外线、或半导体的系统、装置或器件,或者任意以上的组合。可读存储介质的更具体的例子(非穷举的列表)包括:具有一个或多个导线的电连接、便携式盘、硬盘、随机存取存储器(RAM)、只读存储器(ROM)、可擦式可编程只读存储器(EPROM或闪存)、光纤、便携式紧凑盘只读存储器(CD-ROM)、光存储器件、磁存储器件、或者上述的任意合适的组合。
需要说明的是,在本文中,诸如“第一”和“第二”等之类的关系术语仅仅用来将一个实体或者操作与另一个实体或操作区分开来,而不一定要求或者暗示这些实体或操作之间存在任何这种实际的关系或者顺序。而且,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法、物品或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法、物品或者设备所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括要素的过程、方法、物品或者设备中还存在另外的相同要素。
以上仅是本公开的具体实施方式,使本领域技术人员能够理解或实现本公开。对这些实施例的多种修改对本领域的技术人员来说将是显而易见的,本文中所定义的一般原理可以在不脱离本公开的精神或范围的情况下,在其它实施 例中实现。因此,本公开将不会被限制于本文的这些实施例,而是要符合与本文所公开的原理和新颖特点相一致的最宽的范围。
Claims (10)
- 一种调用栈回溯方法,包括:申请第一内存区域,并确定所述第一内存区域中的目标子区域;其中,所述目标子区域是所述第一内存区域的首地址对应的区域;获取上下文参数和当前线程指针;基于所述第一内存区域的首地址、所述上下文参数和所述当前线程指针,执行构造功能模块对应的代码段,对所述目标子区域中存储的内容进行初始化;将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址;其中,所述自定义虚拟功能模块表中的第三个功能模块地址为预设目标回调功能模块的地址;基于所述第一内存区域的首地址,执行栈回溯功能模块WalkStack对应的代码段,以启动调用栈的回溯;在所述调用栈的回溯过程中,执行所述预设目标回调功能模块对应的代码段,并确定所述预设目标回调功能模块对应的代码段的执行次数;根据所述预设目标回调功能模块对应的代码段的执行次数,确定是否结束所述调用栈的回溯。
- 根据权利要求1所述的方法,其中,还包括:在执行所述预设目标回调功能模块对应的代码段的过程中,基于所述第一内存区域的首地址,执行目标信息获取功能模块对应的代码段,以获取目标信息;其中,所述目标信息获取功能模块包括方法指针获取功能模块GetMethod和/或指令偏移值获取功能模块GetDexPc。
- 根据权利要求1所述的方法,其中,还包括:确定所述预设目标回调功能模块对应的代码段的执行次数阈值;相应地,根据所述预设目标回调功能模块对应的代码段的执行次数,确定是否结束所述调用栈的回溯,包括:如果所述预设目标回调功能模块对应的代码段的执行次数达到所述执行次数阈值,则基于执行所述预设目标回调功能模块对应的代码段得到的目标结果,结束所述调用栈的回溯。
- 根据权利要求3所述的方法,其中,确定所述预设目标回调功能模块对应的代码段的执行次数阈值,包括:获取预先设置的所述预设目标回调功能模块对应的代码段的执行次数阈值;或者获取当前运行的线程的类型,并根据所述线程的类型,确定所述预设目标回调功能模块对应的代码段的执行次数阈值;其中,所述线程的类型包括主线程或子线程;或者获取应用程序的当前运行状态信息,并根据所述当前运行状态信息,确定所述预设目标回调功能模块对应的代码段的执行次数阈值;其中,所述当前运行状态信息包括运行正常或者运行异常。
- 根据权利要求2所述的方法,其中,在执行所述方法指针获取功能模块GetMethod对应的代码段之后,还包括:基于获取的方法指针,执行方法描述信息获取功能模块PrettyMethod对应的代码段,获取所述方法指针对应的描述信息。
- 根据权利要求5所述的方法,其中,所述描述信息包括所述方法指针对应的方法所在类名,所述方法还包括:获取预先确定的类名列表;其中,所述类名列表用于记录无需获取指令偏移值的方法指针对应的类名;将所述描述信息中包括的方法所在类名与所述类名列表进行匹配,以在匹配不成功的情况下,执行所述指令偏移值获取功能模块GetDexPc对应的代码段。
- 根据权利要求1所述的方法,其中,基于所述第一内存区域的首地址,执行目标信息获取功能模块对应的代码段,包括:基于所述预设目标回调功能模块对应的代码段的执行次数,确定当前回溯的栈帧的层级;如果所述当前回溯的栈帧的层级属于预设需求层级,则基于所述第一内存 区域的首地址,执行目标信息获取功能模块对应的代码段。
- 一种调用栈回溯装置,包括:内存区域申请模块,用于申请第一内存区域,并确定所述第一内存区域中的目标子区域;其中,所述目标子区域是所述第一内存区域的首地址对应的区域;参数获取模块,用于获取上下文参数和当前线程指针;内容初始化模块,用于基于所述第一内存区域的首地址、所述上下文参数和所述当前线程指针,执行构造功能模块对应的代码段,对所述目标子区域中存储的内容进行初始化;内容更改模块,用于将初始化后的目标子区域中的存储内容更改为自定义虚拟功能模块表的地址;其中,所述自定义虚拟功能模块表中的第三个功能模块地址为预设目标回调功能模块的地址;第一执行模块,用于基于所述第一内存区域的首地址,执行栈回溯功能模块WalkStack对应的代码段,以启动调用栈的回溯;第二执行模块,用于在所述调用栈的回溯过程中,执行所述预设目标回调功能模块对应的代码段,并确定所述预设目标回调功能模块对应的代码段的执行次数;栈回溯确定模块,用于根据所述预设目标回调功能模块对应的代码段的执行次数,确定是否结束所述调用栈的回溯。
- 一种电子设备,包括存储器和处理器,其中,所述存储器中存储有计算机程序,当所述计算机程序被所述处理器执行时,使得所述电子设备实现权利要求1-7中任一项所述的调用栈回溯方法。
- 一种计算机可读存储介质,所述存储介质中存储有计算机程序,当所述计算机程序被计算设备执行时,使得所述计算设备实现权利要求1-7中任一项所述的调用栈回溯方法。
Applications Claiming Priority (2)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110303889.0 | 2021-03-22 | ||
CN202110303889.0A CN112925718B (zh) | 2021-03-22 | 2021-03-22 | 调用栈回溯方法、装置、设备和介质 |
Publications (1)
Publication Number | Publication Date |
---|---|
WO2022199284A1 true WO2022199284A1 (zh) | 2022-09-29 |
Family
ID=76175398
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
PCT/CN2022/076545 WO2022199284A1 (zh) | 2021-03-22 | 2022-02-17 | 调用栈回溯方法、装置、设备和介质 |
Country Status (2)
Country | Link |
---|---|
CN (1) | CN112925718B (zh) |
WO (1) | WO2022199284A1 (zh) |
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116521576A (zh) * | 2023-05-11 | 2023-08-01 | 上海合见工业软件集团有限公司 | Eda软件数据处理系统 |
CN117195204A (zh) * | 2023-11-01 | 2023-12-08 | 建信金融科技有限责任公司 | 异常数据检测方法、装置、电子设备及计算机可读介质 |
CN117827279A (zh) * | 2023-12-13 | 2024-04-05 | 天翼云科技有限公司 | 一种适用于arm处理器thumb模式的栈回溯方法 |
Families Citing this family (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN112925718B (zh) * | 2021-03-22 | 2023-10-20 | 北京字节跳动网络技术有限公司 | 调用栈回溯方法、装置、设备和介质 |
Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20080126767A1 (en) * | 2006-06-22 | 2008-05-29 | Thomas Michael Gooding | Method and Apparatus for Obtaining Stack Traceback Data for Multiple Computing Nodes of a Massively Parallel Computer System |
CN101539883A (zh) * | 2009-05-05 | 2009-09-23 | 北京和利时系统工程有限公司 | 嵌入式系统的错误追踪方法和装置 |
CN108984328A (zh) * | 2017-05-31 | 2018-12-11 | 中兴通讯股份有限公司 | 一种堆栈回溯的方法及装置 |
CN110502357A (zh) * | 2019-07-09 | 2019-11-26 | 北京字节跳动网络技术有限公司 | 一种栈回溯方法、装置、介质和设备 |
CN112925718A (zh) * | 2021-03-22 | 2021-06-08 | 北京字节跳动网络技术有限公司 | 调用栈回溯方法、装置、设备和介质 |
Family Cites Families (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN103839007B (zh) * | 2014-03-03 | 2016-08-31 | 珠海市君天电子科技有限公司 | 一种检测异常线程的方法及系统 |
CN109901915A (zh) * | 2019-01-18 | 2019-06-18 | 江苏艾佳家居用品有限公司 | 一种分布式服务框架中的全链路堆栈信息跟踪方法 |
US11294682B2 (en) * | 2019-05-20 | 2022-04-05 | Microsoft Technology Licensing, Llc | Stack traces using shadow stack |
-
2021
- 2021-03-22 CN CN202110303889.0A patent/CN112925718B/zh active Active
-
2022
- 2022-02-17 WO PCT/CN2022/076545 patent/WO2022199284A1/zh active Application Filing
Patent Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20080126767A1 (en) * | 2006-06-22 | 2008-05-29 | Thomas Michael Gooding | Method and Apparatus for Obtaining Stack Traceback Data for Multiple Computing Nodes of a Massively Parallel Computer System |
CN101539883A (zh) * | 2009-05-05 | 2009-09-23 | 北京和利时系统工程有限公司 | 嵌入式系统的错误追踪方法和装置 |
CN108984328A (zh) * | 2017-05-31 | 2018-12-11 | 中兴通讯股份有限公司 | 一种堆栈回溯的方法及装置 |
CN110502357A (zh) * | 2019-07-09 | 2019-11-26 | 北京字节跳动网络技术有限公司 | 一种栈回溯方法、装置、介质和设备 |
CN112925718A (zh) * | 2021-03-22 | 2021-06-08 | 北京字节跳动网络技术有限公司 | 调用栈回溯方法、装置、设备和介质 |
Cited By (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116521576A (zh) * | 2023-05-11 | 2023-08-01 | 上海合见工业软件集团有限公司 | Eda软件数据处理系统 |
CN116521576B (zh) * | 2023-05-11 | 2024-03-08 | 上海合见工业软件集团有限公司 | Eda软件数据处理系统 |
CN117195204A (zh) * | 2023-11-01 | 2023-12-08 | 建信金融科技有限责任公司 | 异常数据检测方法、装置、电子设备及计算机可读介质 |
CN117195204B (zh) * | 2023-11-01 | 2024-02-13 | 建信金融科技有限责任公司 | 异常数据检测方法、装置、电子设备及计算机可读介质 |
CN117827279A (zh) * | 2023-12-13 | 2024-04-05 | 天翼云科技有限公司 | 一种适用于arm处理器thumb模式的栈回溯方法 |
Also Published As
Publication number | Publication date |
---|---|
CN112925718A (zh) | 2021-06-08 |
CN112925718B (zh) | 2023-10-20 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
WO2022199284A1 (zh) | 调用栈回溯方法、装置、设备和介质 | |
WO2022199283A1 (zh) | 用于确定调用栈栈帧的对象的方法、装置、设备和介质 | |
WO2018077085A1 (zh) | 应用程序处理方法、装置及存储介质 | |
CN108958844A (zh) | 一种应用程序的控制方法及终端 | |
CN110515871B (zh) | 一种中断方法、装置及fpga和存储介质 | |
US11237761B2 (en) | Management of multiple physical function nonvolatile memory devices | |
CN110096388A (zh) | 一种数据备份的方法、装置及计算机存储介质 | |
CN111708431B (zh) | 人机交互方法、装置、头戴显示设备及存储介质 | |
CN105740330B (zh) | 分页展示数据的方法及装置 | |
WO2017166036A1 (zh) | 一种资源统计方法、装置及终端 | |
US20160357473A1 (en) | Electronic device and method of managing memory of electronic device | |
CN112817657A (zh) | 一种应用程序启动项加载方法、装置、系统及存储介质 | |
US8910136B2 (en) | Generating code that calls functions based on types of memory | |
CN111158820A (zh) | 控件点击事件处理方法、装置、电子设备及存储介质 | |
JP2024521963A (ja) | データアクセス方法、装置及び非一時的なコンピュータ可読記憶媒体 | |
CN114527983A (zh) | 一种基于中间件的接口适配方法、装置及电子设备 | |
CN112667246A (zh) | 应用功能扩展的方法、装置及电子设备 | |
WO2023274135A1 (zh) | 一种数据的处理系统、方法以及装置 | |
CN110717050A (zh) | 一种知识图谱数据库的访问方法及装置 | |
CN114048415A (zh) | 表单生成方法及装置、电子设备和计算机可读存储介质 | |
CN116670661A (zh) | 图形处理器的缓存访问方法、图形处理器及电子设备 | |
WO2024067348A2 (zh) | 内存分配器确定方法、装置、电子设备及存储介质 | |
WO2022199331A1 (zh) | 用于获取调用栈中栈帧信息的方法、装置、设备和介质 | |
WO2022262623A1 (zh) | 一种数据交换方法及装置 | |
CN112015751A (zh) | 数据查询方法及相关设备 |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
121 | Ep: the epo has been informed by wipo that ep was designated in this application |
Ref document number: 22773946 Country of ref document: EP Kind code of ref document: A1 |
|
NENP | Non-entry into the national phase |
Ref country code: DE |
|
32PN | Ep: public notification in the ep bulletin as address of the adressee cannot be established |
Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205A DATED 120224) |
|
122 | Ep: pct application non-entry in european phase |
Ref document number: 22773946 Country of ref document: EP Kind code of ref document: A1 |