Skip to content

Commit

Permalink
facilitate non-interactive usage, i.e. linking cpuusage
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Feb 26, 2020
1 parent f596121 commit f9f28df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
3 changes: 2 additions & 1 deletion genman.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

cd src && help2man -n "instrumentation CPU profiler" -N -o cpuusage.1 ./cpuusage
mkdir -p build && cd build && cmake .. && make -s && \
cd ../src && help2man -n "instrumentation CPU profiler" -N -o cpuusage.1 ./cpuusage
exit ${?}

16 changes: 8 additions & 8 deletions src/cpuusage
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ showusage()
# Version
showversion()
{
echo "cpuusage v1.20"
echo "cpuusage v1.21"
echo ""
echo "Copyright (C) 2017-2018 Kristofer Berggren"
echo "Copyright (C) 2017-2020 Kristofer Berggren"
echo ""
echo "cpuusage is distributed under the BSD 3-Clause license."
echo ""
Expand All @@ -91,7 +91,7 @@ fi

# Arguments - regular options
ALL="0"
AUTO="1"
MANUAL="0"
DEBUG="0"
FUNCS=""
INCS=""
Expand Down Expand Up @@ -135,7 +135,7 @@ while getopts "?acdf:i:j:m:o:p:r:sv" OPT; do
READ="${OPTARG}"
;;
s)
AUTO="0"
MANUAL="1"
;;
v)
VERB="true"
Expand Down Expand Up @@ -253,7 +253,7 @@ if [ -f "${TMPDIR}/headers.txt" ] && [ -f "${TMPDIR}/prototypes.txt" ]; then
${VERB} && echo "cpuusage: starting program ${1}"
JSONPATH="${TMPDIR}/cutrace.json"
if [ "${DEBUG}" == "0" ]; then
CU_FILE="${JSONPATH}" CU_AUTO="${AUTO}" \
CU_FILE="${JSONPATH}" CU_MANUAL="${MANUAL}" \
LD_PRELOAD="${TMPDIR}/build/libwrapper.so" \
DYLD_INSERT_LIBRARIES="${TMPDIR}/build/libwrapper.dylib" \
DYLD_FORCE_FLAT_NAMESPACE=1 "${@:1}" &
Expand All @@ -262,7 +262,7 @@ if [ -f "${TMPDIR}/headers.txt" ] && [ -f "${TMPDIR}/prototypes.txt" ]; then
else
LLDBCMDPATH="${TMPDIR}/lldb.cmd"
echo "env CU_FILE=\"${JSONPATH}\"" > "${LLDBCMDPATH}"
echo "env CU_AUTO=\"${AUTO}\"" >> "${LLDBCMDPATH}"
echo "env CU_MANUAL=\"${MANUAL}\"" >> "${LLDBCMDPATH}"
echo "env LD_PRELOAD=\"${TMPDIR}/build/libwrapper.so\"" >> "${LLDBCMDPATH}"
echo "env DYLD_INSERT_LIBRARIES=\"${TMPDIR}/build/libwrapper.dylib\"" >> "${LLDBCMDPATH}"
echo "env DYLD_FORCE_FLAT_NAMESPACE=1" >> "${LLDBCMDPATH}"
Expand Down Expand Up @@ -297,7 +297,7 @@ elif [ -f "${TMPDIR}/nowrappers.txt" ]; then
${VERB} && echo "cpuusage: starting program ${1}"
JSONPATH="${TMPDIR}/cutrace.json"
if [ "${DEBUG}" == "0" ]; then
CU_FILE="${JSONPATH}" CU_AUTO="${AUTO}" \
CU_FILE="${JSONPATH}" CU_MANUAL="${MANUAL}" \
LD_PRELOAD="${LIBDIR}/libcpuusage.so" \
DYLD_INSERT_LIBRARIES="${LIBDIR}/libcpuusage.dylib" \
DYLD_FORCE_FLAT_NAMESPACE=1 "${@:1}" &
Expand All @@ -306,7 +306,7 @@ elif [ -f "${TMPDIR}/nowrappers.txt" ]; then
else
LLDBCMDPATH="${TMPDIR}/lldb.cmd"
echo "env CU_FILE=\"${JSONPATH}\"" > "${LLDBCMDPATH}"
echo "env CU_AUTO=\"${AUTO}\"" >> "${LLDBCMDPATH}"
echo "env CU_MANUAL=\"${MANUAL}\"" >> "${LLDBCMDPATH}"
echo "env LD_PRELOAD=\"${LIBDIR}/libcpuusage.so\"" >> "${LLDBCMDPATH}"
echo "env DYLD_INSERT_LIBRARIES=\"${LIBDIR}/libcpuusage.dylib\"" >> "${LLDBCMDPATH}"
echo "env DYLD_FORCE_FLAT_NAMESPACE=1" >> "${LLDBCMDPATH}"
Expand Down
4 changes: 2 additions & 2 deletions src/cpuusage.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.47.8.
.TH CPUUSAGE "1" "November 2019" "cpuusage v1.20" "User Commands"
.TH CPUUSAGE "1" "February 2020" "cpuusage v1.21" "User Commands"
.SH NAME
cpuusage \- instrumentation CPU profiler
.SH SYNOPSIS
Expand Down Expand Up @@ -103,6 +103,6 @@ Written by Kristofer Berggren
.SH "REPORTING BUGS"
Report bugs at https://github.com/d99kris/cpuusage
.SH COPYRIGHT
Copyright \(co 2017\-2018 Kristofer Berggren
Copyright \(co 2017\-2020 Kristofer Berggren
.PP
cpuusage is distributed under the BSD 3\-Clause license.
12 changes: 8 additions & 4 deletions src/cumain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,24 @@ static std::string cu_addr_to_symbol(void *addr);
/* ----------- Global Functions ---------------------------------- */
void __attribute__ ((constructor)) cu_init(void)
{
char *manual = getenv("CU_AUTO");
if (getenv("LD_PRELOAD") == NULL) return;

char *manual = getenv("CU_MANUAL");
if ((manual != NULL) && (strncmp(manual, "1", 1) == 0))
{
cu_start_tracing(0);
signal(SIGUSR1, cu_start_tracing);
signal(SIGUSR2, cu_stop_tracing);
}
else
{
signal(SIGUSR1, cu_start_tracing);
signal(SIGUSR2, cu_stop_tracing);
cu_start_tracing(0);
}
}

void __attribute__ ((destructor)) cu_fini(void)
{
if (getenv("LD_PRELOAD") == NULL) return;

cu_stop_tracing(0);
}

Expand Down

0 comments on commit f9f28df

Please sign in to comment.