Bug 1089 - Process.MainModule.FileName sometimes reports wrong path
Summary: Process.MainModule.FileName sometimes reports wrong path
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 2.10.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2011-09-27 21:30 UTC by al.smith77
Modified: 2012-07-21 11:59 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Sample code to reproduce (408 bytes, text/plain)
2011-09-27 21:30 UTC, al.smith77

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description al.smith77 2011-09-27 21:30:45 UTC
Created attachment 520 [details]
Sample code to reproduce

Process.MainModule.FileName is inconsistent, depending on how the application is executed.  

If the exe is in the current directory, the output is the path to the exe:

user@ubuntu:~/Projects/test/test/bin/Debug$ /opt/mono-2.10.6/bin/mono ./test.exe
FileName: /home/user/Projects/test/test/bin/Debug/test.exe
FileName2: /home/user/Projects/test/test/bin/Debug/test.exe

If the exe is not in the current directory, the output is the path to mono itself:

user@ubuntu:~/Projects/test/test/bin$ /opt/mono-2.10.6/bin/mono ./Debug/test.exe
FileName: /opt/mono-2.10.6/bin/mono
FileName2: /home/user/Projects/test/test/bin/Debug/test.exe

Note that the response from Assembly.GetEntryAssembly().Location is always correct.

This has been tested on ubuntu and CentOS, with the same results.

Example code:

using System;
using System.Diagnostics;

namespace test
	class MainClass
		public static void Main (string[] args)
			Process process=Process.GetCurrentProcess();

			string fileName=process.MainModule.FileName;
			string fileName2=System.Reflection.Assembly.GetEntryAssembly().Location;
			Console.WriteLine("FileName: " + fileName);
			Console.WriteLine("FileName2: " + fileName2);
Comment 1 Zoltan Varga 2012-01-20 00:10:17 UTC
The problem is in  get_module_name () in processes.c, where process_handle->proc_name is the relative file name, while found_module->filename is the absolute  filename, so the two don't match, falling back to:
		if (procname_ext == NULL)
			/* If it's *still* null, we might have hit the
			 * case where reading /proc/$pid/maps gives an
			 * empty file for this user.
			procname_ext = get_process_name_from_proc (pid);
Comment 2 Robert Wilkens 2012-06-25 11:13:05 UTC
I have a suggested fix, but i can't guarantee it will be accepted


Description and diff in the above pull request.

I am OK if the fix gets rejected though, it is just one possible fix which works for this patch.  It seems to be safe in light of similar code in the same function, as described in the pull request/commit.
Comment 3 Robert Wilkens 2012-06-25 11:25:32 UTC
Ooops-- typo:  "which works for this patch" should read "which works for this sample code".
Comment 4 Miguel de Icaza [MSFT] 2012-07-21 11:59:11 UTC
I merged this patch.