feat: initial profiling feature

This commit is contained in:
zawz 2023-02-16 16:58:47 +01:00
parent f74c97fc7f
commit 3806830e72
8 changed files with 57 additions and 4 deletions

8
include/profiling.hpp Normal file
View file

@ -0,0 +1,8 @@
#ifndef PROFILING_HPP
#define PROFILING_HPP
#include "struc.hpp"
void insert_profiling(_obj* o);
#endif //PROFILING_HPP

View file

@ -1,5 +1,7 @@
#!/bin/bash
DEBUG=
bin=${1-./lxsh}
echo_red()

7
shellcode/profile.sh Normal file
View file

@ -0,0 +1,7 @@
{
> lxshprofile.dat
_lxsh_profile() {
export TIMEFORMAT="%R;%U;%S;$(printf "%s" "$*" | tr '%\n' '_')"
{ time "$@" 2>/dev/null; } 2>> lxshprofile.dat
}
}

View file

@ -19,6 +19,7 @@
#include "debashify.hpp"
#include "exec.hpp"
#include "shellcode.hpp"
#include "profiling.hpp"
#include "errcodes.h"
@ -101,7 +102,7 @@ int main(int argc, char* argv[])
binshebang = basename(shebang);
shebang = "#!/usr/bin/env lxsh";
}
else if(options["bash"])
else if(options["bash"] || options['p'])
{
parse_bash=true;
shebang = "#!/usr/bin/env bash";
@ -199,8 +200,13 @@ int main(int argc, char* argv[])
if(options["debashify"])
concat_sets(req_fcts, debashify(sh) );
add_lxsh_fcts(sh, req_fcts);
if(options['p']) {
req_fcts.insert("_lxsh_profile");
insert_profiling(sh);
sh->shebang = "#!/usr/bin/env bash";
}
add_lxsh_fcts(sh, req_fcts);
// processing before output
// minify
strmap_t varmap, fctmap;

View file

@ -38,6 +38,7 @@ ztd::option_set options( {
ztd::option("debashify", false, "Attempt to turn a bash-specific script into a POSIX shell script"),
ztd::option("remove-unused", false, "Remove unused functions and variables"),
ztd::option("list-cmd", false, "List all commands invoked in the script"),
ztd::option('p', "profiling", false, "Insert profiling function"),
ztd::option("\r [Variable processing]"),
ztd::option("exclude-var", true, "List of matching regex to ignore for variable processing, separated by spaces", "list"),
ztd::option("no-exclude-reserved",false, "Don't exclude reserved variables"),

27
src/profiling.cpp Normal file
View file

@ -0,0 +1,27 @@
#include "profiling.hpp"
#include "struc.hpp"
#include "struc_helper.hpp"
#include "util.hpp"
#include "recursive.hpp"
bool r_insert_profiling(_obj* o) {
switch(o->type)
{
case _obj::block_cmd: {
cmd_t* c = dynamic_cast<cmd_t*>(o);
if( ! c->is_cmdvar && !c->is_argvar() &&
!is_in_vector( c->arg_string(0), std::vector<std::string>({ "set", "shift", "echo", "printf", "[" }) ) &&
c->args != nullptr
) {
c->args->insert(0, make_arg("_lxsh_profile") );
}
};
default: break;
}
return true;
}
void insert_profiling(_obj* o) {
recurse(r_insert_profiling, o);
}

View file

@ -7,8 +7,8 @@
const std::map<const std::string, const lxsh_fct> lxsh_extend_fcts = {
{ "_lxsh_random", { "[K]", "Generate a random number between 0 and 2^(K*8). Default 2", RANDOM_SH} },
{ "_lxsh_random_string", { "[N]", "Generate a random alphanumeric string of length N. Default 20", RANDOM_STRING_SH} },
{ "_lxsh_random_tmpfile", { "[PREFIX] [N]", "Get a random TMP filepath, with N random chars. Default 20", RANDOM_TMPFILE_SH, {"_lxsh_random_string"} }
}
{ "_lxsh_random_tmpfile", { "[PREFIX] [N]", "Get a random TMP filepath, with N random chars. Default 20", RANDOM_TMPFILE_SH, {"_lxsh_random_string"} } },
{ "_lxsh_profile", { "COMMAND...", "Generate profiling data for command", PROFILE_SH } }
};
const std::map<const std::string, const lxsh_fct> lxsh_array_fcts = {

View file

@ -45,3 +45,5 @@ a=a
for I in A B C ; do
echo "$I"
done > >(cat)
{ time { true ; } ; } 2>&1