xref: /aosp_15_r20/external/toolchain-utils/binary_search_tool/android/README.android.md (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1*760c253cSXin Li# Android's binary search tool
2*760c253cSXin Li
3*760c253cSXin Li`binary_search_state.py` is a general binary search triage tool that
4*760c253cSXin Liperforms a binary search on a set of things to try to identify which
5*760c253cSXin Lithing or thing(s) in the set is 'bad'.  `binary_search_state.py` assumes
6*760c253cSXin Lithat the user has two sets, one where everything is known to be good,
7*760c253cSXin Liand one which contains at least one bad item.  `binary_search_state.py`
8*760c253cSXin Lithen copies items from the good and bad sets into a working set and
9*760c253cSXin Litests the result (good or bad).  `binary_search_state.py` requires that
10*760c253cSXin Lia set of scripts be supplied to it for any particular job.  For more
11*760c253cSXin Liinformation on `binary_search_state.py`, see
12*760c253cSXin Li
13*760c253cSXin Lihttps://sites.google.com/a/google.com/chromeos-toolchain-team-home2/home/team-tools-and-scripts/binary-searcher-tool-for-triage
14*760c253cSXin Li
15*760c253cSXin LiThis particular set of scripts is designed to work with
16*760c253cSXin Li`binary_search_state.py` in order to find the bad object or set of
17*760c253cSXin Libad objects in an Android build. Furthermore, it can also help find
18*760c253cSXin Lithe bad compiler pass and transformation when building that bad object.
19*760c253cSXin Li
20*760c253cSXin Li
21*760c253cSXin Li## QUICKSTART
22*760c253cSXin Li
23*760c253cSXin LiAfter setting up your 2 build trees (see Prerequisites section), do the
24*760c253cSXin Lifollowing:
25*760c253cSXin Li
26*760c253cSXin Li-   Decide which test script to use (`boot_test.sh` or
27*760c253cSXin Li    `interactive_test.sh`)
28*760c253cSXin Li-   Get the serial number for the Android device you will use for testing.
29*760c253cSXin Li-   Run the following:
30*760c253cSXin Li
31*760c253cSXin Li    ```
32*760c253cSXin Li    $ cd <android_src>
33*760c253cSXin Li    $ source build/envsetup.sh
34*760c253cSXin Li    $ lunch <android_device_lunch_combo>
35*760c253cSXin Li    $ cd <path_to_toolchain_utils>/binary_search_tool/
36*760c253cSXin Li    $ NUM_JOBS=10 ANDROID_SERIAL=<device_serial> \
37*760c253cSXin Li          ./android/setup.sh <android_src>
38*760c253cSXin Li    ```
39*760c253cSXin Li
40*760c253cSXin Li    If you chose the boot test, then:
41*760c253cSXin Li
42*760c253cSXin Li    ```
43*760c253cSXin Li    TEST_SCRIPT=android/boot_test.sh
44*760c253cSXin Li    ```
45*760c253cSXin Li
46*760c253cSXin Li    If you chose the interactive test, then:
47*760c253cSXin Li
48*760c253cSXin Li    ```
49*760c253cSXin Li    TEST_SCRIPT=android/interactive_test.sh
50*760c253cSXin Li    ```
51*760c253cSXin Li
52*760c253cSXin Li    Finally, run the binary search tool:
53*760c253cSXin Li
54*760c253cSXin Li    ```
55*760c253cSXin Li    $ python ./binary_search_state.py \
56*760c253cSXin Li        --get_initial_items=android/get_initial_items.sh \
57*760c253cSXin Li        --switch_to_good=android/switch_to_good.sh \
58*760c253cSXin Li        --switch_to_bad=android/switch_to_bad.sh \
59*760c253cSXin Li        --test_setup_script=android/test_setup.sh \
60*760c253cSXin Li        --test_script=$TEST_SCRIPT \
61*760c253cSXin Li        --file_args \
62*760c253cSXin Li        --prune
63*760c253cSXin Li    ```
64*760c253cSXin Li
65*760c253cSXin Li    Once you have completely finished doing the binary search/triage,
66*760c253cSXin Li    run the cleanup script:
67*760c253cSXin Li
68*760c253cSXin Li    ```
69*760c253cSXin Li    $ android/cleanup.sh
70*760c253cSXin Li    ```
71*760c253cSXin Li
72*760c253cSXin Li
73*760c253cSXin Li## FILES AND SCRIPTS
74*760c253cSXin Li
75*760c253cSXin LiCheck the header comments for each script for more in depth documentation.
76*760c253cSXin Li
77*760c253cSXin Li`boot_test.sh` - One of two possible test scripts used to determine
78*760c253cSXin Li                 if the Android image built from the objects is good
79*760c253cSXin Li                 or bad. This script tests to see if the image
80*760c253cSXin Li                 booted, and requires no user intervention.
81*760c253cSXin Li
82*760c253cSXin Li`cleanup.sh` - This is called after the binary search tool completes. This
83*760c253cSXin Li               script will clean up the common.sh file generated by setup.sh
84*760c253cSXin Li
85*760c253cSXin Li`get_initial_items.sh` - This script is used to determine all Android objects
86*760c253cSXin Li                         that will be bisected.
87*760c253cSXin Li
88*760c253cSXin Li`test_setup.sh` - This script will build and flash your image to the
89*760c253cSXin Li                  Android device. If the flash fails, this script will
90*760c253cSXin Li                  help the user troubleshoot by trying to flash again or
91*760c253cSXin Li                  by asking the user to manually flash it.
92*760c253cSXin Li
93*760c253cSXin Li`interactive_test.sh` - One of two possible scripts used to determine
94*760c253cSXin Li                        if the Android image built from the objects
95*760c253cSXin Li                        is good or bad.  This script requires user
96*760c253cSXin Li                        interaction to determine if the image is
97*760c253cSXin Li                        good or bad.
98*760c253cSXin Li
99*760c253cSXin Li`setup.sh` - This is the first script the user should call, after
100*760c253cSXin Li             taking care of the prerequisites.  It sets up the
101*760c253cSXin Li             environment appropriately for running the Android
102*760c253cSXin Li             object binary search triage, and it generates the
103*760c253cSXin Li             necessary common script (see below).
104*760c253cSXin Li
105*760c253cSXin Li`switch_to_bad.sh` - This script is used to link objects from the
106*760c253cSXin Li                     'bad' build tree into the work area.
107*760c253cSXin Li
108*760c253cSXin Li`switch_to_good.sh` - This script is used to link objects from the
109*760c253cSXin Li                      'good' build tree into the work area.
110*760c253cSXin Li
111*760c253cSXin Li`generate_cmd.sh` - This script will generate another temporary script, which
112*760c253cSXin Li                    contains the command line options to build the bad object
113*760c253cSXin Li                    file again with pass/transformation level limit.
114*760c253cSXin Li
115*760c253cSXin Li
116*760c253cSXin Li## GENERATED SCRIPTS
117*760c253cSXin Li
118*760c253cSXin Li`common.sh` - contains basic environment variable definitions for
119*760c253cSXin Li              this binary search triage session.
120*760c253cSXin Li
121*760c253cSXin Li## ASSUMPTIONS
122*760c253cSXin Li
123*760c253cSXin Li-   There are two different Android builds, for the same board/lunch combo with
124*760c253cSXin Li    the same set of generated object files.  One build creates a good working
125*760c253cSXin Li    Android image and the other does not.
126*760c253cSXin Li
127*760c253cSXin Li-   The toolchain bug you are tracking down is not related to the linker. If the
128*760c253cSXin Li    linker is broken or generates bad code, this tool is unlikely to help you.
129*760c253cSXin Li
130*760c253cSXin Li
131*760c253cSXin LiPREREQUISITES FOR USING THESE SCRIPTS:
132*760c253cSXin Li
133*760c253cSXin Li1.  Decide where to store each build tree
134*760c253cSXin Li    By default, each build tree is stored in `~/ANDROID_BISECT`. However you
135*760c253cSXin Li    can override this by exporting `BISECT_DIR` set to whatever directory you
136*760c253cSXin Li    please. Keep in mind these build trees take dozens of gigabytes each.
137*760c253cSXin Li
138*760c253cSXin Li2.  Setup your android build environment
139*760c253cSXin Li
140*760c253cSXin Li    ```
141*760c253cSXin Li    cd <android_src>
142*760c253cSXin Li    source build/envsetup.sh
143*760c253cSXin Li    lunch <android_device_lunch_combo>
144*760c253cSXin Li    ```
145*760c253cSXin Li
146*760c253cSXin Li3.  Populate the good build tree
147*760c253cSXin Li
148*760c253cSXin Li    1.  `make clean`
149*760c253cSXin Li    2.  `export BISECT_STAGE=POPULATE_GOOD`
150*760c253cSXin Li    3.  Install your "good" toolchain in Android, this will most likely be
151*760c253cSXin Li        the toolchain that comes preinstalled with the Android source.
152*760c253cSXin Li    4.  Build all of Android: `make -j10`. The "-j" parameter depends on how
153*760c253cSXin Li        many cores your machine has. See Android documentation for more details.
154*760c253cSXin Li
155*760c253cSXin Li4.  Populate the bad build tree
156*760c253cSXin Li
157*760c253cSXin Li    1.  `make clean`
158*760c253cSXin Li    2.  `export BISECT_STAGE=POPULATE_BAD`
159*760c253cSXin Li    3.  Install your "bad" toolchain in Android.
160*760c253cSXin Li    4.  Build all of Android again.
161*760c253cSXin Li
162*760c253cSXin Li5.  Run the android setup script
163*760c253cSXin Li
164*760c253cSXin Li    1.  `cd <path_to_toolchain_utils>/binary_search_tool/`
165*760c253cSXin Li    2.  `NUM_JOBS=<jobs> ANDROID_SERIAL=<android_serial_num>
166*760c253cSXin Li        android/setup.sh <android_src>`
167*760c253cSXin Li
168*760c253cSXin LiWARNING: It's important that you leave the full `out/` directory in your
169*760c253cSXin Li         Android source alone after Step 4. The binary search tool will
170*760c253cSXin Li         use this directory as a skeleton to build each test image while
171*760c253cSXin Li         triaging.
172*760c253cSXin Li
173*760c253cSXin Li## USING THESE SCRIPTS FOR BINARY TRIAGE OF OBJECTS
174*760c253cSXin Li
175*760c253cSXin LiTo use these scripts, you must first run setup.sh, passing it the path to your
176*760c253cSXin LiAndroid source directory. setup.sh will do the following:
177*760c253cSXin Li
178*760c253cSXin Li-   Verify that your build trees are set up correctly (with good, bad).
179*760c253cSXin Li-   Verify that each build tree has the same contents.
180*760c253cSXin Li-   Verify that the android build environment (lunch, etc.) are setup in your
181*760c253cSXin Li    current shell.
182*760c253cSXin Li-   Create the common.sh file that the other scripts passed to the
183*760c253cSXin Li    binary triage tool will need.
184*760c253cSXin Li
185*760c253cSXin Li
186*760c253cSXin LiThis set of scripts comes with two alternate test scripts.  One test
187*760c253cSXin Liscript, `boot_test.sh`, just checks to make sure that the image
188*760c253cSXin Libooted (wait for device to boot to home screen) and assumes that is enough.
189*760c253cSXin LiThe other test script, `interactive_test.sh`, is interactive and asks YOU
190*760c253cSXin Lito tell it whether the image on the android device is ok or not (it
191*760c253cSXin Liprompts you and waits for a response).
192*760c253cSXin Li
193*760c253cSXin Li
194*760c253cSXin LiOnce you have run `setup.sh` (and decided which test script you
195*760c253cSXin Liwant to use) run the binary triage tool using these scripts to
196*760c253cSXin Liisolate/identify the bad object:
197*760c253cSXin Li
198*760c253cSXin Li```
199*760c253cSXin Li./binary_search_state.py \
200*760c253cSXin Li   --get_initial_items=android/get_initial_items.sh \
201*760c253cSXin Li   --switch_to_good=android/switch_to_good.sh \
202*760c253cSXin Li   --switch_to_bad=android/switch_to_bad.sh \
203*760c253cSXin Li   --test_setup_script=android/test_setup.sh \
204*760c253cSXin Li   --test_script=android/boot_test.sh \  # could use interactive_test.sh instead
205*760c253cSXin Li   --prune
206*760c253cSXin Li```
207*760c253cSXin Li
208*760c253cSXin LiAfter you have finished running the tool and have identified the bad
209*760c253cSXin Liobject(s), you will want to run the cleanup script (android/cleanup.sh).
210