• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef INCLUDE_PERFETTO_PROFILING_NORMALIZE_H_
18 #define INCLUDE_PERFETTO_PROFILING_NORMALIZE_H_
19 
20 // Header only code that gets used in other projects.
21 // This is currently used in
22 // * ART
23 // * Bionic
24 // * Heapprofd
25 //
26 // DO NOT USE THE STL HERE. This gets used in parts of Bionic that do not
27 // use the STL.
28 
29 #include <string.h>
30 
31 namespace perfetto {
32 namespace profiling {
33 
34 // Normalize cmdline in place. Stores new beginning of string in *cmdline_ptr.
35 // Returns new size of string (from new beginning).
36 // Modifies string in *cmdline_ptr.
NormalizeCmdLine(char ** cmdline_ptr,size_t size)37 static ssize_t NormalizeCmdLine(char** cmdline_ptr, size_t size) {
38   char* cmdline = *cmdline_ptr;
39   char* first_arg = static_cast<char*>(memchr(cmdline, '\0', size));
40   if (first_arg == nullptr) {
41     errno = EOVERFLOW;
42     return -1;
43   }
44   // For consistency with what we do with Java app cmdlines, trim everything
45   // after the @ sign of the first arg.
46   char* first_at = static_cast<char*>(memchr(cmdline, '@', size));
47   if (first_at != nullptr && first_at < first_arg) {
48     *first_at = '\0';
49     first_arg = first_at;
50   }
51   char* start = static_cast<char*>(
52       memrchr(cmdline, '/', static_cast<size_t>(first_arg - cmdline)));
53   if (start == nullptr) {
54     start = cmdline;
55   } else {
56     // Skip the /.
57     start++;
58   }
59   *cmdline_ptr = start;
60   return first_arg - start;
61 }
62 
63 }  // namespace profiling
64 }  // namespace perfetto
65 
66 #endif  // INCLUDE_PERFETTO_PROFILING_NORMALIZE_H_
67