1# hdc
2
3OpenHarmony Device Connector (hdc) is a command line tool used for debugging. You can use it on a Windows, Linux, or macOS system to interact with devices.
4
5## Environment Setup
6
7Obtain hdc from the **toolchains** folder in OpenHarmony SDK. For first-time use, configure the environment variables as follows.
8
9### Setting the Environment Variable **HDC_SERVER_PORT**
10
11**Windows**
12
131. Choose **This PC > Properties >Advanced system settings > Advanced > Environment Variables**, add the variable **HDC_SERVER_PORT**, and set its value to any port number not in use, such as **7035**.
14
15![Create system variable](figures/hdc_image_001_en.PNG)
16
172. Restart DevEco Studio.
18
19**macOS**
20
211. Start the terminal tool and run the following command:
22
23   ```shell
24   echo $SHELL
25   ```
26
27   - If the command output is **/bin/bash**, open the **.bash_profile** file.
28
29      ```shell
30      vi ~/.bash_profile
31      ```
32
33   - If the command output is **/bin/zsh**, open the **.zshrc** file.
34
35      ```shell
36      vi ~/.zshrc
37      ```
38
392. Press **i** to enter **Insert** mode.
403. Enter the following content to add **HDC_SERVER_PORT** information to **PATH**.
41
42   ```shell
43   HDC_SERVER_PORT=7035
44   launchctl setenv HDC_SERVER_PORT $HDC_SERVER_PORT
45   export HDC_SERVER_PORT
46   ```
47
484. Press **Esc** to exit **Insert** mode. Then enter **:wq** and press **Enter** to save the settings.
495. Run the following command for the environment variable to take effect.
50
51   - If the **.bash_profile** file is opened in step 1, run the following command:
52
53      ```shell
54      source ~/.bash_profile
55      ```
56
57   - If the **.zshrc** file is opened in step 1, run the following command:
58
59      ```shell
60      source ~/.zshrc
61      ```
62
636. Restart DevEco Studio.
64
65### (Optional) Setting Global Environment Variables
66
67> **NOTE**
68> If global environment variables are not set, you can run commands to go to the **toolchains** directory in the SDK and run hdc commands in the directory for debugging.
69
70**Windows**
71
72Choose **This PC > Properties > Advanced system settings > Advances > Environment Variables**, add the **toolchains** path of the SDK to the value of **Path**.
73
74In the following example, the **toolchains** path of the local SDK is <!--RP1-->**/User/username/sdk/openharmony/10/toolchains**<!--RP1End-->.
75
76![System Variables](figures/hdc_img_002_en.PNG)
77
78![Edit Environment Variable](figures/hdc_image_003_en.PNG)
79
80**Linux/macOS**
81
821. Start the terminal tool and run the following command:
83
84   ```shell
85   echo $SHELL
86   ```
87
88   - If the command output is **/bin/bash**, open the **.bash_profile** file.
89
90      ```shell
91      vi ~/.bash_profile
92      ```
93
94   - If the command output is **/bin/zsh**, open the **.zshrc** file.
95
96      ```shell
97      vi ~/.zshrc
98      ```
99
1002. Press **i** to enter **Insert** mode.
101
1023. Enter the following content and add the SDK path to the **PATH**.
103
104   In the following example, the **toolchains** path of the local SDK is <!--RP1-->**_/User/username/sdk/openharmony/10/toolchains_**<!--RP1End-->.
105
106   ```shell
107   HDC_SDK_PATH=/User/username/sdk/openharmony/10/toolchains
108   launchctl setenv HDC_SDK_PATH $HDC_SDK_PATH # This command needs to be executed only on macOS.
109   export PATH=$PATH:$HDC_SDK_PATH
110   ```
111
1124. Press **Esc** to exit **Insert** mode. Then enter **:wq** and press **Enter** to save the settings.
113
1145. Run the following command for the environment variable to take effect.
115
116   - If the **.bash_profile** file is opened in step 1, run the following command:
117
118      ```shell
119      source ~/.bash_profile
120      ```
121
122   - If the **.zshrc** file is opened in step 1, run the following command:
123
124      ```shell
125      source ~/.zshrc
126      ```
127<!--Del-->
128**(Optional for Linux) Granting USB Operation Permission for Non-root Users**
129
130- To temporarily grant the full operation permission on a USB device, run the following command:
131
132   ```shell
133   sudo chmod -R 777 /dev/bus/usb/
134   ```
135
136- To permanently change the operation permission on a USB device, do as follows:
137
138   1. Run the **lsusb** command to obtain the **vendorID** and **productID** of the USB device.
139
140   2. Create an **udev** rule.
141
142      Edit the **udev** loading rule and replace the default **idVendor** and **idProduct** values of the device with the values obtained in the previous step.
143
144      **MODE="0666"** indicates the permissions of **GROUP** (the user group) for the USB device. Ensure that the login user is in the user group.
145
146        ```shell
147        sudo vim /etc/udev/rules.d/90-myusb.rules
148        SUBSYSTEMS=="usb", ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", GROUP="users", MODE="0666"
149        ```
150
151   3. Restart the computer or reload the **udev** rule.
152
153        ```shell
154        sudo udevadm control --reload
155        ```
156
157> **NOTE**
158> A non-root user cannot find the device using hdc in Linux. Granting USB operation permission for non-root users can solve this problem. However, granting the full permission may pose potential security risks. You need to determine whether to grant the permission based on actual requirements.
159
160## Precautions
161
162- If an exception occurs, run the **hdc kill -r** command to kill the hdc process and restart the hdc service.
163
164- If no device information is obtained after the **hdc list targets** command is executed, check whether the hdc process exists in the **Task Manager**. If the process exists, run the **hdc kill -r** command to kill the process and restart the hdc service.
165
166
167## How to Use
168> **NOTE**
169>
170> - The parameters enclosed in [] in a command are optional.
171> - The parameters in italics are variables. Replace them with actual values when running the command.
172<!--DelEnd-->
173
174### Global Option Commands
175
176| Option | Description|
177| -------- | -------- |
178| -h/help | Displays hdc help information.|
179| -v/version | Displays hdc version information.|
180| -t [connect-key] [command] | Connects to the specified device. You can run the **hdc list targets** command to obtain the **connect-key**.|
181| -l [level] | Sets the levels of the logs generated during the running of the tool. The default value is **LOG_INFO**.|
182| wait | Waits for the device to be properly connected and checks whether the device is connected and ready to receive instructions.|
183| checkserver | Obtains the **client-server** version.|
184
1851. Display hdc help information.
186
187   ```shell
188   hdc -h/help
189   ```
190
191   **Return value**
192   | Return Value| Description|
193   | -------- | -------- |
194   | OpenHarmony device connector(HDC) ...<br>---------------------------------global commands:----------------------------------<br>-h/help [verbose]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Print hdc help, 'verbose' for more other cmds<br>..._(Detailed information is omitted here.)_| Help information for hdc commands.|
195
196   **Usage**
197
198   ```shell
199   hdc -h or hdc help
200   ```
201
2022. Display hdc version information.
203
204   ```shell
205   hdc -v/version
206   ```
207
208   **Return value**
209   | Return Value| Description|
210   | -------- | -------- |
211   | Ver: X.X.Xa | hdc (SDK) version information.|
212
213   **Usage**
214
215   ```shell
216   hdc -v or hdc version
217   ```
218
2193. Connect to a device. If there is only one device connected, you do not need to specify **connect-key**. If there are multiple devices connected, you must specify **connect-key** each time.
220
221   ```shell
222   hdc -t [connect-key] [command]
223   ```
224
225   **Parameters**
226   | Name| Description|
227   | -------- | -------- |
228   | connect-key| Device ID.|
229   | command | Command to be executed.|
230
231> **NOTE**
232   > The **connect-key** uniquely identifies a device. If the device is connected over the network, the **connect-key** is the IP address. If the device is connected through USB, the **connect-key** is the USB SN.
233
234   **Return value**
235| Return Value| Description|
236| -------- | -------- |
237| Command output| Command execution result, which may vary with the command.|
238| [Fail]Not match target founded, check connect-key please | No device matches the *connect-key*.|
239| [Fail]Device not founded or connected | The device is not found or connected.|
240| [Fail]ExecuteCommand need connect-key? please confirm a device by help info | You must specify one device if there are multiple devices available.|
241| Unknown operation command... | The command is not supported.|
242
243   **Usage**
244
245   This option must be used with a command. The following uses the **shell** command as an example.
246
247   ```shell
248   hdc list targets  // Display connect-key of all connected devices.
249   hdc -t [connect-key] shell // Replace connect-key with the specified device ID.
250   ```
251
2524. Specify the runtime log level. The default value is **LOG_INFO**.
253
254   ```shell
255   hdc -l [level] [command]
256   ```
257
258   **Parameters**
259   | Parameter| Description|
260   | -------- | -------- |
261   | [level] | Log level.<br>0: LOG_OFF<br>1: LOG_FATAL<br>2: LOG_WARN<br>3: LOG_INFO<br>4: LOG_DEBUG<br>5: LOG_ALL|
262   | command | Command to be executed.|
263
264   **Return value**
265   | Return Value| Description|
266   | -------- | -------- |
267   | Command output| Command execution result, which may vary with the command.|
268   | Log information| Logs of the specified level.|
269
270   **Usage**
271
272   ```shell
273   hdc -l 5 shell ls
274   ```
275
2765. Wait until the device is connected properly.
277
278   ```shell
279   hdc wait // Wait for the device to connect.
280   hdc -t connect-key wait // Wait for the specified device to connect. Replace connect-key with the specified device ID.
281   ```
282
283   **Return value**
284   | Return Value| Description|
285   | -------- | -------- |
286   | None| After the **hdc wait** command is executed, the process ends when a properly connected device is identified.|
287
288   **Usage**
289
290   ```shell
291   hdc wait
292   hdc -t connect-key wait
293   ```
294
2956. Obtain the client-server version information.
296
297   ```shell
298   hdc checkserver
299   ```
300
301   **Return value**
302   | Return Value| Description|
303   | -------- | -------- |
304   | Client version: Ver: X.X.Xa, Server version: Ver: X.X.Xa | Client-server version information.|
305
306   **Usage**
307
308   ```shell
309   hdc checkserver
310   ```
311
312### Commands for Viewing Device List
313
314| Command| Description|
315| -------- | -------- |
316| list targets [-v] | Displays all connected devices. Use **-v** to display detailed device information.|
317
318Display all connected devices.
319
320```shell
321hdc list targets[-v]
322```
323
324**Return value**
325| Return Value| Description|
326| -------- | -------- |
327| Device information| A list of connected devices.|
328| [Empty] | No device is found.|
329
330**Usage**
331
332```shell
333hdc list targets
334hdc list targets -v
335```
336
337### Service Process Commands
338
339| Command| Description|
340| -------- | -------- |
341| target mount | Mounts the system partition in read/write mode (unavailable to user).|
342| target boot [-bootloader\|-recovery] | Restarts a target device. You can use the **-bootloader** option to enter the fastboot mode and the **-recovery** option to enter the recovery mode.|
343| target boot [MODE] | Restart a target device to enter the corresponding mode. **MODE** is an option supported by **reboot** in the **/bin/begetctl** command.|
344| smode [-r] | Grants the **root** permission to the background hdc service running on the device. You can use the **-r** option to revoke the granted permission (unavailable to user).|
345| kill [-r] | Terminates the hdc process. You can use the **-r** option to restart the process.|
346| start [-r] | Starts the hdc process. You can use the **-r** option to restart the process.|
347
3481. Mount the system partition in read/write mode.
349
350   ```shell
351   hdc target mount
352   ```
353
354   **Return value**
355   | Return Value| Description|
356   | -------- | -------- |
357   | Mount finish | Mounting succeeded.|
358   | [Fail]Mount failed | Mounting failed.|
359
360   **Usage**
361
362   ```shell
363   hdc target mount
364   ```
365
3662. Restart the target device.
367
368   ```shell
369   target boot [-bootloader|-recovery]
370   target boot [MODE]
371   ```
372
373   **Parameters**
374   | Name| Description|
375   | -------- | -------- |
376   | Not specified| Restarts a device.|
377   | -bootloader| Restarts a device to enter the **fastboot** mode.|
378   | -recovery | Restarts a device to enter the **recovery** mode.|
379   | MODE | Restarts a device to enter the **MODE** mode. The **MODE** option is supported by **reboot** in the **/bin/begetctl** command. You can run the **hdc shell "/bin/begetctl -h \grep reboot"** command to obtain the options.|  |
380
381   **Usage**
382
383   ```shell
384   hdc target boot -bootloader // Restart the device to enter the fastboot mode.
385   hdc target boot -recovery // Restart the device to enter the recovery mode.
386   hdc target boot shutdown // Shut down the device.
387   ```
388
3893. Grant the **root** permission to the background hdc service running on the device.
390
391   ```shell
392   hdc smode [-r]
393   ```
394
395   **Return value**
396   | Return Value| Description|
397   | -------- | -------- |
398   | No return value| Permission granted.|
399   | [Fail]Error information| Failed to grant the permission.|
400
401   **Usage**
402
403   ```shell
404   hdc smode
405   hdc smode -r  // Revoke the root permission.
406   ```
407
4084. Terminate the hdc service.
409
410   ```shell
411   hdc kill [-r]
412   ```
413
414   **Return value**
415   | Return Value| Description|
416   | -------- | -------- |
417   | Kill server finish | The service is terminated successfully.|
418   | [Fail]Error information| Failed to terminate the service.|
419
420   **Usage**
421
422   ```shell
423   hdc kill
424   hdc kill -r // Restart and terminate the service process.
425   ```
426
4275. Start the hdc service.
428
429   ```shell
430   hdc start [-r]
431   ```
432
433   **Return value**
434   | Return Value| Description|
435   | -------- | -------- |
436   | No return value| The service is started successfully.|
437   | [Fail]Error information| Failed to start the service process.|
438
439   **Usage**
440
441   ```shell
442   hdc start
443   hdc start -r // Restart the service.
444   ```
445
446### Network Commands
447
448| Command| Description|
449| -------- | -------- |
450| fport ls | Lists all port forwarding tasks.|
451| fport _localnode remotenode_ | Sets up a local port forwarding, which forwards data from a local port to a remote port.|
452| rport _remotenode localnode_ | Sets up a remote port forwarding, which forwards data from a remote port to a local port.|
453| fport rm _taskstr_ | Removes a port forwarding task.|
454| tmode usb | This command cannot be used to enable the USB connection channel. You need to enable it on the device setting page.|
455| tmode port [port-number] | Enable the network connection channel of the device. Then the daemon process on the device restarts, and the USB connection is interrupted. You need to reconnect the device.|
456| tmode port close | Disable the network connection channel of the device. Then the daemon process on the device restarts, and the USB connection is interrupted. You need to reconnect the device.|
457| tconn [IP]:[port] [-remove] | Specifies a connection to a device in *IP address: port number* format. Use the **-remove** option to disconnect from the specified device.|
458
4591. List all port forwarding tasks.
460
461   ```shell
462   hdc fport ls
463   ```
464
465   **Return value**
466   | Return Value| Description|
467   | -------- | -------- |
468   | tcp:1234 tcp:1080 [Forward] | Local port forwarding.|
469   | tcp:2080 tcp:2345 [Reverse] | Remote port forwarding.|
470   | [empty] | No port forwarding.|
471
472   **Usage**
473
474   ```shell
475   hdc fport ls
476   ```
477
4782. Set up a local port forwarding, which forwards data from a local port to a remote port.
479
480   ```shell
481   hdc fport localnode remotenode
482   ```
483
484   **Return value**
485   | Return Value| Description|
486   | -------- | -------- |
487   | Forwardport result:OK | The port forwarding is set up properly.|
488   | [Fail]Incorrect forward command | Failed to set up the port forwarding due to parameter errors.|
489   | [Fail]TCP Port listen failed at XXXX | Failed to set up the port forwarding because the local port is in use.|
490
491   **Usage**
492
493   ```shell
494   hdc fport tcp:1234 tcp:1080
495   ```
496
497
4983. Set up a remote port forwarding, which forwards data from a remote port to a local port.
499
500   ```shell
501   hdc rport remotenode localnode
502   ```
503
504   **Return value**
505   | Return Value| Description|
506   | -------- | -------- |
507   | Forwardport result:OK | The port forwarding is set up properly.|
508   | [Fail]Incorrect forward command | Failed to set up the port forwarding due to parameter errors.|
509   | [Fail]TCP Port listen failed at XXXX | Failed to set up the port forwarding because the local port is in use.|
510
511   **Usage**
512
513   ```shell
514   hdc rport tcp:1234 tcp:1080
515   ```
5164. Remove the specified port forwarding.
517
518   ```shell
519   hdc fport rm taskstr
520   ```
521
522   **Parameters**
523   | Parameter| Description|
524   | -------- | -------- |
525   | _taskstr_ | Port forwarding task, in the format of tcp:XXXX tcp:XXXX.|
526
527   **Return value**
528   | Return Value| Description|
529   | -------- | -------- |
530   | Remove forward ruler success, ruler:tcp:XXXX tcp:XXXX | The port forwarding is removed successfully.|
531   | [Fail]Remove forward ruler failed, ruler is not exist tcp:XXXX tcp:XXXX | Failed to remove the port forwarding because the specified forwarding does not exist.|
532
533   **Usage**
534
535   ```shell
536   hdc fport rm tcp:1234 tcp:1080
537   ```
538
539
5405. Enable the USB connection channel of the device.
541
542   ```shell
543   hdc tmode usb
544   ```
545
546   **Usage**
547
548   ```shell
549   hdc tmode usb
550   ```
551   > **NOTE**
552   > This command cannot be used to enable the USB connection channel. You need to enable it on the device setting page.
553
5546. Enable the network connection channel of the device.
555
556   ```shell
557   hdc tmode port [port-number]
558   ```
559
560   **Parameters**
561   | Parameter| Description|
562   | -------- | -------- |
563   | port-number | Port number of the connection. The value ranges from 1 to 65536.|
564
565   **Return value**
566   | Return Value| Description|
567   | -------- | -------- |
568   | Set device run mode successful. | The network connection channel is enabled successfully.|
569   | [Fail]ExecuteCommand need connect-key | Failed to enable the network connection channel because no device exists in the list. |
570   | [Fail]Incorrect port range | The port number is out of range (1 to 65536).|
571
572   **Usage**
573
574   ```shell
575   hdc tmode port 1234
576   ```
577
578   > **NOTE**
579   > Before changing the connection mode, ensure that the remote device and the local executor are on the same network, and the executor can ping the IP address of the remote device.
580   >
581   > Otherwise, do not run this command.
582
583   > **NOTE**
584   > After the command is executed, the remote daemon process exits and restarts, and the USB connection is disconnected. You need to reconnect the USB connection.
585
5867. Disable the network connection channel of the device.
587
588   ```shell
589   hdc tmode port close
590   ```
591
592   **Return value**
593   | Return Value| Description|
594   | -------- | -------- |
595   | [Fail]ExecuteCommand need connect-key | Failed to disable the network connection channel because no device exists in the list.|
596
597   **Usage**
598
599   ```shell
600   hdc tmode port close
601   ```
602   > **NOTE**
603   > After the command is executed, the remote daemon process exits and restarts, and the USB connection is disconnected. You need to reconnect the USB connection.
604
6058. Connect to the specified device through TCP.
606
607   ```shell
608   hdc tconn [IP]:[port] [-remove]
609   ```
610
611   **Parameters**
612   | Parameter| Description|
613   | -------- | -------- |
614   | [IP]:[port]  | Device IP address and port number.|
615   | -remove | (Optional) Disconnects from the specified device.|
616
617   **Return value**
618   | Return Value| Description|
619   | -------- | -------- |
620   | Connect OK | Device connected.|
621   | [Info]Target is connected, repeat opration | The device is already connected.|
622   | [Fail]Connect failed | Failed to connect the specified device.|
623
624   **Usage**
625
626   ```shell
627   hdc tconn 192.168.0.1:8888
628   hdc tconn 192.168.0.1:8888 -remove  // Disconnect from the specified device.
629   ```
630
631### File Commands
632
633| Command| Description|
634| -------- | -------- |
635| file send _localpath remotepath_ | Sends a local file to a remote device.|
636| file recv _remotepath localpath_ | Sends a file from a remote device to the local device.|
637
6381. Send a local file to a remote device.
639
640   ```shell
641   hdc file send localpath remotepath
642   ```
643
644   **Parameters**
645   | Name| Description|
646   | -------- | -------- |
647   | _localpath_ | Path of the file to send on the local device.|
648   | _remotepath_ | Destination path on the remote device.|
649
650   **Return value**
651
652   A success message is displayed if the file is sent successfully. Error information is displayed if the file fails to be sent.
653
654   **Usage**
655
656   ```shell
657   hdc file send E:\example.txt /data/local/tmp/example.txt
658   ```
659
6602. Send a file from a remote device to the local device.
661
662   ```shell
663   hdc file recv remotepath localpath
664   ```
665
666   **Parameters**
667   | Name| Description|
668   | -------- | -------- |
669   | _localpath_ | Destination path on the local device.|
670   | _remotepath_ | Path of the file to send on the remote device.|
671
672   **Return value**
673
674   A success message is displayed if the file is received successfully. Error information is displayed if the file fails to be received.
675
676   **Usage**
677
678   ```shell
679   hdc file recv  /data/local/tmp/a.txt   ./a.txt
680   ```
681
682### App Commands
683
684| Command| Description|
685| -------- | -------- |
686| install _src_ | Installs an app.|
687| uninstall _packageName_ | Uninstalls an app.|
688
6891. Install an app.
690
691   ```shell
692   hdc install [-r|-s] src
693   ```
694
695   **Parameters**
696   | Name| Description|
697   | -------- | -------- |
698   | src| Installation package name.|
699   | -r | Replaces the existing app (.hap).|
700   | -s | Install a shared package (.hsp).|
701
702   **Return value**
703   | Return Value| Description|
704   | -------- | -------- |
705   | AppMod finish | The installation is successful.|
706   | Error information| The installation fails.|
707
708   **Usage**
709
710   For example, install **example.hap**.
711
712   ```shell
713   hdc install E:\example.hap
714   ```
715
7162. Uninstall the app.
717
718   ```shell
719   hdc uninstall [-k|-s] packageName
720   ```
721
722   **Parameters**
723   | Name| Description|
724   | -------- | -------- |
725   | packageName | App installation package.|
726   | -k | Retains **/data** and **/cache**.|
727   | -s | Uninstalls a shared package.|
728
729   **Return value**
730   | Return Value| Description|
731   | -------- | -------- |
732   | AppMod finish | The uninstallation is successful.|
733   | Error information| The uninstallation fails.|
734
735   **Usage**
736
737   For example, uninstall **com.example.hello**.
738
739   ```shell
740   hdc uninstall com.example.hello
741   ```
742
743### Debugging Commands
744
745| Command| Description|
746| -------- | -------- |
747| jpid | Displays the PIDs of all applications that have enabled JDWP on the device.|
748| track-jpid [-a\|-p]  | Displays the PID and name of the application for which JDWP is enabled on the device in real time. If no parameter is specified, only the processes of the **debug** application are displayed. If the **-a** parameter is specified, the processes of the **debug** and **release** applications are displayed. If the **-p** parameter is specified, the **debug** and **release** labels are not displayed.|
749| hilog [options] | Obtains the log information of the device. **options** indicates the parameters supported by HiLog. You can run the **hdc hilog -h** command to obtain the parameter information. |
750| shell [command] | Runs a debugging command specified by *command*. The supported commands vary with the system type or version. You can run the **hdc shell ls /system/bin** command to view the supported commands. |
751
7521. Obtain log information about the device.
753
754   ```shell
755   hdc hilog [options]
756   ```
757
758   **Parameters**
759   | Parameter| Description|
760   | -------- | -------- |
761   | [options] | Parameters supported by HiLog. You can run the **hdc hilog -h** command to obtain the parameter list.|
762
763   **Return value**
764   | Return Value| Description|
765   | -------- | -------- |
766   | Returned information| Log information obtained.|
767
768   **Usage**
769
770   ```shell
771   hdc hilog
772   hdc shell "hilog -r" // Clear the cached logs.
773   ```
774
7752. Display the PIDs of all processes that enable JDWP.
776
777   ```shell
778   hdc jpid
779   ```
780
781   **Return value**
782   | Return Value| Description|
783   | -------- | -------- |
784   | PID list| PIDs of the applications that enable JDWP|
785   | [empty] | No process enables JDWP.|
786
787   **Usage**
788
789   ```shell
790   hdc jpid
791   ```
792
7933. Display the PIDs and application names of the processes that enable JDWP on the device in real time.
794
795   ```shell
796   track-jpid [-a|-p]
797   ```
798
799   **Parameters**
800   | Parameter| Description|
801   | -------- | -------- |
802   | Not specified| Displays only the PIDs, bundle names, and process names of debug applications.|
803   | -a | Displays the PIDs, bundle names, and process names of the debug and release applications. |
804   | -p | Displays the PIDs, bundle names, and process names of the debug and release applications, but does not display the **debug** and **release** labels.|
805
806   **Return value**
807   | Return Value| Description|
808   | -------- | -------- |
809   | PIDs and bundle/process name list| - |
810   | [empty] | If no parameter is specified, no process of the debug application enables JDWP. If the **-a** or **-p** parameter is specified, no process enables JDWP.|
811
812   **Usage**
813
814   ```shell
815   hdc track-jpid
816   ```
817
8184. Run a command.
819
820   ```shell
821   hdc shell [command]
822   ```
823
824   **Parameters**
825   | Parameter| Description|
826   | -------- | -------- |
827   | [command] | Debugging command to be executed. You can run the **help** command to obtain all debugging commands.|
828
829   **Return value**
830   | Return Value| Description|
831   | -------- | -------- |
832   | Command execution result| Execution result of the debugging command.|
833   | /bin/sh: XXX : inaccessible or not found | The specified command is not supported.|
834
835   **Usage**
836
837   ```shell
838   hdc shell ps -ef
839   hdc shell help -a // List all available commands.
840   ```
841
842### Security Command
843
844| Command| Description|
845| -------- | -------- |
846| keygen FILE | Generates a new key pair and save the private key and public key to **FILE** and **FILE.pub** respectively. The file name **FILE** can be customized.|
847
8481. Generate a new key pair.
849
850   ```shell
851   hdc keygen FILE
852   ```
853
854   **Parameters**
855   | Parameter| Description|
856   | -------- | -------- |
857   | FILE | Custom file name.|
858
859   **Usage**
860
861   ```shell
862   hdc keygen key // Generate the key and key.pub files in the current directory.
863   ```
864
865
866## When to Use
867
868### USB Connection
869
870**Checking the Environment**
871| Item| Normal| Exception Handling|
872| -------- | -------- | -------- |
873| USB debugging| USB debugging is enabled.| If the USB debugging mode is not automatically enabled, restart the device.|
874| USB data cable connection| The PC used for debugging is connected to the device USB port using a USB cable.| If you use a USB cable with low bandwidth and no data communication function, the device may fail to be identified. You are advised to use an officially recommended USB cable.|
875| USB port| A USB port on the mainboard (USB port on the rear panel of a desktop computer or USB port on a laptop computer) is used.| If you use a conversion adapter, docking station, or USB port on the front panel of a desktop computer, issues such as low bandwidth and USB sync problems may occur, which results in frequent disconnections. Therefore, direct connection between the PC and the device is recommended.|
876| hdc environment variable| The help information is displayed after the **hdc -h** command is executed.| For details, see "Environment Setup".|
877| Driver| After the device is connected via hdc, **HDC Device** or **HDC Interface** is displayed in **Device Manager**.| Install the driver.|
878
879**Procedure**
880
881```shell
882hdc shell // For USB direct connection, ensure that the device is not in TCP connection mode.
883```
884
885### TCP Connection
886
887**Checking the Environment**
888| Item| Normal| Exception Handling|
889| -------- | -------- | -------- |
890| Network connection| The PC and the device are on the same network.| Connect the PC and device to the same Wi-Fi network or enable the Wi-Fi hotspot on your device.|
891| Network Status| Use **telnet IP:port** to check that the network connection between the PC and the device is normal.| Connect the PC and the device over a stable network.|
892| hdc environment variable| The help information is displayed after the **hdc** command is executed.| For details, see "Environment Setup".|
893
894**Procedure**
895
8961. Connect the PC to the device through a USB port.
897
898
8992. Change the connection mode from USB mode to the TCP mode.
900
901   ```shell
902   hdc tmode port 8710// Add a port number after port. The default port number is 8710.
903   ```
904
9053. Connect to the device over TCP (the device IP address must be obtained in advance).
906
907   ```shell
908   hdc tconn IP:8710
909   ```
910
911   You can view the IP address on the device. The port number is the one specified in the previous step. The default port number is 8710.
912
9134. Check the connection.
914
915   ```shell
916   hdc list targets
917   ```
918
919   If the return value is in the *IP:port* format, the connection is successful.
920
9215. (Optional) Change the TCP mode to the USB mode.
922
923   ```shell
924   hdc tmode usb
925   ```
926
927   The USB mode is restored.
928
929### Remote Connection
930
931The following figure illustrates a remote connection.
932
933![Remote connection structure](figures/hdc_image_004.PNG)
934
935**Server Configuration**
936
937Connect the server to the device using a USB cable and run the following commands:
938
939```shell
940hdc kill          // Terminate the local hdc service.
941hdc -s IP:8710 -m // Enable the hdc service for network forwarding.
942                  // In the command, IP indicates the IP address of the server. To query the IP address, you can run the ipconfig command on Windows and run the ifconfig command on Unix.
943                  // 8710 is the default port number. You can also set it to another port number, for example, 18710.
944                  // After startup, the server prints logs.
945```
946
947**Client Connection**
948
949Ensure that the client can connect to the server IP address, and then run the following command:
950```shell
951hdc -s IP:8710 [command] // IP indicates the IP address of the server,
952                         // and command can be any available hdc command, for example, list targets.
953```
954
955### Obtaining Logs
956
957**Server logs**
958
959Obtain server logs.
960
961```shell
962hdc kill ​
963hdc -l5 start
964```
965
966The collected logs are stored in the following path.
967
968| OS| Path| Remarks|
969| -------- | -------- | -------- |
970| Windows | %temp%\hdc_logs | The following is an example. Replace *Username* with the actual one.<br>C:\Users\Username\AppData\Local\Temp\hdc_logs|
971| Linux | /tmp/hdc_logs | - |
972| MacOS | $TMPDIR/hdc_logs | - |
973
974The hdc_logs folder contains the following types of logs:
975
976|Type| Name Format| Function| Remarks|
977| -------- | -------- | -------- | -------- |
978| Real-time log| hdc.log | Records hdc server logs in real time.| Each time the hdc server is restarted, the original log is renamed and a new hdc.log is recorded.|
979| Temporary historical log| hdc-%Y%m%d-%H%M%S.log | Dumps intermediate files generated during historical log archiving.| For example, **16:18:57.921 on September 19, 2024** is recorded as<br>**20240919-161857921**.<br>The generated temporary log file is stored in the format of **hdc-20240919-161857921.log**.|
980| Archived historical logs| hdc-%Y%m%d-%H%M%S.log.tgz | Compresses and stores historical logs.| The archive file is a compressed file of the **.tgz** type. You can use a decompression tool to obtain the file.<br>For example, if the name of a temporary historical log file is **hdc-20240919-161857921.log**,<br>the name of the corresponding archived historical log file is **hdc-20240919-161857921.log.tgz**.<br>After an archived historical log file is generated, the corresponding temporary historical log file is automatically deleted.|
981| Temporary real-time log| .hdc.cache.log | Records temporary caches generated by real-time logs.| |
982
983**Device Logs**
984
985Enable HiLog to obtain the corresponding logs.
986
987```shell
988hdc shell hilog -w start                             // Enable the function of storing HiLog logs.
989hdc shell ls /data/log/hilog                     // View the stored HilLog logs.
990hdc file recv /data/log/hilog                  // Obtain the stored HilLog logs (including kernel logs).
991```
992
993## FAQs
994
995### Failed to Identify the Target Device
996
997**Symptom**
998
999After the **hdc list targets** command is executed, the command output is **[empty]**.
1000
1001Perform the following operations to locate the fault.
1002
1003- Case 1: Check whether the target device is displayed in **Device Manager**.
1004
1005  Windows:
1006
1007  Check whether **HDC Device** (for a single-port device) or **HDC Interface** (for a composite device) exists under **Universal Serial Bus Controllers** in **Device Manager**.
1008
1009  Linux:
1010
1011  Run the **lsusb** command. Check whether **HDC Device** or **HDC Interface** is displayed in the command output.
1012
1013  macOS:
1014
1015  View USB devices in **System Information** or **System Overview**.
1016
1017  1. Press and hold the **Option** key, and click **Menu**.
1018
1019  2. Choose **System Information** or **System Overview**.
1020
1021  3. Select **USB** on the left.
1022
1023  4. Check whether **HDC Device** or **HDC Interface** exists in the device tree.
1024
1025  **Solution**
1026
1027  If the device is not displayed, it fails to be identified. You can use any of the following methods to resolve the problem:
1028
1029  - Use another physical USB port.
1030  - Replace the USB cable.
1031  - Use another PC for debugging.
1032  - Enable the USB debugging mode.
1033  - Click **OK** to allow USB debugging.
1034  - If a TCP connection can be set up, run the **hdc tmode usb** command to switch to the USB connection.
1035  - Restore the factory settings of the device.
1036
1037- Case 2: A USB device exists, but the driver is damaged. **HDC Device** with a warning icon in yellow is displayed.
1038
1039  This problem usually occurs in Windows. In **Device Manager/Universal Serial Bus Device**, **HDC Device** is displayed with a yellow warning icon, indicating that the device cannot work properly. To resolve the problem, reinstall the driver. If the problem persists, use another USB cable, docking station, or USB port.
1040
1041  **Reinstalling the Driver**
1042
1043  1. Open **Device Manager**, and right-click the **HDC Device** with a warning icon.
1044
1045  2. In the displayed menu, choose **Update driver**.
1046
1047  3. In the first prompt window, select **Browse my computer for drivers**.
1048
1049  4. In the second prompt window, select **Let me pick from a list of available drivers on my computer**.
1050
1051  5. In the third prompt window, deselect **Show compatible hardware**, select **WinUsb Device** for **Manufacture** and **WinUsb Device** for **Model**, and click **Next**.
1052
1053- Case 3: "[Fail]Failed to communicate with daemon" is displayed when the device is connected.
1054
1055  When "[Fail]Failed to communicate with daemon" is displayed after a hdc command is executed.
1056
1057  Possible causes are as follows:
1058
1059  - The hdc or SDK does not match the device. If the device is the latest version, update the hdc or SDK tool to the latest version.
1060  - The port is occupied.
1061
1062    This problem usually occurs when hdc and hdc_std use the same **HDC_SERVER_PORT** or the same default port **8710**. Ensure that either hdc or hdc_std is running. This problem also occurs when other software occupies the default hdc port.
1063
1064- Case 4: "Connect server failed" is displayed when the device is connected.
1065
1066  The possible causes are as follows:
1067
1068  - **Port Preemption**
1069
1070    Solution:
1071    1. Check the software processes that come with hdc, including software with built-in hdc, such as DevEco Studio and DevEco Testing.
1072
1073       Stop these software processes and run hdc commands.
1074
1075    2. Check hdc port status.
1076
1077       For example, if **HDC_SERVER_PORT** is set to **8710**, run the following command to check the port status.
1078
1079       Unix:
1080
1081       ```shell
1082       netstat -an |grep 8710
1083       ```
1084
1085       Windows:
1086
1087       ```shell
1088       netstat -an |findstr 8710
1089       ```
1090
1091       If the port is used by another software, stop the software process or change the **HDC_SERVER_PORT** environment variable to another port number.
1092
1093    3. Check for and stop the hdc server of another version.
1094
1095       Windows:
1096
1097       Choose **Task Manager** > **Details**, right-click the **hdc.exe** process and choose **Open file location**. Check whether the location is the same as that configured in the environment variable. If not, stop the **hdc.exe** process by running the **hdc kill** command or terminating the process in **Task Manager**. Then, run the hdc command again. (The hdc server will be restarted by running the hdc command.)
1098
1099       Unix:
1100
1101       Run the **ps -ef |grep hdc** command to query the hdc server process running in the background and check whether the process startup location is the hdc file location set by the environment variable **Path**. If not, stop the hdc process by running the **hdc kill** or **kill -9 hdc** *PID* command, and run the hdc command again. (The hdc server will be restarted by running the hdc command.)
1102
1103  - **Registry Exception**
1104
1105    Solution: Clear the registry. The procedure is as follows:
1106
1107    1. Press **Win+R** to open the **Run** dialog box, and enter **regedit** to open the registry.
1108    2. Enter the following address in the address bar, and press **Enter** to access the registry of the USB device driver.
1109
1110       ```shell
1111       Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{88bae032-5a81-49f0-bc3d-a4ff138216d6}
1112       ```
1113
1114    3. Right-click **UpperFilters** and choose **Edit**. Back up and clear the value data. The backup data can be used to restore the value data if the problem persists.
1115    4. Refresh the **Device Manager**, remove and reconnect the USB cable to the USB port, or restart the PC.
1116
1117### Failed to Run hdc
1118
1119**Symptom**
1120
1121The **hdc.exe**/hdc binary file cannot be executed using the CLI.
1122
1123**Possible Causes and Solution**
1124
1125- Incorrect runtime environment.
1126
1127  Linux: Ubuntu 18.04 64-bit or later is recommended. If **libc++.so** reference errors are reported, run the **ldd** or **readelf** command to check library reference.
1128
1129  macOS: macOS 11 or later is recommended.
1130
1131  Windows: Windows 10 or Windows 11 64-bit is recommended. If the WinUSB library or driver is missing in an earlier version, use [Zadig](https://github.com/pbatard/libwdi/releases) to update it. For Windows 10 or Windows 11 64-bit, use [Zadig](https://github.com/pbatard/libwdi/releases) to install the libusb-win32 driver. For details, see [Zadig](https://github.com/pbatard/libwdi/releases).
1132
1133- Improper running mode: Use the correct command to run the hdc tool instead of double-clicking the file.
1134
1135### Common Procedure for Troubleshooting Other Problems
1136
11371. Run the **hdc list targets** command.
11382. Check whether **HDC Device** exists in **Device Manager**.
11393. Run the **hdc kill** command to terminate the server, and then run the **hdc -l5 start** command to collect logs. The **hdc.log** file is stored in the **hdc_logs** folder of the **TEMP** directory on the execution end. The specific directory varies with the OS. For details, see [Obtaining Logs](#obtaining-logs).
11404. Locate the problem based on the **hdc.log** file.
1141