• 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 #ifndef DFX_MUSL_CUTIL_H
16 #define DFX_MUSL_CUTIL_H
17 
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <syscall.h>
22 
23 #include "dfx_define.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #ifdef ENABLE_MUSL_CUTIL
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     memset(name, 0, sizeof(name));
44     memset(nameFilter, 0, sizeof(nameFilter));
45 
46     int fd = -1;
47     fd = open(path, O_RDONLY);
48     if (fd < 0) {
49         return false;
50     }
51 
52     if (read(fd, name, NAME_LEN -1) == -1) {
53         close(fd);
54         return false;
55     }
56 
57     char* p = name;
58     int i = 0;
59     while (*p != '\0') {
60         if ((*p == '\n') || (i == NAME_LEN)) {
61             break;
62         }
63         nameFilter[i] = *p;
64         p++, i++;
65     }
66     nameFilter[NAME_LEN - 1] = '\0';
67 
68     size_t cpyLen = strlen(nameFilter) + 1;
69     if (cpyLen > dstSz) {
70         cpyLen = dstSz;
71     }
72     memcpy(dst, nameFilter, cpyLen);
73     close(fd);
74     return true;
75 }
76 
GetThreadName(char * buffer,size_t bufferSz)77 bool GetThreadName(char* buffer, size_t bufferSz)
78 {
79     char path[NAME_LEN];
80     memset(path, '\0', sizeof(path));
81     if (snprintf(path, sizeof(path) - 1, "/proc/%d/comm", getpid()) <= 0) {
82         return false;
83     }
84     return ReadStringFromFile(path, buffer, bufferSz);
85 }
86 
GetProcessName(char * buffer,size_t bufferSz)87 bool GetProcessName(char* buffer, size_t bufferSz)
88 {
89     char path[NAME_LEN];
90     memset(path, '\0', sizeof(path));
91     if (snprintf(path, sizeof(path) - 1, "/proc/%d/cmdline", getpid()) <= 0) {
92         return false;
93     }
94     return ReadStringFromFile(path, buffer, bufferSz);
95 }
96 
GetTimeMilliseconds(void)97 uint64_t GetTimeMilliseconds(void)
98 {
99     struct timeval time;
100     gettimeofday(&time, NULL);
101     return ((uint64_t)time.tv_sec * 1000) + // 1000 : second to millisecond convert ratio
102         (((uint64_t)time.tv_usec) / 1000); // 1000 : microsecond to millisecond convert ratio
103 }
104 
105 #endif
106 #ifdef __cplusplus
107 }
108 #endif
109 #endif