The gemara says that if one is sick and needs to eat something for medicinal purposes, since he benefits from it, he makes a bracha. Rashi explains that whenever one receives a benefit of enjoying what they are eating, aside from the medicinal benefit, they must make a bracha. This is also the approach of Tosafos, however, the Rambam seems to say that as long as the person receives benefit from this eating such as for medicinal purposes where the food heals him, even if there is no enjoyment from the actual eating, a bracha would be made. The Shulchan Aruch 204:8 paskens like Rashi that one would only make a bracha if they receive some enjoyment from the food or medicine they are eating.
The Rama adds that if one were threatened at gun point and forced to eat something that they weren't interested in eating, no bracha would be made. However, in si'if 9 the Shulchan Aruch writes that if one had to eat things that are forbidden due to pikuach nefesh, they would make a bracha on it. The question is, why would one need to make a bracha on medicine or food that they need for pikuach nefesh, so long as they are receiving enjoyment from the eating, yet they wouldn't make a bracha on food when they are being forced to eat it at gun point? There are some achronim cited by M.B. 45 who equate these two halachos and say that just as one would not make a bracha when they are forced to eat at gun point, they would also not make a bracha on foods that they are eating for pikuach nefesh (see sha'ar hatziyun 40 - this is clearly the opinion of the re'ah). However, most achronim cited by the M.B. make a distinction between the two cases. When one is being forced to eat either physically forced by the food being stuffed down their throat, or forced by gun point, they are not making a decision to eat at all. Since the entire act of eating is forced upon them, there is no bracha. But, when one is dying and needs to eat for pikuach nefesh, they are not being forced to do the act of eating, rather they are making a decision to eat due to the dire circumstances. Since they are eating as a result of a decision that they are making, they must make a bracha.
There is a similar distinction made between one who violates an issur that they should have given their lives for i.e. avoda zara, but the violation was to save their life. Do we say that the person is given capital punishment for violating avoda zara or do we consider them an o'nes? The Rambam (Yesodei HaTorah 5:4) says that if they were forced to bow down to avoda zara at gun point we consider them an o'nes (even though they should have given their life) and there is no capital punishment, whereas when one uses the avoda zara for medicinal purpose to save their life, the are considered to be willfully violating avoda zara and are given capital punishment (Yesodei HaTorah 5:6). In the first case they are being forced to do the avoda zara and are considered an o'nes on the act of avoda zara so there is no capital punishment, but in the second they are deciding to use the avoda zara due to their dire circumstance, so they are punished for making a decision to use avoda zara.
The Sha'ar Hatziyun (38) asks that the gemara says one can fulfill the mitzvah of matzah that they are eating by force - כפאוהו ואכל מצה יצא. Why is a mitzvah different than a bracha? The Sha'ar Ha'tziyun offers two approaches. 1. Eating by force qualifies as eating so that the mitzvah is fulfilled, but we can't make someone give thanks on an act that they don't want to do. 2. We don't regard forced eating to be eating at all, but when there is an element of mitzvah such as by matzah, we assume that he later is happy that he ate it and it therefore qualifies as eating. According to the second answer, if one were forced to eat matzah, not only would they fulfill the mitzvah, but would also have to make birchas hamazon since in a mitzvah situation we regard it as eating.