Jump to content

Anders Melander

Members
  • Content Count

    2562
  • Joined

  • Last visited

  • Days Won

    133

Everything posted by Anders Melander

  1. Anders Melander

    MAP2PDB - Profiling with VTune

    Sometimes it writes in the log if it can't find the pdb. Sometimes it doesn't... I guess Intel are focusing their efforts on making hardware I had to use Process Monitor to verify that it actually read the pdb file. That was also the way I discovered that it didn't look for the pdb unless there was a reference to it in the exe file. I think I will need the map file to determine the cause of that. You can mail it to me if that's ok. I haven't thought about dlls at all but I guess there shouldn't be any difference.
  2. Anders Melander

    MAP2PDB - Profiling with VTune

    Fixed. https://bitbucket.org/anders_melander/map2pdb/commits/8c94db07a2da1b503ce6f00c39792ee0b5479387
  3. Anders Melander

    MAP2PDB - Profiling with VTune

    Thanks. Naturally, as something like that always is, the map parser is a mess However I'm not actually associating symbols with source files. I'm associating symbols with modules (i.e. units) based on the symbol address and the module address range. So for example: 0001:001236E0 System.Classes.{System.Generics.Collections}TDictionary<System.Integer,System.Classes.IInterfaceList>.Add is associated with System.Classes because the offset $001236E0 locates it in the range of that module: $000C7730 - $0015F654 ($000C7730+$0009F24): 0001:000C7730 00097F24 C=CODE S=.text G=(none) M=System.Classes ALIGN=4 Later when I parse the line number information I get the correct source file and can associate that, source file and line number+offset, with the module. I.e. System.Generics.Collection.pas in the module SystemClasses. Line numbers for System.Classes(System.Generics.Collections.pas) segment .text 7088 0001:001236E0 7089 0001:001236F1 7090 0001:001236F9 7092 0001:00123701 7093 0001:0012370E 7094 0001:0012371B 7095 0001:0012371F 7097 0001:0012373C 7098 0001:00123755 This way you get both the symbol resolved to the correct module/unit and the code resolved to the correct source lines.
  4. Anders Melander

    MAP2PDB - Profiling with VTune

    The problem is the MSF container format. It's a blocked format with 4096 bytes in each block and up to 4096 blocks in each "interval" (4096*4096 = 16Mb). Each interval starts with a Free Page Map (FPM, a bit like a FAT). Since I wanted to concentrate on getting the PDB layout right my MSF abstraction doesn't implement intervals so once I pass 4096 blocks the file becomes invalid. Here's LLVMs explanation of the MSF format: https://llvm.org/docs/PDB/MsfFile.html A proper implementation of intervals will require a bit of work. At present I can assume that a physical stream offset value equals the logical offset value. Once I add intervals I will have to take into the FPMs into account since the blocks are no longer contiguous. Also when I write I will have to consider that a write can start on once side of the FPMs and continue on the other side of them. It isn't rocket science but it does complicate the IO layer considerably. No. I get those too. I'm not sure what they are for but they are in segment 4 (TLS) and 5 (PDATA= exception data) so you'll not miss them. The warning just means that the address of a symbol couldn't be matched to any of the modules (units). It should be. How do you come to the conclusion that it wasn't patched? Try the -v switch. Okay. There's probably a bug in the command line interface then. I discovered the problem with the position of the -bind switch just before release so there's probably more.
  5. Anders Melander

    MAP2PDB - Profiling with VTune

    Yes it should. As long as the layout of the map file is the same. It's easy enough to fix if there's minor differences but I don't think there are. Yes. I need it for 64-bit myself. The only thing needed to support for 64-bit is the ability to parse and modify a PE64 image. Right now I don't what the difference is between PE32 and PE64. Here's the relevant code: https://bitbucket.org/anders_melander/map2pdb/src/27bb0daa1b4a7e0159b646f4b0cd0d34ffc72fd3/Source/debug.info.pdb.
  6. Anders Melander

    Profiler for Delphi

    For those following this thread:
  7. Anders Melander

    Determining why Delphi App Hangs

    No but someone suggested doing so and as always that got the ball rolling.
  8. Anders Melander

    Determining why Delphi App Hangs

    First of all the application isn't hanging. It's just not processing messages while it's working. When you click on the form Windows detects that the application hasn't pumped the message queue for a while and assumes this is because the application is stuck in an endless loop somewhere. In your case that's a wrong conclusion because eventually your task finishes and everything is good again. The simple work around is to pump the message queue inside your loop - just don't use Application.ProcessMessages. I believe have previously posted some code here that can be used inside a loop to allow the user to interact with a progress dialog (e.g. move it, push an abort button, etc.) without the pitfalls of Application.ProcessMessages but if you can't find it you can at least pump the queue for WM_NULL messages. I think these are what Windows use to detect a hung application: var Msg: TMessage; while PeekMessage(0, Msg, WM_NULL, WM_NULL, PM_REMOVE) do begin if (Msg.message = WM_QUIT) then PostQuitMessage(Msg.wParam); end; If you can reproduce while running in the debugger then just press the Pause button in the debugger and examine the call stack. At run time you can use something like madExcept's freeze detection. It uses the same technique as Windows to detect a hung application and produces a nice call stack when it happens (after prompting the user).
  9. Anders Melander

    Determining why Delphi App Hangs

    Yes. You are wrong. It processes WM_PAINT messages (which is why it's updating) but leave everything else alone.
  10. Anders Melander

    Determining why Delphi App Hangs

    I don't think you understand how the message queue works... How are messages supposed to arrive through Application.OnMessage if the message queue isn't being pumped? And if it is being pumped then your code is unnecessary because the messages are already being dispatched and processed. In short: Your code does nothing that isn't already being done.
  11. Anders Melander

    Profiler for Delphi

    I'm not sure why that is relevant. I only mentioned PDATA because you claimed that the map file doesn't contain the ImageBase value; I pointed out that it does because in your map example ImageBase is the same as the start offset of the PDATA segment. Generally the ImageBase value is probably just the offset with the lowest value from the segment list. Everything is easy once you know how to do it. I understand the MSF container format but I have no clear understanding of the internal structure of the PDB format it contains. In hindsight it would have been faster to just learn what there is to know about the topic, but the fact that MS have stated that they'll change the format as they see fit, kinda turned me away from that idea. I've now also gotten the clear impression, from reading the LLVM source, there there are parts that even the LLVM project don't understand. For example the PublicsStream section which VTune apparently requires. Anyway, I'm now learning about DWARF and how FPC handles it. Thanks for bringing that to my attention.
  12. Anders Melander

    Profiler for Delphi

    Another interpretation could be that VTune's PDB importer extracts more details than its DWARF importer, even if the files contain the same information. I believe the roundtrip test with VTune's matrix example proved that yaml2pdb produces PDBs that cannot be used with VTune. The pdb worked with VTune before going through LLVM's pdb2yaml->yaml2pdb. It didn't work after. Definitely. I have no idea about how they affect performance but they are incredible convenient and I think they improve the readability of the code. Yes it is: 0006:00400000 00000000H .pdata PDATA This corresponds to the ImageBase field in the PE header. I don't know what the deal is with the .tls section. Anyway since all the offsets in the map file are relative, and the process can get reallocated to anywhere at run time, the section addresses are really just nice-to-know. As far as I can see, if you know the actual ImageBase of a process, then the map file contains all the information required to get from physical address to method/function and source code line and vice versa. That's not a duplicate. The map file contains two list of symbols. One is ordered by name and the other by address so if you just do a text search any symbol will occur twice in the map file. I only read the first list and ignore the other.
  13. Anders Melander

    Help debugging code please

    Accessing a string one char beyond the length of the string is fine because Delphi string contains an implicit zero terminating zero. Doing the same on an empty string once worked the same but now causes an AV because the string is nil. I don't know when this changed but it was before 10.3 var Foo: string; begin Foo := 'Hello World'; ShowMessage(IntToStr(Ord( Foo[Length(Foo)+1] ))); // No problem Foo := ''; ShowMessage(IntToStr(Ord( Foo[Length(Foo)+1] ))); // Access Violation end; My guess is the code predates Delphi 2009 since it uses WideString to support unicode.
  14. Anders Melander

    Profiler for Delphi

    So like @Stefan Glienke suggested I ran the VTune example application matrix.exe/matrix.pdb through llvm-pdbutil pdb2yaml to produce yaml and then again with yaml2pdb to produce a new pdb. VTune now failed to resolve addresses so I ran the updated pdb through llvm-pdbutil pdb2yaml once more and compared the first and second yaml file to see what had changed. The only significant change was that the PublicsStream section was now empty. I had observed this empty PublicsStream earlier when testing the roundtrip of the yaml I produce but since I assumed that the pdb produced by llvm-pdbutil can actually be used with VTune I concluded that this was a section that wasn't required and so I disabled the output of it (I can also see in the llvm source that they haven't implemented a writer for the section). I can now see that my assumption was incorrect. While LLVM might have the intention of producing pdb that can be used with VTune I haven't seen any proof that they have actually reached that goal. This means that at least half of this project has been a complete goose chase. Well at least I now know why it doesn't work. I looked at it early on and then decided that it was safer to use llvm-pdbutil than to try and port it to Delphi. Maybe I was wrong 🙂 Also it's cheating; It's using undocumented Visual Studio DLLs to manipulate the pdb files so it requires that VS is installed. If this was a part of the Win SDK I wouldn't have a problem with it but I think the dependency on VS makes it a no-go. I thought DWARF was for for Linux only and that VTune didn't support it on Windows...? If I have understood you correctly you're saying that VTune works with DWARF on Windows both when it's linked into the exe and when it's external (with a reference from the EXE). I haven't investigated the DWARF format yet but I know that it's at least documented (unlike LLVMs YAML) - and maybe I can even reuse some of FPCs code to handle it. I believe we can conclude that whatever values I write is moot since the PublicsStream section is missing from the pdb and that alone is enough to for VTune to not use the line number information that is there. Regardless of that; When you say RVA I assume you mean the PE section RVA as specified in the PE headers? The only place in the pdb that RVAs are references is in the "*Linker *" section. Since the map file doesn't give me the RVA values I simply write the segment offset instead. I don't really understand why the pdb would need the RVA but if that's how it is then I will just have to deal with it. I guess I will have to read the map file and then extract the RVA values from the PE header in order to get all the info required to produce a pdb. I discard duplicate symbols based on segment+offset. Duplicate segment+name are allowed but only one is emitted. I have seen any segment+offset duplicates.
  15. Anders Melander

    Profiler for Delphi

    Maybe. I'm a bit unsure about what they mean by symbols. In my implementation "symbols" are methods (name/offset/size) but I'm not including stack frame, parameter info, locals, return values etc. since I don't have that information available. SymChk reports that I don't have "global symbols" but while I assumed that to mean global exports (which I don't supply) it might well be meaning the globals mentioned in the post. Good idea. The round trip broke the pdb. VTune can no longer resolve and it doesn't report any errors. Just like with my pdb I'll do a diff on the old and new tomorrow. Maybe I should try with a newer version of VTune. I'm not too thrilled about asking for help on the VTune forum about an old unsupported version either (my community registration finally completed).
  16. Anders Melander

    Profiler for Delphi

    It was. This is what happened when I changed the YAML to include the module ObjName property with a reference to the dcu (of course the map file doesn't contain the obj filename so I'm just supplying a dummy filename): Looks like a bug in VTune.
  17. Anders Melander

    Profiler for Delphi

    I've tried that but for some reason I can't get past their community registration. I registered when I downloaded VTune but when I try to post to their forum I get stuck in a registration confirmation loop. Tried with different accounts, different browsers. No. Never used WinDbg. If someone else want to have a go at it I'll be happy to supply a zip with source, exe and pdb. I verified the exe/pdb with symchk from Debugging Tools for Windows and it passed with no problems: D:\Projects\map2pdb\Bin\Win32\Debug>"D:\Development\Debugging Tools for Windows (x86)\symchk.exe" -v map2yaml.exe -s D:\Projects\map2pdb\Bin\Win32\Debug /pf [SYMCHK] Searching for symbols to D:\Projects\map2pdb\Bin\Win32\Debug\map2yaml.exe in path D:\Projects\map2pdb\Bin\Win32\Debug DBGHELP: Symbol Search Path: D:\Projects\map2pdb\Bin\Win32\Debug [SYMCHK] Using search path "D:\Projects\map2pdb\Bin\Win32\Debug" DBGHELP: No header for D:\Projects\map2pdb\Bin\Win32\Debug\map2yaml.exe. Searching for image on disk DBGHELP: D:\Projects\map2pdb\Bin\Win32\Debug\map2yaml.exe - OK DBGHELP: map2yaml - public symbols & lines D:\Projects\map2pdb\Bin\Win32\Debug\map2yaml.pdb [SYMCHK] MODULE64 Info ---------------------- [SYMCHK] Struct size: 1680 bytes [SYMCHK] Base: 0x00400000 [SYMCHK] Image size: 6230016 bytes [SYMCHK] Date: 0x60526592 [SYMCHK] Checksum: 0x00000000 [SYMCHK] NumSyms: 0 [SYMCHK] SymType: SymPDB [SYMCHK] ModName: map2yaml [SYMCHK] ImageName: D:\Projects\map2pdb\Bin\Win32\Debug\map2yaml.exe [SYMCHK] LoadedImage: D:\Projects\map2pdb\Bin\Win32\Debug\map2yaml.exe [SYMCHK] PDB: "D:\Projects\map2pdb\Bin\Win32\Debug\map2yaml.pdb" [SYMCHK] CV: RSDS [SYMCHK] CV DWORD: 0x53445352 [SYMCHK] CV Data: map2yaml.pdb [SYMCHK] PDB Sig: 0 [SYMCHK] PDB7 Sig: {CBB17264-89FA-4AED-A2D7-814EE276EF3E} [SYMCHK] Age: 1 [SYMCHK] PDB Matched: TRUE [SYMCHK] DBG Matched: TRUE [SYMCHK] Line nubmers: TRUE [SYMCHK] Global syms: FALSE [SYMCHK] Type Info: FALSE [SYMCHK] ------------------------------------ SymbolCheckVersion 0x00000002 Result 0x000f0001 DbgFilename DbgTimeDateStamp 0x60526592 DbgSizeOfImage 0x005f1000 DbgChecksum 0x00000000 PdbFilename D:\Projects\map2pdb\Bin\Win32\Debug\map2yaml.pdb PdbSignature {CBB17264-89FA-4AED-A2D7-814EE276EF3E} PdbDbiAge 0x00000001 [SYMCHK] [ 0x00000000 - 0x000f0001 ] Checked "D:\Projects\map2pdb\Bin\Win32\Debug\map2yaml.exe" SYMCHK: FAILED files = 0 SYMCHK: PASSED + IGNORED files = 1 The above just verifies that the exe reference to the pdb is correct and that the pdb is valid and contain line number information. I don't think symchk can validate deeper than that. I get similar output when I run it on the matrix.exe sample provided with VTune, except that one also has symbol & type information in the pdb. Since I could see that VTune was looking in it's own bin directory for files with the same name (sans type) as the modules in my pdb I tried copying the source files there and rename them to match what it looked for. Apparently it wasn't source files it was looking for because it just read the first 56 bytes of each file and it made no difference in the end. My guess is that it was looking for obj files.
  18. Anders Melander

    Profiler for Delphi

    🙂 yeah they need to be fairly recent. Since LLVm only produce they "new" PDB format it doesn't need to support the older formats. I think anything older that 5 years will probably fail.
  19. Anders Melander

    Profiler for Delphi

    Historical reasons probably. https://softwareengineering.stackexchange.com/questions/171565/why-is-the-code-section-called-a-text-section https://stackoverflow.com/questions/1282506/where-did-the-text-segment-get-its-name
  20. Anders Melander

    Profiler for Delphi

    I'm not sure what saying - or asking... I know what the values in the map file means (they're all absolute [*]). It's the values that I need to put in the yaml/pdb that I'm unsure about. [*] Of course all addresses are relative in a sense, since it's only after the exe has been mapped into virtual memory that absolute addresses can be known. When I say "relative to the segment" I mean these from the map file: Start Length Name Class 0001:00401000 0011B0C0H .text CODE 0002:0051D000 00001144H .itext ICODE 0003:0051F000 00003BACH .data DATA 0004:00523000 0000645CH .bss BSS 0005:00000000 00000020H .tls TLS 0006:00400000 00000000H .pdata PDATA
  21. Anders Melander

    Profiler for Delphi

    Yes. As far as I can tell. I have also tried validating the pdb but there aren't really any tools (that I've found) that can analyze a pdb for validity. I think the problem is more about what values I need to provide in the pdb (for example I don't have type information or mangled names so I can't write those) and what the meaning of the values are. One of the things I've struggled with are address values. Are they absolute or relative and if relative, then relative to what. I currently assume the following: Segment: Absolute Module (Unit): Relative to Segment Line: Relative to Module Symbol (method/function): Relative to Module But I have tried just about all different combinations. Since VTune doesn't provide any feedback on the address resolution (other than crash/no crash) I have just to throw everything at the wall to see what sticks. Pretty frustrating. I even tried attaching to the VTune backend with the debugger to examine what it did but I quickly gave up on that as it (not surprising) is massively multi threaded and simply too complex for that approach.
  22. Anders Melander

    Profiler for Delphi

    Don't count your chickens before they hatch... I think you jinxed me there. Here's what works so far: Parse a MAP file and produce an YAML file. Convert the YAML to PDB. Update the EXE with a reference to the PDB. Not crash VTune while it's loading the PDB Here's what doesn't work: Getting VTune to use any of the information in the PDB I know that VTune reads the PDB because I can see in Process Monitor that it looks for the source files. Unfortunately the file names it looks for are the module (i.e. unit) names and not the file names and it doesn't search the source folders I have defined: I'm using VTune 2019 btw since that's the last version to support Windows 7. Anyway if you can stomach watching the sausages getting made then the current source is available here: https://bitbucket.org/anders_melander/map2pdb/ There are two projects: map2yaml and bindpdb. In addition to that the llvm-pdbutil tool is needed. The Tools folder contains a batch file that calls all three in order. Delphi 10.3 or later is required (inline vars).
  23. I haven't looked at it at all (still fighting with the damned PDB stuff) but maybe that could have been done automatically when a new resource module is loaded? That would have made the change backward compatible.
  24. Got it. Wow. Nice that they've added a hook but they really seem to have made that complicated. I'll look at it later.
  25. I don't have time to investigate myself right now but can you see where LoadResStringFunc is assigned? I briefly scanned through the 10.4 source and I couldn't find anything there.
×