Skip to content
Permalink
Browse files

Merge pull request #1 from pdkl95/master

makefile and util scrips
  • Loading branch information...
FooBarWidget committed Mar 18, 2019
2 parents 77d2772 + f748601 commit 484c3874fd25e789a05e786485ff914e43522e2c
Showing with 126 additions and 2 deletions.
  1. +2 −0 .gitignore
  2. +32 −0 Makefile
  3. +60 −0 find_glibc_mapping.sh
  4. +32 −0 find_main_arena.sh
  5. +0 −2 ptmallocdump.c
@@ -0,0 +1,2 @@
libptmallocdump.so
glibc_main_arena_address
@@ -0,0 +1,32 @@

CFLAGS = -g -fPIC -DPIC -Wall -fno-strict-aliasing
LDFLAGS =
CC = gcc -std=c99
RM = rm -f

DUMPER_SRC = ptmallocdump.c
DUMPER_LIB = libptmallocdump.so

GLIBC_MAIN_ARENA_CACHE = glibc_main_arena_address

TARGETS = $(DUMPER_LIB) $(GLIBC_MAIN_ARENA_CACHE)


all: build
build: $(TARGETS)

$(DUMPER_LIB): $(DUMPER_SRC)
$(CC) $(CFLAGS) -shared $^ $(LDFLAGS) -o $@

$(GLIBC_MAIN_ARENA_CACHE): $(DUMPER_LIB)
./find_main_arena.sh > $@

clean:
@for file in $(TARGETS) ; do \
if test -f "$$file" ; then \
echo "$(RM) \"$$file\"" ; \
$(RM) "$$file" ; \
fi ; \
done

.PHONY: all build clean
@@ -0,0 +1,60 @@
#!/bin/sh

die() {
echo "$0: ERROR - $*" 1>&2
exit 1
}

show_usage() {
echo "Usage: $0 [options] <pid>"
echo
echo "OPTIONS"
echo " -a, --add OFFSET Add the given hexidecimal offset"
echo " to the mapped base address."
}

if test $# -lt 1 ; then
show_usage
exit 1
fi

mode=print
offset=0

if type getopt 2>&1 >/dev/null ; then
# have GNU getopt (allows nicer options)
SOPT="ha:"
LOPT="help,add:"
OPTIONS=$(getopt -o "$SOPT" --long "$LOPT" -n "$0" -- "$@") || exit 1
eval set -- "$OPTIONS"
fi

while true ; do
case "$1" in
-h | --help) show_usage ; exit 0 ;;
-a | --add) mode=add ; offset=$2 ; shift 2 ;;
--) shift ; break ;;
-*) die "bad opt: $1" ;;
*) break ;;
esac
done

pid=$1

find_mapping_addr() {
grep '/libc-[0-9.]*\.so$' /proc/${pid}/maps |
grep ' r-xp ' |
cut -d- -f 1
}

mapping_addr="$(find_mapping_addr)"

sum_base_and_offset() {
echo "ibase = 16; obase = 10; ${offset} + ${mapping_addr}" | bc
}

case $mode in
print) echo "${mapping_addr}" ;;
add) sum_base_and_offset ;;
*) die "not a mode: \"${mode}\"" ;;
esac
@@ -0,0 +1,32 @@
#!/bin/sh

DEBUG_FILE_DIR="/usr/lib/debug"

die() {
echo "$0: ERROR - $*" 1>&2
exit 1
}

DUMPER_LIB="$( grep ^DUMPER_LIB Makefile | awk '{ print $3 }' )"
if ! test -f "${DUMPER_LIB}" ; then
die "missing DUMPER_LIB (\"${DUMPER_LIB}\")"
fi

libc_soname_path() {
ldd "${DUMPER_LIB}" |
tr -d '\t' |
grep ^libc.so.6 |
awk '{ print $3 }'
}

real_libc_path="$( readlink -e "$(libc_soname_path)" )"
libc_debug_path="${DEBUG_FILE_DIR}/${real_libc_path}.debug"

if ! test -f "${libc_debug_path}" ; then
die "missing libc debug symbols \"${libc_debug_path}\""
fi

objdump -t "${libc_debug_path}" |
grep ' main_arena' |
awk '{ print $1 }' |
tr 'a-f' 'A-F'
@@ -1,5 +1,3 @@
// gcc -shared -g ptmallocdump.c -fPIC -o libptmallocdump.so -Wall -fno-strict-aliasing

#include <stddef.h>
#include <stdio.h>
#include <stdint.h>

0 comments on commit 484c387

Please sign in to comment.
You can’t perform that action at this time.