Go Down

Topic: Easy command to see disassembly of compiled code (Read 4944 times) previous topic - next topic

CAFxX

I know that it's enough to run

Code: [Select]
<ARDUINOPATH>/hardware/tools/avr/bin/avr-objdump -d <BUILDPATH>/<PROJECTNAME>.cpp.elf > <PROJECTNAME>.asm

but a handy shortcut in one of the menus would totally make my life easier.


radman

I thought it might be possible to do this as a batch file in Windows.
I can run
Code: [Select]
"C:\Program Files\arduino-0022\hardware\tools\avr\bin\avr-objdump" -d

but I get a message "a.out" no such file.
I also cannot see any files with the extension .cpp.elf or .asm in my Arduino folders.
All I can see is .pde files.

Still I don't see why it should not be possible to run a batch file and just pass it the projectname as a parameter.
Then it could be rerun with two keystrokes.

radman

Under Windows XP the batch file below works for me.
The batch file needs to be in your Arduino project folder.
The projects files should be in sub-folders with the same name as the .pde file it contains.
Run the batchfile with the project name as a parameter.
The .asm file will be created in the project sub-folder.

You need to modify the code to have your <USER NAME>
I am also unclear how the folder "build813627593015490753.tmp" (where the .elf file is created) has its name defined, that will probably need changed to whatever your system uses.

Code: [Select]
REM Hold down the shift key while uploading to create hex files etc. in ...Local Settings\Temp
REM Run this batch file in your Arduino projects folder with the name of your project as the parameter.
REM
REM <ARDUINOPATH>/hardware/tools/avr/bin/avr-objdump -d <BUILDPATH>/<PROJECTNAME>.cpp.elf > <PROJECTNAME>.asm
REM
ECHO Producing ASM file for %1
"C:\Program Files\arduino-0022/hardware/tools/avr/bin/avr-objdump" -d "C:\Documents and Settings\<USER NAME>\Local Settings\Temp\build813627593015490753.tmp\%1.cpp.elf" > .\%1\%1.asm

radman

Sorry I messed up here.
I compiled the same sketch several times and I compiled different sketches and they all generated files in the folder build813627593015490753.tmp

What I did not do was exit and re-enter the IDE. When I did that build813627593015490753.tmp was deleted and a new folder with a different name was created.

So the batch file earlier crashed and burned from inadequate testing.
Still there must be some way to do it.

robtillaart

You could clear the Temp directory first then start the IDE and there will only be one build folder. maybe it will work with a wildcard ?

could you try this version?
Code: [Select]

REM Hold down the shift key while uploading to create hex files etc. in ...Local Settings\Temp
REM Run this batch file in your Arduino projects folder with the name of your project as the parameter.
REM
REM <ARDUINOPATH>/hardware/tools/avr/bin/avr-objdump -d <BUILDPATH>/<PROJECTNAME>.cpp.elf > <PROJECTNAME>.asm
REM
ECHO Producing ASM file for %1
"C:\Program Files\arduino-0022/hardware/tools/avr/bin/avr-objdump" -d "C:\Documents and Settings\<USER NAME>\Local Settings\Temp\build*tmp\%1.cpp.elf" > .\%1\%1.asm
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

radman

When the IDE closes it deletes the old build directory.
When it opens it creates a new build directory, the file name seems to have a fixed format but the numeric part appears random.

I think I have solved it in Windows XP by using powershell.
The code for the powershell script is below.
It copies the .hex file into the project directory and also generates an .asm file there.

You have to open the powershell window to run the script so I put a shortcut to it in my Arduino directory.
I called my script disassembly.ps1
Once the window is open I just type;
.\disassembly <project>

Where <project> is the name of my project.
I can then repeat the command using the up-arrow and return keys.

You need to replace <USER NAME> with your own user directory in two places in the script.

Code: [Select]
# Get-ExecutionPolicy                            [will tell you what the policy is, it is probably Restricted]
# Set-ExecutionPolicy RemoteSigned               [change the policy to RemoteSigned, this allows your own unsigned scripts to run plus remote signed scripts]
#
# Put a shortcut to powershell.exe in your Arduino directory.
# Open the powershell window.
# Execute script in the window, you cannot run the script by clicking it.
# Execute by typing .\disassembly.ps1 <projectName>  [substitute your project name for <projectName>]
#
#
# Holding down the shift key while uploading from the IDE will run it in verbose mode and you can confirm the exact directory in which
# hex files etc. are being built in ...Local Settings\Temp
#
# A new build directory, with a unique name, is created each time the IDE is run.
# An example build directory name is build7860606617831677217.tmp
# The directory is deleted when the IDE exits.
#
  param([string]$project)
  Write-Host ""
  Write-Host "Project           : $project"

  $projectPath = "C:\Documents and Settings\<USER NAME>\My Documents\Arduino\$project"
  $buildPath = "C:\Documents and Settings\<USER NAME>\Local Settings\Temp"

  Write-Host "Project Directory : $projectPath"
  foreach ($f in Get-ChildItem $buildPath\build*.tmp)
  {
    Write-Host "Build Directory   : $f"
    & "C:\Program Files\arduino-0022/hardware/tools/avr/bin/avr-objdump" -d "$f\$project.cpp.elf" > "$projectPath\$project.asm"
    Copy-Item $f\$project.cpp.hex $projectPath\$project.cpp.hex
  }
  Write-Host ""




fungus


Sorry I messed up here.
I compiled the same sketch several times and I compiled different sketches and they all generated files in the folder build813627593015490753.tmp

What I did not do was exit and re-enter the IDE. When I did that build813627593015490753.tmp was deleted and a new folder with a different name was created.


Yeah, so long as you don't close the IDE you keep the same temp folder name.

Suggestion #1: Use a hash of the sketch name (or even just the sketch name...!) for the temporary files. This solves a few problems, not least of which is that the IDE isn't very good at deleting the temp folders; I've got hundreds of "buildXXXXXXXXXXXXXX.tmp"s in there.

The other way is to edit your preferences file and set the build path:

...
build.path=h:\temp\Arduino\build



So the batch file earlier crashed and burned from inadequate testing.
Still there must be some way to do it.


Mine is this, adjust paths as necessary:

disassemble_mysketch.bat
------------------------------
set avr=H:\program+files\arduino-0022\hardware\tools\avr\bin\avr-
set build=h:\temp\arduino\build\

rem Name of sketch goes here
set target=mysketch

%avr%objdump.exe -S %build%%target%.cpp.o >disassembly.txt


Also useful is this - to see your SRAM usage:

nm_mysketch.bat
--------------------

set avr=H:\program+files\arduino-0022\hardware\tools\avr\bin\avr-
set build=h:\temp\arduino\build\

rem Name of sketch goes here
set target=mysketch

%avr%nm.exe -S %build%%target%.cpp.elf >nm_out.txt


( the stuff in SRAM starts at 0x00800100 )
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

radman

Quote
the IDE isn't very good at deleting the temp folders; I've got hundreds of "buildXXXXXXXXXXXXXX.tmp"s in there.


I have not noticed that, I get lots of consolexxxxxxxxx.tmp s but the IDE seems to clean out the builds for me.

Quote
The other way is to edit your preferences file and set the build path:
build.path=h:\temp\Arduino\build


I have tried setting build.path . It seems to make the IDE use the specified build directory, which has a constant name, rather than generating random buildxxxxxxxxxxxxx.tmp names - which was the main problem.

I have not had time to play with it fully but it looks good -thanks.



CapnBry

I just created a batch file called AVR Disassemble.bat in
%APPDATA%\Microsoft\Windows\SendTo
Code: [Select]

C:\Arduino\arduino-0022\hardware\tools\avr\bin\avr-objdump.exe -S %1 > %1%.txt


Then, when I want to  disassemble I go to explorer type %TEMP% in the location bar and find the build directory, right click the ELF file and Send To -> AVR Disassemble.

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

lewtwo

#10
Aug 17, 2013, 04:41 pm Last Edit: Aug 17, 2013, 04:42 pm by lewtwo Reason: 1
You folks had such a good idea that I expanded on it. I have written a small FreeBasic program to read the preferences file and find the build.path. Then it searches for the matching sketch directory and writes a CMD file to use avr-readelf to create a header file (foo.hdr.txt) and avr-avr-objdump to create a assembler file (foo.asm.txt). The CMD files can be edited to change the options. The program is open source and public domain. Please see the attached.

This was compiled for the Windows OS. The FreeBasic compiler also runs under linux. I am certain some changes would be required for linux but they should be minor.
engineering is the art of planning and forethought
http://www.keywild.com

Go Up