some fixes for gstrace

This commit is contained in:
huanglianjun
2020-10-09 18:13:17 +08:00
parent 392e780699
commit 144fb21df5
3 changed files with 150 additions and 79 deletions

View File

@ -59,7 +59,43 @@
#define GS_TRC_FUNC_MASK_SZ (GS_TRC_FUNC_MAX / GS_TRC_NUM_BITS) // 8192 #define GS_TRC_FUNC_MASK_SZ (GS_TRC_FUNC_MAX / GS_TRC_NUM_BITS) // 8192
#define TRACE_COMMON_ERROR (-1) #define TRACE_COMMON_ERROR (-1)
#define TRACE_OK (0)
typedef enum trace_msg_code {
TRACE_OK,
TRACE_ALREADY_START,
TRACE_ALREADY_STOP,
TRACE_PARAMETER_ERR,
TRACE_BUFFER_SIZE_ERR,
TRACE_ATTACH_CFG_SHARE_MEMORY_ERR,
TRACE_ATTACH_BUFFER_SHARE_MEMORY_ERR,
TRACE_OPEN_SHARE_MEMORY_ERR,
TRACE_TRUNCATE_ERR,
TRACE_MMAP_ERR,
TRACE_UNLINK_SHARE_MEMORY_ERR,
TRACE_DISABLE_ERR,
TRACE_OPEN_OUTPUT_FILE_ERR,
TRACE_OPEN_INPUT_FILE_ERR,
TRACE_WRITE_BUFFER_HEADER_ERR,
TRACE_WRITE_CFG_HEADER_ERR,
TRACE_WRITE_BUFFER_ERR,
TRACE_READ_CFG_FROM_FILE_ERR,
TRACE_BUFFER_SIZE_FROM_FILE_ERR,
TRACE_MAGIC_FROM_FILE_ERR,
TRACE_READ_INFRA_FROM_FILE_ERR,
TRACE_NO_RECORDS_ERR,
TRACE_READ_SLOT_HEADER_ERR,
TRACE_NUM_SLOT_ERR,
TRACE_SLOT_MAGIC_ERR,
TRACE_READ_SLOT_DATA_ERR,
TRACE_WRITE_FORMATTED_RECORD_ERR,
TRACE_MSG_MAX,
} trace_msg_code;
typedef struct trace_msg {
trace_msg_code msg_code;
const char* msg_string;
} trace_msg_t;
#define securec_check(errno, charList, ...) \ #define securec_check(errno, charList, ...) \
do { \ do { \
@ -190,10 +226,10 @@ typedef struct trace_record {
uint32_t user_data_len; uint32_t user_data_len;
} trace_record; } trace_record;
extern int gstrace_start(int pid, const char* mask, uint64_t bufferSize, const char* trcFile); extern trace_msg_code gstrace_start(int pid, const char* mask, uint64_t bufferSize, const char* trcFile);
extern int gstrace_stop(int pid); extern trace_msg_code gstrace_stop(int pid);
extern int gstrace_dump(int pid, const char* outPath); extern trace_msg_code gstrace_dump(int pid, const char* outPath);
extern int gstrace_config(int pid); extern trace_msg_code gstrace_config(int pid);
extern bool isNumeric(const char* str); extern bool isNumeric(const char* str);
extern uint32_t getFunctionIdxByName(const char* funcName, uint32_t* comp); extern uint32_t getFunctionIdxByName(const char* funcName, uint32_t* comp);

View File

@ -449,7 +449,7 @@ int gstrace_init(int key)
return TRACE_OK; return TRACE_OK;
} }
static int createAndAttachTraceBuffer(int key, uint64_t bufferSize) static trace_msg_code createAndAttachTraceBuffer(int key, uint64_t bufferSize)
{ {
trace_context* pTrcCxt = getTraceContext(); trace_context* pTrcCxt = getTraceContext();
char sBufMemName[TRC_SHARED_MEM_NAME_MAX] = {0}; char sBufMemName[TRC_SHARED_MEM_NAME_MAX] = {0};
@ -459,21 +459,21 @@ static int createAndAttachTraceBuffer(int key, uint64_t bufferSize)
int fdTrc = shm_open(sBufMemName, O_RDWR | O_CREAT, S_IRWXU); int fdTrc = shm_open(sBufMemName, O_RDWR | O_CREAT, S_IRWXU);
if (fdTrc == -1) { if (fdTrc == -1) {
// Failed to initialize trace shared memory buffer // Failed to initialize trace shared memory buffer
return TRACE_COMMON_ERROR; return TRACE_OPEN_SHARE_MEMORY_ERR;
} }
ret = ftruncate(fdTrc, bufferSize + sizeof(trace_infra)); ret = ftruncate(fdTrc, bufferSize + sizeof(trace_infra));
if (ret == -1) { if (ret == -1) {
close(fdTrc); close(fdTrc);
// Failed to set size of trace shared memory buffer // Failed to set size of trace shared memory buffer
return TRACE_COMMON_ERROR; return TRACE_TRUNCATE_ERR;
} }
void* pTrcBufAddress = mmap(NULL, bufferSize + sizeof(trace_infra), PROT_READ | PROT_WRITE, MAP_SHARED, fdTrc, 0); void* pTrcBufAddress = mmap(NULL, bufferSize + sizeof(trace_infra), PROT_READ | PROT_WRITE, MAP_SHARED, fdTrc, 0);
close(fdTrc); close(fdTrc);
if (pTrcBufAddress == MAP_FAILED) { if (pTrcBufAddress == MAP_FAILED) {
// "failed to map memory for trace buffer // "failed to map memory for trace buffer
return TRACE_COMMON_ERROR; return TRACE_MMAP_ERR;
} }
// Anchor our shared memory address in our global variable // Anchor our shared memory address in our global variable
pTrcCxt->pTrcInfra = (trace_infra*)pTrcBufAddress; pTrcCxt->pTrcInfra = (trace_infra*)pTrcBufAddress;
@ -493,26 +493,25 @@ static int createAndAttachTraceBuffer(int key, uint64_t bufferSize)
* shared memory buffers, one for the trace * shared memory buffers, one for the trace
* config and one for the trace infra. * config and one for the trace infra.
*/ */
int gstrace_start(int key, const char* mask, uint64_t bufferSize, const char* trcFile) trace_msg_code gstrace_start(int key, const char* mask, uint64_t bufferSize, const char* trcFile)
{ {
int ret; trace_msg_code ret;
bool bTrcToFile = (trcFile != NULL); bool bTrcToFile = (trcFile != NULL);
trace_context* pTrcCxt = getTraceContext(); trace_context* pTrcCxt = getTraceContext();
if (!bTrcToFile && bufferSize <= 0) { if (!bTrcToFile && bufferSize <= 0) {
/* Buffer size of Shared memory should be greater than 0. */ /* Buffer size of Shared memory should be greater than 0. */
return TRACE_COMMON_ERROR; return TRACE_BUFFER_SIZE_ERR;
} }
bufferSize = bTrcToFile ? MIN_BUF_SIZE : roundToNearestPowerOfTwo(bufferSize); bufferSize = bTrcToFile ? MIN_BUF_SIZE : roundToNearestPowerOfTwo(bufferSize);
if (attachTraceCfgSharedMem(key) != 0) { if (attachTraceCfgSharedMem(key) != 0) {
/* Failed to attached to shared memory. */ /* Failed to attached to shared memory. */
return TRACE_COMMON_ERROR; return TRACE_ATTACH_CFG_SHARE_MEMORY_ERR;
} }
if (pTrcCxt->pTrcCfg->bEnabled) { if (pTrcCxt->pTrcCfg->bEnabled) {
printf("Trace has already been activated.\n"); return TRACE_ALREADY_START;
return TRACE_OK;
} }
/* set the mask if it's passed in */ /* set the mask if it's passed in */
@ -520,8 +519,8 @@ int gstrace_start(int key, const char* mask, uint64_t bufferSize, const char* tr
trace_mask st_mask = {0}; trace_mask st_mask = {0};
parseMask(mask, &st_mask); parseMask(mask, &st_mask);
ret = memcpy_s(&pTrcCxt->pTrcCfg->gs_trc_mask, sizeof(trace_mask), &st_mask, sizeof(trace_mask)); errno_t rcs = memcpy_s(&pTrcCxt->pTrcCfg->gs_trc_mask, sizeof(trace_mask), &st_mask, sizeof(trace_mask));
securec_check(ret, "\0", "\0"); securec_check(rcs, "\0", "\0");
pTrcCxt->pTrcCfg->options |= GS_TRC_CFG_MASK_OPTION; pTrcCxt->pTrcCfg->options |= GS_TRC_CFG_MASK_OPTION;
} }
@ -531,8 +530,8 @@ int gstrace_start(int key, const char* mask, uint64_t bufferSize, const char* tr
pTrcCxt->pTrcCfg->size = bufferSize; pTrcCxt->pTrcCfg->size = bufferSize;
pTrcCxt->pTrcCfg->bTrcToFile = bTrcToFile; pTrcCxt->pTrcCfg->bTrcToFile = bTrcToFile;
if (bTrcToFile) { if (bTrcToFile) {
ret = snprintf_s(pTrcCxt->pTrcCfg->filePath, MAX_PATH_LEN, MAX_PATH_LEN - 1, "%s", trcFile); int rc = snprintf_s(pTrcCxt->pTrcCfg->filePath, MAX_PATH_LEN, MAX_PATH_LEN - 1, "%s", trcFile);
securec_check_ss_c(ret, "\0", "\0"); securec_check_ss_c(rc, "\0", "\0");
ret = TRACE_OK; ret = TRACE_OK;
} }
pTrcCxt->pTrcCfg->status = TRACE_STATUS_RECORDING; pTrcCxt->pTrcCfg->status = TRACE_STATUS_RECORDING;
@ -545,19 +544,17 @@ int gstrace_start(int key, const char* mask, uint64_t bufferSize, const char* tr
return ret; return ret;
} }
int gstrace_stop(int key) trace_msg_code gstrace_stop(int key)
{ {
char sBufMemName[TRC_SHARED_MEM_NAME_MAX] = {0}; char sBufMemName[TRC_SHARED_MEM_NAME_MAX] = {0};
trace_context* pTrcCxt = getTraceContext(); trace_context* pTrcCxt = getTraceContext();
if (attachTraceCfgSharedMem(key) != TRACE_OK) { if (attachTraceCfgSharedMem(key) != TRACE_OK) {
printf("Failed to attached to shared memory.\n"); return TRACE_ATTACH_CFG_SHARE_MEMORY_ERR;
return TRACE_COMMON_ERROR;
} }
if (!pTrcCxt->pTrcCfg->bEnabled || pTrcCxt->pTrcCfg->status != TRACE_STATUS_RECORDING) { if (!pTrcCxt->pTrcCfg->bEnabled || pTrcCxt->pTrcCfg->status != TRACE_STATUS_RECORDING) {
printf("Trace had already been deactived.\n"); return TRACE_ALREADY_STOP;
return TRACE_OK;
} }
pTrcCxt->pTrcCfg->status = TRACE_STATUS_PREPARE_STOP; pTrcCxt->pTrcCfg->status = TRACE_STATUS_PREPARE_STOP;
@ -572,8 +569,7 @@ int gstrace_stop(int key)
if (shm_unlink(sBufMemName) == -1) { if (shm_unlink(sBufMemName) == -1) {
perror("shm_unlink"); perror("shm_unlink");
printf("Failed to delete shared memory.\n"); return TRACE_UNLINK_SHARE_MEMORY_ERR;
return TRACE_COMMON_ERROR;
} }
pTrcCxt->pTrcCfg->size = 0; pTrcCxt->pTrcCfg->size = 0;
@ -877,7 +873,7 @@ void gstrace_tryblock_exit(bool inCatch, int* oldTryCounter)
gtCurTryCounter = oldTryCounter; gtCurTryCounter = oldTryCounter;
} }
static int dump_trace_context(int fd) static trace_msg_code dump_trace_context(int fd)
{ {
trace_context* pTrcCxt = getTraceContext(); trace_context* pTrcCxt = getTraceContext();
size_t bytesWritten; size_t bytesWritten;
@ -885,21 +881,19 @@ static int dump_trace_context(int fd)
// Write the trace config header first // Write the trace config header first
bytesWritten = write(fd, (void*)pTrcCxt->pTrcCfg, sizeof(trace_config)); bytesWritten = write(fd, (void*)pTrcCxt->pTrcCfg, sizeof(trace_config));
if (bytesWritten != sizeof(trace_config)) { if (bytesWritten != sizeof(trace_config)) {
printf("Failed to write trace config header\n"); return TRACE_WRITE_CFG_HEADER_ERR;
return TRACE_COMMON_ERROR;
} }
// Write the trace buffer header next // Write the trace buffer header next
bytesWritten = write(fd, (void*)pTrcCxt->pTrcInfra, sizeof(trace_infra)); bytesWritten = write(fd, (void*)pTrcCxt->pTrcInfra, sizeof(trace_infra));
if (bytesWritten != sizeof(trace_infra)) { if (bytesWritten != sizeof(trace_infra)) {
printf("Failed to write trace header\n"); return TRACE_WRITE_BUFFER_HEADER_ERR;
return TRACE_COMMON_ERROR;
} else { } else {
return TRACE_OK; return TRACE_OK;
} }
} }
static int dump_trace_buffer(int fd, const char* outPath) static trace_msg_code dump_trace_buffer(int fd, const char* outPath)
{ {
trace_context* pTrcCxt = getTraceContext(); trace_context* pTrcCxt = getTraceContext();
size_t bytesWritten, bytesToWrite; size_t bytesWritten, bytesToWrite;
@ -911,8 +905,7 @@ static int dump_trace_buffer(int fd, const char* outPath)
bytesToWrite = pTrcCxt->pTrcInfra->total_size - sizeof(trace_infra); bytesToWrite = pTrcCxt->pTrcInfra->total_size - sizeof(trace_infra);
bytesWritten = write(fd, pBuf, bytesToWrite); bytesWritten = write(fd, pBuf, bytesToWrite);
if (bytesWritten != bytesToWrite) { if (bytesWritten != bytesToWrite) {
printf("Failed to write trace buffer \n"); return TRACE_WRITE_BUFFER_ERR;
return TRACE_COMMON_ERROR;
} else { } else {
printf("Shared memory buffer has been dumped to file: %s.\n", outPath); printf("Shared memory buffer has been dumped to file: %s.\n", outPath);
return TRACE_OK; return TRACE_OK;
@ -929,31 +922,27 @@ static int dump_trace_buffer(int fd, const char* outPath)
// this function will put the dump in // this function will put the dump in
// "tmp/trace_dump" // "tmp/trace_dump"
// ------------------------------------------- // -------------------------------------------
int gstrace_dump(int key, const char* outPath) trace_msg_code gstrace_dump(int key, const char* outPath)
{ {
trace_context* pTrcCxt = getTraceContext(); trace_context* pTrcCxt = getTraceContext();
int ret; trace_msg_code ret;
if (attachTraceCfgSharedMem(key) != TRACE_OK) { if (attachTraceCfgSharedMem(key) != TRACE_OK) {
printf("Failed to attached to shared memory.\n"); return TRACE_ATTACH_CFG_SHARE_MEMORY_ERR;
return TRACE_COMMON_ERROR;
} }
if (!pTrcCxt->pTrcCfg->bEnabled) { if (!pTrcCxt->pTrcCfg->bEnabled) {
printf("trace is disable.\n"); return TRACE_DISABLE_ERR;
return TRACE_COMMON_ERROR;
} }
if (attachTraceBufferSharedMem(key) != TRACE_OK) { if (attachTraceBufferSharedMem(key) != TRACE_OK) {
printf("failed to attach to trace buffer shared mem\n"); return TRACE_ATTACH_BUFFER_SHARE_MEMORY_ERR;
return TRACE_COMMON_ERROR;
} }
// open output file // open output file
int fd = trace_open_filedesc(outPath, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); int fd = trace_open_filedesc(outPath, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
if (fd == -1) { if (fd == -1) {
printf("Failed to open file\n"); return TRACE_OPEN_OUTPUT_FILE_ERR;
return TRACE_COMMON_ERROR;
} }
ret = dump_trace_context(fd); ret = dump_trace_context(fd);
@ -981,7 +970,7 @@ static char* getStatusString(int status)
} }
} }
int gstrace_config(int key) trace_msg_code gstrace_config(int key)
{ {
int rc; int rc;
trace_context* pTrcCxt = getTraceContext(); trace_context* pTrcCxt = getTraceContext();
@ -989,7 +978,7 @@ int gstrace_config(int key)
rc = attachTraceCfgSharedMem(key); rc = attachTraceCfgSharedMem(key);
if (rc != TRACE_OK) { if (rc != TRACE_OK) {
printf("Failed to attached to shared memory.\n"); printf("Failed to attached to shared memory.\n");
return TRACE_COMMON_ERROR; return TRACE_ATTACH_CFG_SHARE_MEMORY_ERR;
} }
if (pTrcCxt->pTrcCfg->bEnabled) { if (pTrcCxt->pTrcCfg->bEnabled) {

View File

@ -1103,41 +1103,38 @@ static void gsTrcFormatOneTraceRecord(
} }
} }
static int readAndCheckTrcMeta(int fdInput, trace_config* trc_cfg, trace_infra* trc_infra) static trace_msg_code readAndCheckTrcMeta(int fdInput, trace_config* trc_cfg, trace_infra* trc_infra)
{ {
size_t bytesRead; size_t bytesRead;
bytesRead = read(fdInput, (void*)trc_cfg, sizeof(trace_config)); bytesRead = read(fdInput, (void*)trc_cfg, sizeof(trace_config));
if (bytesRead != sizeof(trace_config)) { if (bytesRead != sizeof(trace_config)) {
perror("Failed to read trace config."); perror("read err.");
return TRACE_COMMON_ERROR; return TRACE_READ_CFG_FROM_FILE_ERR;
} }
if ((trc_cfg->size & (trc_cfg->size - 1)) != 0) { if ((trc_cfg->size & (trc_cfg->size - 1)) != 0) {
perror("invalid buffer size in trace file."); return TRACE_BUFFER_SIZE_FROM_FILE_ERR;
return TRACE_COMMON_ERROR;
} }
if (trc_cfg->trc_cfg_magic_no != GS_TRC_CFG_MAGIC_N) { if (trc_cfg->trc_cfg_magic_no != GS_TRC_CFG_MAGIC_N) {
perror("invalid magic number in trace file."); return TRACE_MAGIC_FROM_FILE_ERR;
return TRACE_COMMON_ERROR;
} }
bytesRead = read(fdInput, (void*)trc_infra, sizeof(trace_infra)); bytesRead = read(fdInput, (void*)trc_infra, sizeof(trace_infra));
if (bytesRead != sizeof(trace_infra)) { if (bytesRead != sizeof(trace_infra)) {
perror("Failed to read trace infra."); perror("read err.");
return TRACE_COMMON_ERROR; return TRACE_READ_INFRA_FROM_FILE_ERR;
} }
if (trc_infra->g_Counter == 0) { if (trc_infra->g_Counter == 0) {
perror("No trace records were captured"); return TRACE_NO_RECORDS_ERR;
return TRACE_COMMON_ERROR;
} }
return TRACE_OK; return TRACE_OK;
} }
static int readAndFormatTrcRec(int fdInput, int fdOutput, uint64_t counter) static trace_msg_code readAndFormatTrcRec(int fdInput, int fdOutput, uint64_t counter)
{ {
uint64_t totalNumRecs = 0; uint64_t totalNumRecs = 0;
char rec_tmp_buf[MAX_TRC_RC_SZ] = {'\0'}; char rec_tmp_buf[MAX_TRC_RC_SZ] = {'\0'};
@ -1147,20 +1144,18 @@ static int readAndFormatTrcRec(int fdInput, int fdOutput, uint64_t counter)
// Read the contents of a the header slot // Read the contents of a the header slot
size_t bytesRead = read(fdInput, (void*)&rec_tmp_buf, SLOT_SIZE); size_t bytesRead = read(fdInput, (void*)&rec_tmp_buf, SLOT_SIZE);
if (bytesRead != SLOT_SIZE) { if (bytesRead != SLOT_SIZE) {
perror("Invalid trace file - faild to read trace slot header"); perror("Invalid trace file");
return TRACE_COMMON_ERROR; return TRACE_READ_SLOT_HEADER_ERR;
} }
trace_slot_head* pHdr = (trace_slot_head*)rec_tmp_buf; trace_slot_head* pHdr = (trace_slot_head*)rec_tmp_buf;
// since slots for one record is contiguous, we can do following check. // since slots for one record is contiguous, we can do following check.
if (pHdr->num_slots_in_area == 0 || pHdr->num_slots_in_area > MAX_TRC_SLOTS) { if (pHdr->num_slots_in_area == 0 || pHdr->num_slots_in_area > MAX_TRC_SLOTS) {
perror("Invalid trace file - num slot in header is zero."); return TRACE_NUM_SLOT_ERR;
return TRACE_COMMON_ERROR;
} }
if (pHdr->hdr_magic_number != SLOT_AREAD_HEADER_MAGIC_NO) { if (pHdr->hdr_magic_number != SLOT_AREAD_HEADER_MAGIC_NO) {
perror("Invalid trace file - num slot in header is zero."); return TRACE_SLOT_MAGIC_ERR;
return TRACE_COMMON_ERROR;
} }
if (pHdr->num_slots_in_area > 1 && pHdr->num_slots_in_area <= MAX_TRC_SLOTS) { if (pHdr->num_slots_in_area > 1 && pHdr->num_slots_in_area <= MAX_TRC_SLOTS) {
@ -1169,8 +1164,8 @@ static int readAndFormatTrcRec(int fdInput, int fdOutput, uint64_t counter)
bytesRead = read(fdInput, (void*)&rec_tmp_buf[SLOT_SIZE], size_to_read); bytesRead = read(fdInput, (void*)&rec_tmp_buf[SLOT_SIZE], size_to_read);
if (bytesRead != size_to_read) { if (bytesRead != size_to_read) {
perror("Invalid trace file - faild to read trace slot data."); perror("Invalid trace file.");
return TRACE_COMMON_ERROR; return TRACE_READ_SLOT_DATA_ERR;
} }
} }
@ -1190,8 +1185,8 @@ static int readAndFormatTrcRec(int fdInput, int fdOutput, uint64_t counter)
// now write the buffer to the file // now write the buffer to the file
size_t bytesWritten = write(fdOutput, rec_out_buf, strlen(rec_out_buf)); size_t bytesWritten = write(fdOutput, rec_out_buf, strlen(rec_out_buf));
if (bytesWritten != strlen(rec_out_buf)) { if (bytesWritten != strlen(rec_out_buf)) {
perror("Error while writing formatted trace record"); perror("write error");
return TRACE_COMMON_ERROR; return TRACE_WRITE_FORMATTED_RECORD_ERR;
} }
totalNumRecs++; totalNumRecs++;
} }
@ -1209,9 +1204,9 @@ static int readAndFormatTrcRec(int fdInput, int fdOutput, uint64_t counter)
// the formatted file will be the same name // the formatted file will be the same name
// as the binary file with the extension .fmt // as the binary file with the extension .fmt
// ------------------------------------------- // -------------------------------------------
static int formatTrcDumpFile(const char* inputPath, const char* outputPath) static trace_msg_code formatTrcDumpFile(const char* inputPath, const char* outputPath)
{ {
int ret; trace_msg_code ret;
trace_infra trc_infra; trace_infra trc_infra;
trace_config trc_cfg; trace_config trc_cfg;
@ -1219,7 +1214,7 @@ static int formatTrcDumpFile(const char* inputPath, const char* outputPath)
int fdInput = trace_open_filedesc(inputPath, O_RDONLY, 0); int fdInput = trace_open_filedesc(inputPath, O_RDONLY, 0);
if (fdInput == -1) { if (fdInput == -1) {
perror("Failed to open input file"); perror("Failed to open input file");
return TRACE_COMMON_ERROR; return TRACE_OPEN_INPUT_FILE_ERR;
} }
/* Read config header to validate the file and buffer size */ /* Read config header to validate the file and buffer size */
@ -1230,7 +1225,7 @@ static int formatTrcDumpFile(const char* inputPath, const char* outputPath)
if (fdOutput == -1) { if (fdOutput == -1) {
close(fdInput); close(fdInput);
perror("Failed to open output file"); perror("Failed to open output file");
return TRACE_COMMON_ERROR; return TRACE_OPEN_OUTPUT_FILE_ERR;
} }
uint64_t maxSlots = trc_cfg.size / SLOT_SIZE; uint64_t maxSlots = trc_cfg.size / SLOT_SIZE;
@ -1402,9 +1397,61 @@ static void printUsage(int argc, char** argv)
printf("\t-t get statistics for every n seconds, file name will be {global file name}.step\n"); printf("\t-t get statistics for every n seconds, file name will be {global file name}.step\n");
} }
trace_msg_t trace_message[] = {
{TRACE_OK, "Success!\n"},
{TRACE_ALREADY_START, "Trace has already been activated.\n"},
{TRACE_ALREADY_STOP, "Trace has already been deactived.\n"},
{TRACE_PARAMETER_ERR, "Parameter not correct.\n"},
{TRACE_BUFFER_SIZE_ERR, "Invalid share memory buffer size.\n"},
{TRACE_ATTACH_CFG_SHARE_MEMORY_ERR, "Attached to trace config failed.\n"},
{TRACE_ATTACH_BUFFER_SHARE_MEMORY_ERR, "Attached to trace buffer failed.\n"},
{TRACE_OPEN_SHARE_MEMORY_ERR, "Failed to initialize trace buffer.\n"},
{TRACE_TRUNCATE_ERR, "Failed to set size of trace buffer.\n"},
{TRACE_MMAP_ERR, "Failed to map memory for trace buffer.\n"},
{TRACE_UNLINK_SHARE_MEMORY_ERR, "Failed to delete trace buffer.\n"},
{TRACE_DISABLE_ERR, "Trace is disable.\n"},
{TRACE_OPEN_OUTPUT_FILE_ERR, "Failed to open trace output file.\n"},
{TRACE_OPEN_INPUT_FILE_ERR, "Failed to open trace input file.\n"},
{TRACE_WRITE_BUFFER_HEADER_ERR, "Failed to write trace buffer header.\n"},
{TRACE_WRITE_CFG_HEADER_ERR, "Failed to write trace config header.\n"},
{TRACE_WRITE_BUFFER_ERR, "Failed to write trace buffer.\n"},
{TRACE_READ_CFG_FROM_FILE_ERR, "Failed to read trace config.\n"},
{TRACE_BUFFER_SIZE_FROM_FILE_ERR, "Invalid buffer size in trace file.\n"},
{TRACE_MAGIC_FROM_FILE_ERR, "Invalid magic number in trace file.\n"},
{TRACE_READ_INFRA_FROM_FILE_ERR, "Failed to read trace infra.\n"},
{TRACE_NO_RECORDS_ERR, "No trace records were captured.\n"},
{TRACE_READ_SLOT_HEADER_ERR, "Faild to read trace slot header.\n"},
{TRACE_NUM_SLOT_ERR, "Invalid trace file, num slot in header is zero.\n"},
{TRACE_SLOT_MAGIC_ERR, "Invalid trace file, magic number is not correct.\n"},
{TRACE_READ_SLOT_DATA_ERR, "Faild to read trace slot data.\n"},
{TRACE_WRITE_FORMATTED_RECORD_ERR, "Failed to write formatted trace record.\n"},
{TRACE_MSG_MAX, "Failed!\n"},
};
static void print_result_message(trace_msg_code rc, int argc, char** argv)
{
if (rc == TRACE_OK) {
(void)printf("[GAUSS-TRACE] %s %s", argv[1], trace_message[rc].msg_string);
} else if ((rc > TRACE_OK) && (rc < TRACE_MSG_MAX)) {
if (rc == trace_message[rc].msg_code) {
(void)printf("[GAUSS-TRACE] %s", trace_message[rc].msg_string);
} else {
(void)printf("[GAUSS-TRACE] Trace failed for error %05d.\n", rc);
}
} else {
(void)printf("[GAUSS-TRACE] %s %s\n", argv[1], trace_message[TRACE_MSG_MAX].msg_string);
}
if (rc == TRACE_PARAMETER_ERR) {
printUsage(argc, argv);
}
return;
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
int rc = TRACE_OK; trace_msg_code rc = TRACE_OK;
int pid; int pid;
char* trcFile = NULL; char* trcFile = NULL;
char* outputFile = NULL; char* outputFile = NULL;
@ -1414,8 +1461,7 @@ int main(int argc, char** argv)
char* mask = NULL; char* mask = NULL;
if (argc < 2) { if (argc < 2) {
printUsage(argc, argv); rc = TRACE_PARAMETER_ERR;
rc = TRACE_COMMON_ERROR;
goto exit; goto exit;
} }
@ -1433,7 +1479,7 @@ int main(int argc, char** argv)
rc = gstrace_start(pid, mask, uBufferSize, trcFile); rc = gstrace_start(pid, mask, uBufferSize, trcFile);
} else if (0 == strcmp(argv[1], "stop") && pid != -1) { } else if (0 == strcmp(argv[1], "stop") && pid != -1) {
// Stop will disable tracing and delete the shared memory buffer // Stop will disable tracing and delete the shared memory buffer
gstrace_stop(pid); rc = gstrace_stop(pid);
} else if (0 == strcmp(argv[1], "config") && pid != -1) { } else if (0 == strcmp(argv[1], "config") && pid != -1) {
rc = gstrace_config(pid); rc = gstrace_config(pid);
} else if (0 == strcmp(argv[1], "dump") && pid != -1 && outputFile != NULL) { } else if (0 == strcmp(argv[1], "dump") && pid != -1 && outputFile != NULL) {
@ -1451,10 +1497,10 @@ int main(int argc, char** argv)
/* step stats file will be {outputFile}.step */ /* step stats file will be {outputFile}.step */
anlyzeDumpFile(trcFile, strlen(trcFile), outputFile, strlen(outputFile), stepSize); anlyzeDumpFile(trcFile, strlen(trcFile), outputFile, strlen(outputFile), stepSize);
} else { } else {
printUsage(argc, argv); rc = TRACE_PARAMETER_ERR;
rc = TRACE_COMMON_ERROR;
} }
exit: exit:
return rc; print_result_message(rc, argc, argv);
return (int)rc;
} }