#!/bin/bash

if [ "$#" -ne 2 ]; then
	echo "Usage: ${0} /path/to/pdf /path/to/libhammer.so"
	echo "WARNING: For now, parameters are passed unchecked to perf probe"
	exit 1
fi

PDFPATH=$1
HAMMERPATH=$2

perf probe -x $PDFPATH pdf.c:1223 'pdf tail xr_td dict dict_ dopen body header objdef indobj obj array array_ lbrack rbrack elemd elemd_ elemr elemr_ robj dobj name npair string hexstr rangle langle litstr stream xrstm xstream xrefs xrsub xrhead xrent xrgen xroff a85string ahexstream k_v'

perf probe -x $HAMMERPATH h_arena_malloc_raw '$params'
perf probe -x $PDFPATH init_runlengthdecode_parser '$vars'
perf probe -x $PDFPATH kstream '$params'
perf probe -x $PDFPATH kxstream '$params'
perf probe -x $PDFPATH FlateDecode '$params'
perf probe -x $PDFPATH LZWDecode '$params'
perf probe -x $PDFPATH RunLengthDecode '$params'
perf probe -x $PDFPATH ASCII85Decode '$params'
perf probe -x $PDFPATH ASCIIHexDecode '$params'

# virtual functions to be called by h_parse

# parse_choice (tracepoint inside loop)
# originally we'd have pushed/popped parsers here in the processing script, but you can't use locals in an array subscript at tracepoint definition
perf probe -x $HAMMERPATH choice.c:28 '$params' 's' 'i' 's->p_array'
# perf probe -n -x $HAMMERPATH choice.c:28 '$params' 's' 'i' 's->p_array + i * 8'

# parse_sequence
#perf probe -x $HAMMERPATH sequence.c:16 '$params' 's' 'i' 's->p_array'
perf probe -x $HAMMERPATH parse_sequence '$params' 's' 's->p_array'
#perf probe -n -x $HAMMERPATH sequence.c:16 '$params' 's' 'i' 's->p_array'

# parse_difference
# HTwoParsers
perf probe -x $HAMMERPATH difference.c:14 '$params' 'parsers' 'parsers->p1' 'parsers->p2'

# parse_many
# HRepeat
perf probe -x $HAMMERPATH many.c:16 '$params' 'env_' 'env_->p' 'env_->sep' 'env_->count'

# parse_action
# not sure what to do with this
perf probe -x $HAMMERPATH action.c:15 '$params' 'a' 'a->p' 'a->action'

# env is just the HParser with this. we probably want to see them in the trace buta we don't care about the parameters
# env is again the HParser
perf probe -x $HAMMERPATH parse_and '$params'

# TODO: parse_attr_bool
perf probe -x $HAMMERPATH parse_attr_bool '$params'
# parse_bind
# BindEnv
perf probe -x $HAMMERPATH bind.c:34 '$params' 'be->p' 'be->k'

perf probe -x $HAMMERPATH parse_bits '$params'

# parse_butnot
# HTwoParsers
perf probe -x $HAMMERPATH butnot.c:12 '$params' 'parsers' 'parsers->p1' 'parsers->p2'

# HCharset is just an int
perf probe -x $HAMMERPATH parse_charset '$params'

# parse_ch
# env is uintptr_t
perf probe -x $HAMMERPATH ch.c:8 '$params' 'c'

# parse_end
# we don't care about the parameters here either
perf probe -x $HAMMERPATH parse_end '$params'

# parse_endianness
# HParseEndianness
perf probe -x $HAMMERPATH endianness.c:22 '$params' 'e->endianness'

# parse_epsilon
perf probe -x $HAMMERPATH parse_epsilon '$params'

# parse_ignore
perf probe -x $HAMMERPATH parse_ignore '$params'

# parse_ignoreseq
# TODO: HParseIgnoreSeq locals
perf probe -x $HAMMERPATH parse_ignoreseq '$params'

# parse_indirect
# env is HParseIndirectEnv, but since it's never cast and saved as a local, can't really capture env->parser
perf probe -x $HAMMERPATH parse_indirect '$params'

# parse_int_range
# HRange
perf probe -x $HAMMERPATH int_range.c:12 '$params' 'r_env->p' 'r_env->lower' 'r_env->upper'

# parse_not
# HParser
perf probe -x $HAMMERPATH parse_not '$params'

perf probe -x $HAMMERPATH parse_nothing '$params'

perf probe -x $HAMMERPATH parse_optional '$params'

# HSequence
perf probe -x $HAMMERPATH permutation.c:76 '$params' 's' 's_len=s->len' 's->p_array'

# parse_skip
# size_t
perf probe -x $HAMMERPATH seek.c:12 '$params' 'n'

# parse_seek
# HSeek (TODO)
perf probe -x $HAMMERPATH seek.c:19 '$params' 's'

# parse_tell
perf probe -x $HAMMERPATH parse_tell '$params'

# parse_token
# HToken (TODO)
perf probe -x $HAMMERPATH parse_token '$params'

perf probe -x $HAMMERPATH parse_unimplemented '$params'
perf probe -x $HAMMERPATH parse_put '$params'
perf probe -x $HAMMERPATH parse_get '$params'
perf probe -x $HAMMERPATH parse_whitespace '$params'

# parse_xor
# HTwoParsers
perf probe -x $HAMMERPATH xor.c:12 '$params' 'parsers' 'parsers->p1' 'parsers->p2'

# Packrat backend
perf probe -x $HAMMERPATH h_do_parse '$params' 'state->arena:x'
perf probe -x $HAMMERPATH perform_lowlevel_parse '$params' 'state->arena:x' 'parser->env:x'
#perf probe -x $HAMMERPATH recall '$params' 'k->parser'
perf probe -x $HAMMERPATH h_packrat_parse '$params'
# return tracepoints don't play well with locals, so $params is superfluous here
# the values you get may or may not be bogus
#perf probe -x $HAMMERPATH h_do_parse%return '$params' '$retval'
perf probe -x $HAMMERPATH perform_lowlevel_parse%return '$retval'
#perf probe -x $HAMMERPATH recall%return '$retval'
perf probe -x $HAMMERPATH h_packrat_parse%return '$retval'
#perf probe -x $HAMMERPATH parse_choice%return
#perf probe -x $HAMMERPATH parse_sequence%return
#perf probe -x $HAMMERPATH parse_difference%return
#perf probe -x $HAMMERPATH parse_many%return
#perf probe -x $HAMMERPATH parse_action%return
#perf probe -x $HAMMERPATH parse_and%return
#perf probe -x $HAMMERPATH parse_attr_bool%return
#perf probe -x $HAMMERPATH parse_bind%return
#perf probe -x $HAMMERPATH parse_bits%return
#perf probe -x $HAMMERPATH parse_butnot%return
#perf probe -x $HAMMERPATH parse_charset%return
#perf probe -x $HAMMERPATH parse_ch%return
#perf probe -x $HAMMERPATH parse_end%return
#perf probe -x $HAMMERPATH parse_endianness%return
#perf probe -x $HAMMERPATH parse_epsilon%return
#perf probe -x $HAMMERPATH parse_ignore%return
#perf probe -x $HAMMERPATH parse_ignoreseq%return
#perf probe -x $HAMMERPATH parse_indirect%return
#perf probe -x $HAMMERPATH parse_int_range%return
#perf probe -x $HAMMERPATH parse_not%return
#perf probe -x $HAMMERPATH parse_nothing%return
#perf probe -x $HAMMERPATH parse_optional%return
#perf probe -x $HAMMERPATH parse_permutation%return
#perf probe -x $HAMMERPATH parse_skip%return
#perf probe -x $HAMMERPATH parse_seek%return
#perf probe -x $HAMMERPATH parse_tell%return
#perf probe -x $HAMMERPATH parse_token%return
#perf probe -x $HAMMERPATH parse_unimplemented%return
#perf probe -x $HAMMERPATH parse_put%return
#perf probe -x $HAMMERPATH parse_get%return
#perf probe -x $HAMMERPATH parse_whitespace%return
#perf probe -x $HAMMERPATH parse_xor%return
