Archive for the ‘Mono/.NET’ Category

Patch for Monodevelop 2.0 debugger for Mono 2.4.2.2

Sunday, July 26th, 2009

A while ago I posted a how-to on compiling Mono 2.4 from source on Ubuntu. After the release of a minor version update for mono, Mono 2.4.2.2 it was impossible to compile debugger support for Monodevelop 2.0 using Mono 2.4.2.2 sources.

The reason is because in mono 2.4.2.2 the mono-debugger was also updated, to version 2.4.2.1, breaking compatibility with monodevelop-debugger-mdb-2.0. I found a way to solve this and provide a patch.

Download the patch from here.

Applying the patch

Copy the patch from where you downloaded it, to the directory containing the source directory for monodevelop-debugger-mdb-2.0, e.g (~/src/mono-2.4.2.2).

And patch the sources like this:

patch -p0 < monodevelop-debugger-mdb-2.0.mono-debugger-2.4.2.1.patch

The resulting output  output should be something similar to this:

anirothan@lapitop:~/src/mdb-patch/original$ patch -p0 < monodevelop-debugger-mdb-2.0.mono-debugger-2.4.2.1.patch
patching file monodevelop-debugger-mdb-2.0/Mono.Debugging.Server.Mdb/RuntimeInvokeManager.cs
anirothan@lapitop:~/src/mdb-patch/original$

Now you can confiure & make & make install the sources as usual.

Monodevelop 2.0 debugger with Mono 2.4.2.2

Sunday, July 26th, 2009

A while ago I posted a how-to on compiling Mono 2.4 from source on Ubuntu. Well, the mono project released an update: mono 2.4.2.2 about a month ago. So, obviously people wanted to update their systems and tried to compile the new source packages. But there seems to be a problem. The debugger plugin for monodevelop 2.0 (monodevelop-debugger-mdb-2.0) failed to compile.

Responding to a series of comments on my blog mentioning this annoying problem I looked into the situation and I have produced a patch for monodevelop-debugger-mbd-2.0 sources. You can download the patch from here, and the instructions on applying it in this post.

As it turns out, in mono 2.4.2.2 there is a new version of the mono debugger, version 2.4.2.1. And with this new version came some breaking changes. Monodevelop uses the mono debugger through a plugin (contained in this source package: monodevelop-debugger-mdb-2.0.tar.bz2). The sources of the plugin were not updated for the new version of mono debugger so any attempt to compile them failed.

Following is an excerpt from the compilation indicating the problem:

./RuntimeInvokeManager.cs(49,50): warning CS0612: `Mono.Debugger.Thread.RuntimeInvoke(Mono.Debugger.Languages.TargetFunctionType, Mono.Debugger.Languages.TargetStructObject, Mono.Debugger.Languages.TargetObject[], bool, bool)' is obsolete
./RuntimeInvokeManager.cs(57,68): error CS1501: No overload for method `AbortInvocation' takes `0' arguments
./RuntimeInvokeManager.cs(78,60): error CS1501: No overload for method `AbortInvocation' takes `0' arguments
Compilation failed: 2 error(s), 2 warnings
make[1]: *** [../build/DebuggerServer.exe] Error 1
make[1]: Leaving directory `/home/anirothan/src/mono-2.4.2.2/monodevelop-debugger-mdb-2.0/Mono.Debugging.Server.Mdb'
make: *** [all-recursive] Error 1

The problem is with the monodevelop-debugger-mdb-2.0/Mono.Debugging.Server.Mdb/RuntimeInvokeManager.cs file. On line 49 the RuntimeInvoke() mehtod is deprecated in the new version of mono debugger as indicated by the warning message, and the two invocations to AbortInvocation() on lines 57 and 78 have incompatible signatures with the method’s definition.

Invocation of AbortInvocation in monodevelop-debugger-mdb-2.0/Mono.Debugging.Server.Mdb/RuntimeInvokeManager.cs lines 55-58 and 76-79:

res.Abort ();
res.CompletedEvent.WaitOne ();
ctx.Thread.AbortInvocation ();
WaitToStop (ctx.Thread);

The AbortInovacation() method is defined in the mono debugger package, in mono-debugger-2.4.2.1/classes/Thread.cs.  If you compare this method between mono-debugger-2.4 and mono-debugger-2.4.2.1 you will see that in 2.4 it is public whereas in 2.4.2.1 it is made internal. Therefore, we couldn’t just figure out what the parameter to AbortInvocation() should be since there would be now way of invoking it.

AbortInvocation() in mono-debugger-2.4

public void AbortInvocation ()
{
  CommandResult result;

  lock (this) {
    check_alive ();
    result = servant.AbortInvocation ();
  }

  result.Wait ();
}

AbortInvocation() in mono-debugger-2.4.2.1

internal void AbortInvocation (long rti_id)
{
  lock (this) {
    check_alive ();
    servant.AbortInvocation (rti_id);
  }
}

However, all is not lost. It seems that in RuntimeInvokeManager.cs there is an obvious code duplication in lines 55-58 and 76-79 where AbortInvocation() is called. It seems that these lines have been moved from the monodevelop debugger plugin to the mono debugger itself. Indeed, below is how Abort() is defined in the RuntimeInvokeResult class in mono-debugger-2.4.2.1/classes/Thread.cs and mono-debugger-2.4/classes/Thread.cs:

in 2.4:

public override void Abort ()
{
  thread.Stop ();
}

and in 2.4.2.1

public override void Abort ()
{
  Thread.AbortInvocation (ID);
  completed_event.WaitOne ();
}

There it goes! The offending call to AbortInvocation(), which caused the compilation to fail, has been moved to Abort(), which is already being called on lines 55 and 76.

So, all we need to do is delete lines 56, 57 and 77,78 from RuntimeInvokeManager.cs. If we try to compile monodevelop-debugger-mdb-2.0 after this simple change we will see no more error messages and the debugger will work in monodevelop 2.0.

RuntimeInvokeManager.cs

Using Interactive C# (REPL)

Thursday, June 18th, 2009

The REPL, that is Read Eval Print Loop, is a feature provided by many programming language environments today, such as Python, Ruby, Scala and Haskel. For those who are not familiar with the notion, you could think of REPL as an interactive interpreter (even though it’s not always implemented as one). You can find more info here.

It practically is an interactive prompt (like a UNIX shell, or Windows PowerShell), that accepts expressions in your favorite programming language syntax. There was no practical implementation of a REPL for C# until the Mono project implemented a C# Interactive Shell in Mono 2.2, called csharp. It comes with two flavors: 1). as a command line shell under the name csharp and 2). with a graphical front end called GSharp.

I am sure that every developer, novice or seasoned, needs to test out some small snippet of code, from time to time, to make sure that it does what it is supposed to do. This is also true for C# developers. However, in the absence of a REPL for C#, the only way to do that was by creating a whole new project in Visual Studio or MonoDevelop with a very simple console application. Not only it seems like an overkill if you just want to test a few lines of code, but you also end up with a bunch of little test projects that you are never going to need.

However, now that Mono implemented a C# REPL there is no need to create a test console project for everything. It is extremely useful if you want to test out some piece of code.

An example from my own experience is when, in a C# project I have been working on, I needed to dynamically create a list of enum field values and their string names. I was not 100% percent sure of the way I thought of implementing this, so I wanted to test it out first.

Here it is, in the csharp interactive C# shell, running from a Linux command line:

anirothan@netbook:~$ mono-2.4 csharp
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> var fileModeNames = Enum.GetNames(typeof(System.IO.FileMode));
csharp> fileModeNames;
{ "CreateNew", "Create", "Open", "OpenOrCreate", "Truncate", "Append" }
csharp> var fileModeValues = Enum.GetValues(typeof(System.IO.FileMode));
csharp> fileModeValues;
{ CreateNew, Create, Open, OpenOrCreate, Truncate, Append }
csharp> var pairs = new Dictionary<string, System.IO.FileMode>();
csharp> for(int i=0; i<fileModeValues.Length; ++i)
 > pairs.Add(fileModeNames[i], fileModeValues.GetValue(i));
{interactive}(2,7): error CS1502: The best overloaded method match for `System.Collections.Generic.Dictionary<string,System.IO.FileMode>.Add(string, System.IO.FileMode)' has some invalid arguments
/opt/mono-2.4/lib/mono/2.0/mscorlib.dll (Location of the symbol related to previous error)
{interactive}(2,7): error CS1503: Argument `#2' cannot convert `object' expression to type `System.IO.FileMode'
csharp> var pairsCorrect = new Dictionary<string, object>();     
csharp> for(int i=0; i<fileModeValues.Length; ++i)               
 > pairsCorrect.Add(fileModeNames[i], fileModeValues.GetValue(i));
csharp> pairsCorrect;
{{ "CreateNew", CreateNew }, { "Create", Create }, { "Open", Open }, { "OpenOrCreate", OpenOrCreate }, { "Truncate", Truncate }, { "Append", Append }}
csharp> pairsCorrect["CreateNew"]
 > ;
CreateNew
csharp> quit;
null

That was fairly easy and simple. The intention was to create a dictionary of enum field names and values. For the sake of testing I used the System.IO.FileMode enum.

The flexibility of the csharp REPL does not end there. Here is an extract from a session in gsharp (The gtk+ graphical front-end to csharp) which actually creates a window with a dropdown list.

csharp> LoadPackage("gtk-sharp-2.0");
csharp> using Gtk;
csharp> var window = new Window("Simple dropdown list binding test with csharp REPL");
csharp> var vbox = new VBox();
csharp> var label = new Label("This is a simple test.");
csharp> vbox.PackStart(label);
csharp> var model = new ListStore(typeof(System.IO.FileMode), typeof(string));
csharp> var fileModeNames = Enum.GetNames(typeof(System.IO.FileMode));
csharp> fileModeNames;
{ "CreateNew", "Create", "Open", "OpenOrCreate", "Truncate", "Append" }
csharp> var fileModeValues = Enum.GetValues(typeof(System.IO.FileMode));
csharp> fileModeValues;
{ CreateNew, Create, Open, OpenOrCreate, Truncate, Append }
csharp> var model = new ListStore(typeof(System.IO.FileMode), typeof(string));
csharp> for(int i=0; i<fileModeValues.Length; ++i)
 > model.AppendValues(fileModeValues.GetValue(i), fileModeValues[i]);
{interactive}(2,48): error CS0021: Cannot apply indexing with [] to an expression of type `System.Array'
csharp> for(int i=0; i<fileModeValues.Length; ++i)
model.AppendValues(fileModeValues.GetValue(i), fileModeNames[i]);
csharp> var dropDown = new ComboBox();
csharp> var renderer = new CellRendererText();
csharp> dropDown.PackStart(renderer, true);
csharp> dropDown.AddAttribute(renderer, "text", 1);
csharp> dropDown.Model = model;
{ { CreateNew, "CreateNew" }, { Create, "Create" }, { Open, "Open" }, { OpenOrCreate, "OpenOrCreate" }, { Truncate, "Truncate" }, { Append, "Append" } }
csharp> vbox.PackEnd(dropDown);
csharp> window.Add(vbox);
csharp> window.ShowAll();
csharp>
The GSharp C# Interactive Shell in action.

The GSharp C# Interactive Shell in action.

There are some limitations though. For example, you cannot create classes (but you can put .cs files in the ~/.config/csharp directory that will be loaded automatically to the interactive shell) and there is an apparent lack of code completion features. However, these limitations are already being worked on and I hope they will become available with mono 2.6.

Building Mono 2.4 from source on Ubuntu 8.10

Wednesday, April 1st, 2009

Sad to say that the latest versions of Ubuntu and Debian do not provide the latest packages of Mono preferring stability over bleeding edge. Also, according to the packagers’  mailing list it is not an easy task to provide packages for mono and all the software that binds to it.

I am a sucker for bleeding edge, especially when it comes to thinks like Mono. The latest stable release of Mono, version 2.4 has a lot of new features like the C# interactive repl shell, SIMD extensions and better support for C# 3/.NET 3.5 features, like extension methods and LINQ. See the release notes here. I was disappointed when the most recent version of mono debian packages I could find were for the 2.0.1 version on third-party repositories.

What to do then? Download the source, configure and build to run in parallel to the version of mono installed in my Ubuntu desktop (the mono run-time is always installed on Ubuntu by default, even the live CD runs it, since it is needed to run .NET applications, like F-Spot, that have become a standard in the GNOME desktop). It turns out that it’s not that difficult as it sounds.

Step 1: Install prerequisites for compilation.

First of all, you will need to setup the appropriate tools for configuring and compiling source code. You can install these through the Synaptic package manager but for the sake of speed let’s use some command line magik Wink

Open a console and type in the following to install the gcc compiler, and development header files needed to compile the mono compilers, tools and library stack.

user@system$> sudo apt-get update
user@system$> sudo apt-get install build-essential autoconf automake \
bison flex gtk-sharp2-gapi boo gdb valac libfontconfig1-dev \
libcairo2-dev libpango1.0-dev libfreetype6-dev libexif-dev \
libjpeg62-dev libtiff4-dev libgif-dev zlib1g-dev libatk1.0-dev \
libglib2.0-dev libgtk2.0-dev libglade2-dev libart-2.0-dev \
libgnomevfs2-dev libgnome-desktop-dev libgnome2-dev libgnomecanvas2-dev \
libgnomeui-dev libgnomeprint2.2-dev libgnomeprintui2.2-dev \
libpanel-applet2-dev libnautilus-burn-dev librsvg2-dev \
libgtkhtml3.14-dev libgtksourceview2.0-dev libgtksourceview-dev \
libvte-dev libwnck-dev libnspr4-dev libnss3-dev libxul-dev \
libwebkit-dev libvala-dev

This should install all the necessary software and development headers needed for the compilation.

Step 2: Download mono 2.4 source archives.

Go to http://ftp.novell.com/pub/mono/sources-stable/ . You don’t need every archive in that list.
In this guide we will use: mono-2.4.tar.bz2libgdiplus-2.4.tar.bz2gluezilla-2.4.tar.bz2xsp-2.4.tar.bz2mono-tools-2.4.tar.bz2gecko-sharp-2.0-0.13.tar.bz2mono-debugger-2.4.tar.bz2mono-addins-0.4.zipgtk-sharp-2.12.8.tar.bz2gnome-sharp-2.20.1.tar.bz2gnome-desktop-sharp-2.20.1.tar.bz2webkit-sharp-0.2.tar.bz2

Also go to the MonoDevelop website and download the sources for monodevelop 2.0: monodevelop-2.0, monodevelop-debugger-mdb-2.0, monodevelop-debugger-gdb-2.0, monodevelop-database-2.0, monodevelop-java-2.0, monodevelop-vala-2.0

Create a directory in your home directory for the extracted sources, e.g. src/mono-2.4.

user@system$> mkdir -p src/mono-2.4; cd src/mono-2.4

Extract the downloaded archives to that directory.

Step 3: Prepare the parallel environment

Now it’s time to prepare our system for two versions of the mono runtime. A good idea is to install everything related to mono 2.2 to some path like /opt/mono-2.2 and create shell script to load an environment that will use the version located in that path instead of the default installation. That way you will not render your existing mono installation unstable or broken.

Now create a script that will load the separate mono-2.4 environment.

user@system$> cat > mono-2.4-environment
#!/bin/bash
MONO_PREFIX=/opt/mono-2.4
GNOME_PREFIX=/opt/gnome-2.4
export DYLD_LIBRARY_PATH=$MONO_PREFIX/lib:$DYLD_LIBRARY_PATH
export LD_LIBRARY_PATH=$MONO_PREFIX/lib:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=$MONO_PREFIX/include:$GNOME_PREFIX/include
export ACLOCAL_PATH=$MONO_PREFIX/share/aclocal
export PKG_CONFIG_PATH=$MONO_PREFIX/lib/pkgconfig:$GNOME_PREFIX/lib/pkgconfig
PATH=$MONO_PREFIX/bin:$PATH
PS1="[mono-2.4] \w @ "

[Press Ctrl+D for EOF to write the file and get back to the prompt]

user@system$> sudo mv mono-2.4-environment /usr/local/bin
user@system$> sudo chmod +x /usr/local/bin/mono-2.4-environment

And create a second script that will load the environment and execute its argurments.

user@system$> cat > mono-2.4
#!/bin/bash
MONO_PREFIX=/opt/mono-2.4
GNOME_PREFIX=/opt/gnome-2.4
export DYLD_LIBRARY_PATH=$MONO_PREFIX/lib:$DYLD_LIBRARY_PATH
export LD_LIBRARY_PATH=$MONO_PREFIX/lib:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=$MONO_PREFIX/include:$GNOME_PREFIX/include
export ACLOCAL_PATH=$MONO_PREFIX/share/aclocal
export PKG_CONFIG_PATH=$MONO_PREFIX/lib/pkgconfig:$GNOME_PREFIX/lib/pkgconfig
PATH=$MONO_PREFIX/bin:$PATH

exec "$@"

[Press Ctrl+D for EOF to write the file and get back to the prompt]

user@system$> sudo mv mono-2.4 /usr/local/bin
user@system$> sudo chmod +x /usr/local/bin/mono-2.4

Load the environment:

user@system$> source mono-2.4-environment

After this command, the prompt should change and contain [mono-2.4] to indicate that you are operating in a Mono 2.4 environment.

[mono-2.4] ~ @ cd ~/src/mono-2.4

NOTE! For the sake of readability the prompt indication in the instructions will remain the generic user@system$> but for the rest of the guide you must make sure that you follow each step while in the 2.4 environment.

And now create the directory where everything will be installed:

user@system$> sudo mkdir -p /opt/mono-2.4

Step 4: libgdiplus

Before compiling the main sources of mono we must compile libgdiplus if we want to have an implementation of the System.Drawing namespace.

user@system$> cd libgdiplus-2.4
user@system$> ./configure --prefix=/opt/mono-2.4 --with-pango

The output of the configure script should be something like this:

---
Configuration summary

   * Installation prefix = /opt/mono-2.4
   * Cairo = 1.8.0 (system)
   * Text = pango
   * EXIF tags = yes
   * Codecs supported:

      - TIFF: yes
      - JPEG: yes
      - GIF: yes
      - PNG: yes
      NOTE: if any of the above say 'no' you may install the
            corresponding development packages for them, rerun
            autogen.sh to include them in the build.

---

If any of the options say no then you have a missing dependency. You might want to use the Synaptic package manager to find the appropriate package to install. However, these dependencies are optional.

Now it’s time to build the source.

user@system$> make
user@system$> sudo make install

Step 5: mono-2.4

Now it’s time to compile mono’s main sources which will produce, among others, the c# compilers and the base class libraries.

user@system$> cd ../mono-2.4
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install

Verify that the mono compilers have been installed:

user@system$> which gmcs

This should output: /opt/mono-2.4/bin/gmcs
Your existing mono installation will be preserved since everything is installed to the /opt/mono-2.4 directory.

Step 6: gtk+ and gnome

Now it’s turn to compile libraries needed for desktop environment, namely gtk-sharp, gnome-sharp, gnome-desktop-sharp, gtksourceview-sharp.

user@system$> cd ../gtk-sharp-2.12.8
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install
user@system$> cd ../gnome-sharp-2.20.1
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install
user@system$> cd ../gnome-desktop-sharp-2.20.1
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install

Step 7: gluezilla, gecko-sharp, webkit-sharp

Now lets compile libraries for embedding the gecko and webkit html rendering engines.

user@system$> cd ../gluezilla-2.4
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install
user@system$> cd ../gecko-sharp-2.0-0.13
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install
user@system$> cd ../webkit-sharp-0.2
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install

Step 8: build Mono.Addins

Turn to compile the Mono.Addins libraries:

user@system$> cd ../mono-addins-0.4
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install

Step 9: build Mono tools

Now it’s turn to compile the marvelous collection of Mono tools, including Gendarme and GSharp (the C# interactive shell).

user@system$> cd ../mono-tools-2.4
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install

After this you have installed the gsharp tool. Test it out:

user@system$> gsharp

Play a little with the C# repl Smile Type quit; when you’re done.

Step 10: build Mono XSP

This is actually something very useful. XSP is a standalone web server developed in C#. It can be run through the command line or even embedded into your application by referencing the Mono.WebServer assembly.

user@system$> cd ../xsp-2.4
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install

Step 11: build the Mono Debugger

Getting closer to the end of our compilation session, let’s build the mono debugger allowing us to debug the faulty software we write.

user@system$> cd ../mono-debugger-2.4
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install

Step 12: build MonoDevelop 2.0

Now what would all this be without a nice IDE? MonoDevelop 2.0 brings us finally debugger support!

user@system$> cd ../monodevelop-2.0
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install
user@system$> cd ../monodevelop-debugger-mdb-2.0
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install
user@system$> cd ../monodevelop-debugger-gdb-2.0
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install
user@system$> cd ../monodevelop-database-2.0
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install
user@system$> cd ../monodevelop-java-2.0
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install
user@system$> cd ../monodevelop-vala-2.0
user@system$> ./configure --prefix=/opt/mono-2.4
user@system$> make
user@system$> sudo make install

And… That’s it. You now have a working mono-2.4 environment and an excellent IDE to work with.

You can launch any .NET program with Mono 2.4 with:

user@system$> mono-2.4 PATH_TO_THE_PROGRAM_AND_ITS_ARGUMENTS_IF_ANY

For example to launch MonoDevelop 2.0:

user@system#> mono-2.4 monodevelop

NOTE! This will work only for shell scripts that are normally distributed with mono applications. If you look at /opt/mono-2.4/bin/monodevelop you will see that it is a shell script that essentially runs something like this: mono /opt/mono-2.4/lib/monodevelop/bin/MonoDevelop.exe. Trying to execute a .NET application, e.g. called main.exe, by typing mono-2.4 main.exe will fail. You sould type mono-2.4 mono main.exe instead.

Enjoy hacking!