Skip to content

Commit

Permalink
Plug the test reources build into gradle scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jbachorik committed Jun 4, 2024
1 parent 9e6c379 commit 22201f7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
20 changes: 20 additions & 0 deletions ddprof-lib/gtest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ tasks.withType(StripSymbols).configureEach { task ->
}
}

def buildResourcesTask = tasks.register("buildResources", Exec) {
group = 'build'
description = "Build the resources for the Google Tests"

onlyIf {
hasGtest && !project.hasProperty('skip-native') && os().isLinux()
}

def targetDir = project(':ddprof-lib').file('build/test/resources/unresolved-functions')

commandLine "sh", "-c", "cd ${project(':ddprof-lib').projectDir}/src/test/resources/unresolved-functions && make TARGET_DIR=${targetDir}"

inputs.files project(':ddprof-lib').files('src/test/resources/unresolved-functions')
outputs.file "${targetDir}/main"
}

def gtestAll = tasks.register("gtest") {
onlyIf {
hasGtest && !project.hasProperty('skip-native')
Expand Down Expand Up @@ -155,6 +171,10 @@ tasks.whenTaskAdded { task ->
gtestLinkTask.dependsOn compileTask
}
gtestTask.get().dependsOn gtestExecuteTask.get()
if (os.isLinux()) {
// custom binaries for tests are built only on linux
gtestExecuteTask.get().dependsOn buildResourcesTask
}
gtestAll.get().dependsOn gtestExecuteTask.get()
}
}
Expand Down
7 changes: 1 addition & 6 deletions ddprof-lib/src/main/cpp/symbols_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ bool ElfParser::parseFile(CodeCache* cc, const char* base, const char* file_name
}

size_t length = (size_t)lseek64(fd, 0, SEEK_END);
fprintf(stdout, "===> Parsing file: %s, length=%lu\n", file_name, length);
void* addr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);

Expand All @@ -85,7 +84,6 @@ bool ElfParser::parseFile(CodeCache* cc, const char* base, const char* file_name
} else {
ElfParser elf(cc, base != nullptr ? base : (const char*)addr, addr, file_name, length, false);
if (elf.validHeader()) {
fprintf(stdout, "===> Loading symbols for: %s (%lu)\n", file_name, length);
elf.loadSymbols(use_debug);
}
munmap(addr, length);
Expand Down Expand Up @@ -333,17 +331,15 @@ bool ElfParser::loadSymbolsUsingDebugLink() {
}

void ElfParser::loadSymbolTable(const char* symbols, size_t total_size, size_t ent_size, const char* strings) {
fprintf(stdout, "===> Loading symbol table at %p, base=%p, size=%lu, entry_size=%lu\n", symbols, _base, total_size, ent_size);
for (const char* symbols_end = symbols + total_size; symbols < symbols_end; symbols += ent_size) {
ElfSymbol* sym = (ElfSymbol*)symbols;
if (sym->st_name != 0 && sym->st_value != 0) {
// sanity check the offsets not to exceed the file size
if (_length == 0 || (sym->st_name < _length && sym->st_value < _length)) {
// Skip special AArch64 mapping symbols: $x and $d
if (sym->st_size != 0 || sym->st_info != 0 || strings[sym->st_name] != '$') {
_cc->add(_base + sym->st_value, (int)sym->st_size, strings + sym->st_name);
}
} else {
fprintf(stdout, "===> Skipping symbol: value=%lu, name=%u, stt=%d\n", sym->st_value, sym->st_name, sym->st_info & 0xf);
}
}
}
Expand Down Expand Up @@ -450,7 +446,6 @@ void Symbols::parseLibraries(CodeCacheArray* array, bool kernel_symbols) {

while ((len = getline(&str, &str_size, f)) > 0) {
str[len - 1] = 0;
fprintf(stdout, "===> Parsing library: %s\n", str);
MemoryMapDesc map(str);
if (!map.isReadable() || map.file() == NULL || map.file()[0] == 0) {
continue;
Expand Down
3 changes: 1 addition & 2 deletions ddprof-lib/src/test/cpp/elfparser_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ TEST(Elf, readSymTable) {
if (getcwd(cwd, sizeof(cwd)) == nullptr) {
exit(1);
}
fprintf(stdout, "Current working directory: %s\n", cwd);
char path[PATH_MAX];
snprintf(path, sizeof(path) - 1, "%s/../src/test/resources/libj9jit.so", cwd);
snprintf(path, sizeof(path) - 1, "%s/../build/test/resources/unresolved-functions/main", cwd);
if (access(path, R_OK) != 0) {
fprintf(stdout, "Missing test resource %s. Skipping the test\n", path);
exit(0);
Expand Down
5 changes: 3 additions & 2 deletions ddprof-lib/src/test/resources/unresolved-functions/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
TARGET_DIR = ../build/test/resources/unresolved-functions
all:
gcc -c main.c -o main.o
gcc -o main main.o -T linker.ld
gcc -c main.c -o $(TARGET_DIR)/main.o
gcc -o $(TARGET_DIR)/main $(TARGET_DIR)/main.o -T linker.ld
36 changes: 33 additions & 3 deletions ddprof-lib/src/test/resources/unresolved-functions/linker.ld
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
PHDRS
{
headers PT_PHDR PHDRS ;
interp PT_INTERP ;
text PT_LOAD FILEHDR PHDRS ;
data PT_LOAD ;
}

SECTIONS
{
. = 0x10000;
.text : { *(.text) }
.text : {
*(.text)
} :text

. = 0x20000;
.data : { *(.data) }
.bss : { *(.bss) }
.data : {
*(.data)
} :data

.bss : {
*(.bss)
}

. = 0x30000;
unresolved_symbol = .;
. = 0xffffffffffffffff;
unresolved_function = .;

/* Add the .init_array section */
.init_array : {
__init_array_start = .;
KEEP(*(.init_array))
__init_array_end = .;
}

/* Add the .fini_array section */
.fini_array : {
__fini_array_start = .;
KEEP(*(.fini_array))
__fini_array_end = .;
}
}

0 comments on commit 22201f7

Please sign in to comment.