The MASM Forum

General => The Campus => Topic started by: bullcriss on May 10, 2019, 06:37:01 PM

Title: MASM32 debug of Windows exe
Post by: bullcriss on May 10, 2019, 06:37:01 PM
Hello friends,

I have Windows 7 and have installed MASM32
Microsoft (R) Macro Assembler Version 6.14.8444

With it I have successfully created applications that run perfectly in Windows 7.

Now I want to use a debugger and I decided to use X64DBG.
I have downloaded and installed it and it runs ok but it does not show the source code of the program I am debugging.

I am sure I have compiled the program with the proper flags for assembler :
/c /coff /Cp /nologo /Fm /Zi /Zd
and linker :
/SUBSYSTEM:WINDOWS /DEBUG /VERSION:4.0

In the log tab of x64dbg I can see this :
QuoteInitialization successful!
Loading plugins...
Handling command line...
  "C:\x64dbg\release\x32\x32dbg.exe" "C:\ASMPROJECTS\TESTWIN\TESTWIN.exe"
Debugging: C:\ASMPROJECTS\TESTWIN\TESTWIN.exe
Database file: C:\x64dbg\release\x32\db\TESTWIN.exe.dd32
Process Started: 00400000 C:\ASMPROJECTS\TESTWIN\TESTWIN.exe
  "C:\ASMPROJECTS\TESTWIN\TESTWIN.exe"
  argv[0]: C:\ASMPROJECTS\TESTWIN\TESTWIN.exe
Skipping unsupported debug type IMAGE_DEBUG_TYPE_MISC in module testwin.exe...
Skipping unsupported debug type IMAGE_DEBUG_TYPE_CODEVIEW in module testwin.exe...
Did not find any supported debug types in module testwin.exe!

Which seems to inform about a problem with DEBUG TYPE.

I have this problem also with a basic asm program like this :

.386
.model flat, stdcall
option casemap:none

include    \masm32\include\windows.inc
include    \masm32\include\kernel32.inc
include    \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
MyBoxTitle  db  "A Simple Little Message Box !",0
MyBoxText   db  "This is just a 'Message Box', not even a real 'window', but it's made with Masm32 Assembly code!",0

.code
MyMsgBox:
    invoke  MessageBox, 0, ADDR MyBoxText, ADDR MyBoxTitle, MB_OK
    invoke  ExitProcess,0
end MyMsgBox


Can anyone help me ?
Thank you in advance for your help!
BC
Title: Re: MASM32 debug of Windows exe
Post by: jj2007 on May 10, 2019, 06:57:42 PM
X64Dbg is for 64-bit code. Try OllyDbg (http://www.ollydbg.de/version2.html), the interface is simpler but almost identical.

P.S.: Sorry, I just see that you use x32dbg.exe. Try to use only /debug for the linker (it works for me).
Title: Re: MASM32 debug of Windows exe
Post by: bullcriss on May 10, 2019, 07:37:12 PM
Yes X64DBG has a 32 bit version.
I tried setting just
/DEBUG
in the linker options but when I link the application I get the following error:
Quote\Masm32\Bin\Link @"C:\ASMPROJECTS\TESTWIN\link.war"

Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/DEBUG "/LIBPATH:\Masm32\Lib" "C:\ASMPROJECTS\TESTWIN\TESTWIN.obj" "/OUT:C:\ASMPROJECTS\TESTWIN\TESTWIN.exe"
LINK : fatal error LNK1221: a subsystem can't be inferred and must be defined

Make finished. 1 error(s) occured.

Do you use MASM 6.14 too?

UPDATE:
with linker settings
/DEUG /SUBSYSTEM:WINDOWS
The error is fixed but the source is not available in x64dbg...

UPDATE 2:
I've also tried using ollydbg, both version 1 and 2, and neither show the source code, just like x64dbg...  :icon_redface:
Title: Re: MASM32 debug of Windows exe
Post by: fearless on May 10, 2019, 07:54:11 PM
/SUBSYSTEM:WINDOWS /DEBUG /DEBUGTYPE:CV /PDB:"filename.pdb" /VERSION:4.0 /LIBPATH:"x:\path to lib files" /OUT:"filename.exe" filename.obj filename.res

I'm using a newer linker version (Microsoft (R) Incremental Linker Version 12.00.31101.0) than the default one that comes with masm32 (Microsoft (R) Incremental Linker Version 5.12.8078)

http://masm32.com/board/index.php?topic=7444.msg85501#msg85501 (http://masm32.com/board/index.php?topic=7444.msg85501#msg85501)
Title: Re: MASM32 debug of Windows exe
Post by: jj2007 on May 10, 2019, 08:03:25 PM
Quote from: bullcriss on May 10, 2019, 07:37:12 PMwith linker settings
/DEBUG /SUBSYSTEM:WINDOWS
The error is fixed but the source is not available in x64dbg...

Yes, I forgot /Subsystem, of course. Re symbols, no idea what's wrong there: No symbols with x32Dbg.exe, and I don't see anything relevant in the options. Same exe with OllyDbg shows all symbols...

Btw use polink, it's included in the Masm32 SDK and produces correct symbols.
Title: Re: MASM32 debug of Windows exe
Post by: bullcriss on May 10, 2019, 08:36:27 PM
Thank you, I tried all the options you suggested, and I also tried to download previous versions of x64dbg, but no success.

At this point my main doubt is about the version of MASM32 that I'm using, with the included linker.

I've tried downloading MASM 8.0 SDK from Microsoft's website but the installer aborts with the error :
Microsoft Visual C++ Express Edition 2005 required

So now I have to find out how to dowload that one...
Title: Re: MASM32 debug of Windows exe
Post by: fearless on May 10, 2019, 08:55:45 PM
Im using Microsoft (R) Macro Assembler Version 6.14.8444
I bundled up the v12 ms link/lib etc here: https://github.com/mrfearless/libraries/blob/master/Masm64/mslinkv12.zip?raw=true
Title: Re: MASM32 debug of Windows exe
Post by: jj2007 on May 10, 2019, 10:01:19 PM
With UAsm64 (http://www.terraspace.co.uk/uasm.html#p2) and polink (already included in the Masm32 SDK), options

/Zd /Zi /Zf for the assembler
/debug /Subsystem:console for the linker

... OllyDbg shows all symbols. The problem seems to be x32Dbg 8)

@fearless: No symbols with the link v12 from your archive.
Title: Re: MASM32 debug of Windows exe
Post by: bullcriss on May 10, 2019, 10:03:01 PM
I copied the files in the zip from your link into my masm32\bin folder, overwriting the already existing files.
What I get now from the linker is :
Quote\Masm32\Bin\Link @"C:\ASMPROJECTS\TESTWIN\link.war"

Microsoft (R) Incremental Linker Version 12.00.31101.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/SUBSYSTEM:WINDOWS /DEBUG /VERSION:4.0 /DEBUGTYPE:CV "/LIBPATH:\Masm32\Lib" "C:\ASMPROJECTS\TESTWIN\TESTWIN.obj" "/OUT:C:\ASMPROJECTS\TESTWIN\TESTWIN.exe"
LINK : fatal error LNK1207: incompatible PDB format in 'C:\ASMPROJECTS\TESTWIN\TESTWIN.pdb'; delete and rebuild

Make finished. 1 error(s) occured.

I want to say thank you very much to everyone for your help, I appreciate it!

UPDATE
I deleted the obj and pdb files and now it links properly but... X64DBG is not showing any errors anymore in the log, about the debug type... STILL, no source code is displayed....
Title: Re: MASM32 debug of Windows exe
Post by: fearless on May 10, 2019, 10:46:43 PM
what build/version of x64dbg are you using?

do you see lines loaded in the symbols log (symbols tab, at the bottom shows pdb info, loading, line info etc)

do you see source code file line numbers in the comments in the cpu view similar to the images shown in this post (http://masm32.com/board/index.php?topic=7444.msg85501#msg85501 (http://masm32.com/board/index.php?topic=7444.msg85501#msg85501)) - MUIProgressBar.asm:14 etc - for your program i would think it should display Testwin.asm:x in comments, where x is a line no

If the line numbers are not showing then its not loading the pdb (wrong debug format, older x64dbg version, or some other issue)

Can you zip up your test project so i can check it out on my end. Thanks
Title: Re: MASM32 debug of Windows exe
Post by: BugCatcher on May 10, 2019, 10:53:10 PM
The pdf file  format was changed about 6 months ago and x32debug went with the new format. I put new files ml.exe, link.exe and lib.exe version 14.16.27026.1 from visual studio 2017 into masm32\bin dir. I put rc.exe version 10.0.17763.132 from Microsoft kits into masm32\bin. Delete old pdf build new. That should give you source code.

*Also if you didn't know, x64debug doesn't show source code until you step into user code.
Title: Re: MASM32 debug of Windows exe
Post by: TimoVJL on May 10, 2019, 10:55:21 PM
What x64dbg is able to show source ? May 9 2019 don't :(

EDIT: Thank's BugCatcher :t

An old QT4 based x32dbg works a bit better with older linker / pdb.

if you want to force x32dbg.exe to use msdia140.dll, make a copy of x32dbg.exe and add to it's manifest
  <file name="msdia140.dll">
    <comClass clsid="{E6756135-1E65-4D17-8576-610761398C3C}" />
  </file>
Title: Re: MASM32 debug of Windows exe
Post by: bullcriss on May 10, 2019, 11:20:54 PM
what build/version of x64dbg are you using?


The last one, Mag 9 2019.

do you see source code file line numbers in the comments in the cpu view similar to the images shown in this post ( (http://masm32.com/board/index.php?%5B/b)

When I start debugging TESTWIN, I get this :
https://i.vgy.me/Boz2Vc.png

do you see lines loaded in the symbols log (symbols tab, at the bottom shows pdb info, loading, line info etc)

This is the symbols tab:
https://i.vgy.me/dLizKI.png
And I just noticed there are some error messages regarding the PDB....

Can you zip up your test project so i can check it out on my end. Thanks

I attached the TESTWIN project... :icon_confused:
Title: Re: MASM32 debug of Windows exe
Post by: fearless on May 10, 2019, 11:35:16 PM
ok i adjusted the WinAsm project properties debug settings and recompiled it. Also untick in x64dbg the system breakpoint (Options->Settings->Events-System Breakpoint), keep attach and entry breakpoints - that way it wont stop in the middle of ntdll.dll and will stop at the entrypoint.


Once at entrypoint, highlight a line that has source code line number in it, right click and select open source file.
(https://i.postimg.cc/HLm47j3v/testwin-entrypoint-sourceview.png)


(https://i.postimg.cc/d1vHv4z1/testwin.png)

(https://i.postimg.cc/LsjCrrQw/testwin-properties.png)
Title: Re: MASM32 debug of Windows exe
Post by: aw27 on May 11, 2019, 02:03:30 AM
I see that fearless x32dbg has a side bar with an option "Open Source File". I don't have it, is it some plugin?.
I noticed that fearless has a few plugins on the x32/64dbg website.

BTW, frequently, Windbg does not start the source code debugging unless we open the source file. Probably the same happens with x32dbg.
Title: Re: MASM32 debug of Windows exe
Post by: fearless on May 11, 2019, 02:16:18 AM
In x64dbg, the right click context menu option "Open Source File" only shows if you right click on a line that has line comments, and the pdb with symbols and line info is successfully loaded and the source files for the target being debugged can be located (otherwise this menu option doesn't show). Choosing "Open Source File" will then switch to the Source View tab and display the corresponding line in the source code, allowing you to set breakpoints from the Source View tab and/or step through the source code where applicable, typically F8 to step over functions etc.
Title: Re: MASM32 debug of Windows exe
Post by: aw27 on May 11, 2019, 02:24:32 AM
Quote from: fearless on May 11, 2019, 02:16:18 AM
In x64dbg, the right click context menu option "Open Source File" only shows if you right click on a line that has line comments, and the pdb with symbols and line info is successfully loaded and the source files for the target being debugged can be located (otherwise this menu option doesn't show). Choosing "Open Source File" will then switch to the Source View tab and display the corresponding line in the source code, allowing you to set breakpoints from the Source View tab and/or step through the source code where applicable, typically F8 to step over functions etc.
I can't make it work even after placing comment. I downloaded the latest version.
All right, I saw it,  :badgrin:  It is big context menu, we need a good eye to spot the load source file option.
Title: Re: MASM32 debug of Windows exe
Post by: TimoVJL on May 11, 2019, 02:35:22 AM
As it comes with a copy of msdia140.dll , use it without register ;)

EDIT: a missing info, add to manifest:  <file name="msdia140.dll">
    <comClass clsid="{E6756135-1E65-4D17-8576-610761398C3C}" />
  </file>
Title: Re: MASM32 debug of Windows exe
Post by: bullcriss on May 11, 2019, 10:39:25 PM
It works, finally!

Since I made several tests with various versions of assembler and linker, I didn't know anymore how I got it fixed, so I had to reinstall everything from scratch, then I proceeding trying again the several options that you all told me here.

FInally I installed Masm 6.14 again but the Linker 5.14 that comes with it builds PDB files wrong.   I had to install the patch mentioned by fearless.
THEN all worked, that's what fixed it.

I am beginning to use x64dbg and it's a great tool, thank you fearless.
Practical question about it: is there a way to get the source code view to follow the execution, as you go on step by step?  If the execution jumps elsewhere outside the portion on the screen, it gets out of sight.

Thank you everyone again!
Title: Re: MASM32 debug of Windows exe
Post by: fearless on May 12, 2019, 02:37:52 AM
Quote from: bullcriss on May 11, 2019, 10:39:25 PM
is there a way to get the source code view to follow the execution, as you go on step by step?  If the execution jumps elsewhere outside the portion on the screen, it gets out of sight.

You can step whilst in the source code view with F8, however if you go into an api function or execution jumps to another module, then the source code view typically wont have any visibility of that.


If your in the cpu view and debugging/stepping etc, you can always right click on a line that has a source code line number in the comments, and select open source file again, which should switch to the appropriate line no in the source view tab.

Its a useful feature, but has some inherent limitations due to the nature of syncing disassembly to source code line numbers from the pdb - if there is a pdb (like common windows dll's and you have setup a symbol store and downloaded the pdbs - (right click on a dll in symbols tab, download module)) you will get debug symbols for functions, variable names etc, but no source.
Title: Re: MASM32 debug of Windows exe
Post by: jj2007 on May 12, 2019, 03:59:29 AM
Quote from: fearless on May 12, 2019, 02:37:52 AMIts a useful feature, but has some inherent limitations due to the nature of syncing disassembly to source code line numbers

Especially true for macros, of course: It is practically impossible to follow a source when it enters a macro.