!2411 修复当调用gstrace codepath时,如果输出文件异常,返回后会残留临时文件

Merge pull request !2411 from Haolan/bugfix_gstrace_02
This commit is contained in:
opengauss-bot
2022-11-30 10:03:11 +00:00
committed by Gitee
2 changed files with 21 additions and 0 deletions

View File

@ -137,6 +137,7 @@ public:
void outputStat(FILE* fp) override;
void putIfUnexisted(pid_t pid, pid_t tid);
void putFuncStatIfUnexisted(uint32_t func_id, func_stat* func_stat_map);
void removeAllTempFiles(map_flow::iterator it);
trace_msg_code mergeFiles(const char* outPath, size_t len);
void flushThreadFlows();

View File

@ -420,6 +420,24 @@ void DumpFileFlowVisitor::flushThreadFlows()
}
}
/**
* @brief If exception occures, remove all temp file before exit
*
* @param it map_flow::iterator
* @return trace_msg_code trace status code
*/
void DumpFileFlowVisitor::removeAllTempFiles(map_flow::iterator it){
//If exception occures, remove all temp file from where iterator begins
for ( ; it != mapFlows.end(); ++it) {
char tmpPath[MAX_PATH_LEN] = {0};
int ret;
ret = snprintf_s(tmpPath, MAX_PATH_LEN, MAX_PATH_LEN - 1, "tid.%d", it->first);
securec_check_ss_c(ret, "\0", "\0");
ret = remove(tmpPath);
}
}
trace_msg_code DumpFileFlowVisitor::mergeFiles(const char* outPath, size_t len)
{
FILE* fpOut = NULL;
@ -430,6 +448,7 @@ trace_msg_code DumpFileFlowVisitor::mergeFiles(const char* outPath, size_t len)
fpOut = trace_fopen(outPath, "w+");
if (fpOut == NULL) {
this->removeAllTempFiles(mapFlows.begin());
return TRACE_OPEN_OUTPUT_FILE_ERR;
}
@ -445,6 +464,7 @@ trace_msg_code DumpFileFlowVisitor::mergeFiles(const char* outPath, size_t len)
// Open the file with read mode
FILE* fpIn = trace_fopen(tmpPath, "r");
if (NULL == fpIn) {
this->removeAllTempFiles(it);
(void)trace_fclose(fpOut);
free(buffer);
return TRACE_OPEN_TMP_FILE_ERR;