How to debug server code

edited September 2015 in Photon Server
Couple of methods I came up with for debugging server code. Hope it will help someone to save up some time. :-) Btw I'm using Visual Studio 2008.

First, be sure to setup your Visual Studio project to update server's files automatically. There are two ways to achieve this: 1) go to your server project properties, and in Build tab select Output path to point to the path were Photon is loading your dlls (for example: "e:\Projects\Photon\Server\deploy\Mmo\Photon.MmoDemo.Server\bin\"). 2) Another way to do this is to go to Build Events and add a post build event to copy the files there. Something like: "copy "$(TargetDir)" "$(SolutionDir)\..\..\deploy\Mmo\Photon.MmoDemo.Server\bin\"", also don't forget to choose Run the post-build event: When the build updates the project output. Setup Photon to automatically load changed dlls, and after you hit F5 or Ctrl+Shift+B in Visual Studio, the files will end up were they should be, and Photon will restart the server with your new code.

Now for the debugging part, there are 3 methods. But before debugging make sure Photon is using 2.0 version of .Net runtime! To do this edit your PhotonSocketServer.config file and for instance config add this:
<Runtime
	Assembly="PhotonHostRuntime, Version=2.0.0.0, Culture=neutral"
	Type="PhotonHostRuntime.PhotonDomainManager"
	CLRVersion="v2.0">
</Runtime>

1. Attaching debugger.

Go to Debug -> Attach to Process, select PhotonSocketServer.exe. Also for Attach to: select only Managed code. If you select Native code, then detaching debugger will kill server process. After that hit Attach, and there you go.

2. Launching debugger from your code.

In your Application class (the one that inherits Photon.SocketServer.Application) override Setup() method and add this line: System.Diagnostics.Debugger.Launch(); . When your server code will be launched you get a nice message box that will ask you if you want to attach the debugger. You can debug startup code this way!

3. Attaching debugger automatically.

This one is my favorite, as it attaches the debugger by simply clicking F5 in Visual Studio. For this create a dummy empty C++ project in your solution. Add main.cpp and a stub for main(). Select it as startup project. Setup project dependencies that building this one will build your server too. Go to project properties and in Debugging tab change Command to where PhotonSocketServer.exe is located, for example: "e:\Projects\Photon\Server\deploy\bin_Win32\PhotonSocketServer.exe". For Attach choose Yes. Set Debugger Type to Managed Only (mixing with native kills the server after detaching debugger). And voila! Hit F5 and it will compile, update your server, and attach a debugger in just a couple seconds. :-) And you can debug startup code this way too!
«1

Comments

  • Nice! I did not know about #2 and #3, will try this asap.
    We should add this post to the docs.
    Thanks for sharing!
  • Thanks alot for your post. Just what I was looking for.
  • edited September 2011
    I'd like to share my approach which is similar to #3 just a bit simpler :).

    1. Open solution with your server app, set project's build path to correct one within Photon app
    2. Right click solution > Add > Existing Project...
    3. Choose PhotonSocketServer.exe
    4. Right click and set it as startup project
    5. Right click again and go to properties
    5a. Set arguments to "/debug Instance1" without quotation
    5b. Set debugger type to Managed
    6. Hit F5 and you are rolling right from very first line of managed code 8-).

    Note: as it runs completely under hood of Visual Studio when you stop debugging it will also stop server. If you want to run server outside and just attach to it, you can change "Attach = Yes" in step 5 properties.
  • BorisBoris ✭✭
    Xeevis wrote:
    4. Right click and set it as startup project
    just tried this, but there is no option to set the exe as startup project?
  • BorisBoris ✭✭
    Smilediver wrote:
    3. Attaching debugger automatically.

    This one is my favorite, as it attaches the debugger by simply clicking F5 in Visual Studio. For this create a dummy empty C++ project in your solution. Add main.cpp and a stub for main(). Select it as startup project. Setup project dependencies that building this one will build your server too. Go to project properties and in Debugging tab change Command to where PhotonSocketServer.exe is located, for example: "e:\Projects\Photon\Server\deploy\bin_Win32\PhotonSocketServer.exe". For Attach choose Yes. Set Debugger Type to Managed Only (mixing with native kills the server after detaching debugger). And voila! Hit F5 and it will compile, update your server, and attach a debugger in just a couple seconds. :-) And you can debug startup code this way too!
    This does actually work without the C++ project, too.
    Sort of like Xeevis wrote, it was a bit different when I did it though:

    project properties, debug page
    1) start external program: bin_Win64\PhotonSocketServer.exe
    2) command line arguments: /debug Instance1
    3) working directory: \bin_Win64\

    when using VS2008 make sure to set the CLRVersion to 2.0;
    no need to add the exe to the solution.
  • Boris wrote:
    Xeevis wrote:
    4. Right click and set it as startup project
    just tried this, but there is no option to set the exe as startup project?
    I'm using VS2010 and its in context menu. It's practically the same thing as setting it through debug properties, you just get more options like environmental variables, quick change to whether you want to start or attach to process. Or quickly change between different photon builds also its tied to solution and not project itself, but no big deal really.
  • BorisBoris ✭✭
    I tried on both VS- maybe you have a different version or other plugins installed
  • boris, maybe you add .exe file to project, you should add it in solution not in current project
  • BorisBoris ✭✭
    Figured it out: Step 2 as described above adds the exe as solution item, not as a project.
    Instead I had to choose "Add->Existing Project" and now VS allows me to select it as startup project.
  • You are right Boris its supposed to be "Existing Project" im sorry if I caused confusion my bad.
  • Hello,

    I tried to right click my solution and then select add an existing project. But when I try to select the PhotonSocketServer.exe file, my Visual Studio won't allow me to add it. I'm using Microsoft Visual C# 2010 Express.
  • Debugging won't work with the express edition as it doesn't support the "attach to local process" feature.
  • Boris,

    Are there other ways to debug the Photon Server?
  • You can unit test your code without photon - and debug these tests.
    Lite and MMO demo both have a"disconnected" namespace in the test project that shows how to do this.
    With photon you will have to use logging.
    I don't know of any other way.
  • Hi, I've tried to debug the code directly in vs2008, but I can't get it to hit the breakpoints that I set in the code, even though they're shown as active inside the text editor. I'm wondering if there's some specific setting I need to set in order for that to happen? Also, I've read that it's possible for me to have a Debug Window and log debug messages to it while running the PhotonSocketServer.exe, so I was wondering if that was still the case.

    I've got some debug information working while logging specifically to a file, so I'm just trying to figure out if there's some other way to get some debug information without always relying on the log file.

    Thanks!
  • You are probably starting your server with .NET 4, make sure you run .NET 2 if you debug with VS2008:
    <Runtime
       Assembly="PhotonHostRuntime, Version=2.0.0.0, Culture=neutral"
       Type="PhotonHostRuntime.PhotonDomainManager"
       CLRVersion="v2.0">
    </Runtime>
    

    I don't think Photon did ever offer a debug window for logging, do you remember where you read about this?
  • I've read that here http://cjrgaming.com/node/120 but i might have misunderstood it. :)
  • the console logger will work only for unit tests. photon doesn't open a console so it won't work. maybe there is a way to start a console from within your code, not sure if .net allows that. you could also start a windows forms front-end and configure a logger to write to a text box - not that I would recommend it but it's an option.
  • yeah I agree, it doesn't look like it's going to be worth the effort to set that up. So far using visual studio debugger and log file has been proving sufficient to my debugging needs. Thanks again for the tip about CLRVersion in the config file :)
  • Boris wrote:
    You are probably starting your server with .NET 4, make sure you run .NET 2 if you debug with VS2008:
    <Runtime
       Assembly="PhotonHostRuntime, Version=2.0.0.0, Culture=neutral"
       Type="PhotonHostRuntime.PhotonDomainManager"
       CLRVersion="v2.0">
    </Runtime>
    

    How would that be done with Photon 3? I've tried replacing the existing Runtime declaration in PhotonServer.config with the one you gave - but then, Photon doesn't start up anymore.

    In the log, I'm getting:
    3320: 19:52:08.898 - CService::OnException() - Exception: CCLRHost::Start() - Type load error for "PhotonHostRuntime.PhotonDomainManager" in assembly "PhotonHostRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=02C301B61B060C4D"
    
  • just add ONLY the CLRVersion="v2.0", don't change the rest of the <Runtime> node.
    (the photon host runtime version there is breaking it)
  • Awesome - that did it. Thank you for the quick reply! I actually thought I had tried that already but probably I had missed something there. Now debugging can start, yeeha! :-)
  • about debug server code i am try edit some code,
    i use VS2010 and .Net 4.0 follow this tutorial but cant debug it...
    only use VS2008 and .net2.0 can do this debug mode???

    i hope can check Parameter changes at debug and running time.
    i should how do it about open like windows or console?

    thx a lots ~~
  • You can debug your code with VS2010 and .net 4.0 if you follow instructions given here.
    I have changed the CLR version in config file to 2.0 (as stated in the first post) and everything is working for me.
  • i change vs2008 is ok now
    at 2010 i cant attach to process it...
    maybe my sysytem have some problem :(
    thx
  • Is the Photon V3.0 can be debugged? I try many times by above ways, it's can work! and many other developers can not debug as me. but, the Photon 2.0 is ok! So, what's wrong? Please give me a hand!!
  • frank: Yes, Photon 3 can be debugged.
    Which IDE/Debugger are you using? Which OS do you have and is it 32 or 64 bit? Did you configure the runtime for Photon as described above?
  • Hi, Boris!

    My IDE is VS2008 and the OS is WinXP32, It would be i make the bad config, so, i try again.

    Thanks Boris!
  • Hello, developers!

    Is it possible to have clear message in logs (or any other way) which configuration option was violated by client?
    For example, my client broke rule MaximumTimeout="3000" which defined in Instance1 section of PhotonServer.config, after this I had disconnected but which reason I did not know from logs and waste much time to understand it.

    P S. Note, I still use Photon2, because of this reason viewtopic.php?f=5&t=1256
  • Hi devs

    Trying to debug Photon 3 server code.
    Did all like is said above, but the server fails to load my application.
    Says that 4.0 Net compiled module cannot be loaded into 2.0 runtime.
    And I cant build server with 2.0 cus photon dll reference 3.0 dlls.
Sign In or Register to comment.