Search Unity

UWP IL2CPP debug port number?

Discussion in 'Windows' started by sjando, Jan 13, 2020.

  1. sjando

    sjando

    Joined:
    Nov 6, 2017
    Posts:
    18
    Is it possible to retrieve the port number used to connect a manager debugger at runtime in a UWP player? I'm often working in a network the multicast advertisement process doesn't work and I need to enter that value manually.

    I've heard that for the editor and perhaps certain other platforms that this is inferred from the process ID but that doesn't appear to be the case for UWP. The only place I seem to be able to find this number is by running the app from Visual Studio, watching for the logged profiler multicast message (example below) and noting the last three digits of the "GUID" in that message, which seems to match the last 3 digits of the debug port.

    debug.png

    What does this GUID correspond to and can I access it at runtime in a script? It would be enormously helpful to be able to display this somewhere inside the app as required.
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Nice observation. The way port is calculated is "debuggerPort = 56000 + guid % 1000;". If you're on a recent Unity version, you should also see this message in the log:

    Code (csharp):
    1. Starting managed debugger on port 56037
    The GUID is just a random number derived from the time app starts... Unfortunately, there isn't a good way to retrieve it as we don't expose this functionality. You might hate this, but one way to achieve it is to parse the player log file on startup.
     
  3. sjando

    sjando

    Joined:
    Nov 6, 2017
    Posts:
    18
    Thanks! I do hate it but parsing the player log at least sounds like a viable workaround for now :p

    Not sure if there's an official channel for providing this sort of feedback but given the discovery process can be very difficult to use inside an enterprise network:
    • It'd be really useful to have this GUID or derived port number properly exposed to applications somehow e.g. Application.debugPort or something
    • Would also be really useful to display the port number on the "you may attach a managed debugger now if you want" dialog when enabled
    • Not sure if this is a UWP specific thing but certain devices (e.g. HoloLens 2) have some TCP/IP over USB functionality. This'll have a valid IPv4 address to which both the managed debugger and Unity profiler can be manually attached if you know the port numbers (which is tricky sometimes as per this thread). However the multicast discovery process seems to ignore this, typically utilizing only the primary 802.11 adapter. If a development player would multicast one packet per adapter with the corresponding IP, including the USB adapter, the automated discovery process might "just work" over USB and mitigate a lot of the difficulties here.

    [EDIT] For anyone that stumbles upon this, reading the player log isn't the solution, it appears to be locked by whatever is writing it and any attempt to read it at runtime results in a sharing violation. Ended up creating a socket in app code to join the multicast group which allows me to see the outbound message, parse out the port and expose it however I need. Hopefully in future this won't be necessary.
     
    Last edited: Jan 14, 2020
  4. CPlusSharp22

    CPlusSharp22

    Joined:
    Dec 1, 2012
    Posts:
    111
    This is absurdly annoying. I have to attach the debugger to an instance where I dont know the port ahead of time. And it crashes on launch, which Im trying to debug. So I have less than 10 seconds to attach the debugger to a port I cant read until the countdown starts.

    What kind of absurd design is this? It's so counter intuitive.
     
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619
    Have you tried the "Wait for Managed Debugger" option? If enabled, the application should not proceed to start until the debugger is attached.

    upload_2020-7-16_7-21-5.png
     
  6. CPlusSharp22

    CPlusSharp22

    Joined:
    Dec 1, 2012
    Posts:
    111
    Yes I have, that's the problem if you read my comment. You enable that, and Unity starts counting down from 10 waiting for the debugger. You have 10 seconds to find the port from the log, and input it into the debugger. Even then, sometimes it doesn't work until you time it just after 0 on the timer. You can't know the port ahead of time, it's super silly.
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Is it a C# code crash or a hard app crash?

    By the way, that 10 second countdown sounds suspicious. Unless we recently changed it, it just popped up a message box waiting indefinitely.

    EDIT: I just looked at the source code and I can't see the 10 second thing anywhere. Can you describe/screenshot what you're seeing? That sounds like a serious bug.
     
  8. CPlusSharp22

    CPlusSharp22

    Joined:
    Dec 1, 2012
    Posts:
    111
    It's a headless mode -nographics app, so it's not going to pop up a message box.
    It's a hard app crash. I have a ticket open with unity, looks like my particular crash is related to Incremental GarbageCollection issues somewhere in Unity on Linux. But it's all kind of irrelevant, we should be able to specify or grab port number somehow.
     
  9. CPlusSharp22

    CPlusSharp22

    Joined:
    Dec 1, 2012
    Posts:
    111
    Also, just found out it is not just headless/nographics standalone that does the countdown from 10 seconds.
    It's also on Android devices (shows up in logcat). I presume iOS as well.
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Interesting, I didn't realize that in the headless scenario, message box wouldn't appear on Linux. It definitely appears on Windows even in -batchmode -nographics.

    Can you submit a bug on this? I assume we'd want to change this in two ways:

    1. Make sure that it pauses the execution everywhere. In headless scenarios where message box is not possible, we can wait for input in the console.
    2. We should display a port either in the message box or in the console (for headless scenarios).
     
    CPlusSharp22 likes this.