-
Notifications
You must be signed in to change notification settings - Fork 861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create ColorConfig class and use it in nearpc #1317
Conversation
53f9438
to
b6921f7
Compare
Codecov Report
@@ Coverage Diff @@
## dev #1317 +/- ##
==========================================
- Coverage 55.03% 55.01% -0.03%
==========================================
Files 156 155 -1
Lines 19408 19401 -7
Branches 1808 1811 +3
==========================================
- Hits 10681 10673 -8
Misses 8278 8278
- Partials 449 450 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
b6921f7
to
79bbd1b
Compare
79bbd1b
to
9359a9d
Compare
Does it make sense to call c = ColorConfig("nearpc", [
["symbol", "normal", "color for nearpc command (symbol)"],
["address", "normal", "color for nearpc command (address)"],
...
]) Also maybe we should make a factory function out of this since compared to the old behavior, where the caller had to 1) load a global imported attribute and 2) call it, here, we will: 1) load the global Also since we are touching this code, it is generally not great that def color_sth(txt):
return PARAM1 + PARAM2 + PARAM3 + txt + NORMAL (where Currently a color function does call Maybe what we should do is to change
Here is an example code how it could look like. Ofc we would have to compute the proper color escape code prefix in the trigger function (that we will plug with GDB to be executed when the theme color parameter is changed): Also, if we are even crazier, we could avoid the creation and access into a "theme parameters dictionary" by dynamically creating the I know this sounds and is a bit of a premature optimization, since those things are called "only" few thousands of times , but small calls like this tends to add up. And if we look into how the performance of current solution is, its kinda stupid that for 10 executions of the FWIW, I have also tried checking the performance of this PR and it was fairly similar. However, I probably didn't make a proper benchmark even though I also checked it against the Btw here is the profiling code I used. I just modified what we had in
#!/bin/bash
if ! (($#)); then
cat <<-_EOF_
$0: [profile-script]
Example: $0 context.py
_EOF_
exit 1
fi
module=$(basename "${1/.py/}")
basedir=$(dirname "$0")
# Quick and dirty script to profile pwndbg using cProfile.
make -C "${basedir}" test >/dev/null
# -ex "set dereference-limit 20" \
# -ex "set memoize off" \
gdb --quiet --nx \
"${basedir}/test" \
-ex "source ${basedir}/../gdbinit.py" \
-ex "set context-stack-lines 50" \
-ex "set dereference-limit 20" \
-ex "b main" \
-ex "r" \
-ex "python
import cProfile;
import profiling.${module} as profile;
profile.warmup();
cProfile.run('profile.run()', '${basedir}/stats')" \
-ex "quit"
python3 -c "
import pstats
p = pstats.Stats('${basedir}/stats')
p.strip_dirs().sort_stats('tottime').print_stats(20)
"
if command -v pyprof2calltree >/dev/null 2>&1 && command -v kcachegrind >/dev/null 2>&1; then
pyprof2calltree -k -i "${basedir}/stats"
fi
# vim: ts=4 sw=4 noet And context.py: import pwndbg.commands.context
def warmup():
pwndbg.commands.context.context()
def run():
for i in range(10):
pwndbg.commands.context.context() And executed as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I went a bit offtopic in my previous comment: I generally like this PR apart from minor things:
- we should probably not have a way to
add_color_param
afterColorConfig
is initialized - the
raise AttributeError
should probably have a meaningful error string
But generally, we should further refactor/change this and coloring relevant code as I described in the comment above.
We can either do this in this PR, or merge this one and make another one with the changes I mentioned.
The old way of defining theme color parameters was verbose, as you needed to first add a Parameter and then add a function for each parameter that applies the correct color function to the argument. The code between all these functions was almost identical. Given the verbosity of defining the color parameters, it made sense to keep them in a separate module and import them where we need them.
This PR adds a
ColorConfig
class. Any command that wants to add color parameters can create an instance of it locally and call theadd_color_param
method for each parameter. We no longer need to pass in long names likenearpc-branch-marker-color
, we can just pass inbranch-marker
and the rest is added for you. The Parameter that's created is stored in this config class. The command can then call any of the color functions using theColorConfig
object, i.e.c.branch_marker(...)
, and the__getattr__
method is able to handle all of these functions without you needing to define them. Because of this, we're also able to delete the entire file that originally defined all those functions. The benefit here is that it's easy to see what the configuration options are for a command, as now they're in the command source file itself.I suspect performance might be brought up, so to specifically address it, this should be pretty similar to what we have now, and I haven't noticed any performance issues while testing. Maybe there's a better approach that can speed things up a few milliseconds, but it will probably sacrifice the other benefits here.
This PR only converts the nearpc.py color file. Future PRs will convert the rest.