aboutsummaryrefslogtreecommitdiff
path: root/include/perfetto/profiling/normalize.h
blob: 7b7da1d3c83273f72c427b3b1343c648214680f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef INCLUDE_PERFETTO_PROFILING_NORMALIZE_H_
#define INCLUDE_PERFETTO_PROFILING_NORMALIZE_H_

// Header only code that gets used in other projects.
// This is currently used in
// * ART
// * Bionic
// * Heapprofd
//
// DO NOT USE THE STL HERE. This gets used in parts of Bionic that do not
// use the STL.

#include <string.h>

namespace perfetto {
namespace profiling {

// Normalize cmdline in place. Stores new beginning of string in *cmdline_ptr.
// Returns new size of string (from new beginning).
// Modifies string in *cmdline_ptr.
static ssize_t NormalizeCmdLine(char** cmdline_ptr, size_t size) {
  char* cmdline = *cmdline_ptr;
  char* first_arg = static_cast<char*>(memchr(cmdline, '\0', size));
  if (first_arg == nullptr) {
    errno = EOVERFLOW;
    return -1;
  }
  // For consistency with what we do with Java app cmdlines, trim everything
  // after the @ sign of the first arg.
  char* first_at = static_cast<char*>(memchr(cmdline, '@', size));
  if (first_at != nullptr && first_at < first_arg) {
    *first_at = '\0';
    first_arg = first_at;
  }
  char* start = static_cast<char*>(
      memrchr(cmdline, '/', static_cast<size_t>(first_arg - cmdline)));
  if (start == nullptr) {
    start = cmdline;
  } else {
    // Skip the /.
    start++;
  }
  *cmdline_ptr = start;
  return first_arg - start;
}

}  // namespace profiling
}  // namespace perfetto

#endif  // INCLUDE_PERFETTO_PROFILING_NORMALIZE_H_