square / pylink

Python Library for device debugging/programming via J-Link
https://pylink.readthedocs.io/en/latest/
Other
352 stars 128 forks source link

flash_file() from GUI - WindowsError: exception: access violation writing 0x000000CE00000000 #29

Closed zamora18 closed 6 years ago

zamora18 commented 6 years ago

Hello,

I have created a GUI (using PyQt4) that implements pylink flash_file() capability. It works great when developing in my IDE (PyCharm), but after bundling the GUI in an executable (using pyinstaller) I get this error (with varying locations 0x00____ ):

WindowsError: exception: access violation writing 0x000000CE00000000

This is the full error output:

Traceback (most recent call last):
  File "main_gui.py", line 87, in run
  File "main_gui.py", line 57, in _flash_binary
  File "build\bdist.win-amd64\egg\pylink\jlink.py", line 172, in wrapper
  File "build\bdist.win-amd64\egg\pylink\jlink.py", line 2021, in flash_file
WindowsError: exception: access violation writing 0x000000CE00000000

Also, it seems that the Segger J-flash/J-Link software is executed because this window pops up, but is stuck in this state: image

I am able to open the J-Link and connect to the target just fine, but it crashes when trying to flash the file to the target. Here is a segment of my code that calls the flash_file method. It successfully executes all of the code but raises the Windows Exception in the line jlink.flash_file(bin_file, 0x0).

def _flash_binary():
    # Instantiate J-Link
    jlink = pylink.JLink()
    # Binary file to be used
    bin_file = 'f446re_led_blink_fast.bin'

    # Open a connection to the J-Link using Serial Number
    try:
        serial_num = 600107062
        jlink.open(serial_num)
        print "J-Link Opened."
    except pylink.errors.JLinkException as e:
        print "Error CONNECTING to J-LINK: <%s>" % e

    # Set appropriate interface to flash target
    jlink.set_tif(pylink.JLinkInterfaces.SWD)

    # Connect to the STM target device/micro-controller.
    try:
        target = 'STM32F446RE'
        jlink.connect(target)
        print "Connected to <%s> successfully." % target
    except pylink.errors.JLinkException as e:
        print "Error CONNECTING to TARGET: <%s>" % e

    try:
        # Flash the selected file to the target.
        jlink.flash_file(bin_file, 0x0)
        print "Flashed <%s> to target successfully." % bin_file
    except pylink.errors.JLinkException as e:
        print "Error FLASHING file to TARGET: " % e

Is there a way to show more detail on what is happening on the pylink side? The message does not seem very informative to me. Or could you inform me on what is happening and if there is a solution?

Thank you!

hkpeprah commented 6 years ago

WindowsError generally means some windows issue with the system call. I'd check that the DLL you're using corresponds to the version of the Python interpreter that you're running in. After that, enabling logging is the best way to get more verbose output:

import logging
logging.basicConfig(level=logging.DEBUG)  # For maximum logging
sstallion commented 6 years ago

Very cool! Would you be interested in contributing this as an example application?

On Thu, Apr 19, 2018 at 10:57 AM, zamora18 notifications@github.com wrote:

Hello,

I have created a GUI (using PyQt4) that implements pylink flash_file() capability. It works great when developing in my IDE (PyCharm), but after bundling the GUI in an executable (using pyinstaller) I get this error (with varying locations 0x00____ ):

WindowsError: exception: access violation writing 0x000000CE00000000

This is the full error output:

Traceback (most recent call last): File "main_gui.py", line 87, in run File "main_gui.py", line 57, in _flash_binary File "build\bdist.win-amd64\egg\pylink\jlink.py", line 172, in wrapper File "build\bdist.win-amd64\egg\pylink\jlink.py", line 2021, in flash_file WindowsError: exception: access violation writing 0x000000CE00000000

Also, it seems that the Segger J-flash/J-Link software is executed because this window pops up, but is stuck in this state: [image: image] https://user-images.githubusercontent.com/15371219/39003315-d581e4fc-43b7-11e8-9cf4-28e1bdf130b8.png

Is there a way to show more detail on what is happening on the pylink side? The message does not seem very informative to me. Or could you inform me on what is happening and if there is a solution?

Thank you!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/square/pylink/issues/29, or mute the thread https://github.com/notifications/unsubscribe-auth/AFtLiBuX40YcI8U_W4B7tzIDtl8QF8J_ks5tqLPigaJpZM4TcD8S .

zamora18 commented 6 years ago

Just so you are aware, I edited my original post. @hkpeprah Since the program performs the rest of the pylink functionality just fine (pylink.JLink(), jlink.open(), etc) I wouldn't think that the problem is with the DLL? Or am I wrong in thinking that? I did not find any significant differences in the logging messages between the two, but maybe you see something I do not? I will include both below: gui_output.txt pycharm_output.txt Are you familiar with pyinstaller? I am not very familiar, but in my research so far I haven't found anything that has helped me. Would it be beneficial to send my code?

@sstallion The GUI is a larger project, and the pylink functionality is only a part of that large project. I'm not sure it is something that would be very beneficial to contribute... However, I have created a Very simple gui (in trying to isolate/debug this problem) that I would be more than happy to contribute (though it isn't anything special).

hkpeprah commented 6 years ago

Huh, interesting. So pycharm works, but the GUI fails? Seems like it's failing on the line to download the file. I've used pyinstaller before, and I know it creates some weird issues with some things. I would attempt to try flash() instead of flash_file() (you'll have to read the file into a binary array), and it may help.

zamora18 commented 6 years ago

Correct, pycharm works but the GUI executable fails. I tried flash() but unfortunately I am still having the same error.

sstallion commented 6 years ago

Interesting. Is it possible that a different Python runtime is being used in the installer version?

zamora18 commented 6 years ago

It's possible, but I'm not sure that is what's happening. There is some documentation on run-time information for pyinstaller but I did not see anything relevant that page. I'm doing some reading and I'll look into that more.

zamora18 commented 6 years ago

I have attempted multiple solutions and still have the same problem. I created an issue in the pyinstaller repository, but they say the problem is not on their end.

After having done some research I believe the issue lies in how the binary is downloaded. Without understanding much of how it is implemented, it believe it currently requires accessing memory on the computer, which is denied because of DEP (Data Execution Prevention) protection. Would there potentially be another way to download the file? Regardless of it being because of DEP, I do believe it is a problem with downloading (see log files below).

There was a suggested "fix" on the pyinstaller issue thread to run this command

os.system("start cmd /c JLinkSTM32.exe -SetDeviceFamily 2")

before the program crashes, but it has not worked for me.

I will include the logs comparing my successful flash_file() with the failed attempts:

I really appreciate any help you can offer.

hkpeprah commented 6 years ago

Does what's in this PR fix the issue for you? https://github.com/square/pylink/pull/25/files If so, I'll look into cleaning up + merging the work there.

zamora18 commented 6 years ago

I am not sure if this will fix this issue (#29), but I will test the fix for #10

zamora18 commented 6 years ago

Update: I re-attempted using both flash_file() and flash() methods.

Therefore I believe my issue relates to a problem with how the DLL downloads the binary files. Is there anything that can be done on the pylink end? Do you have any recommendations for me going forward?

zamora18 commented 6 years ago

After some final investigation I am closing this issue due to the problem most likely being due to DEP on Windows. My program executes successfully on Windows 7, but not any Windows 10 computers. I believe this is due to the changes in DEP for Windows since the release of Windows 7.