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 72 : STATIC TraStatus ScdWaitDirCreate(const char* dirPath)
23 : {
24 72 : int32_t count = 0;
25 252 : while (access(dirPath, F_OK) != 0) {
26 198 : SCD_DLOG_DBG("directory path \"%s\" has not create, retry time=%d.", dirPath, count);
27 198 : count++;
28 198 : if (count > SCD_MAX_RETRY_TIME) {
29 18 : SCD_DLOG_ERR("directory path \"%s\" does not exist.", dirPath);
30 18 : return TRACE_FAILURE;
31 : }
32 180 : (void)usleep(SCD_SLEEP_INTERVAL); // 100ms
33 : }
34 54 : return TRACE_SUCCESS;
35 : }
36 :
37 54 : STATIC TraStatus ScdInitLogCat(const ScdProcessArgs* args)
38 : {
39 54 : TraStatus ret = StacktraceLogInit(STACKTRACE_LOG_TITLE_SUB);
40 54 : if (ret != TRACE_SUCCESS) {
41 18 : return TRACE_FAILURE;
42 : }
43 :
44 36 : StacktraceLogSetPath(args->filePath, args->fileName);
45 36 : return TRACE_SUCCESS;
46 : }
47 :
48 36 : STATIC void ScdReleaseLogCat(const ScdProcessArgs* args)
49 : {
50 36 : uint32_t flag = (uint32_t)O_CREAT | (uint32_t)O_RDWR;
51 36 : flag |= (args->handleType == SCD_DUMP_THREADS_TXT) ? (uint32_t)O_APPEND : (uint32_t)O_TRUNC;
52 36 : StacktraceLogSetPathSuffix(
53 72 : args->filePath, args->fileName, (args->handleType == SCD_DUMP_THREADS_TXT) ? ".txt" : ".log");
54 36 : StackcoreLogSaveWithFlag(flag);
55 36 : StackcoreLogExit();
56 36 : }
57 :
58 90 : int32_t MAIN(int32_t argc, const char** argv)
59 : {
60 : (void)argc;
61 : (void)argv;
62 : // parse args
63 90 : ScdProcessArgs args = {0};
64 90 : TraStatus ret = ScdUtilReadStdin((void*)&args, sizeof(ScdProcessArgs));
65 90 : if (ret != TRACE_SUCCESS) {
66 18 : SCD_DLOG_ERR("read args from stdin failed, ret=%d.", ret);
67 18 : return SCD_ERR_CODE_READ_STDIN;
68 : }
69 :
70 : // wait directory create
71 72 : ret = ScdWaitDirCreate(args.filePath);
72 72 : if (ret != TRACE_SUCCESS) {
73 18 : SCD_DLOG_ERR("wait directory create failed, ret=%d.", ret);
74 18 : return SCD_ERR_CODE_WAIT_DIR;
75 : }
76 :
77 54 : ret = ScdInitLogCat(&args);
78 54 : if (ret != TRACE_SUCCESS) {
79 18 : SCD_DLOG_ERR("init log cat failed, ret=%d.", ret);
80 18 : return SCD_ERR_CODE_INIT_LOGCAT;
81 : }
82 36 : STACKTRACE_LOG_RUN("sub process start, pid=%d.", getpid());
83 36 : STACKTRACE_LOG_RUN("handle signal(%d) received by thread(%u).", args.signo, args.crashTid);
84 36 : ret = ScdProcessDump(&args);
85 36 : if (ret != TRACE_SUCCESS) {
86 18 : STACKTRACE_LOG_ERR("process dump failed, ret=%d.", ret);
87 18 : ScdReleaseLogCat(&args);
88 18 : return SCD_ERR_CODE_DUMP_INFO;
89 : }
90 :
91 18 : STACKTRACE_LOG_RUN("process dump success, ret=%d.", ret);
92 18 : ScdReleaseLogCat(&args);
93 18 : return SCD_ERR_CODE_SUCCESS;
94 : }
|