Skip to content

Commit ae2013e

Browse files
authored
Merge pull request #48778 from makortel/cmsTraceFunctionTest
Extend `cmsTraceFunction` test to `putenv()` and `--abort`
2 parents 73d3859 + f388711 commit ae2013e

File tree

2 files changed

+87
-8
lines changed

2 files changed

+87
-8
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
#include <cstring>
4+
#include <stdlib.h>
5+
6+
class ScheduleItems {
7+
public:
8+
ScheduleItems() {}
9+
void initMisc();
10+
};
11+
12+
void ScheduleItems::initMisc() { std::cout << "ScheduleItems::initMisc() called" << std::endl; }
13+
14+
void my_putenv(const char* env, char* put) {
15+
putenv(put);
16+
std::cout << "putenv() called" << std::endl;
17+
std::cout << env << "=" << std::getenv(env) << std::endl;
18+
}
19+
20+
int main() {
21+
// putenv() expects modifiable char array that lives throughout the program
22+
char* foo1 = new char[10];
23+
char* foo2 = new char[10];
24+
char* foo3 = new char[10];
25+
std::strncpy(foo1, "FOO=1", 10);
26+
std::strncpy(foo2, "FOO=2", 10);
27+
std::strncpy(foo3, "FOO=3", 10);
28+
29+
my_putenv("FOO", foo1);
30+
ScheduleItems obj;
31+
obj.initMisc();
32+
my_putenv("FOO", foo2);
33+
my_putenv("FOO", foo3);
34+
return 0;
35+
}
Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
11
#!/bin/bash -ex
2-
g++ -o test-cmsTraceFunction-setenv $(dirname $0)/test-cmsTraceFunction-setenv.cpp
3-
cmsTraceFunction --startAfterFunction ScheduleItems::initMisc setenv ./test-cmsTraceFunction-setenv 2>&1 | grep setenv > setenv.log
4-
rm -f test-cmsTraceFunction-setenv
5-
setenv_count=$(grep '^setenv() called' setenv.log | wc -l)
6-
break_setenv=$(grep 'Breakpoint .* in setenv ()' setenv.log | wc -l)
7-
if [ ${setenv_count} != 3 ] || [ ${break_setenv} != 2 ] ; then
8-
exit 1
9-
fi
2+
3+
TRACE="cmsTraceFunction --startAfterFunction ScheduleItems::initMisc setenv -f putenv"
4+
5+
check_func() {
6+
local func_name="$1"
7+
local src_name="$2"
8+
local trace_opts="$3"
9+
local exe_name="test-cmsTraceFunction-${func_name}"
10+
local raw_log="${func_name}_raw.log"
11+
local log="${func_name}.log"
12+
13+
g++ -o "$exe_name" "$(dirname $0)/$src_name"
14+
set +e
15+
$TRACE $trace_opts ./$exe_name 2>&1 > "$raw_log"
16+
local ret=$?
17+
set -e
18+
grep "$func_name" "$raw_log" > "$log"
19+
20+
if [ ${trace_opts} = "--abort" ]; then
21+
call_count_expected=1
22+
break_count_expected=1
23+
24+
if [ ${ret} = 0 ]; then
25+
echo "cmsTraceFunction exited with exit code 0, expected non-zero exit code"
26+
exit 1
27+
fi
28+
else
29+
call_count_expected=3
30+
break_count_expected=2
31+
32+
if [ ${ret} != 0 ]; then
33+
echo "cmsTraceFunction exited with exit code $ret, expected zero exit code"
34+
exit 1
35+
fi
36+
fi
37+
38+
local call_count=$(grep -c "^${func_name}() called" "$log")
39+
local break_count=$(grep -c "Breakpoint .* in ${func_name} ()" "$log")
40+
if [ ${call_count} != ${call_count_expected} ] || [ ${break_count} != ${break_count_expected} ] ; then
41+
echo "Unexpected number of ${func_name} calls ${call_count} or breakpoints ${break_count}; expecting calls ${call_count_expected} and breakpoints ${break_count_expected}"
42+
exit 1
43+
fi
44+
}
45+
46+
# Check setenv
47+
check_func "setenv" "test-cmsTraceFunction-setenv.cpp" ""
48+
check_func "setenv" "test-cmsTraceFunction-setenv.cpp" "--abort"
49+
50+
51+
# Check putenv
52+
check_func "putenv" "test-cmsTraceFunction-putenv.cpp" ""
53+
check_func "putenv" "test-cmsTraceFunction-putenv.cpp" "--abort"

0 commit comments

Comments
 (0)