[Crash-utility] [PATCH V2] extensions: add extend -s option to show all available shared object file

2020-03-02 Thread Wang Long
When we load extensions, sometime we do not know the exact name of the shared
object file.

This patch add -s option for extend cmd to show all available shared object 
file.

for example:

crash> extend -s
./trace.so
/usr/lib64/crash/extensions/dminfo.so
/usr/lib64/crash/extensions/echo.so
/usr/lib64/crash/extensions/eppic.so
/usr/lib64/crash/extensions/snap.so
/usr/lib64/crash/extensions/trace.so
./extensions/dminfo.so
./extensions/eppic.so
./extensions/echo.so
./extensions/snap.so
./extensions/trace.so
crash> extend -s -l
extend: -l and -s are mutually exclusive
Usage:
  extend [shared-object ...] | [-u [shared-object ...]] | -s
Enter "help extend" for details.
crash> extend -s -u
extend: -u and -s are mutually exclusive
Usage:
  extend [shared-object ...] | [-u [shared-object ...]] | -s
Enter "help extend" for details.
crash>

Also, this patch update the help for extend command:
add the search order "5. the ./extensions subdirectory of the current directory"

Changes since v1:
- -s option also check the current working directory
- fix warning

Signed-off-by: Wang Long 
---
 extensions.c | 75 
 help.c   |  4 +++-
 2 files changed, 74 insertions(+), 5 deletions(-)

diff --git a/extensions.c b/extensions.c
index 24b91de..d23b1e3 100644
--- a/extensions.c
+++ b/extensions.c
@@ -20,10 +20,13 @@
 
 static int in_extensions_library(char *, char *);
 static char *get_extensions_directory(char *);
+static void show_all_extensions(void);
+static void show_extensions(char *);
 
-#define DUMP_EXTENSIONS   (0)
-#define LOAD_EXTENSION(1)
-#define UNLOAD_EXTENSION  (2)
+#define DUMP_EXTENSIONS(0)
+#define LOAD_EXTENSION (1)
+#define UNLOAD_EXTENSION   (2)
+#define SHOW_ALL_EXTENSIONS(4)
 
 /*
  *  Load, unload, or list the extension libaries.
@@ -36,14 +39,30 @@ cmd_extend(void)
 
flag = DUMP_EXTENSIONS;
 
-while ((c = getopt(argcnt, args, "lu")) != EOF) {
+while ((c = getopt(argcnt, args, "lus")) != EOF) {
 switch(c)
 {
+   case 's':
+   if (flag & UNLOAD_EXTENSION) {
+   error(INFO,
+   "-s and -u are mutually exclusive\n");
+   argerrs++;
+   }else if (flag & LOAD_EXTENSION) {
+   error(INFO,
+   "-s and -l are mutually exclusive\n");
+   argerrs++;
+   } else
+   flag |= SHOW_ALL_EXTENSIONS;
+   break;
case 'l':
if (flag & UNLOAD_EXTENSION) {
error(INFO, 
"-l and -u are mutually exclusive\n");
argerrs++;
+   } else if (flag & SHOW_ALL_EXTENSIONS) {
+   error(INFO, 
+   "-l and -s are mutually exclusive\n");
+   argerrs++;
} else
flag |= LOAD_EXTENSION;
break;
@@ -53,6 +72,10 @@ cmd_extend(void)
 error(INFO, 
 "-u and -l are mutually exclusive\n");
 argerrs++;
+   } else if (flag & SHOW_ALL_EXTENSIONS) {
+   error(INFO, 
+   "-u and -s are mutually exclusive\n");
+   argerrs++;
 } else
 flag |= UNLOAD_EXTENSION;
break;
@@ -100,6 +123,11 @@ cmd_extend(void)
optind++;
}
break;
+
+   case SHOW_ALL_EXTENSIONS:
+   show_all_extensions();
+   break;
+
}
 }
 
@@ -182,6 +210,45 @@ dump_extension_table(int verbose)
} while ((ext = ext->prev));
 }
 
+static void
+show_extensions(char *dir) {
+   DIR *dirp;
+   struct dirent *dp;
+   char filename[BUFSIZE*2];
+
+dirp = opendir(dir);
+   if (!dirp)
+   return;
+
+for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
+   sprintf(filename, "%s%s%s", dir,
+   LASTCHAR(dir) == '/' ? "" : "/",
+   dp->d_name);
+
+   if (!is_shared_object(filename))
+   continue;
+   fprintf(fp, "%s\n", filename);
+   }
+
+   closedir(dirp);
+}
+
+static void
+show_all_extensions(void)
+{
+   char *dir;
+
+   show_extensions("./");
+
+   if ((dir = getenv("CRASH_EXTENSIONS")))
+   show_extensions(dir);
+
+   if (BITS64())
+   

Re: [Crash-utility] [PATCH] extensions: add extend -s option to show all available shared object file

2020-03-02 Thread Dave Anderson


- Original Message -
> When we load extensions, sometime we do not know the exact name of the shared
> object file.
> 
> This patch add -s option for extend cmd to show all available shared object
> file.

Hello Wang,

I think this patch is a good idea.  Couple things, though...

Building with "make warn" shows these warnings:

  $ make warn
  ...
  cc -c -g -DX86_64 -DLZO -DSNAPPY -DGDB_7_6  extensions.c -Wall -O2 
-Wstrict-prototypes -Wmissing-prototypes -fstack-protector -Wformat-security 
  extensions.c:23:1: warning: function declaration isn’t a prototype 
[-Wstrict-prototypes]
   static void show_all_extensions();
   ^
  extensions.c:212:6: warning: no previous prototype for 'show_extensions' 
[-Wmissing-prototypes]
   void show_extensions(char *dir) {
^
  extensions.c: In function 'show_extensions':
  extensions.c:216:6: warning: variable 'found' set but not used 
[-Wunused-but-set-variable]
int found;
^
  extensions.c: At top level:
  extensions.c:236:1: warning: function declaration isn’t a prototype 
[-Wstrict-prototypes]
   show_all_extensions()
   ^
  ...

And secondly, your show_extensions() function doesn't check the
current working directory.  Even though the description below 
only applies when an extension module argument is supplied, it 
makes sense that it should also apply to your -s option:

  $ help extend
  ...
If the shared-object filename is not expressed with a fully-qualified
pathname, the following directories will be searched in the order shown,
and the first instance of the file that is found will be selected:
  
   1. the current working directory
   2. the directory specified in the CRASH_EXTENSIONS environment variable
   3. /usr/lib64/crash/extensions (64-bit architectures)
   4. /usr/lib/crash/extensions
   5. the ./extensions subdirectory of the current directory
  ... 
  
Thanks,
  Dave



> 
> for example:
> 
> crash> extend -s
> /usr/lib64/crash/extensions/dminfo.so
> /usr/lib64/crash/extensions/echo.so
> /usr/lib64/crash/extensions/eppic.so
> /usr/lib64/crash/extensions/snap.so
> /usr/lib64/crash/extensions/trace.so
> ./extensions/dminfo.so
> ./extensions/eppic.so
> ./extensions/echo.so
> ./extensions/snap.so
> ./extensions/trace.so
> crash> extend -s -l
> extend: -l and -s are mutually exclusive
> Usage:
>   extend [shared-object ...] | [-u [shared-object ...]] | -s
> Enter "help extend" for details.
> crash> extend -s -u
> extend: -u and -s are mutually exclusive
> Usage:
>   extend [shared-object ...] | [-u [shared-object ...]] | -s
> Enter "help extend" for details.
> crash>
> 
> Also, this patch update the help for extend command:
> add the search order "5. the ./extensions subdirectory of the current
> directory"
> 
> Signed-off-by: Wang Long 
> ---
>  extensions.c | 72
>  
>  help.c   |  4 +++-
>  2 files changed, 71 insertions(+), 5 deletions(-)
> 
> diff --git a/extensions.c b/extensions.c
> index 24b91de..bdf9e93 100644
> --- a/extensions.c
> +++ b/extensions.c
> @@ -20,10 +20,12 @@
>  
>  static int in_extensions_library(char *, char *);
>  static char *get_extensions_directory(char *);
> +static void show_all_extensions();
>  
> -#define DUMP_EXTENSIONS   (0)
> -#define LOAD_EXTENSION(1)
> -#define UNLOAD_EXTENSION  (2)
> +#define DUMP_EXTENSIONS(0)
> +#define LOAD_EXTENSION (1)
> +#define UNLOAD_EXTENSION   (2)
> +#define SHOW_ALL_EXTENSIONS(4)
>  
>  /*
>   *  Load, unload, or list the extension libaries.
> @@ -36,14 +38,30 @@ cmd_extend(void)
>  
>   flag = DUMP_EXTENSIONS;
>  
> -while ((c = getopt(argcnt, args, "lu")) != EOF) {
> +while ((c = getopt(argcnt, args, "lus")) != EOF) {
>  switch(c)
>  {
> + case 's':
> + if (flag & UNLOAD_EXTENSION) {
> + error(INFO,
> + "-s and -u are mutually exclusive\n");
> + argerrs++;
> + }else if (flag & LOAD_EXTENSION) {
> + error(INFO,
> + "-s and -l are mutually exclusive\n");
> + argerrs++;
> + } else
> + flag |= SHOW_ALL_EXTENSIONS;
> + break;
>   case 'l':
>   if (flag & UNLOAD_EXTENSION) {
>   error(INFO,
>   "-l and -u are mutually exclusive\n");
>   argerrs++;
> + } else if (flag & SHOW_ALL_EXTENSIONS) {
> + error(INFO,
> + "-l and -s are mutually exclusive\n");
> + argerrs++;
>   } else
>   flag |= LOAD_EXTENSION;
>