Problem Diagnosis

Last Updated on : 2023-04-25 03:12:12download

TuyaOS provides a Core Dump tool to capture the stack trace when a segmentation fault (segfault) occurs, helping you find the function call that caused the problem.

This tool is only applicable to the chip platforms that Tuya has adapted. Its usage on non-adapted platforms is not verified. It is recommended to simulate segfaults to test if this tool works for your platform. If it does not work, use your platform-specific debugger instead.

This topic describes how to implement the problem diagnosis feature with TuyaOS Gateway Development Framework.


A device might be restarted due to a segfault in a program. If a segfault occurs during the running of business logic, the corresponding feature might not work. In some cases, exceptions are not found in the testing stages but after going live.

For devices running on Linux, you can enable core dumps to create a .core file when various errors occur. You can use GDB to read this file to track function calls and identify the line of code that caused the problem. However, accumulative .core files will use a lot of storage space. Generally, for IoT devices, core dumps are enabled only in the debugging stages.

Given this, TuyaOS provides a lightweight Core Dump tool to help you track down program errors on deployed devices.

How it works

The SDK handles exception signals. When an exception in the program occurs, the SDK saves the stack trace to a file that is only a few KB in size. You can use the Core Dump tool to examine the stack trace and determine the function that caused the problem by reviewing the function at the top of the stack and the function call stack.

Generally, the executable program does not come with a symbol table. But, be sure to use the -g option to compile an additional program with debugging information that can be read by the Core Dump tool for stack trace analysis.

Development guide

After gateway initialization, call tuya_gw_app_debug_start to enable the problem diagnosis feature to capture the stack trace when exceptions occur in the program.

When you implement local logs in Log Management, package the stack trace file into the log file so that you can get the stack information at the time of a crash.


int main(int argc, char **argv)

    // TuyaOS

    // Set authorization information.

    // Gateway pre-initialization.

    // Gateway initialization.

    // Gateway startup.

    // Enable problem diagnosis. The parameter is the directory where the stack trace file resides.

    while (1) {

    return OPRT_OK;

When a segfault occurs, copy the stack trace file and the program compiled with debug information to the directory of the Core Dump tool and run Core Dump for analysis. Note that the name of the debugging program must be the same as the executable program.

Run the following command to use Core Dump:

python3 -d <dump file>

Source code

import argparse
import os

parser = argparse.ArgumentParser(description='SDK Coredump Analyzer')
    '-d', '--dump_file', required=True, type=str, help='crash dump file')
args = parser.parse_args()

sys_so = ["", "libc-", "libpthread-", "", "ld-", "", "stdc++", "uClibc", "libgcc"]

crash dump file format:
stack dump:
00000c00 00000001 7fd10000 00000001
stack dump End
dump text section
00400000-00897000 r-xp 00000000 00:08  237597    /var/tmp/tyZ3Gw
def parse_dump_file(filename):
    is_stack = False
    is_text = False
    stack = []
    text = {}

    if not os.path.isfile(filename):
        return stack, text

    with open(filename, 'r') as f:
        for line in f:
            if line.find("stack dump:") != -1:
                is_stack = True

            if line.find("stack dump End") != -1:
                is_stack = False

            if line.find("dump text section") != -1:
                is_text = True

            if is_stack:

            if is_text and line.find("r-xp") != -1:
                text_content = line.split()
                if len(text_content) != 6:
                    print("parse text section error")

                addr = text_content[0]
                path = text_content[-1]
                filename = os.path.basename(path)

                # Filter system so
                is_omit = False
                for so_name in sys_so:
                    if filename.find(so_name) != -1:
                        is_omit = True

                if is_omit:

                addr_range = addr.split('-')
                if len(addr_range) != 2:

                text[filename] = addr_range

    return stack, text

def dump_addr2line(stack, text):
    for addr in stack:
        addr = int(addr, 16)
        for name in text:
            addr_start = int(text[name][0], 16)
            addr_end = int(text[name][1], 16)
            if addr >= addr_start and addr <= addr_end:
                # Shared object need to offset
                if name.find(".so") != -1:
                    addr = addr - addr_start
                addr = str(hex(addr))
                if not os.path.exists(name):
                    print("{} is not found".format(name))
                os.system('addr2line {} -e {} -f'.format(addr, name))

def main():
    dump_file = args.dump_file
    print("crash dump file: {}".format(dump_file))
    stack, text = parse_dump_file(dump_file)
    dump_addr2line(stack, text)

if __name__ == '__main__':

Example of parsing

kyson@LAPTOP-ORFJBPHU:~/workspace/tuya/tools/crash_dump$ python3 -d 959_user_iot_1645100484
crash dump file: 959_user_iot_1645100484

Stack trace analysis prints the stack trace at the time of a segfault. In the print output, you can focus on the function at the top of the stack and review other information as needed. The above example shows the segfault occurs in the function emberAfSendDefaultResponseWithCallback. With the context, you can then identify the line of code that caused the segfault.