If you run your python.exe under gdb you should be able to set a future breakpoint on your _PyEval_EvalMiniFrameEx function and debug from there.<br><br><div class="gmail_quote">On Wed, Jan 14, 2009 at 8:28 PM,  <span dir="ltr">&lt;<a href="mailto:skip@pobox.com">skip@pobox.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
I&#39;ve recently been working on generating C functions on-the-fly which inline<br>
the C code necessary to implement the bytecode in a given Python function.<br>
For example, this bytecode:<br>
<br>
 &nbsp; &nbsp;&gt;&gt;&gt; dis.dis(f)<br>
 &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 LOAD_FAST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 (a)<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3 LOAD_CONST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 (1)<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;6 BINARY_ADD<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;7 RETURN_VALUE<br>
<br>
is transformed into this rather boring bit of C code:<br>
<br>
 &nbsp; &nbsp;#include &quot;Python.h&quot;<br>
<br>
 &nbsp; &nbsp;#include &quot;code.h&quot;<br>
 &nbsp; &nbsp;#include &quot;frameobject.h&quot;<br>
 &nbsp; &nbsp;#include &quot;eval.h&quot;<br>
 &nbsp; &nbsp;#include &quot;opcode.h&quot;<br>
 &nbsp; &nbsp;#include &quot;structmember.h&quot;<br>
<br>
 &nbsp; &nbsp;#include &quot;opcode_mini.h&quot;<br>
<br>
 &nbsp; &nbsp;PyObject *<br>
 &nbsp; &nbsp;_PyEval_EvalMiniFrameEx(PyFrameObject *f, int throwflag)<br>
 &nbsp; &nbsp;{<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;static int jitting = 1;<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PyEval_EvalFrameEx_PROLOG1();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;co = f-&gt;f_code;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PyEval_EvalFrameEx_PROLOG2();<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;oparg = 0;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LOAD_FAST_IMPL(oparg);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;oparg = 1;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LOAD_CONST_IMPL(oparg);<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BINARY_ADD_IMPL();<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RETURN_VALUE_IMPL();<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PyEval_EvalFrameEx_EPILOG();<br>
 &nbsp; &nbsp;}<br>
<br>
The PROLOG1, PROLOG2 and EPILOG macros are just chunks of code from<br>
PyEval_EvalFrameEx.<br>
<br>
I have the code compiling and linking, and dlopen and dlsym seem to work,<br>
returning apparently valid pointers, but when I try to call the function I<br>
get<br>
<br>
 &nbsp; &nbsp;Program received signal EXC_BAD_ACCESS, Could not access memory.<br>
 &nbsp; &nbsp;Reason: KERN_PROTECTION_FAILURE at address: 0x0000000c<br>
 &nbsp; &nbsp;0x0058066d in _PyEval_EvalMiniFrameEx (f=0x230d30, throwflag=0) at MwDLSf.c:17<br>
<br>
Line 17 is the PROLOG1 macro. &nbsp;I presume it&#39;s probably barfed on the very<br>
first instruction. &nbsp;(This is all on an Intel Mac running Leopard BTW.)<br>
<br>
Here are the commands generated to compile and link the C code:<br>
<br>
 &nbsp; &nbsp;gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall \<br>
 &nbsp; &nbsp; &nbsp; &nbsp;-Wstrict-prototypes -g -DPy_BUILD_CORE -DNDEBUG \<br>
 &nbsp; &nbsp; &nbsp; &nbsp;-I/Users/skip/src/python/py3k-t/Include \<br>
 &nbsp; &nbsp; &nbsp; &nbsp;-I/Users/skip/src/python/py3k-t -c dTd5cl.c \<br>
 &nbsp; &nbsp; &nbsp; &nbsp;-o /tmp/MwDLSf.o<br>
 &nbsp; &nbsp;gcc -L/opt/local/lib -bundle -undefined dynamic_lookup -g \<br>
 &nbsp; &nbsp; &nbsp; &nbsp;/tmp/dTd5cl.o -L/Users/skip/src/python/py3k-t -lpython3.1 \<br>
 &nbsp; &nbsp; &nbsp; &nbsp;-o /tmp/MwDLSf.so<br>
<br>
(It just uses the distutils compiler module to build .so files.) &nbsp;The .so<br>
file looks more-or-less ok:<br>
<br>
 &nbsp; &nbsp;% otool -L /tmp/MwDLSf.so<br>
 &nbsp; &nbsp;/tmp/MwDLSf.so:<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.3)<br>
<br>
though nm doesn&#39;t show that any undefined _Py* symbols so I suspect I&#39;m not<br>
linking it correctly. &nbsp;The Python executable was built without<br>
--enable-shared. &nbsp;I&#39;ve tried building with that config flag, but that just<br>
gives me fits during debugging because it always wants to find libpython in<br>
the installation directory even if I&#39;m running python.exe from the build<br>
directory. &nbsp;Installing is a little tedious because it relies on a properly<br>
functioning interpreter.<br>
<br>
dlopen is called very simply:<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp;handle = dlopen(shared, RTLD_NOW);<br>
<br>
I used RTLD_NOW because that&#39;s what sys.getdlopenflags() returns. &nbsp;I&#39;m not<br>
calling dlclose for the time being.<br>
<br>
I&#39;m not exactly sure where I should go from here. &nbsp;I&#39;d be more than happy to<br>
open an item in the issue tracker. &nbsp;I was hoping to get something a bit<br>
closer to working before doing that though. &nbsp;The failure to properly load<br>
the compiled function makes it pretty much impossble to debug the generated<br>
code beyond what the compiler can tell me.<br>
<br>
Any suggestions?<br>
<br>
Skip<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/greg%40krypto.org" target="_blank">http://mail.python.org/mailman/options/python-dev/greg%40krypto.org</a><br>
</blockquote></div><br>