[PATCH] Add pldd(1)

2012-02-24 Thread Yaakov (Cygwin/X)
The pldd(1) command apparently originates from Solaris and was added to
glibc-2.15[1].  Patches and new file attached.


Yaakov

[1] http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=NEWS
/* pldd.cc

   Copyright 2012 Red Hat, Inc.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#define WIN32_LEAN_AND_MEAN
#define UNICODE
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

struct option longopts[] =
{
  {"help", no_argument, NULL, '?'},
  {"version", no_argument, NULL, 'V'},
  {"usage", no_argument, NULL, 0},
  {0, no_argument, NULL, 0}
};
const char *opts = "?V";

__attribute__((noreturn))
static void
print_help (void)
{
  printf ("Usage: pldd [OPTION...] PID\n\n"
  "List dynamic shared objects loaded into a process.\n\n"
  "  -?, --help Give this help list\n"
  "  --usageGive a short usage message\n"
  "  -V, --version  Print program version\n");
  exit (EXIT_SUCCESS);
}

__attribute__((noreturn))
static void
print_usage (void)
{
  printf ("Usage: pldd [-?V] [--help] [--usage] [--version] PID\n");
  exit (EXIT_SUCCESS);
}

__attribute__((noreturn))
static void
print_version ()
{
  printf ("pldd (cygwin) %d.%d.%d\n"
	  "List dynamic shared objects loaded into process.\n"
	  "Copyright (C) 2012 Red Hat, Inc.\n\n"
	  "This program comes with NO WARRANTY, to the extent permitted by law.\n"
	  "You may redistribute copies of this program under the terms of\n"
	  "the Cygwin license. Please consult the CYGWIN_LICENSE file for details.\n",
	  CYGWIN_VERSION_DLL_MAJOR / 1000,
	  CYGWIN_VERSION_DLL_MAJOR % 1000,
	  CYGWIN_VERSION_DLL_MINOR);
  exit (EXIT_SUCCESS);
}

__attribute__((noreturn))
static void
print_nargs (void)
{
  fprintf (stderr, "Exactly one parameter with process ID required.\n"
   "Try `pldd --help' or `pldd --usage' for more information.\n");
  exit (EXIT_FAILURE);
}

int
main (int argc, char *argv[])
{
  int optch, pid, winpid, i;
  char *pidfile, *exefile, *exename;
  FILE *fd;
  HANDLE hProcess;
  HMODULE hModules[1024];
  DWORD cbModules;

  while ((optch = getopt_long (argc, argv, opts, longopts, &optind)) != -1)
switch (optch)
  {
  case '?':
print_help ();
break;
  case 'V':
print_version ();
break;
  case 0:
if (strcmp( "usage", longopts[optind].name ) == 0)
  print_usage ();
break;
  default:
break;
  }

  argc -= optind;
  argv += optind;
  if (argc != 1)
print_nargs ();

  pid = atoi (argv[0]);

  if ((pid == 0))
error (1, 0, "invalid process ID '%s'", argv[0]);

  pidfile = (char *) malloc (32);
  sprintf(pidfile, "/proc/%d/winpid", pid);
  fd = fopen (pidfile, "rb");
  if (!fd)
error (1, ENOENT, "cannot open /proc/%d", pid);
  fscanf (fd, "%d", &winpid);

  exefile = (char *) malloc (32);
  exename = (char *) malloc (MAX_PATH);
  sprintf(exefile, "/proc/%d/exename", pid);
  fd = fopen (exefile, "rb");
  fscanf (fd, "%s", exename);

  hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, winpid);
  if (!hProcess)
error (1, EPERM, "cannot attach to process %d", pid);

  printf ("%d:\t%s\n", pid, exename);

  EnumProcessModules (hProcess, hModules, sizeof(hModules), &cbModules);
  /* start at 1 to skip the executable itself */
  for (i = 1; i < (cbModules / sizeof(HMODULE)); i++)
{
  TCHAR winname[MAX_PATH];
  char posixname[MAX_PATH];
  GetModuleFileNameEx (hProcess, hModules[i], winname, MAX_PATH);
  cygwin_conv_path (CCP_WIN_W_TO_POSIX, winname, posixname, MAX_PATH);
  printf ("%s\n", posixname);
}

  return 0;
}
2012-02-??  Yaakov Selkowitz  

	* Makefile.in (CYGWIN_BINS): Add pldd.
	(pldd.exe): Add -lpsapi to ALL_LDFLAGS.
	* pldd.c: New file.
	* utils.sgml (pldd): New section.

Index: Makefile.in
===
RCS file: /cvs/src/src/winsup/utils/Makefile.in,v
retrieving revision 1.98
diff -u -p -r1.98 Makefile.in
--- Makefile.in	29 Jan 2012 09:41:06 -	1.98
+++ Makefile.in	24 Feb 2012 07:44:28 -
@@ -53,7 +53,7 @@ MINGW_CXX:= ${srcdir}/mingw ${CX
 # List all binaries to be linked in Cygwin mode.  Each binary on this list
 # must have a corresponding .o of the same name.
 CYGWIN_BINS := ${addsuffix .exe,cygpath getconf getfacl ldd locale kill mkgroup \
-mkpasswd mount passwd ps regtool setfacl setmetamode ssp tzset umount}
+mkpasswd mount passwd pldd ps regtool setfacl setmetamode ssp tzset umount}
 
 # List all binaries to be linked in MinGW mode.  Each binary on this list
 # must have a corresponding .o of the same name.
@@ -81,6 +81,7 @@ ps.exe: ALL_LDFLAGS += -lcygwin -lpsapi 
 strace.exe: MINGW_LDFLAGS += -lntdll
 
 ldd.exe: ALL_LDFLAGS += -lpsapi
+pldd.exe: ALL_LDFLAGS += -lpsapi
 

Re: [PATCH] Add pthread_getname_np, pthread_setname_np

2012-02-24 Thread Corinna Vinschen
On Feb 23 21:38, Yaakov (Cygwin/X) wrote:
> This patchset adds pthread_getname_np and pthread_setname_np.  These
> were added to glibc in 2.12[1] and are also present in some form on
> NetBSD and several UNIXes.  IIUC recent versions of GDB can benefit from
> this support.

Thanks for your patch, but I don't think it's the whole thing.

Consider, if you implement pthread_[gs]etname_np as you did, then you
have pthread names which are only available to the process in which
the threads are running.  So, how could GDB get the information for
its inferior process?

Actually GDB reads the thread name using a target specific function
which is so far only implemented for Linux.  It does not use
pthread_getname_np, rather it reads the name from /proc/$PID/task/$TID/comm.

And that's a bit of a problem in Cygwin.  Every Cygwin process is
multi-threaded (think signals), but only the application-started threads
are pthreads.

So, again, thanks for doing this, but I think this requires more work to
be useful.  The basic task is to provide /proc/$PID/task for all threads
running in a Cygwin process.  If that's available, the pthread_[gs]etname_np
will become useful and their (different) implementation probably falls into
place.


Corinna

-- 
Corinna Vinschen  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader  cygwin AT cygwin DOT com
Red Hat


Re: [PATCH] Add pldd(1)

2012-02-24 Thread Corinna Vinschen
On Feb 24 02:38, Yaakov (Cygwin/X) wrote:
> The pldd(1) command apparently originates from Solaris and was added to
> glibc-2.15[1].  Patches and new file attached.

Looks good, works fine.  Please apply.


Thanks,
Corinna

-- 
Corinna Vinschen  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader  cygwin AT cygwin DOT com
Red Hat


Re: [PATCH] Add pthread_getname_np, pthread_setname_np

2012-02-24 Thread Yaakov (Cygwin/X)
On Fri, 2012-02-24 at 10:38 +0100, Corinna Vinschen wrote:
> On Feb 23 21:38, Yaakov (Cygwin/X) wrote:
> > This patchset adds pthread_getname_np and pthread_setname_np.  These
> > were added to glibc in 2.12[1] and are also present in some form on
> > NetBSD and several UNIXes.  IIUC recent versions of GDB can benefit from
> > this support.
> 
> Thanks for your patch, but I don't think it's the whole thing.
> 
> Consider, if you implement pthread_[gs]etname_np as you did, then you
> have pthread names which are only available to the process in which
> the threads are running.

My implementation is based on NetBSD's[1].  So what purpose do these
functions serve then on that it and the UNIXes?  (Serious question.)


Yaakov

[1] http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libpthread/pthread.c?rev=1.125




Re: [PATCH] Add pthread_getname_np, pthread_setname_np

2012-02-24 Thread Corinna Vinschen
On Feb 24 05:00, Yaakov (Cygwin/X) wrote:
> On Fri, 2012-02-24 at 10:38 +0100, Corinna Vinschen wrote:
> > On Feb 23 21:38, Yaakov (Cygwin/X) wrote:
> > > This patchset adds pthread_getname_np and pthread_setname_np.  These
> > > were added to glibc in 2.12[1] and are also present in some form on
> > > NetBSD and several UNIXes.  IIUC recent versions of GDB can benefit from
> > > this support.
> > 
> > Thanks for your patch, but I don't think it's the whole thing.
> > 
> > Consider, if you implement pthread_[gs]etname_np as you did, then you
> > have pthread names which are only available to the process in which
> > the threads are running.
> 
> My implementation is based on NetBSD's[1].  So what purpose do these
> functions serve then on that it and the UNIXes?  (Serious question.)

See the source of the pthread_setname_np function.  There's a call to
the kernel:

  thread->pt_name = cp;
  (void)_lwp_setname(thread->pt_lid, cp);

_lwp_setname ultimately calls the kernel function sys__lwp_setname in
http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/kern/sys_lwp.c?rev=1.53
So the kernel knows the name and the sys__lwp_getname entry point
can be used to fetch the name of a thread in another process.  How
exactly this is fetched by which BSD tool, I don't know, but it's
all in the sources :)


Corinna

-- 
Corinna Vinschen  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader  cygwin AT cygwin DOT com
Red Hat


Re: [PATCH] Add pthread_getname_np, pthread_setname_np

2012-02-24 Thread Pedro Alves
Just FYI,

Windows' way to have the program affect thread names in the
debugger is with SetThreadName, which throws a magic exception
which the debugger can catch.  GDB doesn't know about this though.

 http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx

Just for completeness...  I don't know if there's a native
method that's closer to pthread_setname_np's semantics.

-- 
Pedro Alves


Re: [PATCH] Add pldd(1)

2012-02-24 Thread Christopher Faylor
On Fri, Feb 24, 2012 at 10:47:07AM +0100, Corinna Vinschen wrote:
>On Feb 24 02:38, Yaakov (Cygwin/X) wrote:
>> The pldd(1) command apparently originates from Solaris and was added to
>> glibc-2.15[1].  Patches and new file attached.
>
>Looks good, works fine.  Please apply.

It's not entirely fine.  Minor nits:

- The comment says "pldd.cc" but it's pldd.c.

- There was no ChangeLog entry announcing its checkin.

cgf


Re: [PATCH] Add pldd(1)

2012-02-24 Thread Corinna Vinschen
On Feb 24 15:31, Christopher Faylor wrote:
> On Fri, Feb 24, 2012 at 10:47:07AM +0100, Corinna Vinschen wrote:
> >On Feb 24 02:38, Yaakov (Cygwin/X) wrote:
> >> The pldd(1) command apparently originates from Solaris and was added to
> >> glibc-2.15[1].  Patches and new file attached.
> >
> >Looks good, works fine.  Please apply.
> 
> It's not entirely fine.  Minor nits:
> 
> - The comment says "pldd.cc" but it's pldd.c.
> 
> - There was no ChangeLog entry announcing its checkin.

Oh, right.  The ChangeLog entry was part of the patch submission, though.
Yaakov, could you apply the ChangeLog as well, please?


Corinna

-- 
Corinna Vinschen  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader  cygwin AT cygwin DOT com
Red Hat