调试toybox

官网

下载

toybox-0.8.9.tar.gz

编译

export CFLAGS=-g
make

调试

$ gdb --args ./generated/unstripped/toybox ls

GNU gdb (Ubuntu 10.2-0ubuntu1~20.04~1) 10.2
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./generated/unstripped/toybox...
(gdb) start
Temporary breakpoint 1 at 0x126f0: file main.c, line 279.
Starting program: /vol_8t/work/Perf_issue/toybox/toybox-0.8.9/generated/unstripped/toybox ls

Temporary breakpoint 1, main (argc=2, argv=0x7fffffffda28) at main.c:279
279     {
(gdb) l
274       }
275       xputc('\n');
276     }
277
278     int main(int argc, char *argv[])
279     {
280       int i;
281
282       for (i = 0; i < argc; i++)
283           printf("argv[%d] = %s\n", i, argv[i]);
(gdb)

杂记

以top命令为例,在toys/posix/ps.c中有关于top命名支持的参数的介绍:

USE_TOP(NEWTOY(top, ">0O*h" "Hk*o*p*u*s#<1d%<100=3000m#n#<1bq[!oO]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE))

关于特殊字符的作用的介绍,参考lib/args.c:

点击查看代码
// Design goals:
//   Don't use getopt() out of libc.
//   Don't permute original arguments (screwing up ps/top output).
//   Integrated --long options "(noshort)a(along)b(blong1)(blong2)"

/* This uses a getopt-like option string, but not getopt() itself. We call
 * it the get_opt string.
 *
 * Each option in the get_opt string corresponds to a bit position in the
 * return value. The rightmost argument is (1<<0), the next to last is (1<<1)
 * and so on. If the option isn't seen in argv[], its bit remains 0.
 *
 * Options which have an argument fill in the corresponding slot in the global
 * union "this" (see generated/globals.h), which it treats as an array of longs
 * (note that sizeof(long)==sizeof(pointer) is guaranteed by LP64).
 *
 * You don't have to free the option strings, which point into the environment
 * space. List objects should be freed by main() when command_main() returns.
 *
 * Example:
 *   Calling get_optflags() when toys.which->options="ab:c:d" and
 *   argv = ["command", "-b", "fruit", "-d", "walrus"] results in:
 *
 *     Changes to struct toys:
 *       toys.optflags = 5 (I.E. 0101 so -b = 4 | -d = 1)
 *       toys.optargs[0] = "walrus" (leftover argument)
 *       toys.optargs[1] = NULL (end of list)
 *       toys.optc = 1 (there was 1 leftover argument)
 *
 *     Changes to union this:
 *       this[0]=NULL (because -c didn't get an argument this time)
 *       this[1]="fruit" (argument to -b)
 */

// What you can put in a get_opt string:
//   Any otherwise unused character (all letters, unprefixed numbers) specify
//   an option that sets a flag. The bit value is the same as the binary digit
//   if you string the option characters together in order.
//   So in "abcdefgh" a = 128, h = 1
//
//   Suffixes specify that this option takes an argument (stored in GLOBALS):
//       Note that pointer and long are always the same size, even on 64 bit.
//     : string argument, keep most recent if more than one
//     * string argument, appended to a struct arg_list linked list.
//     # signed long argument
//       <LOW     - die if less than LOW
//       >HIGH    - die if greater than HIGH
//       =DEFAULT - value if not specified
//     - signed long argument defaulting to negative (say + for positive)
//     . double precision floating point argument (with CFG_TOYBOX_FLOAT)
//       Chop this option out with USE_TOYBOX_FLOAT() in option string
//       Same <LOW>HIGH=DEFAULT as #
//     @ occurrence counter (which is a long)
//     % time offset in milliseconds with optional s/m/h/d suffix
//     (longopt)
//     | this is required. If more than one marked, only one required.
//     ; Option's argument is optional, and must be collated: -aARG or --a=ARG
//     ^ Stop parsing after encountering this argument
//    " " (space char) the "plus an argument" must be separate
//        I.E. "-j 3" not "-j3". So "kill -stop" != "kill -s top"
//
//   At the beginning of the get_opt string (before any options):
//     <0 die if less than # leftover arguments (default 0)
//     >9 die if > # leftover arguments (default MAX_INT)
//     0 Include argv[0] in optargs
//     ^ stop at first nonoption argument
//     ? Pass unknown arguments through to command (implied when no flags).
//     & first arg has imaginary dash (ala tar/ps/ar) which sets FLAGS_NODASH
//     ~ Collate following bare longopts (as if under short opt, repeatable)
//
//   At the end: [groups] of previously seen options
//     - Only one in group (switch off)    [-abc] means -ab=-b, -ba=-a, -abc=-c
//     + Synonyms (switch on all)          [+abc] means -ab=-abc, -c=-abc
//     ! More than one in group is error   [!abc] means -ab calls error_exit()
//       primarily useful if you can switch things back off again.
//
//   You may use octal escapes with the high bit (127) set to use a control
//   character as an option flag. For example, \300 would be the option -@

// Notes from getopt man page
//   - and -- cannot be arguments.
//     -- force end of arguments
//     - is a synonym for stdin in file arguments
//   -abcd means -a -b -c -d (but if -b takes an argument, then it's -a -b cd)

另外还有一定需要注意的是,上面参数的顺序跟ps.c中定义的top结构体对应的成员变量是相反的。

    struct {
      long n, m, d, s;
      struct arg_list *u, *p, *o, *k, *O;
    } top;
posted @ 2024-11-28 11:35  dolinux  阅读(17)  评论(0编辑  收藏  举报