| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include <stdbool.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "drgn_internal.h" |
| 8 |
#include "dwarf_constants.h" |
| 9 |
#include "path.h" |
| 10 |
#include "util.h" |
| 11 |
|
| 12 |
bool path_iterator_next(struct path_iterator *it, const char **component_ret, |
| 13 |
size_t *component_len_ret) |
| 14 |
{ |
| 15 |
while (it->num_components) { |
| 16 |
struct nstring *cur = &it->components[it->num_components - 1]; |
| 17 |
while (cur->len > 0) { |
| 18 |
if (cur->str[cur->len - 1] == '/') { |
| 19 |
if (cur->len == 1) { |
| 20 |
/* |
| 21 |
* This is an absolute path. Emit an |
| 22 |
* empty component. Components joined |
| 23 |
* before this one and remaining ".." |
| 24 |
* components are not meaningful. |
| 25 |
*/ |
| 26 |
it->num_components = 0; |
| 27 |
it->dot_dot = 0; |
| 28 |
*component_ret = ""; |
| 29 |
*component_len_ret = 0; |
| 30 |
return true; |
| 31 |
} |
| 32 |
/* Skip redundant slashes. */ |
| 33 |
cur->len--; |
| 34 |
continue; |
| 35 |
} |
| 36 |
|
| 37 |
size_t component_start = cur->len - 1; |
| 38 |
while (component_start > 0 && |
| 39 |
cur->str[component_start - 1] != '/') |
| 40 |
component_start--; |
| 41 |
size_t component_len = cur->len - component_start; |
| 42 |
cur->len = component_start; |
| 43 |
if (component_len == 1 && |
| 44 |
cur->str[component_start] == '.') { |
| 45 |
/* Skip "." components. */ |
| 46 |
} else if (component_len == 2 && |
| 47 |
cur->str[component_start] == '.' && |
| 48 |
cur->str[component_start + 1] == '.') { |
| 49 |
/* Count ".." components. */ |
| 50 |
it->dot_dot++; |
| 51 |
} else if (it->dot_dot) { |
| 52 |
it->dot_dot--; |
| 53 |
} else { |
| 54 |
*component_ret = &cur->str[component_start]; |
| 55 |
*component_len_ret = component_len; |
| 56 |
return true; |
| 57 |
} |
| 58 |
} |
| 59 |
it->num_components--; |
| 60 |
} |
| 61 |
|
| 62 |
if (it->dot_dot) { |
| 63 |
/* |
| 64 |
* Leftover ".." components must be above the current |
| 65 |
* directory. |
| 66 |
*/ |
| 67 |
it->dot_dot--; |
| 68 |
*component_ret = ".."; |
| 69 |
*component_len_ret = 2; |
| 70 |
return true; |
| 71 |
} |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
bool path_ends_with(struct path_iterator *haystack, |
| 76 |
struct path_iterator *needle) |
| 77 |
{ |
| 78 |
for (;;) { |
| 79 |
const char *h_component, *n_component; |
| 80 |
size_t h_component_len, n_component_len; |
| 81 |
|
| 82 |
if (!path_iterator_next(needle, &n_component, &n_component_len)) |
| 83 |
return true; |
| 84 |
|
| 85 |
if (!path_iterator_next(haystack, &h_component, |
| 86 |
&h_component_len)) |
| 87 |
return false; |
| 88 |
|
| 89 |
if (h_component_len != n_component_len || |
| 90 |
memcmp(h_component, n_component, h_component_len) != 0) |
| 91 |
return false; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
bool die_matches_filename(Dwarf_Die *die, const char *filename) |
| 96 |
{ |
| 97 |
if (!filename || !filename[0]) |
| 98 |
return true; |
| 99 |
|
| 100 |
struct nstring die_components[2]; |
| 101 |
struct path_iterator die_path = { |
| 102 |
.components = die_components, |
| 103 |
}; |
| 104 |
|
| 105 |
Dwarf_Die cu_die; |
| 106 |
Dwarf_Attribute attr_mem, *attr; |
| 107 |
attr = dwarf_attr_integrate(dwarf_diecu(die, &cu_die, NULL, NULL), |
| 108 |
DW_AT_comp_dir, &attr_mem); |
| 109 |
const char *path = dwarf_formstring(attr); |
| 110 |
if (path) { |
| 111 |
die_path.components[die_path.num_components].str = path; |
| 112 |
die_path.components[die_path.num_components].len = strlen(path); |
| 113 |
die_path.num_components++; |
| 114 |
} |
| 115 |
|
| 116 |
path = dwarf_decl_file(die); |
| 117 |
if (!path) |
| 118 |
return false; |
| 119 |
/* |
| 120 |
* If the declaration file name is absolute, the compilation directory |
| 121 |
* component will be ignored. |
| 122 |
*/ |
| 123 |
die_path.components[die_path.num_components].str = path; |
| 124 |
die_path.components[die_path.num_components].len = strlen(path); |
| 125 |
die_path.num_components++; |
| 126 |
|
| 127 |
struct path_iterator needle = { |
| 128 |
.components = (struct nstring []){ |
| 129 |
{ filename, strlen(filename) } |
| 130 |
}, |
| 131 |
.num_components = 1, |
| 132 |
}; |
| 133 |
|
| 134 |
return path_ends_with(&die_path, &needle); |
| 135 |
} |
| 136 |
|
| 137 |
LIBDRGN_PUBLIC bool drgn_filename_matches(const char *haystack, |
| 138 |
const char *needle) |
| 139 |
{ |
| 140 |
struct path_iterator haystack_path = { |
| 141 |
.components = (struct nstring []){ |
| 142 |
{ haystack, strlen(haystack) } |
| 143 |
}, |
| 144 |
.num_components = 1, |
| 145 |
}; |
| 146 |
struct path_iterator needle_path = { |
| 147 |
.components = (struct nstring []){ |
| 148 |
{ needle, strlen(needle) } |
| 149 |
}, |
| 150 |
.num_components = 1, |
| 151 |
}; |
| 152 |
return path_ends_with(&haystack_path, &needle_path); |
| 153 |
} |