• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "dfx_cutil.h"
17 
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <syscall.h>
21 #include <unistd.h>
22 
23 #include <sys/time.h>
24 
25 #include "dfx_define.h"
26 #include "securec.h"
27 #include "stdio.h"
28 #include "string.h"
29 
GetProcStatus(struct ProcInfo * procInfo)30 int GetProcStatus(struct ProcInfo* procInfo)
31 {
32     procInfo->pid = syscall(SYS_getpid);
33     procInfo->tid = syscall(SYS_gettid);
34     procInfo->ppid = syscall(SYS_getppid);
35     procInfo->ns = (syscall(SYS_getpid) == 1);
36     return 0;
37 }
38 
ReadStringFromFile(const char * path,char * dst,size_t dstSz)39 bool ReadStringFromFile(const char* path, char* dst, size_t dstSz)
40 {
41     char name[NAME_LEN];
42     char nameFilter[NAME_LEN];
43 
44     (void)memset_s(name, sizeof(name), '\0', sizeof(name));
45     (void)memset_s(nameFilter, sizeof(nameFilter), '\0', sizeof(nameFilter));
46 
47     int fd = -1;
48     fd = open(path, O_RDONLY);
49     if (fd < 0) {
50         return false;
51     }
52 
53     if (read(fd, name, NAME_LEN -1) == -1) {
54         close(fd);
55         return false;
56     }
57 
58     char* p = name;
59     int i = 0;
60     while (*p != '\0') {
61         if ((*p == '\n') || (i == NAME_LEN)) {
62             break;
63         }
64         nameFilter[i] = *p;
65         p++, i++;
66     }
67     nameFilter[NAME_LEN - 1] = '\0';
68 
69     if (memcpy_s(dst, dstSz, nameFilter, strlen(nameFilter) + 1) != 0) {
70         perror("Failed to copy name.");
71         close(fd);
72         return false;
73     }
74 
75     close(fd);
76     return true;
77 }
78 
GetThreadName(char * buffer,size_t bufferSz)79 bool GetThreadName(char* buffer, size_t bufferSz)
80 {
81     char path[NAME_LEN];
82     (void)memset_s(path, sizeof(path), '\0', sizeof(path));
83     if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "/proc/%d/comm", getpid()) <= 0) {
84         return false;
85     }
86     return ReadStringFromFile(path, buffer, bufferSz);
87 }
88 
GetProcessName(char * buffer,size_t bufferSz)89 bool GetProcessName(char* buffer, size_t bufferSz)
90 {
91     char path[NAME_LEN];
92     (void)memset_s(path, sizeof(path), '\0', sizeof(path));
93     if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "/proc/%d/cmdline", getpid()) <= 0) {
94         return false;
95     }
96     return ReadStringFromFile(path, buffer, bufferSz);
97 }
98 
GetTimeMilliseconds(void)99 uint64_t GetTimeMilliseconds(void)
100 {
101     struct timeval time;
102     gettimeofday(&time, NULL);
103     return ((uint64_t)time.tv_sec * 1000) + // 1000 : second to millisecond convert ratio
104         (((uint64_t)time.tv_usec) / 1000); // 1000 : microsecond to millisecond convert ratio
105 }