]> sourceware.org Git - newlib-cygwin.git/blob - winsup/utils/pldd.c
Cygwin: add 3.2.1 release file and add fixes up to this point
[newlib-cygwin.git] / winsup / utils / pldd.c
1 /* pldd.c
2
3 This file is part of Cygwin.
4
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
7 details. */
8
9 #define WIN32_LEAN_AND_MEAN
10 #define UNICODE
11 #include <errno.h>
12 #include <error.h>
13 #include <getopt.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/cygwin.h>
17 #include <cygwin/version.h>
18 #include <windows.h>
19 #include <psapi.h>
20
21 struct option longopts[] =
22 {
23 {"help", no_argument, NULL, '?'},
24 {"version", no_argument, NULL, 'V'},
25 {"usage", no_argument, NULL, 0},
26 {0, no_argument, NULL, 0}
27 };
28 const char *opts = "?V";
29
30 __attribute__((noreturn))
31 static void
32 print_help (void)
33 {
34 printf ("Usage: pldd [OPTION...] PID\n\n"
35 "List dynamic shared objects loaded into a process.\n\n"
36 " -?, --help Give this help list\n"
37 " --usage Give a short usage message\n"
38 " -V, --version Print program version\n");
39 exit (EXIT_SUCCESS);
40 }
41
42 __attribute__((noreturn))
43 static void
44 print_usage (void)
45 {
46 printf ("Usage: pldd [-?V] [--help] [--usage] [--version] PID\n");
47 exit (EXIT_SUCCESS);
48 }
49
50 __attribute__((noreturn))
51 static void
52 print_version ()
53 {
54 printf ("pldd (cygwin) %d.%d.%d\n"
55 "List dynamic shared objects loaded into process.\n"
56 "Copyright (C) 2012 Cygwin Authors\n\n"
57 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
58 "You may redistribute copies of this program under the terms of\n"
59 "the Cygwin license. Please consult the CYGWIN_LICENSE file for details.\n",
60 CYGWIN_VERSION_DLL_MAJOR / 1000,
61 CYGWIN_VERSION_DLL_MAJOR % 1000,
62 CYGWIN_VERSION_DLL_MINOR);
63 exit (EXIT_SUCCESS);
64 }
65
66 __attribute__((noreturn))
67 static void
68 print_nargs (void)
69 {
70 fprintf (stderr, "Exactly one parameter with process ID required.\n"
71 "Try `pldd --help' or `pldd --usage' for more information.\n");
72 exit (EXIT_FAILURE);
73 }
74
75 int
76 main (int argc, char *argv[])
77 {
78 int optch, pid, winpid, i;
79 char *pidfile, *exefile, *exename;
80 FILE *fd;
81 HANDLE hProcess;
82 HMODULE hModules[1024];
83 DWORD cbModules;
84
85 while ((optch = getopt_long (argc, argv, opts, longopts, &optind)) != -1)
86 switch (optch)
87 {
88 case '?':
89 print_help ();
90 break;
91 case 'V':
92 print_version ();
93 break;
94 case 0:
95 if (strcmp ("usage", longopts[optind].name) == 0)
96 print_usage ();
97 break;
98 default:
99 break;
100 }
101
102 argc -= optind;
103 argv += optind;
104 if (argc != 1)
105 print_nargs ();
106
107 pid = atoi (argv[0]);
108
109 if ((pid == 0))
110 error (1, 0, "invalid process ID '%s'", argv[0]);
111
112 pidfile = (char *) malloc (32);
113 sprintf (pidfile, "/proc/%d/winpid", pid);
114 fd = fopen (pidfile, "rb");
115 if (!fd)
116 error (1, ENOENT, "cannot open /proc/%d", pid);
117 fscanf (fd, "%d", &winpid);
118 fclose (fd);
119
120 exefile = (char *) malloc (32);
121 exename = (char *) malloc (MAX_PATH);
122 sprintf (exefile, "/proc/%d/exename", pid);
123 fd = fopen (exefile, "rb");
124 if (!fd)
125 error (1, ENOENT, "cannot open /proc/%d", pid);
126 fscanf (fd, "%s", exename);
127 fclose (fd);
128
129 hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
130 0, winpid);
131 if (!hProcess)
132 error (1, EPERM, "cannot attach to process %d", pid);
133
134 printf ("%d:\t%s\n", pid, exename);
135
136 EnumProcessModules (hProcess, hModules, sizeof (hModules), &cbModules);
137 /* start at 1 to skip the executable itself */
138 for (i = 1; i < (cbModules / sizeof (HMODULE)); i++)
139 {
140 TCHAR winname[MAX_PATH];
141 char posixname[MAX_PATH];
142 GetModuleFileNameEx (hProcess, hModules[i], winname, MAX_PATH);
143 cygwin_conv_path (CCP_WIN_W_TO_POSIX, winname, posixname, MAX_PATH);
144 printf ("%s\n", posixname);
145 }
146
147 return 0;
148 }
This page took 0.039897 seconds and 5 git commands to generate.