Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "scd_core.h"
12 : #include "scd_process.h"
13 : #include "scd_log.h"
14 : #include "scd_util.h"
15 : #include "stacktrace_err_code.h"
16 : #include <fcntl.h>
17 :
18 : #define SCD_ARGC_NUM 2
19 : #define SCD_SLEEP_INTERVAL (100 * 1000) // ms
20 : #define SCD_MAX_RETRY_TIME 10
21 :
22 64 : STATIC TraStatus ScdWaitDirCreate(const char *dirPath)
23 : {
24 64 : int32_t count = 0;
25 224 : while (access(dirPath, F_OK) != 0) {
26 176 : SCD_DLOG_DBG("directory path \"%s\" has not create, retry time=%d.", dirPath, count);
27 176 : count++;
28 176 : if (count > SCD_MAX_RETRY_TIME) {
29 16 : SCD_DLOG_ERR("directory path \"%s\" does not exist.", dirPath);
30 16 : return TRACE_FAILURE;
31 : }
32 160 : (void)usleep(SCD_SLEEP_INTERVAL); // 100ms
33 : }
34 48 : return TRACE_SUCCESS;
35 : }
36 :
37 48 : STATIC TraStatus ScdInitLogCat(const ScdProcessArgs *args)
38 : {
39 48 : TraStatus ret = StacktraceLogInit(STACKTRACE_LOG_TITLE_SUB);
40 48 : if (ret != TRACE_SUCCESS) {
41 16 : return TRACE_FAILURE;
42 : }
43 :
44 32 : StacktraceLogSetPath(args->filePath, args->fileName);
45 32 : return TRACE_SUCCESS;
46 : }
47 :
48 32 : STATIC void ScdReleaseLogCat(const ScdProcessArgs *args)
49 : {
50 32 : uint32_t flag = (uint32_t)O_CREAT | (uint32_t)O_RDWR;
51 32 : flag |= (args->handleType == SCD_DUMP_THREADS_TXT) ? (uint32_t)O_APPEND : (uint32_t)O_TRUNC;
52 32 : StacktraceLogSetPathSuffix(args->filePath, args->fileName,
53 32 : (args->handleType == SCD_DUMP_THREADS_TXT) ? ".txt" : ".log");
54 32 : StackcoreLogSaveWithFlag(flag);
55 32 : StackcoreLogExit();
56 32 : }
57 :
58 80 : int32_t MAIN(int32_t argc, const char **argv)
59 : {
60 : (void)argc;
61 : (void)argv;
62 : // parse args
63 80 : ScdProcessArgs args = {0};
64 80 : TraStatus ret = ScdUtilReadStdin((void *)&args, sizeof(ScdProcessArgs));
65 80 : if (ret != TRACE_SUCCESS) {
66 16 : SCD_DLOG_ERR("read args from stdin failed, ret=%d.", ret);
67 16 : return SCD_ERR_CODE_READ_STDIN;
68 : }
69 :
70 : // wait directory create
71 64 : ret = ScdWaitDirCreate(args.filePath);
72 64 : if (ret != TRACE_SUCCESS) {
73 16 : SCD_DLOG_ERR("wait directory create failed, ret=%d.", ret);
74 16 : return SCD_ERR_CODE_WAIT_DIR;
75 : }
76 :
77 48 : ret = ScdInitLogCat(&args);
78 48 : if (ret != TRACE_SUCCESS) {
79 16 : SCD_DLOG_ERR("init log cat failed, ret=%d.", ret);
80 16 : return SCD_ERR_CODE_INIT_LOGCAT;
81 : }
82 32 : STACKTRACE_LOG_RUN("sub process start, pid=%d.", getpid());
83 32 : STACKTRACE_LOG_RUN("handle signal(%d) received by thread(%u).", args.signo, args.crashTid);
84 32 : ret = ScdProcessDump(&args);
85 32 : if (ret != TRACE_SUCCESS) {
86 16 : STACKTRACE_LOG_ERR("process dump failed, ret=%d.", ret);
87 16 : ScdReleaseLogCat(&args);
88 16 : return SCD_ERR_CODE_DUMP_INFO;
89 : }
90 :
91 16 : STACKTRACE_LOG_RUN("process dump success, ret=%d.", ret);
92 16 : ScdReleaseLogCat(&args);
93 16 : return SCD_ERR_CODE_SUCCESS;
94 : }
|