Tool: Update some new features and refinements

 - Support name the output database file.
 - Support input specific name database file.
 - Fix the defect that delete all database file in the path.
 - UI option and terminal options are conflicting now.
 - Terminal messages are reversed, summary info shows at last.
 - Support sort messages in terminal mode.
 - Update help message and document.
 - Other code and structure refinements.

Signed-off-by: Jianliang Shen <jianliang.shen@arm.com>
Change-Id: I597f05bebc4f20d316f400ed3d099bd9dbbd972a
diff --git a/code-size-analyze-tool/README.rst b/code-size-analyze-tool/README.rst
index add23e2..e9abcf2 100644
--- a/code-size-analyze-tool/README.rst
+++ b/code-size-analyze-tool/README.rst
@@ -24,7 +24,7 @@
 
 .. code-block:: bash
 
-    sudo apt-get install python3-dev libmysqlclient-dev libncursesw6 libncurses6
+    sudo apt-get install python3-dev libmysqlclient-dev libncursesw6 libncurses6 libsqlite3-dev
     pip install mysqlclient XlsxWriter
 
 Windows
@@ -44,56 +44,72 @@
 
 .. code-block:: bash
 
-    python3 code_size_analyze.py [-h] [-i MAP_FILE_INPUT] [-u]
-                                 <--gnuarm|--armcc>
-                                 [-a] [-s] [-l] [-o] [-f] [-d]
-                                 [--dump_section SECTION_NAME]
-                                 [--dump_library LIBRARY_NAME]
-                                 [--dump_obj OBJ_NAME]
-                                 [--dump_function DUMP_FUNCTION_NAME]
-                                 [--dump_data DUMP_DATA_NAME]
-                                 [--search_func FUNCTION_NAME]
-                                 [--search_data DATA_NAME]
+    usage: code_size_analyze.py [-h] [--gnuarm | --armcc] [--db-name <data.db>]
+                                [-u | -S | -s | -l | -o | -f | -d |
+                                 --dump-sec <sec> | --dump-lib <lib> |
+                                 --dump-obj <obj> | --dump-func <func> |
+                                 --dump-data <data> | --search-func <func> |
+                                 --search-data <data>]
+                                [--sort-by-size | --sort-by-name |
+                                 --sort-by-obj | --sort-by-lib |
+                                 --sort-by-sec] [--desc | --asc]
+                                 file_input
 
-    options:
-        -h, --help                          show this help message and exit
-        -i MAP_FILE_INPUT                   map file path <path>/tfm_s.map
-        -u, --ui                            show UI
-        --gnuarm                            gnuarm map file input
-        --armcc                             armclang map file input
-        -a, --all                           show total
-        -s, --list_section                  list section
-        -l, --list_library                  list library
-        -o, --list_obj                      list object file
-        -f, --list_function                 list function
-        -d, --list_data                     list data
-        --dump_section SECTION_NAME         dump section
-        --dump_library LIBRARY_NAME         dump library
-        --dump_obj OBJ_NAME                 dump object file
-        --dump_function DUMP_FUNCTION_NAME  dump function
-        --dump_data DUMP_DATA_NAME          dump data
-        --search_func FUNCTION_NAME         search function
-        --search_data DATA_NAME             search data
+    positional arguments:
+        file_input            map or database file
+
+    optional arguments:
+        -h, --help            show this help message and exit
+        --gnuarm              GNUARM model
+        --armcc               ARMCLANG model
+        --db-name <data.db>   database name to save
+        -u, --ui              start UI to analyze
+        -S, --Summary         show summary message
+        -s, --list-section    list section
+        -l, --list-library    list library
+        -o, --list-obj        list object file
+        -f, --list-function   list function
+        -d, --list-data       list data
+        --dump-sec <sec>      dump section
+        --dump-lib <lib>      dump library
+        --dump-obj <obj>      dump object file
+        --dump-func <func>    dump function
+        --dump-data <data>    dump data
+        --search-func <func>  search function
+        --search-data <data>  search data
+        --sort-by-size        list by size order
+        --sort-by-name        list by name order
+        --sort-by-obj         list by object file name order
+        --sort-by-lib         list by library file name order
+        --sort-by-sec         list by section name order
+        --desc                sort with desc order
+        --asc                 sort with asc order
 
 Create database
 ===============
 
 It is required to input map file path for the script before show the UI. One of
-the options like ``--gnuarm`` or ``--armcc`` is required. Keep the compiler
-option same from the beginning to the end.
+the options like ``--gnuarm`` or ``--armcc`` is required.
+
+The default database name created is ``data.db``. Use ``--db-name`` to name the
+output file if necessary. For example, saving two different databases to compare
+later.
 
 .. code-block:: bash
 
-    python3 code_size_analyze.py -i MAP_FILE_INPUT <--gnuarm|--armcc>
+    $: python code_size_analyze.py tfm_s.map <--gnuarm|--armcc> --db-name tfm_s.db
 
-Show the UI
-===========
+UI mode
+=======
 
 The script ui.py supplies a menu to choose what developers may be interested.
+You can enter UI mode by analyzing map file directly or by importing database
+file path. The latter way is suggested as it runs more quickly.
 
 .. code-block:: bash
 
-    python3 code_size_analyze.py -u <--gnuarm|--armcc>
+    $: python code_size_analyze.py tfm_s.map <--gnuarm|--armcc> -u
+    $: python code_size_analyze.py tfm_s.db -u
 
 There are several keys to use UI.
 
@@ -106,56 +122,111 @@
 * ``s`` or ``S``: Enter output file name to save the content of current page.
 * ``:`` : Start search and enter the function or data name.
 
+Terminal mode
+=============
+
+In terminal mode, it is better to analyze database file rather than map file.
+
 Dump detail information
-=======================
+-----------------------
 
 You can get the list of all sections, libraries, object files, functions or
 data. You can also dump the specific symbol with the name.
 
 .. code-block:: bash
 
-    python3 code_size_analyze.py <--gnuarm|--armcc> -s
-    python3 code_size_analyze.py <--gnuarm|--armcc> --dump_section SECTION_NAME
+    $: python code_size_analyze.py tfm_s.map --armcc --db-name test.db -S
+    ───────────────────────────────────────────────
+    Code size       : 56676         55.35   KB
+    -----------------------------------------------
+    RO data         : 3732          3.64    KB
+    RW data         : 204           0.20    KB
+    ZI data         : 24588         24.01   KB
+    Flash size      : 60612         59.19   KB = Code + RO + RW
+    RAM size        : 24792         24.21   KB = RW + ZI
+    ───────────────────────────────────────────────
 
+    $: python code_size_analyze.py tfm_s.db -s
+    $: python code_size_analyze.py tfm_s.db --dump-sec <sec>
 
 Search specific function or data
-================================
+--------------------------------
 
 You can search the target with keyword in command line. For example:
 
 .. code-block:: bash
 
-    python3 code_size_analyze.py <--gnuarm|--armcc> --search_func FUNCTION_NAME
-    python3 code_size_analyze.py <--gnuarm|--armcc> --search_data DATA_NAME
+    $: python code_size_analyze.py tfm_s.db --search-func <func>
+    $: python code_size_analyze.py tfm_s.db --search-data <data>
+
+Sort Table
+----------
+
+You can sort the messages in terminal mode. The script supplies five options and
+two orders. For example:
+
+.. code-block:: bash
+
+    $: python code_size_analyze.py tfm_s.db -l --sort-by-size --asc
+    ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
+    Name                                              Flash size  RAM size    Code        RO data     RW data     ZI data     Inc. data   Debug
+    --------------------------------------------------------------------------------------------------------------------------------------------------
+    libtfm_qcbor_s.a                                  758         0           758         0           0           0           4           17046
+    libtfm_sprt.a                                     1016        0           1016        0           0           0           0           41004
+    c_w.l                                             1248        96          1248        0           0           96          86          1892
+    libtfm_psa_rot_partition_attestation.a            2497        557         2492        5           0           557         68          51865
+    libtfm_spm.a                                      4112        657         3932        136         44          613         168         52958
+    libtfm_psa_rot_partition_its.a                    5090        116         5030        32          28          88          28          49804
+    libtfm_psa_rot_partition_crypto.a                 6062        3232        6062        0           0           3232        36          92472
+    libplatform_s.a                                   6486        316         5582        780         124         192         404         94887
+    libmbedcrypto.a                                   28408       2292        26138       2262        8           2284        1066        226489
+    ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
+
+Not all symbols support to be sorted by the whole five options, refer to the
+table to get more information.
+
++----------------+---------+---------+--------------+----------+------+
+| Options        | Section | Library | Object files | Function | Data |
++================+=========+=========+==============+==========+======+
+| --sort-by-size |    √    |    √    |       √      |    √     |   √  |
++----------------+---------+---------+--------------+----------+------+
+| --sort-by-name |    √    |    √    |       √      |    √     |   √  |
++----------------+---------+---------+--------------+----------+------+
+| --sort-by-sec  |    √    |         |              |    √     |   √  |
++----------------+---------+---------+--------------+----------+------+
+| --sort-by-lib  |         |    √    |       √      |    √     |   √  |
++----------------+---------+---------+--------------+----------+------+
+| --sort-by-obj  |         |         |       √      |    √     |   √  |
++----------------+---------+---------+--------------+----------+------+
 
 *******************
 Code size diff tool
 *******************
 
 Use ``code_size_diff.py`` to diff two diffrent build results with same compiler.
-Firstly, use ``code_size_analyze.py`` to prepare two different databases. For
-example:
 
 .. code-block:: bash
 
-    usage: code_size_diff.py [-h] -i [input_dbs [input_dbs ...]]
-                             [-a] [-f] [-d] [-o] [-l]
+    usage: code_size_diff.py [-h] (-S | -f | -d | -o | -l) based_db compared_db
+
+    positional arguments:
+        based_db             based databse
+        compared_db          compared databse
 
     optional arguments:
-    -h, --help            show this help message and exit
-    -i [input_dbs [input_dbs ...]], --input [input_dbs [input_dbs ...]]
-                          Input two different data base files
-    -a, --diff_all        diff summary
-    -f, --diff_function   diff function
-    -d, --diff_data       diff data
-    -o, --diff_obj        diff object file
-    -l, --diff_lib        diff library
+        -h, --help            show this help message and exit
+        -S, --diff-Summary    diff summary
+        -f, --diff-function   diff function
+        -d, --diff-data       diff data
+        -o, --diff-obj        diff object file
+        -l, --diff-lib        diff library
 
-Then compare two database with the diff tool, the branch1 is base.
+Firstly, use ``code_size_analyze.py`` to prepare two different databases. Then
+compare two database with the diff tool, the branch1 is base.
 
 .. code-block:: bash
 
-    python3 code_size_diff.py -i output/branch1.db output/branch2.db -a
+    $: python code_size_diff.py output/branch1.db output/branch2.db -S
     Code size:  +++         48928   B               47.78   KB
     RO data:    +++         29440   B               28.75   KB
     RW data:    ---         64      B               0.06    KB
@@ -163,9 +234,6 @@
     Flash size: +++         78304   B               76.47   KB
     RAM size:   ---         564     B               0.55    KB
 
-The summary information change will be printed. Enter ``-h`` to get more usages
-of diff tool.
-
 --------------
 
 *Copyright (c) 2021-2022, Arm Limited. All rights reserved.*