Hattie Louise James was sitting on her front porch in Charlotte, N.C., when two police detectives emerged from their car. There had been a shooting, they said. Two officers were dead. The gun had been traced back to her.

"I liked to had another heart attack," said the 72-year-old James, a retired hospital worker.

The .32-caliber revolver used to kill Charlotte-Mecklenburg police officers Sean Clark and Jeff Shelton in April 2007 started out as a legally owned weapon. James bought it in 1991 at Hyatt Coin and Gun Shop in Charlotte, but it was stolen a year later from her husband's car. Fifteen years after that, it passed into the hands of 25-year-old Demeatrius Montgomery.

In September, Montgomery was convicted of gunning down the officers outside a low-income housing complex in northeast Charlotte.

Clark and Shelton are two of 511 police officers killed by firearms in the United States from the beginning of 2000 through this past Sept. 30.

Until now, no one has conducted a comprehensive study of how the killers got their guns.

To trace these guns, The Washington Post did a year-long investigation, including building a database of every police officer shot to death in the past decade. (More than 1,900 officers were wounded by firearms during the same period.) Through documents and interviews, The Post was able to track how the suspects obtained their weapons in 341 of the deaths.

This kind of analysis is made more difficult by a law passed by Congress in 2003 that bars federal law enforcement from releasing information that links guns used in crimes back to the original purchasers. To penetrate that secrecy, The Post interviewed more than 350 police officials, prosecutors, defense attorneys, judges, gun dealers, gun buyers, suspects and survivors.

In 30 cases, the newspaper obtained confidential firearms traces generated by the Bureau of Alcohol, Tobacco, Firearms and Explosives. The ATF's reports track guns recovered at crime scenes back to dealers and original buyers, listing a gun's model, caliber and serial number.

The Post review shows how guns got into the hands of police officers' killers and - in a nation with more than 250 million guns in circulation - how a moment of panic can have deadly consequences.

Among the findings:

• Legal purchase was the leading source of weapons used to kill police officers. In 107 slayings, the killers acquired their firearms legally. In 170 deaths, The Post could not determine how the shooters got their guns, including 29 killings in which weapons were not recovered.

• Stolen guns turned up in 77 deaths. Separately, guns obtained or taken from relatives or friends who legally owned them were used in 46 killings.

• Fifty-one officers were killed when their department-issued firearms or other officers' guns were turned against them.

• In 41 instances, guns were illegally obtained on the streets through sale or barter.

• Sixteen times, someone bought a weapon for a person prohibited from having a gun, an unlawful transaction known as a straw purchase. The straw buyers were federally prosecuted in fewer than half of those cases. Three were illegally purchased at gun shows or from private sellers.

• The two deadliest situations for police are traffic stops and domestic disputes. Ninety-one of the officers were killed while making traffic stops; 76 were responding to domestic-disturbance calls. The officers killed at traffic stops were generally slain by felons wielding illegal guns; the weapons used to kill police in domestic situations were often obtained through legal purchases. Only 13 percent of the weapons in the traffic stops were legal, compared with 47 percent in the domestic calls.

• More than 200 of the shooters were felons who were prohibited by federal law from possessing firearms. Many had spent time in prison for illegal handgun possession. At least 45 were on probation or parole when they killed an officer. At least four were previously convicted of murder or manslaughter, including a Texas man who had done time for two separate slayings and was on parole at the time he killed his third victim: a 40-year-old sheriff's deputy with a wife and three children.

• Handguns were used to kill 365 officers; long guns - rifles and shotguns - were used to kill 140 officers.

The ratio of handguns to long guns in The Post review - about 70 percent to 30 percent - is close to being the inverse of the ratio of all guns in the nation: 40 percent handguns to 60 percent long guns.

But the ratio found by The Post matches that for U.S. homicides in general, experts say, reflecting the preference among criminals for handguns because they are generally cheaper and easier to conceal.

The most common handgun used was the 9mm semiautomatic pistol, which was used to kill at least 85 of the police officers.