Skip to content

Commit 32742e0

Browse files
authored
Merge pull request #180 from DataDog/jb/tiny_downstream
A few minor downport issues
2 parents 0c9e478 + 3d4be3a commit 32742e0

File tree

24 files changed

+355
-187
lines changed

24 files changed

+355
-187
lines changed

ddprof-lib/gtest/build.gradle

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,37 @@ tasks.withType(StripSymbols).configureEach { task ->
3737
}
3838
}
3939

40-
def buildResourcesTask = tasks.register("buildResources", Exec) {
40+
def buildNativeLibsTask = tasks.register("buildNativeLibs") {
4141
group = 'build'
42-
description = "Build the resources for the Google Tests"
42+
description = "Build the native libs for the Google Tests"
4343

4444
onlyIf {
4545
hasGtest && !project.hasProperty('skip-native') && !project.hasProperty('skip-gtest') && os().isLinux()
4646
}
4747

48-
def targetDir = project(':ddprof-lib').file('build/test/resources/unresolved-functions')
49-
50-
commandLine "sh", "-c", "cd ${project(':ddprof-lib').projectDir}/src/test/resources/unresolved-functions && make TARGET_DIR=${targetDir}"
51-
52-
inputs.files project(':ddprof-lib').files('src/test/resources/unresolved-functions')
53-
outputs.file "${targetDir}/main"
54-
}
55-
56-
def buildSmallLibTask = tasks.register("buildSmallLib", Exec) {
57-
group = 'build'
58-
description = "Build the small-lib shared library for the Google Tests"
59-
60-
onlyIf {
61-
hasGtest && !project.hasProperty('skip-native') && !project.hasProperty('skip-gtest') && os().isLinux()
48+
def srcDir = project(':ddprof-lib').file('src/test/resources/native-libs')
49+
def targetDir = project(':ddprof-lib').file('build/test/resources/native-libs/')
50+
51+
// Move the exec calls to the execution phase
52+
doLast {
53+
srcDir.eachDir { dir ->
54+
def libName = dir.name
55+
def libDir = file("${targetDir}/${libName}")
56+
def libSrcDir = file("${srcDir}/${libName}")
57+
58+
exec {
59+
commandLine "sh", "-c", """
60+
echo "Processing library: ${libName} @ ${libSrcDir}"
61+
mkdir -p ${libDir}
62+
cd ${libSrcDir}
63+
make TARGET_DIR=${libDir}
64+
"""
65+
}
66+
}
6267
}
6368

64-
def sourceDir = project(':ddprof-lib').file('src/test/resources/small-lib')
65-
def targetDir = project(':ddprof-lib').file('build/test/resources/small-lib')
66-
67-
commandLine "sh", "-c", """
68-
mkdir -p ${targetDir}
69-
cd ${sourceDir} && g++ -fPIC -shared -o ${targetDir}/libsmall-lib.so *.cpp
70-
"""
71-
72-
inputs.files fileTree(sourceDir) {
73-
include '**/*.cpp'
74-
}
75-
outputs.file "${targetDir}/libsmall-lib.so"
69+
inputs.files project(':ddprof-lib').files('src/test/resources/native-libs/**/*')
70+
outputs.dir "${targetDir}"
7671
}
7772

7873
def gtestAll = tasks.register("gtest") {
@@ -204,7 +199,7 @@ tasks.whenTaskAdded { task ->
204199
gtestTask.get().dependsOn gtestExecuteTask.get()
205200
if (os().isLinux()) {
206201
// custom binaries for tests are built only on linux
207-
gtestExecuteTask.get().dependsOn(buildResourcesTask, buildSmallLibTask)
202+
gtestExecuteTask.get().dependsOn(buildNativeLibs)
208203
}
209204
gtestAll.get().dependsOn gtestExecuteTask.get()
210205
}

ddprof-lib/src/main/cpp/arch.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
/*
2-
* Copyright 2017 Andrei Pangin
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.
2+
* Copyright The async-profiler authors
3+
* SPDX-License-Identifier: Apache-2.0
154
*/
165

176
#ifndef _ARCH_H
187
#define _ARCH_H
198

209
#include <stddef.h>
2110

11+
#ifndef unlikely
12+
# define unlikely(x) (__builtin_expect(!!(x), 0))
13+
#endif
14+
15+
#define callerPC() __builtin_return_address(0)
16+
2217
#ifdef _LP64
2318
# define LP64_ONLY(code) code
2419
#else // !_LP64
2520
# define LP64_ONLY(code)
2621
#endif // _LP64
2722

23+
2824
typedef unsigned char u8;
2925
typedef unsigned short u16;
3026
typedef unsigned int u32;

ddprof-lib/src/main/cpp/callTraceStorage.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
/*
2-
* Copyright 2020 Andrei Pangin
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.
2+
* Copyright The async-profiler authors
3+
* SPDX-License-Identifier: Apache-2.0
154
*/
165

176
#include "callTraceStorage.h"

ddprof-lib/src/main/cpp/codeCache.cpp

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
/*
2-
* Copyright 2016 Andrei Pangin
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.
2+
* Copyright The async-profiler authors
3+
* SPDX-License-Identifier: Apache-2.0
154
*/
165

176
#include "codeCache.h"
@@ -274,48 +263,88 @@ void CodeCache::findSymbolsByPrefix(std::vector<const char *> &prefixes,
274263
}
275264
}
276265

277-
void CodeCache::addImport(void **entry, const char *name) {
278-
switch (name[0]) {
279-
case 'd':
280-
if (strcmp(name, "dlopen") == 0) {
281-
_imports[im_dlopen] = entry;
266+
void CodeCache::saveImport(ImportId id, void** entry) {
267+
for (int ty = 0; ty < NUM_IMPORT_TYPES; ty++) {
268+
if (_imports[id][ty] == nullptr) {
269+
_imports[id][ty] = entry;
270+
return;
271+
}
282272
}
283-
break;
284-
case 'p':
285-
if (strcmp(name, "pthread_create") == 0) {
286-
_imports[im_pthread_create] = entry;
287-
} else if (strcmp(name, "pthread_exit") == 0) {
288-
_imports[im_pthread_exit] = entry;
289-
} else if (strcmp(name, "pthread_setspecific") == 0) {
290-
_imports[im_pthread_setspecific] = entry;
273+
}
274+
275+
void CodeCache::addImport(void **entry, const char *name) {
276+
switch (name[0]) {
277+
case 'c':
278+
if (strcmp(name, "calloc") == 0) {
279+
saveImport(im_calloc, entry);
280+
}
281+
break;
282+
case 'd':
283+
if (strcmp(name, "dlopen") == 0) {
284+
saveImport(im_dlopen, entry);
285+
}
286+
break;
287+
case 'f':
288+
if (strcmp(name, "free") == 0) {
289+
saveImport(im_free, entry);
290+
}
291+
break;
292+
case 'm':
293+
if (strcmp(name, "malloc") == 0) {
294+
saveImport(im_malloc, entry);
295+
}
296+
break;
297+
case 'p':
298+
if (strcmp(name, "pthread_create") == 0) {
299+
saveImport(im_pthread_create, entry);
300+
} else if (strcmp(name, "pthread_exit") == 0) {
301+
saveImport(im_pthread_exit, entry);
302+
} else if (strcmp(name, "pthread_setspecific") == 0) {
303+
saveImport(im_pthread_setspecific, entry);
304+
} else if (strcmp(name, "poll") == 0) {
305+
saveImport(im_poll, entry);
306+
}
307+
break;
308+
case 'r':
309+
if (strcmp(name, "realloc") == 0) {
310+
saveImport(im_realloc, entry);
311+
}
312+
break;
291313
}
292-
break;
293-
}
294314
}
295315

296316
void **CodeCache::findImport(ImportId id) {
297317
if (!_imports_patchable) {
298318
makeImportsPatchable();
299319
_imports_patchable = true;
300320
}
301-
return _imports[id];
321+
return _imports[id][PRIMARY];
302322
}
303323

304324
void CodeCache::patchImport(ImportId id, void *hook_func) {
305-
void **entry = findImport(id);
325+
if (!_imports_patchable) {
326+
makeImportsPatchable();
327+
_imports_patchable = true;
328+
}
329+
330+
for (int ty = 0; ty < NUM_IMPORT_TYPES; ty++) {void **entry = _imports[id][ty];
306331
if (entry != NULL) {
307332
*entry = hook_func;
308-
}
333+
}}
309334
}
310335

311336
void CodeCache::makeImportsPatchable() {
312337
void **min_import = (void **)-1;
313338
void **max_import = NULL;
314339
for (int i = 0; i < NUM_IMPORTS; i++) {
315-
if (_imports[i] != NULL && _imports[i] < min_import)
316-
min_import = _imports[i];
317-
if (_imports[i] != NULL && _imports[i] > max_import)
318-
max_import = _imports[i];
340+
for (int j = 0; j < NUM_IMPORT_TYPES; j++) {
341+
void** entry = _imports[i][j];
342+
if (entry == NULL) continue;
343+
if (entry < min_import)
344+
min_import = entry;
345+
if (entry > max_import)
346+
max_import = entry;
347+
}
319348
}
320349

321350
if (max_import != NULL) {

ddprof-lib/src/main/cpp/codeCache.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
/*
2-
* Copyright 2017 Andrei Pangin
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.
2+
* Copyright The async-profiler authors
3+
* SPDX-License-Identifier: Apache-2.0
154
*/
165

176
#ifndef _CODECACHE_H
@@ -35,9 +24,20 @@ enum ImportId {
3524
im_pthread_create,
3625
im_pthread_exit,
3726
im_pthread_setspecific,
27+
im_poll,
28+
im_malloc,
29+
im_calloc,
30+
im_realloc,
31+
im_free,
3832
NUM_IMPORTS
3933
};
4034

35+
enum ImportType {
36+
PRIMARY,
37+
SECONDARY,
38+
NUM_IMPORT_TYPES
39+
};
40+
4141
class NativeFunc {
4242
private:
4343
short _lib_index;
@@ -58,7 +58,7 @@ class NativeFunc {
5858
if (posix_memalign((void**)(&func), sizeof(NativeFunc*), sizeof(NativeFunc)) != 0) {
5959
return -1;
6060
}
61-
return func->_lib_index;
61+
return func->_lib_index;
6262
}
6363

6464
static bool isMarked(const char *name) { return from(name)->_mark != 0; }
@@ -100,7 +100,7 @@ class CodeCache {
100100
unsigned int _plt_offset;
101101
unsigned int _plt_size;
102102

103-
void **_imports[NUM_IMPORTS];
103+
void **_imports[NUM_IMPORTS][NUM_IMPORT_TYPES];
104104
bool _imports_patchable;
105105
bool _debug_symbols;
106106

@@ -113,6 +113,7 @@ class CodeCache {
113113

114114
void expand();
115115
void makeImportsPatchable();
116+
void saveImport(ImportId id, void** entry);
116117

117118
public:
118119
explicit CodeCache(const char *name, short lib_index = -1,

ddprof-lib/src/main/cpp/flightRecorder.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
/*
2-
* Copyright 2018 Andrei Pangin
3-
* Copyright 2021, 2023 Datadog, Inc
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License");
6-
* you may not use this file except in compliance with the License.
7-
* You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
2+
* Copyright The async-profiler authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright 2021, 2025 Datadog, Inc
165
*/
176

187
#include <assert.h>
@@ -133,7 +122,7 @@ void Lookup::fillJavaMethodInfo(MethodInfo *mi, jmethodID method,
133122
jvmtiEnv *jvmti = VM::jvmti();
134123

135124
jvmtiPhase phase;
136-
jclass method_class;
125+
jclass method_class = NULL;
137126
// invariant: these strings must remain null, or be assigned by JVMTI
138127
char *class_name = nullptr;
139128
char *method_name = nullptr;
@@ -590,10 +579,10 @@ bool Recording::parseAgentProperties() {
590579
if (get_agent_props != NULL && to_string != NULL) {
591580
jobject props = env->CallStaticObjectMethod(vm_support, get_agent_props);
592581
jniExceptionCheck(env);
593-
if (props != NULL) {
582+
if (props != NULL && !env->ExceptionCheck()) {
594583
jstring str = (jstring)env->CallObjectMethod(props, to_string);
595584
jniExceptionCheck(env);
596-
if (str != NULL) {
585+
if (str != NULL && !env->ExceptionCheck()) {
597586
_agent_properties = (char *)env->GetStringUTFChars(str, NULL);
598587
}
599588
}

ddprof-lib/src/main/cpp/profiler.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
/*
2-
* Copyright 2016 Andrei Pangin
3-
* Copyright 2024 Datadog, Inc
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License");
6-
* you may not use this file except in compliance with the License.
7-
* You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
11-
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
2+
* Copyright The async-profiler authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright 2024, 2025 Datadog, Inc
165
*/
176

187
#include "profiler.h"

0 commit comments

Comments
 (0)